-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocess.py
305 lines (249 loc) · 10 KB
/
preprocess.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
import emoji
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet
import nltk
import re
import os
import shutil
from itertools import combinations
from string import punctuation
nltk.download('omw-1.4')
nltk.download('stopwords')
nltk.download('averaged_perceptron_tagger')
nltk.download('punkt')
nltk.download('wordnet')
def create_directory(dirname):
if os.path.exists(dirname):
shutil.rmtree(dirname)
os.mkdir(dirname)
def get_wordnet_pos(tag):
if tag.startswith('J'):
return wordnet.ADJ
elif tag.startswith('V'):
return wordnet.VERB
elif tag.startswith('N'):
return wordnet.NOUN
elif tag.startswith('R'):
return wordnet.ADV
else:
return None
def remove_emoji(process):
face_list = [":D", ":)", ":(", ":/", ":P", "<3", ";)"]
for face in face_list:
process = process.replace(face, "")
process_string = emoji.demojize(process)
return process_string
def restore_subject(process):
pat_is = re.compile("(it|he|she|that|this|there|here)(\'s)", re.I)
pat_s = re.compile("(?<=[a-zA-Z])\'s")
pat_s2 = re.compile("(?<=s)\'s?")
pat_not = re.compile("(?<=[a-zA-Z])\'t")
pat_would = re.compile("(?<=[a-zA-Z])\'d")
pat_will = re.compile("(?<=[a-zA-Z])\'ll")
pat_am = re.compile("(?<=[I|i])\'m")
pat_are = re.compile("(?<=[a-zA-Z])\'re")
pat_ve = re.compile("(?<=[a-zA-Z])\'ve")
process = pat_is.sub(r"\1 is", process)
process = pat_s.sub("", process)
process = pat_s2.sub("", process)
process = pat_not.sub(" not", process)
process = pat_would.sub(" would", process)
process = pat_will.sub(" will", process)
process = pat_am.sub(" am", process)
process = pat_are.sub(" are", process)
process = pat_ve.sub(" have", process)
process = process.replace('\'', ' ')
process_string = re.sub("\d+(?:st|nd|rd|th)", "#order", process)
return process_string
def remove_punctuation(process):
process_string = re.sub(r'[^a-zA-Z0-9]', ' ', process)
return process_string
def replace_number(process):
process_string = ""
tokens = nltk.word_tokenize(process)
tagged_sent = nltk.pos_tag(tokens)
for tag in tagged_sent:
if tag[0].isdigit():
process_string += "#number "
else:
process_string += (tag[0]+" ")
return process_string
def remove_stopword(process):
process_string = ""
nltk_stopwords = nltk.corpus.stopwords.words('english')
tokens = nltk.word_tokenize(process)
tagged_sent = nltk.pos_tag(tokens)
for tag in tagged_sent:
if (tag[0] not in nltk_stopwords):
process_string += (tag[0]+" ")
return process_string
def lemmatize(process):
process_string = ""
wnl = WordNetLemmatizer()
tokens = nltk.word_tokenize(process)
process = ""
tagged_sent = nltk.pos_tag(tokens)
for tag in tagged_sent:
wordnet_pos = get_wordnet_pos(tag[1]) or wordnet.NOUN
process_string += (wnl.lemmatize(tag[0], pos=wordnet_pos) + " ")
return process_string
def string_process(content, used_list):
process = content
# replace _comma_ to ,
if "1" in used_list:
process = process.replace("_comma_", ",")
# replace / to or
if "2" in used_list:
process = process.replace('/', ' or ')
# replace & to and
if "3" in used_list:
process = process.replace('&', ' and ')
# remove emoji
if "4" in used_list:
process = remove_emoji(process)
# he's -> he is
if "5" in used_list:
process = restore_subject(process)
# removing punctuation
if "6" in used_list:
process = remove_punctuation(process)
# replace int to #number
if "7" in used_list:
process = replace_number(process)
# remove stopword
if "8" in used_list:
process = remove_stopword(process)
# lemmatize
if "9" in used_list:
process = lemmatize(process)
if len(process) > 0 and process[-1] != '.':
process += '.'
return process
def group_by(df, mode, conversion, used_list, dirname):
total_data = []
total_label = []
total_count = []
conv_id = list(df.conv_id)
prompt = list(df.prompt)
utterance = list(df.utterance)
if mode != "test":
label = list(df.label)
listKeys = list(set(conv_id))
listKeys.sort(key=conv_id.index)
total_dict = dict.fromkeys(listKeys, "")
if mode == "train" or mode == "valid":
conv_label = dict(zip(conv_id, label))
for idx, id in enumerate(conv_id):
if "utterance" == conversion:
total_dict[id] += string_process(utterance[idx], used_list)
total_dict[id] += " "
if "prompt" == conversion:
total_dict[id] += string_process(prompt[idx], used_list)
total_dict[id] += " "
if "utterance+prompt" == conversion:
total_dict[id] += string_process(prompt[idx], used_list)
total_dict[id] += " "
total_dict[id] += string_process(utterance[idx], used_list)
total_dict[id] += " "
for id in list(total_dict.keys()):
total_data.append(total_dict[id])
total_label.append(conv_label[id])
new_df = pd.DataFrame(columns=["data", "label"])
new_df["data"] = total_data
new_df["label"] = total_label
elif mode == "test":
uniconv_id, count = np.unique(np.array(conv_id), return_counts=True)
conv_count = dict(zip(uniconv_id, count))
for idx, id in enumerate(conv_id):
if "utterance" == conversion:
total_dict[id] += string_process(utterance[idx], used_list)
total_dict[id] += " "
if "prompt" == conversion:
total_dict[id] += string_process(prompt[idx], used_list)
total_dict[id] += " "
if "utterance+prompt" == conversion:
total_dict[id] += string_process(prompt[idx], used_list)
total_dict[id] += " "
total_dict[id] += string_process(utterance[idx], used_list)
total_dict[id] += " "
for id in list(total_dict.keys()):
total_data.append(total_dict[id])
total_count.append(conv_count[id])
new_df = pd.DataFrame(columns=["data", "count"])
new_df["data"] = total_data
new_df["count"] = total_count
if mode == "train":
new_df.to_csv(dirname+"/fixed_group_train.csv", index=False)
print(dirname+"/fixed_group_train.csv is processed.")
elif mode == "valid":
new_df.to_csv(dirname+"/fixed_group_valid.csv", index=False)
print(dirname+"/fixed_group_valid.csv is processed.")
elif mode == "test":
new_df.to_csv(dirname+"/fixed_group_test.csv", index=False)
print(dirname+"/fixed_group_test.csv is processed.")
if __name__ == "__main__":
train_df = pd.DataFrame(pd.read_csv("fixed_train.csv"))
valid_df = pd.DataFrame(pd.read_csv("fixed_valid.csv"))
test_df = pd.DataFrame(pd.read_csv("fixed_test.csv"))
conversion = ["utterance", "prompt", "utterance+prompt"]
if os.path.exists("data") == False:
os.mkdir("data")
for conv in conversion:
dirname = "+".join([
"1", "2", "3", "4", "5", "6", "7", "8", "9"])+"("+conv+")"
create_directory("data/"+dirname)
group_by(train_df, "train", conv, [
"1", "2", "3", "4", "5", "6", "7", "8", "9"], "data/"+dirname)
group_by(valid_df, "valid", conv, [
"1", "2", "3", "4", "5", "6", "7", "8", "9"], "data/"+dirname)
group_by(test_df, "test", conv, [
"1", "2", "3", "4", "5", "6", "7", "8", "9"], "data/"+dirname)
combination1 = list(combinations("123456789", 8))
for comb in combination1:
comb = sorted(list(comb))
dirname = "+".join(comb)+"(utterance+prompt)"
dirname = "data/"+dirname
create_directory(dirname)
group_by(train_df, "train", "utterance+prompt", comb, dirname)
group_by(valid_df, "valid", "utterance+prompt", comb, dirname)
group_by(test_df, "test", "utterance+prompt", comb, dirname)
for conv in conversion:
dirname = "+".join(["3", "5"])+"("+conv+")"
create_directory("data/"+dirname)
group_by(train_df, "train", conv, ["3", "5"], "data/"+dirname)
group_by(valid_df, "valid", conv, ["3", "5"], "data/"+dirname)
group_by(test_df, "test", conv, ["3", "5"], "data/"+dirname)
for conv in conversion:
dirname = "+".join(["3"])+"("+conv+")"
create_directory("data/"+dirname)
group_by(train_df, "train", conv, ["3"], "data/"+dirname)
group_by(valid_df, "valid", conv, ["3"], "data/"+dirname)
group_by(test_df, "test", conv, ["3"], "data/"+dirname)
for conv in conversion:
dirname = "+".join(["5"])+"("+conv+")"
create_directory("data/"+dirname)
group_by(train_df, "train", conv, ["5"], "data/"+dirname)
group_by(valid_df, "valid", conv, ["5"], "data/"+dirname)
group_by(test_df, "test", conv, ["5"], "data/"+dirname)
for conv in conversion:
dirname = "+".join(["1", "2", "3", "4", "5", "6"])+"("+conv+")"
create_directory("data/"+dirname)
group_by(train_df, "train", conv, [
"1", "2", "3", "4", "5", "6"], "data/"+dirname)
group_by(valid_df, "valid", conv, [
"1", "2", "3", "4", "5", "6"], "data/"+dirname)
group_by(test_df, "test", conv, [
"1", "2", "3", "4", "5", "6"], "data/"+dirname)
for number in range(1, 11):
dirname = "+".join([str(number)])+"(utterance+prompt)"
dirname = "data/"+dirname
create_directory(dirname)
group_by(train_df, "train", "utterance+prompt",
[str(number)], dirname)
group_by(valid_df, "valid", "utterance+prompt",
[str(number)], dirname)
group_by(test_df, "test", "utterance+prompt",
[str(number)], dirname)