-
Notifications
You must be signed in to change notification settings - Fork 6
/
Generative_Model_ubuntuDataProcessing.py
219 lines (179 loc) · 5.32 KB
/
Generative_Model_ubuntuDataProcessing.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
import csv
import numpy as np
import matplotlib.pyplot as plt
import re
import pickle
contexts = []
responses = []
cntr = 0
filename = 'C:/MyStuff/SEM3/DL/Project1/datasets/ubuntu-ranking-dataset-creator-master/udc.tar/data/train.csv'
file = open(filename, encoding="utf8")
csvReader = csv.reader(file)
for row in csvReader:
contexts.append(row[0])
responses.append(row[1])
cntr += 1
# lengths = []
# for i in contexts:
# lengths.append(len(i))
#
# # fixed bin size
# bins = np.arange(-100, 100, 5) # fixed bin size
# plt.xlim([min(lengths)-5, max(lengths)+5])
# plt.hist(lengths, bins=bins, alpha=0.5)
# plt.title('Random Gaussian data (fixed bin size)')
# plt.xlabel('variable X (bin size = 5)')
# plt.ylabel('count')
# plt.axis([0, 150, 0, 9000])
# plt.show()
replacements = open('replacements.txt', 'r').read().split('\n')
def replaceText(txt):
txt = txt.lower()
for replace in replacements:
replacement = replace.split(',')
txt = re.sub(replacement[0], replacement[1], txt)
txt = re.sub(r"[-()\"#/@;:<>{}`+=~|.!?,_]", "", txt)
txt = re.sub(r"eou", "", txt)
txt = re.sub(r"eot", "", txt)
return txt
replacedQues = []
replacedAns = []
print('initially')
for i in range(2):
print(contexts[i])
print(responses[i])
for que in contexts:
replacedQues.append(replaceText(que))
for ans in responses:
replacedAns.append(replaceText(ans))
print('replaced6')
for i in range(2):
print(replacedQues[i])
print(replacedAns[i])
# removing conversations with large difference in lengths
lenDiff = 20
consiseQues = []
consiseAns = []
for i in range(0, len(replacedQues)):
if abs(len(replacedAns[i]) - len(replacedQues[i])) <= lenDiff:
consiseQues.append(replacedAns[i])
consiseAns.append(replacedQues[i])
print(len(replacedQues))
print(len(replacedAns))
print(len(consiseAns))
print(len(consiseQues))
vocabulary = {}
for que in contexts:
for w in que.split():
if w not in vocabulary:
vocabulary[w] = 1
else:
vocabulary[w] += 1
for ans in responses:
for w in ans.split():
if w not in vocabulary:
vocabulary[w] = 1
else:
vocabulary[w] += 1
print (len(vocabulary))
queW2int = {}
cntr = 4
for cont in consiseQues:
for w in cont.split():
if w not in queW2int:
queW2int[w] = cntr;
cntr += 1
ansW2int = {}
cntr = 4
for cont in consiseAns:
for w in cont.split():
if w not in ansW2int:
ansW2int[w] = cntr;
cntr += 1
wilds = ['<PAD>','<EOS>','<UNK>','<GO>']
cntr = 0
for wild in wilds:
queW2int[wild] = cntr
cntr += 1
cntr = 0
for wild in wilds:
ansW2int[wild] = cntr
cntr += 1
queint2W = {v_i: v for v, v_i in queW2int.items()}
ansint2W = {v_i: v for v, v_i in ansW2int.items()}
unkcntr = 0
queInt = []
for que in consiseQues:
ints = []
for w in que.split():
if w not in queW2int:
unkcntr += 1
ints.append(queW2int['<UNK>'])
else:
if w in vocabulary:
if vocabulary[w] > 15 and vocabulary[w] < 100000:
ints.append(queW2int[w])
else:
ints.append(queW2int[w])
queInt.append(ints)
ansInt = []
for ans in consiseAns:
ints = []
for w in ans.split():
if w not in ansW2int:
unkcntr += 1
ints.append(ansW2int['<UNK>'])
else:
if w in vocabulary:
if vocabulary[w] > 15 and vocabulary[w] < 100000:
ints.append(ansW2int[w])
else:
ints.append(ansW2int[w])
ansInt.append(ints)
train_valid_split = int(len(queInt)*0.15)
train_questions = queInt[train_valid_split:]
train_answers = ansInt[train_valid_split:]
valid_questions = queInt[:train_valid_split]
valid_answers = ansInt[:train_valid_split]
PAD = queW2int['<PAD>']
for i in range(0, len(train_questions)):
ans_len = len(train_answers[i])
que_len = len(train_questions[i])
extra_array = [PAD] * abs(ans_len - que_len)
if ans_len > que_len:
train_questions[i] += extra_array
else:
train_answers[i] += extra_array
if i < 10:
print (train_answers[i])
print()
print (train_questions[i])
print()
for i in range(0, len(valid_questions)):
ans_len = len(valid_answers[i])
que_len = len(valid_questions[i])
extra_array = [PAD] * abs(ans_len - que_len)
if ans_len > que_len:
valid_questions[i] += extra_array
else:
valid_answers[i] += extra_array
print (len(train_questions))
for i in range(25):
print(i, queint2W[i], "->", queW2int[queint2W[i]])
for i in range(25):
print(i, ansint2W[i], "->", ansW2int[ansint2W[i]])
print('unkcntr ', unkcntr)
#store train_questions train_answers valid_questions and valid_answers and word2int and int2word
data = {}
data['train_questions'] = train_questions
data['train_answers'] = train_answers
data['valid_questions'] = valid_questions
data['valid_answers'] = valid_answers
data['queW2int'] = queW2int
data['queint2W'] = queint2W
data['ansW2int'] = ansW2int
data['ansint2W'] = ansint2W
data['vocab'] = vocabulary
pickle_out = open("vubuntuProcessedData.pickle","wb")
pickle.dump(data, pickle_out)
pickle_out.close()