-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsome_preprocessing.py
executable file
·184 lines (163 loc) · 6.38 KB
/
some_preprocessing.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
173
174
175
176
177
178
179
180
181
182
183
import random
import numpy as np
import re
import pickle
def clean_str(s):
s = re.sub(r"[^A-Za-z0-9:(),!?\'\`]", " ", s)
s = re.sub(r" : ", ":", s)
s = re.sub(r"\'s", " \'s", s)
s = re.sub(r"\'ve", " \'ve", s)
s = re.sub(r"n\'t", " n\'t", s)
s = re.sub(r"\'re", " \'re", s)
s = re.sub(r"\'d", " \'d", s)
s = re.sub(r"\'ll", " \'ll", s)
s = re.sub(r",", " , ", s)
s = re.sub(r"!", " ! ", s)
s = re.sub(r"\(", " \( ", s)
s = re.sub(r"\)", " \) ", s)
s = re.sub(r"\?", " \? ", s)
s = re.sub(r"\s{2,}", " ", s)
return s.strip().lower()
def remove_punctuation(s):
s = re.sub(r"[^A-Za-z0-9]", " ", s)
return s.strip().lower()
def p1():
file_list = ['emotion.neg.0.txt', 'emotion.pos.0.txt']
select_ratio = 0.9
split_point = np.int32(5331 * 0.9)
for file in file_list:
with open('data/' + file, 'r', encoding="ISO-8859-1") as f1:
lines = f1.readlines()
random.shuffle(lines)
lines_train = lines[: split_point]
lines_test = lines[split_point:]
with open('data/' + file + '.train', 'w') as f2:
for line in lines_train:
line = clean_str(line.strip())
f2.write(line + '\r')
with open('data/' + file + '.test', 'w') as f2:
for line in lines_test:
line = clean_str(line.strip())
f2.write(line + '\r')
with open('vocb.txt', 'w') as wf:
voc_dict = {}
for file in file_list:
with open('data/' + file + '.train', 'r', encoding="ISO-8859-1") as rf:
for word in rf.read().split():
if word not in voc_dict:
voc_dict[word] = 1
else:
voc_dict[word] += 1
idx = 0
for voc in voc_dict:
wf.write(voc + ' ' + str(idx) + '\n')
idx += 1
def p1_1():
file_list = ['emotion.neg.0.txt', 'emotion.pos.0.txt']
select_ratio = 0.9
split_point = np.int32(5331 * 0.9)
for file in file_list:
with open('data/' + file, 'r', encoding="ISO-8859-1") as f1:
lines = f1.readlines()
random.shuffle(lines)
lines_train = lines[: split_point]
lines_test = lines[split_point:]
with open('data/' + file + '.train', 'w') as f2:
for line in lines_train:
line = line.strip()
f2.write(line + '\r')
with open('data/' + file + '.test', 'w') as f2:
for line in lines_test:
line = line.strip()
f2.write(line + '\r')
def p2():
from os import listdir
from os.path import isfile, join
path_neg = ['data/aclImdb/test/neg', 'data/aclImdb/train/neg']
path_pos = ['data/aclImdb/test/pos', 'data/aclImdb/train/pos']
with open('data/emotion_large_neg', 'w') as f1:
for path in path_neg:
onlyfiles = [f for f in listdir(path) if isfile(join(path, f))]
for file in onlyfiles:
with open(join(path, file), 'r') as f2:
line = clean_str(f2.read().strip())
f1.write(line+'\n')
with open('data/emotion_large_pos', 'w') as f1:
for path in path_pos:
onlyfiles = [f for f in listdir(path) if isfile(join(path, f))]
for file in onlyfiles:
with open(join(path, file), 'r') as f2:
line = clean_str(f2.read().strip())
f1.write(line+'\n')
file_list = ['emotion_large_neg', 'emotion_large_pos']
with open('vocb.txt', 'w') as wf:
voc_dict = {}
for file in file_list:
with open('data/' + file, 'r', encoding="ISO-8859-1") as rf:
for word in rf.read().split():
if word not in voc_dict:
voc_dict[word] = 1
else:
voc_dict[word] += 1
idx = 0
for voc in voc_dict:
wf.write(voc + ' ' + str(idx) + '\n')
idx += 1
wf.write('<unk> ' + str(idx))
def p2_1():
file_list = ['IMDB50K_train/emotion_pos.0.txt', 'IMDB50K_train/emotion_pos.1.txt', 'IMDB50K_train/emotion_pos.2.txt',
'IMDB50K_train/emotion_pos.3.txt',
'IMDB50K_train/emotion_neg.0.txt', 'IMDB50K_train/emotion_neg.1.txt',
'IMDB50K_train/emotion_neg.2.txt', 'IMDB50K_train/emotion_neg.3.txt']
with open('vocb.txt', 'w') as wf:
voc_dict = {}
for file in file_list:
with open('data/' + file, 'r', encoding="ISO-8859-1") as rf:
for word in rf.read().split():
if word not in voc_dict:
voc_dict[word] = 1
else:
voc_dict[word] += 1
idx = 0
for voc in voc_dict:
wf.write(voc + ' ' + str(idx) + '\n')
idx += 1
wf.write('<unk> ' + str(idx))
def p3():
with open('results/prediction2500', 'br') as f:
data = pickle.load(f)
wrong_neg = []
s_neg = list(data[:534])
for item in range(len(s_neg)):
print(item)
if s_neg[item] == 1:
wrong_neg.append(item)
wrong_pos = []
s_pos = list(data[534:])
for item in range(len(s_pos)):
if s_pos[item] == 0:
wrong_pos.append(item)
def p4():
file_list = ['emotion.neg.0.txt.test', 'emotion.neg.0.txt.train', 'emotion.pos.0.txt.test', 'emotion.pos.0.txt.train']
for file in file_list:
with open('data/' + file, 'r', encoding="ISO-8859-1") as f1:
with open('data/' + file + '.0', 'w') as f2:
for line in f1.readlines():
f2.write(' '.join(remove_punctuation(line).split()) + '\n')
def p4_1():
file_list = ['emotion.neg.0.txt.train', 'emotion.pos.0.txt.train']
with open('vocb.txt', 'w') as wf:
voc_dict = {}
for file in file_list:
with open('data/' + file, 'r', encoding="ISO-8859-1") as f1:
for word in f1.read().split():
if word not in voc_dict:
voc_dict[word] = 1
else:
voc_dict[word] += 1
idx = 0
for voc in voc_dict:
wf.write(voc + ' ' + str(idx) + '\n')
idx += 1
wf.write('<unk> ' + str(idx))
p4_1()