-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_city_csv.py
399 lines (321 loc) · 11.7 KB
/
filter_city_csv.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 6 23:42:43 2014
Code to read data extracted from xml wikipedia dump
and filtering it to extract relevant data from guide books
@author: dvats
"""
# filter the city csv file
# remove stopwords
# merge guides together
import numpy as np
import pandas as pd
from collections import Counter
from nltk.corpus import wordnet as wn
from nltk.corpus.reader import NOUN
from nltk.stem import wordnet
import nltk
import re
def get_wordnet_pos(treebank_tag):
"""
Get proper label for adjective, verb, noun, and adverb
"""
if treebank_tag.startswith('J'):
return wordnet.wordnet.ADJ
elif treebank_tag.startswith('V'):
return wordnet.wordnet.VERB
elif treebank_tag.startswith('N'):
return wordnet.wordnet.NOUN
elif treebank_tag.startswith('R'):
return wordnet.wordnet.ADV
else:
return ''
def ConvertToSingular(word):
"""
convert word to singular
(eventually not used in final code since I use the nltk package)
"""
synsets = wn.synsets(word, NOUN)
if len(synsets) > 0:
# then noun
lmtzr = wordnet.WordNetLemmatizer()
return lmtzr.lemmatize(word)
else:
return word
def MyStemWord(word,inp):
"""
Stems words using nltk and wordnet
"""
lmtzr = wordnet.WordNetLemmatizer()
if inp == '':
return lmtzr.lemmatize(word)
else:
return lmtzr.lemmatize(word, inp)
def FilterData(text, sw, i):
"""
input a string and output a clean string
text: string
sw: list of stopwords
i: guide number used for debugging
"""
print i
if type(1.0) == type(text):
return ' '
# remove punctuations
text = re.sub(r'\[.*?\]|\(.*?\)|\W', ' ', text)
ff = open("./word_files/remove_puncs.txt")
list_words = ff.read().split()
ff.close()
for ll in list_words:
text = text.replace(ll," ")
# remove stopwords and additional words
# stem words using wordnet
text = text.lower().split()
pp_tag = nltk.pos_tag(text)
text = np.array([MyStemWord(s[0], get_wordnet_pos(s[1])) \
for s in pp_tag if s[0] not in sw])
# remove all two letter words
text = np.array([s for s in text if len(s) > 2])
return " ".join(text)
def MergeStringColumns(df,strs):
"""
merge strings with the DataFrame df
return numpy array
"""
if type(df[strs[0]]) == float(1.0):
df[strs[0]] = ''
if len(strs) == 1:
return np.array(df[strs[0]])
if type(df[strs[1]]) == float(1.0):
df[strs[1]] = ''
tmp = df[strs[0]] + ' ' + df[strs[1]]
for i in range(len(strs)-1,len(strs)):
if type(df[strs[i]]) == float(1.0):
df[strs[i]] = ''
tmp = tmp + ' ' + df[strs[i]]
return np.array(tmp)
def GetTopWord(text,num_w):
"""
Return num_w most common words in the string text
"""
cc = Counter(text.split()).most_common(num_w)
return ' '.join([cw[0] for cw in cc])
def RemoveWordsInKeys(text,kys):
text = [s for s in text.split() if s not in kys]
return " ".join(text)
def RemoveLowFrequencyWords(guide_data, num_w):
"""
Find low frequency words and store these words in
a file ./word_files/extra_stopwords
NOTE: I only run this in the java code when learning a
statistical model for computational efficiency
"""
txt_all = ' '.join(guide_data)
cc = Counter(txt_all.split())
kys = np.array(cc.keys())
vls = np.array(cc.values())
kys = kys[vls < num_w]
print "Number of words to remove is " + str(len(kys))
# save list of words in kys
f = open('./word_files/extra_stopwords.txt', 'w')
for wds in kys:
f.write("%s\n" % wds)
f.close()
# remove words from guide_data that are in keys
#guide_data = np.array([RemoveWordsInKeys(text,kys) for text in guide_data])
def RemoveCityReference(guide_data,title):
"""
If the word in title appears in guide_data, remove it
"""
for i, tt in enumerate(title):
# clean the title first
gg = guide_data[i]
tt = tt.split('(')[0].lower()
gg = gg.replace(tt,'')
guide_data[i] = gg
return guide_data
def AddRegionsFromPrevious(main_data, main_data_copy):
"""
Some Region entries are not filled. This code tries to fill
it by looking at the region entries in LinkBefore
"""
region = np.array(main_data["Region"])
link_before = np.array(main_data["LinkBefore"])
region_copy = np.array(main_data_copy["Region"])
title = np.array(main_data_copy["title"])
link_copy = np.array(main_data_copy["LinkBefore"])
for i, reg in enumerate(region):
if reg == '':
# get link before
lb = link_before[i]
if lb != '':
lb = lb.lstrip().rstrip()
lb = ' '.join(lb.split('_'))
ind = np.where(title == lb)[0] # index for link before
if len(ind) > 0:
ind = ind[0]
reg_new = region_copy[ind]
if reg_new != '':
# print i
region[i] = reg_new
else:
# go two levels down
region[i] = region[i] + ' '.join(link_copy[ind].split('_'))
lb_new = link_copy[ind]
ind = np.where(title == lb_new)[0] #index for link before link before
if len(ind) > 0:
ind = ind[0]
reg_new = region_copy[ind]
if reg_new != '':
region[i] = reg_new
else: # go two levels down
region[i] = region[i] + ' '.join(link_copy[ind].split('_'))
lb_new = link_copy[ind]
ind = np.where(title == lb_new)[0] #index for link before link before
if len(ind) > 0:
ind = ind[0]
reg_new = region_copy[ind]
if reg_new != '':
region[i] = reg_new
else:
region[i] = region[i] + ' '.join(link_copy[ind].split('_'))
return region
"""
START OF MAIN CODE
"""
print "Reading File..."
main_data = pd.read_csv("./data/TravelData.csv").fillna(value = '')
print "Done Reading File..."
# load stop words
ff = open("./word_files/ExtraWords.txt")
sw = ff.read()
sw = sw.split()
ff.close()
ff = open("./word_files/extra_stopwords.txt")
sw_extra = set(ff.read().split())
main_data_copy = main_data.copy()
n_rows = np.max(main_data.count())
# list of things to accept
gg = ['usable_city', 'usable_district', \
'star_district', 'star_city', \
'guide_district', 'guide_city', \
'outline_city', 'outline_district']
# filter data according to GuideClass
combine = ["See", "Do", "Learn", "Eat", "Drink"]
main_data = main_data[main_data["GuideClass"].isin(gg)].reset_index(drop=True)
get_text = MergeStringColumns(main_data, combine)
print "Filtering Data.."
guide_data = np.array([FilterData(text, sw, i) for i, text in enumerate(get_text)])
main_data["all_data"] = guide_data
print "Done Filtering Data ..."
# drop the columns in combine
for cols in combine:
main_data = main_data.drop(cols, 1)
# remove strings where guide_data = ' '
ind = np.array(main_data["all_data"]) != ''
main_data = main_data[ind]
main_data.index = range(len(main_data))
guide_data = np.array(main_data["all_data"])
title = np.array(main_data["title"])
# now merge different guides
# if the title of a guide is text1/text2, move contents to
# guide with title text1
print "Merging guides..."
ind = [True] * int(len(title))
for i, tt in enumerate(title):
tt = tt.split('/')
if len(tt) > 1:
# get first element
#print ' '.join(tt)
tt = tt[0].lstrip().rstrip()
index_tt = np.where(title == tt)[0]
# merge guides
guide_data[index_tt] = guide_data[index_tt] + ' ' + guide_data[i]
ind[i] = False
# reassign guide data
# merge New York City separately since it is not labeled properly
ind_nyc = np.where(title == "New York City")[0]
i1 = np.where(title == 'Manhattan')[0]
i2 = np.where(title == 'Brooklyn')[0]
i3 = np.where(title == 'Queens')[0]
i4 = np.where(title == 'Bronx')[0]
i5 = np.where(title == 'Staten Island')[0]
ind[i1[0]] = False
ind[i2[0]] = False
ind[i3[0]] = False
ind[i4[0]] = False
ind[i5[0]] = False
guide_data[ind_nyc] = (guide_data[ind_nyc] + ' ' + guide_data[i1] + ' ' +
guide_data[i2] + ' ' +
guide_data[i3] + ' ' +
guide_data[i4] + ' ' +
guide_data[i5])
main_data["all_data"] = guide_data
main_data = main_data[ind]
main_data.index = range(len(main_data))
print "Done merging guides..."
# delete guides that have very less information
word_num = np.array([len(g.split()) for g in np.array(main_data["all_data"])])
main_data = main_data[word_num > 50].reset_index(drop=True)
# try to populate region properly so that county/state can be accurately
# detected
print "Detecting Country/Region of every city"
# update region in main_data
region = AddRegionsFromPrevious(main_data,main_data_copy)
main_data["Region"] = region
print "Done detecting Country/Region of every city"
# remove words that only occur once
# precomputed to avoid computations
#print "Now Removing Words that occur once"
#guide_data = np.array(main_data["all_data"])
print "Removing low frequency words"
RemoveLowFrequencyWords(np.array(main_data["all_data"]), 20)
print "Done removing low frequency words"
guide_data = np.array(main_data["all_data"])
title = np.array(main_data["title"])
# if guide_data contains a reference to its city, remove it
print "Removing city reference from guide"
guide_data = RemoveCityReference(guide_data,title)
main_data["all_data"] = guide_data
print "Done removing city reference"
# write the files
# run a bash command to delete files in ./temp_dir/
for i, tt in enumerate(guide_data):
f = open("./guide_data/" + str(i) + ".txt", 'w')
f.write(tt)
f.close()
# Find top words
main_data["top_words"] = np.array([GetTopWord(text,10) for text in guide_data])
# write file to a new csv file
main_data.to_csv('./data/FilteredTravelData.csv')
# code not needed
#main_data["all_data"] = guide_data
## already saved in sw_extra from a previous iteration
#part_word = "sjhshksnsmbdkwnd" # some random word
#pw = " " + part_word + " "
#all_text = pw.join(guide_data)
#regex = re.compile(r'\b(' + r'|'.join(sw_extra) + r')\b\s*')
#out = regex.sub("", all_text)
#guide_data = np.array(out.split(part_word))
#main_data["all_data"] = guide_data
#main_data = pd.read_csv("./data/FilteredTravelData.csv").fillna('')
#ind = np.array(main_data["all_data"] == '')
#word_num = np.array([len(gg.split()) for gg in np.array(main_data["all_data"])])
#ind = ind + np.array(word_num < 50)
#ind = np.logical_not(ind)
#
#guide_data = np.array(main_data["all_data"])[ind]
#title = np.array(main_data["title"])[ind]
#top_words = np.array(main_data["top_words"])[ind]
#Region = np.array(main_data["Region"])[ind]
#url = np.array(main_data["url"])[ind]
#word_num = word_num[ind]
#RemoveLowFrequencyWords(guide_data, 20)
#
#for i, tt in enumerate(guide_data):
# f = open("./guide_data/" + str(i) + ".txt", 'w')
# f.write(tt)
# f.close()
#
#guide_data = np.array(main_data["all_data])
#RemoveLowFrequencyWords(guide_data, 20)