-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprepare_data.py
More file actions
137 lines (125 loc) · 5.44 KB
/
Copy pathprepare_data.py
File metadata and controls
137 lines (125 loc) · 5.44 KB
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
import pickle
import os
import re
import sys
if sys.version_info[0] == 2:
# check typo error
import enchant
d = enchant.Dict("en_US")
MIN_LENGTH = 3
MAX_LENGTH = 50
class ImdbData(object):
def __init__(self):
self.data = ''
# self.data4 = ''
# self.data5 = ''
# self.data6 = ''
# self.data7 = ''
# self.data8 = ''
# self.data9 = ''
# self.data10 = ''
# self.data11= ''
# self.data12 = ''
# self.data13 = ''
# self.data14 = ''
# self.data15 = ''
# self.data16 = ''
# self.data17 = ''
# self.data18 = ''
# self.data19 = ''
# self.data20 = ''
self.trimmed_sentences = []
@staticmethod
def _normalize_text(text):
"""Remove impurities from the text"""
text = re.sub(r"<br />", "", text)
# text = re.sub(r"[^A-Za-z0-9!?\'\`]", " ", text)
text = re.sub(r"it's", " it is", text)
text = re.sub(r"that's", " that is", text)
text = re.sub(r"\'s", " 's", text)
text = re.sub(r"\'ve", " have", text)
text = re.sub(r"won't", " will not", text)
text = re.sub(r"don't", " do not", text)
text = re.sub(r"can't", " can not", text)
text = re.sub(r"cannot", " can not", text)
text = re.sub(r"n\'t", " n\'t", text)
text = re.sub(r"\'re", " are", text)
text = re.sub(r"\'d", " would", text)
text = re.sub(r"\'ll", " will", text)
text = re.sub(r"!", " ! ", text)
text = re.sub(r"\?", " ? ", text)
text = re.sub(r"\s{2,}", " ", text)
return text
def load_data(self):
data_set = []
for path_train in ['dataset/train/pos/', 'dataset/train/neg/', 'dataset/test/pos/', 'dataset/test/neg/']:
data_set.extend([open(path_train + f).read().split('.') for f in os.listdir(path_train) if f.endswith('.txt')])
for i in data_set:
for j in i:
trimmed_sentence = self._normalize_text(j)
if MIN_LENGTH < len(trimmed_sentence.split()) < MAX_LENGTH:
flag = 0
flag2 = 0
for k in ',!?()[]-:<>{}/=+_*^%$#@~"':
if trimmed_sentence.find(k) != -1:
flag = 1
if flag == 0:
if sys.version_info[0] == 2:
for l in trimmed_sentence.split():
if not d.check(l):
flag2 = 1
if flag2 == 0:
if trimmed_sentence[0] == ' ':
self.trimmed_sentences.append(trimmed_sentence[1:])
else:
self.trimmed_sentences.append(trimmed_sentence)
return self
def save_to_txt(self, trimmed_sentences, filename='imdb100000_max16-eng-eng.txt'):
for sentence in trimmed_sentences[:300000]:
if len(self.data.split('\n')) == 100001:
break
if 2 < len(sentence.split()) < 17:
self.data += sentence + '.\n'
with open(filename, 'wt') as fo:
fo.write(self.data[:-1])
# def save_to_txt(self, trimmed_sentences, filename='imdb1000000-eng-eng.txt'):
# for sentence in trimmed_sentences[100000:300000]:
# if len(sentence.split()) == 15:
# self.data15 += sentence + '.\n'
# if len(self.data15.split('\n')) == 1001:
# with open('imdb_len_15.txt', 'wt') as fo:
# fo.write(self.data15[:-1])
# if len(sentence.split()) == 16:
# self.data16 += sentence + '.\n'
# if len(self.data16.split('\n')) == 1001:
# with open('imdb_len_16.txt', 'wt') as fo:
# fo.write(self.data16[:-1])
# if len(sentence.split()) == 17:
# self.data17 += sentence + '.\n'
# if len(self.data17.split('\n')) == 1001:
# with open('imdb_len_17.txt', 'wt') as fo:
# fo.write(self.data17[:-1])
# if len(sentence.split()) == 18:
# self.data18 += sentence + '.\n'
# if len(self.data18.split('\n')) == 1001:
# with open('imdb_len_18.txt', 'wt') as fo:
# fo.write(self.data18[:-1])
# if len(sentence.split()) == 19:
# self.data19 += sentence + '.\n'
# if len(self.data19.split('\n')) == 1001:
# with open('imdb_len_19.txt', 'wt') as fo:
# fo.write(self.data19[:-1])
# if len(sentence.split()) == 20:
# self.data20 += sentence + '.\n'
# if len(self.data20.split('\n')) == 1001:
# with open('imdb_len_20.txt', 'wt') as fo:
# fo.write(self.data20[:-1])
def save_to_pkl(self, trimmed_sentences, filename='imdb-eng-eng.pkl'):
for sentence in trimmed_sentences:
self.data += sentence + '\n'
pickle.dump(self.data[:-1], open(filename, 'wb'))
def load_to_pkl(filename='imdb-eng-eng.pkl'):
pickle.load(open(filename, 'rb'))
if __name__ == '__main__':
data = ImdbData()
data.save_to_txt(data.load_data().trimmed_sentences)