-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpre_processing.py
172 lines (132 loc) · 5.02 KB
/
pre_processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from nltk.tokenize import word_tokenize
import random,re, os, nltk
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
def clean_str(string):
"""
Tokenization/string cleaning for all datasets except for SST.
Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
# string = re.sub(r"[0-9]", " ", string)
string = re.sub(r"\'s", " is", string)
string = re.sub(r"\'ve", " have", string)
string = re.sub(r"n\'t", " not", string)
string = re.sub(r"\'re", " are", string)
string = re.sub(r"\'d", " would", string)
string = re.sub(r"\'ll", " will", string)
string = re.sub(r",", " ", string)
string = re.sub(r"!", " ", string)
string = re.sub(r"\(", " ", string)
string = re.sub(r"\)", " ", string)
string = re.sub(r"\?", " ", string)
string = re.sub(r"\'", " ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower()
def remove_short(string):
results = []
for word in string.split(' '):
if len(word) < 3:
continue
else:
results.append(word)
return ' '.join(results)
def stem_corpus():
stemmer = WordNetLemmatizer()
with open('data/mr/text_train.txt') as f:
raw_text = f.read()
with open('data/mr/label_train.txt') as f:
raw_labels = f.read()
labels = []
for raw_label in raw_labels.split('\n'):
if raw_label == '1':
labels.append('pos')
elif raw_label == '0':
labels.append('neg')
else:
if len(raw_label) == 0:
continue
raise ValueError(raw_label)
corpus = raw_text.split('\n')
corpus = [clean_str(doc) for doc in corpus]
corpus = [remove_short(doc) for doc in corpus]
tokenized_corpus = [word_tokenize(doc) for doc in corpus]
results = []
for line in tokenized_corpus:
results.append(' '.join([stemmer.lemmatize(word) for word in line]))
results = list(zip(labels, results))
results = ['\t'.join(line) for line in results]
random.shuffle(results)
with open('data/mr/mr-train-stemmed.txt', 'w') as f:
f.write('\n'.join(results))
def cut_datasets():
for dataset in ['r8', 'r52', 'oh']:
with open(os.path.join('.', 'data', dataset, dataset+'-stemmed.txt')) as f:
all_cases = f.read().split('\n')
print('datasets: ', dataset, ', total length:', len(all_cases))
cut_index = int(len(all_cases) * 0.9)
train_cases = all_cases[:cut_index]
dev_cases = all_cases[cut_index+1:]
with open(os.path.join('.', 'data', dataset, dataset+'-train-stemmed.txt'), 'w') as f:
f.write('\n'.join(train_cases))
with open(os.path.join('.', 'data', dataset, dataset+'-dev-stemmed.txt'), 'w') as f:
f.write('\n'.join(dev_cases))
class Ohsumed(object):
def __init__(self):
self.base = './ohsumed-first-20000-docs'
def cal_freq(self):
results = {}
for _, _, file_names in os.walk(self.base):
for file in file_names:
if file not in results.keys():
results[file] = 1
else:
results[file] += 1
single_target = []
for file in results.keys():
if results[file] == 1:
single_target.append(file)
with open('ohsumed-single-index.txt', 'w') as f:
f.write(','.join(single_target))
print(len(single_target))
def make_set(self):
set = 'train'
current = os.path.join(self.base, set)
result = []
with open('ohsumed-single-index.txt') as f:
raw = f.read()
indexs = raw.split(',')
for dir, _, file_names in os.walk(current):
if len(file_names) == 0:
continue
type = dir[-3:]
for file in file_names:
if file in indexs:
with open(os.path.join(dir, file)) as f:
text = f.read()
text = text.replace('\n', ' ')
text = self.clean_text(text)
result.append('\t'.join([type, text]))
random.shuffle(result)
with open('train-shuffled.txt','w') as f:
f.write('\n'.join(result))
@staticmethod
def clean_text(text):
# stop_words = stopwords.words('english')
stop_words = []
stop_words.extend(['!', ',' ,'.' ,'?' ,'-s' ,'-ly' ,'</s> ', 's'])
stemmer = WordNetLemmatizer()
text = remove_short(text)
text = clean_str(text)
text = word_tokenize(text)
text = [word for word in text if word not in stop_words]
text = [stemmer.lemmatize(word) for word in text]
return ' '.join(text)
if __name__ == '__main__':
# stem_corpus()
# shuffle_20ng()
# o = Ohsumed()
# o.make_set()
# nltk.download('wordnet')
# clean_mr()
cut_datasets()