-
Notifications
You must be signed in to change notification settings - Fork 6
/
find_testfiles2.py
189 lines (166 loc) · 5.17 KB
/
find_testfiles2.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
from __future__ import division
import math
import time
import os
import csv
import sys
import re
from twokenize import tokenize
import nltk
from sklearn.externals import joblib
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def is_url(s):
return s.startswith('http://') or s.startswith('https://') or s.startswith('ftp://') \
or s.startswith('ftps://') or s.startswith('smb://')
def clean_str(string, TREC=False):
"""
Tokenization/string cleaning for all datasets except for SST.
Every dataset is lower cased except for TREC
"""
string = re.sub(r"\'m", " \'m", string)
string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"\'ve", " \'ve", string)
string = re.sub(r"n\'t", " n\'t", string)
string = re.sub(r"\'re", " \'re", string)
string = re.sub(r"\'d", " \'d", string)
string = re.sub(r"\'ll", " \'ll", string)
string = re.sub(r"`", " ` ", string)
string = re.sub(r",", " , ", string)
return string.strip()
def process_token(c, word):
"""
Use NLTK to replace named entities with generic tags.
Also replace URLs, numbers, and paths.
"""
nodelist = ['PERSON', 'ORGANIZATION', 'GPE', 'LOCATION', 'FACILITY', 'GSP']
if hasattr(c, 'label'):
if c.label() in nodelist:
return "__%s__" % c.label()
if is_url(word):
return "__URL__"
elif is_number(word):
return "__NUMBER__"
elif os.path.isabs(word):
return "__PATH__"
return word
def process_line(s, clean_string=True):
"""
Processes a line by iteratively calling process_token.
"""
if clean_string:
s = clean_str(s)
tokens = tokenize(s)
sent = nltk.pos_tag(tokens)
chunks = nltk.ne_chunk(sent, binary=False)
return [process_token(c,token).lower().encode('UTF-8') for c,token in map(None, chunks, tokens)]
def writeFiles(csvname, data, listbool=False, overwrite=False):
"""
Writes to .csv files (overwrite optional).
"""
with open(csvname,'a+') as out:
csv_out = csv.writer(out)
for row in data:
if listbool:
for col in row:
csv_out.writerow(col)
else:
csv_out.writerow(row)
def getFilesFromCsv(files):
"""
Produces a list of files from some csv file
"""
filelist = []
with open(files, 'r') as c1:
c1 = csv.reader(c1, delimiter = ',')
for f, folder in c1:
filelist.append([f, folder])
return filelist
def makeBadfileDict(badfiles):
"""
Produces a dictionary of badfiles
"""
filedict = {}
with open(badfiles, 'r') as c1:
c1 = csv.reader(c1, delimiter = ',')
for f, folder in c1:
filedict[f + folder] = f
return filedict
def getRawfiles(rawfolder):
"""
Produces a list of all dialogue files
"""
filelist = []
folders = [f for f in os.listdir(rawfolder)]
for folder in folders:
rawfiles = rawfolder + folder
files = [f for f in os.listdir(rawfiles)]
for f in files:
filelist.append([f, folder])
return filelist
def getUtterlist(c2):
"""
Generates the list of utterances from the file. Can also return 'raw' list without processing.
"""
utterlist = []
for row in c2:
row = row.split('\t')
if any(row[3:]):
utter = ''.join(row[3:])
utter_tok = process_line(utter)
utter = ' '.join(utter_tok)
utterlist.append(utter)
return utterlist
def getUtterlistFromTest(context):
return context.split(' __EOS__ ')
def makeUtterDict(testset):
"""
Makes a dictionary of all the contexts in the test set.
"""
utterdict = {}
with open(testset, 'r') as c1:
c1 = csv.reader(c1, delimiter = ',')
for context, response, flag in c1:
if int(flag) == 1:
total_context = context + ' __EOS__ ' + response
test_utterlist = getUtterlistFromTest(total_context)[0:3]
test_utterlist = ' __EOS__ '.join(test_utterlist)
utterdict[test_utterlist] = 0
return utterdict
def findTestfiles(dialoguepath, utterdict, filelist, newtestpath, filesperprint = 100):
newtestlist = []
k = 0
for folder, f in filelist:
newpath = dialoguepath + folder + '/' + f
with open(newpath, 'r') as c1:
uttersplit = c1.read().split('\n')
utterlist = getUtterlist(uttersplit)[0:3]
new_context = ' __EOS__ '.join(utterlist)
if new_context in utterdict:
newtestlist.append([f, folder])
if k % filesperprint == 0:
print 'Finished file ' + str(k)
writeFiles(newtestpath, newtestlist)
newtestlist = []
k += 1
badfiles = './badfiles_4.csv'
dialoguepath = './dialogs/'
testset = './testset_1.csv'
seg_index = sys.argv[1]
segfile = './dialogsegs/dialoguesegment_' + str(seg_index) + '.csv'
newtestpath = './newtestfiles_' + str(seg_index) + '.csv'
if __name__ == '__main__':
print 'Retrieving file list'
all_file_list = getFilesFromCsv(segfile)
bad_file_list = getFilesFromCsv(badfiles)
all_file_list = list(set(tuple(x) for x in all_file_list) - set(tuple(x) for x in bad_file_list))
print 'Making testset dictionary'
utterdict = makeUtterDict(testset)
print 'Done initialization. Finding testfiles'
findTestfiles(dialoguepath, utterdict, all_file_list, newtestpath)
#all_file_list = getRawfiles(dialoguepath)
#train_file_list = list(set(tuple(x) for x in all_file_list) - set(tuple(x) for x in test_file_list)) - set(tuple(x) for x in bad_file_list))