-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
433 lines (363 loc) · 13.6 KB
/
utils.py
File metadata and controls
433 lines (363 loc) · 13.6 KB
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
"""
Script containing various utility functions for
topic search by whitening algorithm
Copyright (C) 2016 Avik Ray
Code by Avik Ray
Contact: avik@utexas.edu
"""
import ConfigParser
import os
import numpy as np
import scipy
import scipy.io
import urllib2
import socket
import ssl
#---------------------------------------------------------------
# Splits word by document matrix into train, val, and test sets
#---------------------------------------------------------------
def splitData(corpusMatFile,config):
# Load word by document matrix
M = scipy.io.loadmat(corpusMatFile)['M']
# Get number of documents in corpus
M = M.tocsc()
numWords = M.shape[0]
numDocs = M.shape[1]
print 'Number of words = ' + str(numWords)
print 'Number of documents = ' + str(numDocs)
# Train-Val-Test split
fraction = [config['trainFrac'],config['valFrac'],config['testFrac']]
filterFile = config['filterFile']
fw = open(filterFile,'w')
tot = sum(fraction)
trainMin = 0
trainMax = fraction[0]/float(tot)
valMin = fraction[0]/float(tot)
valMax = (fraction[0]+fraction[1])/float(tot)
testMin = (fraction[0]+fraction[1])/float(tot)
testMax = (fraction[0]+fraction[1]+fraction[2])/float(tot)
for i in range(numDocs):
r = np.random.rand()
if r>=trainMin and r<trainMax:
fw.write("1 ")
elif r>=valMin and r<valMax:
fw.write("2 ")
else:
fw.write("3 ")
fw.close()
print "Split complete ! Filter file written."
return
#---------------------------------------------------------------
# Reads filter file with train, val, and test splits
#---------------------------------------------------------------
def readFilter(config):
filterFile = config['filterFile']
filterList = [[],[],[]]
print 'Reading filters ...'
with open(filterFile,'r') as fr:
line = fr.readline()
line = line.strip().split()
for i in range(len(line)):
if line[i]=="1":
filterList[0].append(i)
elif line[i]=="2":
filterList[1].append(i)
else:
filterList[2].append(i)
return filterList
#---------------------------------------------------------------
# Gets word index from dictionary
#---------------------------------------------------------------
def getWordIndex(dictFile, word):
fr = open(dictFile,'r')
index = 0
for line in fr:
if line.strip() == word:
print 'Word found! Index = ' + str(index)
fr.close()
return index
else:
index += 1
fr.close()
print 'Word not found!'
return -1
#---------------------------------------------------------------
# Gets word in dictionary given an index
#---------------------------------------------------------------
def getWord(dictFile, index):
fr = open(dictFile,'r')
count = 0
for line in fr:
if count == index:
word = line.strip()
fr.close()
return word
else:
count += 1
fr.close()
print 'Index exceeds dictionary size!'
return ''
#---------------------------------------------------------------
# Saves top N words in the topic retuned by search by whiten
# algorithm
#---------------------------------------------------------------
def saveTopWords(dictFile, N, mu, labelWord):
#Sort in descending order
sortLikelihoodIdx = np.argsort(-mu)
print 'Printing top ' + str(N) + ' words from the topic ...'
print '------------------------'
topCount = 0
dirName = 'res'
fileName = labelWord + '_topwords_whiten.txt'
filePath = os.path.join(dirName,fileName)
fw = open(filePath,'w')
top10List = []
for i in range(len(mu)):
if i < N:
topWord = getWord(dictFile,sortLikelihoodIdx[i])
if i<10:
if len(topWord)>4:
# Removes zzz_ from start of word (zzz_ is used for tokening proper nouns)
if topWord[:4]=="zzz_":
wordnzzz = topWord[4:]
top10List.append(wordnzzz)
topWord = wordnzzz
else:
top10List.append(topWord)
else:
top10List.append(topWord)
print str(i+1) + '<< ' + topWord + ' >> prob = ' + str(mu[sortLikelihoodIdx[i]])
fw.write(topWord + ' ' + str(mu[sortLikelihoodIdx[i]]) + '\n')
if mu[sortLikelihoodIdx[i]] > 0:
topCount += 1
fw.close()
print 'Result saved !'
return top10List
#---------------------------------------------------------------
# Saves top N words in the topic retuned by search by NMF
# algorithm
#---------------------------------------------------------------
def saveTopWordsNMF(dictFile, N, mu, labelWord):
#Sort in descending order
sortLikelihoodIdx = np.argsort(-mu)
print 'Printing top ' + str(N) + ' words from the topic ...'
print '------------------------'
topCount = 0
dirName = 'res'
fileName = labelWord + '_topwords_NMF.txt'
filePath = os.path.join(dirName,fileName)
fw = open(filePath,'w')
top10List = []
for i in range(len(mu)):
if i < N:
topWord = getWord(dictFile,sortLikelihoodIdx[i])
if i<10:
if len(topWord)>4:
# Removes zzz_ from start of word (zzz_ is used for tokening proper nouns)
if topWord[:4]=="zzz_":
wordnzzz = topWord[4:]
top10List.append(wordnzzz)
topWord = wordnzzz
else:
top10List.append(topWord)
else:
top10List.append(topWord)
print str(i+1) + '<< ' + topWord + ' >> prob = ' + str(mu[sortLikelihoodIdx[i]])
fw.write(topWord + ' ' + str(mu[sortLikelihoodIdx[i]]) + '\n')
if mu[sortLikelihoodIdx[i]] > 0:
topCount += 1
fw.close()
print 'Result saved !'
return top10List
#---------------------------------------------------------------
# Saves top N words in the topic retuned by search by SS-NMF
# algorithm
#---------------------------------------------------------------
def saveTopWordsSSNMF(dictFile, N, mu, labelWord):
#Sort in descending order
sortLikelihoodIdx = np.argsort(-mu)
print 'Printing top ' + str(N) + ' words from the topic ...'
print '------------------------'
topCount = 0
dirName = 'res'
fileName = labelWord + '_topwords_SSNMF.txt'
filePath = os.path.join(dirName,fileName)
fw = open(filePath,'w')
top10List = []
for i in range(len(mu)):
if i < N:
topWord = getWord(dictFile,sortLikelihoodIdx[i])
if i<10:
if len(topWord)>4:
# Removes zzz_ from start of word (zzz_ is used for tokening proper nouns)
if topWord[:4]=="zzz_":
wordnzzz = topWord[4:]
top10List.append(wordnzzz)
topWord = wordnzzz
else:
top10List.append(topWord)
else:
top10List.append(topWord)
print str(i+1) + '<< ' + topWord + ' >> prob = ' + str(mu[sortLikelihoodIdx[i]])
fw.write(topWord + ' ' + str(mu[sortLikelihoodIdx[i]]) + '\n')
if mu[sortLikelihoodIdx[i]] > 0:
topCount += 1
fw.close()
print 'Result saved !'
return top10List
#---------------------------------------------------------------
# Computes PMI score for search by Whitening algorithm based on
# top 20 words in the topic
#---------------------------------------------------------------
def computePMIWhiten(labelWord):
dirName = 'res'
fileName = labelWord + '_topwords_whiten.txt'
filePath = os.path.join(dirName,fileName)
fr = open(filePath,'r')
pmi = 0.0
count = 0
print 'Computing top 20 pmi ...'
for i in range(20):
line = fr.readline()
line = line.strip().split()
word1 = labelWord
word2 = line[0]
query = "http://palmetto.aksw.org/palmetto-webapp/service/npmi?words="+word1+"%20"+word2
req = urllib2.Request(query)
try:
timeoutTimeSec = 5
response = urllib2.urlopen(req, timeout = timeoutTimeSec)
pmi += float(response.read())
count += 1
response.close()
except (ssl.SSLError, urllib2.URLError, socket.timeout, socket.error), e:
print 'Timeout Error! Could not connect to palmetto !'
print e
fr.close()
if count>0:
pmi = pmi/float(count)
else:
pmi = 0
return pmi
#---------------------------------------------------------------
# Computes PMI score for search by NMF algorithm based on
# top 20 words in the topic
#---------------------------------------------------------------
def computePMINMF(labelWord):
dirName = 'res'
fileName = labelWord + '_topwords_NMF.txt'
filePath = os.path.join(dirName,fileName)
fr = open(filePath,'r')
pmi = 0.0
count = 0
print 'Computing top 20 pmi ...'
for i in range(20):
line = fr.readline()
line = line.strip().split()
word1 = labelWord
word2 = line[0]
query = "http://palmetto.aksw.org/palmetto-webapp/service/npmi?words="+word1+"%20"+word2
req = urllib2.Request(query)
try:
timeoutTimeSec = 5
response = urllib2.urlopen(req, timeout = timeoutTimeSec)
pmi += float(response.read())
count += 1
except (ssl.SSLError, urllib2.URLError, socket.timeout, socket.error), e:
print 'Timeout Error! Could not connect to palmetto !'
print e
fr.close()
if count>0:
pmi = pmi/float(count)
else:
pmi = 0
return pmi
#---------------------------------------------------------------
# Computes PMI score for search by SS-NMF algorithm based on
# top 20 words in the topic
#---------------------------------------------------------------
def computePMISSNMF(labelWord):
dirName = 'res'
fileName = labelWord + '_topwords_SSNMF.txt'
filePath = os.path.join(dirName,fileName)
fr = open(filePath,'r')
pmi = 0.0
count = 0
print 'Computing top 20 pmi ...'
for i in range(20):
line = fr.readline()
line = line.strip().split()
word1 = labelWord
word2 = line[0]
query = "http://palmetto.aksw.org/palmetto-webapp/service/npmi?words="+word1+"%20"+word2
req = urllib2.Request(query)
try:
timeoutTimeSec = 5
response = urllib2.urlopen(req, timeout = timeoutTimeSec)
pmi += float(response.read())
count += 1
except (ssl.SSLError, urllib2.URLError, socket.timeout, socket.error), e:
print 'Timeout Error! Could not connect to palmetto !'
print e
fr.close()
if count>0:
pmi = pmi/float(count)
else:
pmi = 0
return pmi
#---------------------------------------------------------------
# Load config class from config file
#---------------------------------------------------------------
def loadConfig(configFile):
#configFile = "configCorpus.txt"
cfg = ConfigParser.ConfigParser()
try:
cfg.read(configFile)
# Load configs
config = {}
config['corpusFile'] = cfg.get("SETTING","corpus_file")
config['dictFile'] = cfg.get("SETTING","dictionary_file")
config['matFile'] = cfg.get("SETTING","mat_file_name")
config['filterFile'] = cfg.get("SETTING","filter_file")
config['stopwordsFile'] = cfg.get("SETTING","stopwords_file")
config['trainFrac'] = cfg.getfloat("SETTING","train_fraction")
config['valFrac'] = cfg.getfloat("SETTING","validation_fraction")
config['testFrac'] = cfg.getfloat("SETTING","test_fraction")
config['rareWordTh'] = cfg.getint("SETTING","rare_word_threshold")
config['K'] = cfg.getint("SETTING","num_topics")
config['N'] = cfg.getint("SETTING","top_words_to_display")
config['alpha0'] = cfg.getfloat("SETTING","alpha_0")
config['weight'] = cfg.getfloat("SETTING","weight")
return config
except (ConfigParser.Error,ConfigParser.NoSectionError), e:
print 'Error loading config file.'
print e
return {}
#---------------------------------------------------------------
# Preprocess dataset.
# 1) Converts bag of words to sparse matrix
# 2) Removes rare and stop words to truncate the vocabulary
#---------------------------------------------------------------
def preprocess(config):
matFile = config['matFile']
corpusFile = config['corpusFile']
rareWordTh = config['rareWordTh']
print 'Converting corpus to mat file ...'
cmdStr = 'python uci_to_scipy.py ' + corpusFile + ' ' + matFile
os.system(cmdStr)
print 'Copying stopwords file ...'
stopwordsFile = config['stopwordsFile']
fw = open('stopwords.txt','w')
fr = open(stopwordsFile,'r')
for line in fr:
fw.write(line)
fr.close()
fw.close()
print 'Removing rare words ...'
rareWordTh = config['rareWordTh']
dictFile = config['dictFile']
cmdStr = 'python truncate_vocabulary.py ' + matFile + ' ' + dictFile + ' ' + str(rareWordTh)
os.system(cmdStr)
truncMatFile = matFile + '.trunc'
truncDictFile = dictFile + '.trunc'
return (truncMatFile, truncDictFile)