-
Notifications
You must be signed in to change notification settings - Fork 0
/
wikiBEAGLE.py
491 lines (433 loc) · 13.7 KB
/
wikiBEAGLE.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
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#set the length of the vectors (don't change this after you've begun learning)
vectorLength = 2**10
########
# Import all the modules we'll need
########
import sys
import datetime
import multiprocessing
import signal
import time
import string
import re
import numpy
import cPickle
import urllib2
import os
import shutil
import tty
import select
import nltk.data
import math
########
# Load the english sentence punkt data
########
tokenizer = nltk.data.load('file:./english.pickle')
########
# Check if multiple cores were requested
########
if len(sys.argv)==1:
numCores = 1
else:
numCores = int(sys.argv[1])
########
# Classes to handle quitting safely (from: http://code.activestate.com/recipes/203830/)
########
class NotTTYException(Exception): pass
class TerminalFile:
def __init__(self,infile):
if not infile.isatty():
raise NotTTYException()
self.file=infile
#prepare for getch
self.save_attr=tty.tcgetattr(self.file)
newattr=self.save_attr[:]
newattr[3] &= ~tty.ECHO & ~tty.ICANON
tty.tcsetattr(self.file, tty.TCSANOW, newattr)
def __del__(self):
#restoring stdin
import tty #required this import here
tty.tcsetattr(self.file, tty.TCSADRAIN, self.save_attr)
def getch(self):
if select.select([self.file],[],[],0)[0]:
c=self.file.read(1)
else:
c=''
return c
########
# Some text cleaning functions
########
#define a function to delete some html in text
def delHtml(text):
done = False
while not done:
if '<' in text:
i = 0
while text[i]!='<':
i += 1
if '>' in text:
j = i
while text[j]!='>':
j += 1
text = text[0:i]+text[(j+1):]
else:
done = True
else:
done = True
return(text)
#define a function to delete anything inside parens
def removeEncaps(text):
for encaps in [['(',')'],['[',']']]:
done = False
while not done:
if encaps[0] in text:
i = 0
while text[i]!=encaps[0]:
i += 1
if encaps[1] in text:
j = i
while text[j]!=encaps[1]:
j += 1
text = text[0:i]+text[(j+1):]
else:
done = True
else:
done = True
return(text)
#define a function that cleans up a page of text from wikipedia, returning a list of sentences
def cleanPage(page):
pageLines = page.readlines()
pageLinesFinal = []
for thisPageLine in pageLines:
if thisPageLine[0:3]=='<p>':
line = delHtml(thisPageLine)
line = removeEncaps(line)
line = line.replace('\n','')
line = line.lower()
line = line.replace('/',' ')
line = line.replace('citation needed',' ')
line = ''.join(re.findall('[a-z 0-9 ,;:.!?-]', line))
line = line.replace('mr.','mr')
line = line.replace('mrs.','mrs')
line = line.replace('ms.','ms')
line = line.replace('dr.','dr')
line = line.replace('ex.','ex')
line = line.replace('e.g.','eg')
line = line.replace('etc.','etc')
sentenceList = tokenizer.tokenize(line)
for line in sentenceList:
line = line.replace(',',' , ')
line = line.replace(';',' ; ')
line = line.replace(':',' : ')
line = line.replace('.',' . ')
line = line.replace('!',' ! ')
line = line.replace('?',' ? ')
line = line.replace(' - ',' ')
line = line.replace('-',' - ')
line = line.strip()
while ' ' in line:
line = line.replace(' ',' ')
if line!='':
words = line.split(' ')
j = 0
while j<len(words):
if ('http' in words[j]) or ('ftp' in words[j]) or ('linkback' in words[j]) or (len(words[j])>30) or (words[j]==''):
trash = words.pop(j)
del trash
else:
j = j+1
if len(words)>2:
pageLinesFinal.append('` '+' '.join(words))
if pageLinesFinal[-1][-1]=='.':
pageLinesFinal[-1] = pageLinesFinal[-1][:-1]+"'"
return pageLinesFinal
########
# Functions borrowed from holoword.py
########
def normalize(a):
'''
Normalize a vector to length 1.
'''
return a / numpy.sum(a**2.0)**0.5
def cconv(a, b):
'''
Computes the circular convolution of the (real-valued) vectors a and b.
'''
return numpy.fft.ifft(numpy.fft.fft(a) * numpy.fft.fft(b)).real
def ordConv(a, b, p1, p2):
'''
Performs ordered (non-commutative) circular convolution on the vectors a and
b by first permuting them according to the index vectors p1 and p2.
'''
return cconv(a[p1], b[p2])
def seqOrdConv(l , p1, p2 ):
'''
Given a list of vectors, iteratively convolves them into a single vector
(i.e., "binds" them together: (((1+2)+3)+4)+5 ). Used to combine characters in ngrams.
'''
return reduce(lambda a,b: normalize(ordConv(a, b, p1, p2)), l)
#modified from holoword.py to use the number vectors instead of characters
def getOpenNGrams(word, charVecList, charPlaceholder):
ngrams = []
sizes = range(len(word))[2:len(word)]
sizes.append(len(word))
for size in sizes:
for i in xrange(len(word)):
if i+size > len(word): break
tmp = []
for char in word[i:(i+size)]:
tmp.append(charVecList[char])
ngrams.append(tmp)
if i+size == len(word): continue
for b in xrange(1, size):
for e in xrange(1, len(word)-i-size+1):
tmp = []
for char in word[i:(i+b)]:
tmp.append(charVecList[char])
tmp.append(charPlaceholder)
for char in word[(i+b+e):(i+e+size)]:
tmp.append(charVecList[char])
ngrams.append(tmp)
return ngrams
########
# Initialize the character, placeholder and permutation vectors
########
numpy.random.seed(112358) #set the numpy random seed for (some) replicability
chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '`', "'", '-', ',', ';', ':', '.', '!', '?']
charVecList = {}
for char in chars:
charVecList[char] = normalize(numpy.random.randn(vectorLength) * vectorLength**-0.5)
charPlaceholder = normalize(numpy.random.randn(vectorLength) * vectorLength**-0.5)
wordPlaceholder = normalize(numpy.random.randn(vectorLength) * vectorLength**-0.5)
perm1 = numpy.random.permutation(vectorLength)
perm2 = numpy.random.permutation(vectorLength)
########
# Initialize an object that can open urls
########
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
########
# Initialize some multiprocessing queues
########
queueToLearner = multiprocessing.Queue()
queueFromLearner = multiprocessing.Queue()
########
# Define the loop performed by each learner
########
def learnerLoop(coreNum,queueToLearner,queueFromLearner):
os.mkdir('wikiBEAGLEdata/'+str(coreNum))
indexList = {}
coList = {}
while True:
page = 0
while page==0:
if not queueToLearner.empty():
try:
del orderList
except:
pass
tmp = open('wikiBEAGLEdata/'+str(coreNum)+'/indexList','wb')
cPickle.dump(indexList,tmp)
tmp.close()
tmp = open('wikiBEAGLEdata/'+str(coreNum)+'/coList','wb')
cPickle.dump(coList,tmp)
tmp.close()
queueFromLearner.put('done')
sys.exit()
try:
page = opener.open('http://en.wikipedia.org/wiki/Special:Random')
except:
pass
if not page==0:
pageLines = cleanPage(page)
if len(pageLines)==0:
page = 0
for line in pageLines:
if not queueToLearner.empty():
try:
del orderList
except:
pass
tmp = open('wikiBEAGLEdata/'+str(coreNum)+'/indexList','wb')
cPickle.dump(indexList,tmp)
tmp.close()
tmp = open('wikiBEAGLEdata/'+str(coreNum)+'/coList','wb')
cPickle.dump(coList,tmp)
tmp.close()
queueFromLearner.put('done')
sys.exit()
uniqueWords = []
words = line.split(' ')
queueFromLearner.put(['tokens',len(words)])
for word in words:
if word not in coList:
coList[word] = {}
if word not in uniqueWords:
uniqueWords.append(word)
queueFromLearner.put(['words',uniqueWords])
for word in uniqueWords:
if (not (word in indexList)): #word is new to this learner
newSize = len(indexList)+1
if newSize==1: #first new word, initialize lists
formList = numpy.memmap('wikiBEAGLEdata/'+str(coreNum)+'/form', mode='w+', dtype='float', shape=(newSize,vectorLength))
orderList = numpy.memmap('wikiBEAGLEdata/'+str(coreNum)+'/order', mode='w+', dtype='float', shape=(newSize,vectorLength))
else: #resize existing lists
del orderList
formList = numpy.memmap('wikiBEAGLEdata/'+str(coreNum)+'/form', mode='r+', dtype='float', shape=(newSize,vectorLength))
orderList = numpy.memmap('wikiBEAGLEdata/'+str(coreNum)+'/order', mode='r+', dtype='float', shape=(newSize,vectorLength))
formList[newSize-1] = normalize(numpy.add.reduce([seqOrdConv(ngram,perm1,perm2) for ngram in getOpenNGrams(word, charVecList, charPlaceholder)]))
indexList[word] = newSize-1
#code co-occurrences
for word in uniqueWords:
for otherWord in uniqueWords:
if otherWord in coList[word]:
coList[word][otherWord] += 1
else:
coList[word][otherWord] = 1
#perform order encoding
for j in range(len(words)):
word = words[j]
index = indexList[word]
for order in [1,2,3,4,5,6,7]: #only encode up to 7-grams
order = order + 1
for k in range(order):
if (j-k)>=0:
if (j+(order-k))<=len(words):
wordsTmp = words[(j-k):(j+(order-k))]
forms = [formList[indexList[wordTmp]] for wordTmp in wordsTmp]
forms[k] = wordPlaceholder
orderList[index] += seqOrdConv(forms,perm1,perm2)
#done a sentence
del(forms) #so that we can resize formList later
orderList.flush()
formList.flush()
queueFromLearner.put('sentence')
queueFromLearner.put('page')
########
# Define a function to start the learners
########
def filterFileList(files,filter):
i = 0
while i < len(files):
if files[i][0] in filter:
trash = files.pop(i)
del trash
else:
i = i + 1
return files
def startLearners():
if not os.path.exists('wikiBEAGLEdata'):
os.mkdir('wikiBEAGLEdata')
runNum = 0
else:
files = os.listdir('wikiBEAGLEdata')
files = filterFileList(files,['.','c','o','f','p','i','w'])
if len(files)>0:
runNum = max(map(int,files))+1
else:
runNum = 0
for i in range(numCores):
exec('learnerProcess'+str(i)+' = multiprocessing.Process(target=learnerLoop,args=('+str(runNum+i)+',queueToLearner,queueFromLearner,))')
exec('learnerProcess'+str(i)+'.start()')
########
# Define a function to stop the learners
########
def killAndCleanUp(pageNum, sentenceNum, tokenNum, timeTaken):
learners_alive = numCores
wordNum = len(wordList)
timeToPrint = str(datetime.timedelta(seconds=round(timeTaken)))
os.system('clear')
print 'wikiBEAGLE\n\nPages: '+str(pageNum)+'\nSentences: '+str(sentenceNum)+'\nWords: '+str(wordNum)+'\nTokens: '+str(tokenNum)+'\nTime: '+timeToPrint
print '\nKilling learners... still alive: ' + str(learners_alive)
lastUpdateTime = time.time()
queueToLearner.put('die')
dataList = []
while learners_alive>0:
if queueFromLearner.empty():
time.sleep(1)
else:
message = queueFromLearner.get()
if message=='done':
learners_alive -= 1
elif message=='page':
pageNum += 1
elif message=='sentence':
sentenceNum += 1
elif message[0]=='tokens':
tokenNum = tokenNum + message[1]
elif message[0]=='words':
for word in message[1]:
if not (word in wordList):
wordList.append(word)
if (time.time()-lastUpdateTime)>1:
timeTaken += (time.time()-lastUpdateTime)
lastUpdateTime = time.time()
wordNum = len(wordList)
timeToPrint = str(datetime.timedelta(seconds=round(timeTaken)))
os.system('clear')
print 'wikiBEAGLE\n\nPages: '+str(pageNum)+'\nSentences: '+str(sentenceNum)+'\nWords: '+str(wordNum)+'\nTokens: '+str(tokenNum)+'\nTime: '+timeToPrint
print '\nKilling learners... still alive: ' + str(learners_alive)
timeTaken += (time.time()-lastUpdateTime)
wordNum = len(wordList)
timeToPrint = str(datetime.timedelta(seconds=round(timeTaken)))
os.system('clear')
print 'wikiBEAGLE\n\nPages: '+str(pageNum)+'\nSentences: '+str(sentenceNum)+'\nWords: '+str(wordNum)+'\nTokens: '+str(tokenNum)+'\nTime: '+timeToPrint+'\n\n'
tmp = open('wikiBEAGLEdata/progress.txt','w')
tmp.write('\n'.join(map(str,[pageNum, sentenceNum, wordNum, tokenNum, int(round(timeTaken))])))
tmp.close()
tmp = open('wikiBEAGLEdata/wordList','wb')
cPickle.dump(wordList,tmp)
tmp.close()
return [pageNum,sentenceNum,tokenNum]
########
# Initialize progress record
########
if os.path.exists('wikiBEAGLEdata/progress.txt'):
tmp = open('wikiBEAGLEdata/progress.txt','r')
pageNum, sentenceNum, wordNum, tokenNum, timeTaken = map(int,tmp.readlines())
tmp.close()
tmp = open('wikiBEAGLEdata/wordList','rb')
wordList = cPickle.load(tmp)
tmp.close()
else:
pageNum = 0
sentenceNum = 0
tokenNum = 0
timeTaken = 0
wordList = []
lastUpdateTime = time.time()
wordNum = len(wordList)
timeToPrint = str(datetime.timedelta(seconds=round(timeTaken + (time.time()-lastUpdateTime))))
os.system('clear')
print 'wikiBEAGLE\n\nPages: '+str(pageNum)+'\nSentences: '+str(sentenceNum)+'\nWords: '+str(wordNum)+'\nTokens: '+str(tokenNum)+'\nTime: '+timeToPrint+'\n\n(Press "q" to quit)'
########
# Go!
########
stdin = TerminalFile(sys.stdin)
startLearners()
while True:
if stdin.getch()=='q':
killAndCleanUp(pageNum, sentenceNum, tokenNum, timeTaken)
sys.exit()
else:
if queueFromLearner.empty():
time.sleep(1)
else:
message = queueFromLearner.get()
if message=='page':
pageNum += 1
elif message=='sentence':
sentenceNum += 1
elif message[0]=='tokens':
tokenNum = tokenNum + message[1]
elif message[0]=='words':
for word in message[1]:
if not (word in wordList):
wordList.append(word)
if (time.time()-lastUpdateTime)>1:
timeTaken += (time.time()-lastUpdateTime)
lastUpdateTime = time.time()
wordNum = len(wordList)
timeToPrint = str(datetime.timedelta(seconds=round(timeTaken)))
os.system('clear')
print 'wikiBEAGLE\n\nPages: '+str(pageNum)+'\nSentences: '+str(sentenceNum)+'\nWords: '+str(len(wordList))+'\nTokens: '+str(tokenNum)+'\nTime: '+timeToPrint+'\n\n(Press "q" to quit)'