-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallelGram.py
485 lines (368 loc) · 16.9 KB
/
parallelGram.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
__author__ = 'joe'
import os
import collections
import numpy as np
import pandas as pd
import pickle
import gzip
from collections import defaultdict
from collections import Counter
import io
import requests
import urllib
import dill
import multiprocessing
import sys
def getFiles(ngram_path):
"""
Gets list of ngram file names
:param ngram_path:
:return: list of ngram file names
"""
ngram_files = []
for f in os.listdir(ngram_path):
if os.path.isfile(os.path.join(ngram_path, f)) and not f.startswith('.'):
ngram_files.append(f)
return(ngram_files)
def getWords(wordPath):
"""
Get list of words from specified file
:param wordPath:
:return: Get the words specified in input file
"""
with open(wordPath, 'rb') as wf:
words = [w.strip() for w in wf.readline().split(',')]
return(words)
def getNegWords(wordPath):
"""
Get list of words from specified file
:param wordPath:
:return: Get the words specified in input file
"""
with open(wordPath, 'rb') as wf:
lb1 = r'(?<!'
lb2 = r' )'
negReg = ''.join([lb1 + nw.strip() + lb2 for nw in wf.readline().split(',')])
return(negReg)
def chunks(l, nJobs):
"""
Split l files into nJobs
:param l:
:param nJobs:
:return: Yield an index range for l of n items
"""
n = len(l)/nJobs
# For item i in a range that is a length of l,
for i in range(0, len(l), n):
# Create an index range for l of n items:
yield l[i:i + n]
def mergeDicts(dictList):
"""
Because this program splits the search across jobs,
we need a way to combine the results. This function does that.
:param dictList:
:return: A dictionary containing all co-occurrences
"""
uniondict = defaultdict(lambda: defaultdict(lambda: 0))
for d in dictList:
for k1, v1 in d.items():
for k2, v2 in v1.items():
if type(v2) == int:
uniondict[k1][k2] += int(v2)
return (uniondict)
def gnTotal(url):
"""
Google provides a count of total ngrams for a given size.
This function downloads that file and gets totals for each year.
:param url:
:return:
"""
response = urllib.urlopen(url)
lines = response.readlines()[0].split('\t')
total_counts = {'year': [], 'total_words': [], 'total_pages': [], 'total_volumes': []}
for line in lines:
line = line.split(',')
if len(line) == 4:
total_counts['year'].append(line[0])
total_counts['total_words'].append(line[1])
total_counts['total_pages'].append(line[2])
total_counts['total_volumes'].append(line[3])
totalCountsDf = pd.DataFrame.from_dict(total_counts)
return(totalCountsDf)
def chunkGrams3(ngram_chunk, baseWords, targetWords, output_path, ngram_path, negReg):
"""
This function using pattern matching to identify co-occurrences.
:param ngram_chunk: The files to search through for given job
:param baseWords: List of base words
:param targetWords: List of target words
:param output_path: Where should results be stored?
:param ngram_path: Where are the ngrams?
:param negWords: Negation words to exclude by
:return: Nothing. Dumps results to pickeled file
"""
wordFreqs = defaultdict(lambda: defaultdict(lambda: 0))
coFreqs = defaultdict(lambda: defaultdict(lambda: 0))
wordVols = defaultdict(lambda: defaultdict(lambda: 0))
coVols = defaultdict(lambda: defaultdict(lambda: 0))
file_counter = 0
total_files = len(ngram_chunk) + 1
baseWords = baseWords + ['behebung']
targetWords = targetWords
for ngram_file in ngram_chunk:
file_counter +=1
print 'Analyzing file {0} of {1} in chunk'.format(file_counter, total_files)
cur_file = gzip.open(os.path.join(ngram_path, ngram_file), 'rb')
for line in cur_file:
dat = line.split('\t')
phrase = dat[0].lower()
if any(baseWord in phrase for baseWord in baseWords): # If contains baseword then
if any(targetWord in phrase for targetWord in targetWords): # If also contains target word, then
for bWord in baseWords:
reg = negReg + r'\b' + re.escape(bWord) + r'\b'
try:
re.search(reg, phrase).group(0)
wordFreqs[dat[1]][bWord] += int(dat[2])
wordVols[dat[1]][bWord] += int(dat[3].replace('\n', ''))
for tWord in targetWords:
regT = negReg + r'\b' + re.escape(tWord) + r'\b'
try:
re.search(regT, phrase).group(0)
wordFreqs[dat[1]][tWord] += int(dat[2])
wordVols[dat[1]][tWord] += int(dat[3].replace('\n', ''))
coFreqs[dat[1]]['_'.join([bWord,tWord])] += int(dat[2])
coVols[dat[1]]['_'.join([bWord,tWord])] += int(dat[3].replace('\n', ''))
except:
continue
except:
continue
else:
for bWord in baseWords:
reg = negReg + r'\b' + re.escape(bWord) + r'\b'
try:
re.search(reg, phrase).group(0)
wordFreqs[dat[1]][bWord] += int(dat[2])
wordVols[dat[1]][bWord] += int(dat[3].replace('\n', ''))
except:
continue
elif any(targetWord in phrase for targetWord in targetWords):
for tWord in targetWords:
regT = negReg + r'\b' + re.escape(tWord) + r'\b'
try:
print re.search(regT, phrase).group(0)
wordFreqs[dat[1]][tWord] += int(dat[2])
wordVols[dat[1]][tWord] += int(dat[3].replace('\n', ''))
except:
continue
dictFreqs = mergeDicts(dictList=[wordFreqs,coFreqs])
dictVols = mergeDicts(dictList=[wordVols,coVols])
ds = [dictFreqs, dictVols]
print 'Writing {0}'.format(output_path)
dill.dump(ds, open(output_path, 'wb'))
def chunkGrams2(ngram_chunk, baseWords, targetWords, output_path, ngram_path):
"""
This function using pattern matching to identify co-occurrences.
:param ngram_chunk: The files to search through for given job
:param baseWords: List of base words
:param targetWords: List of target words
:param output_path: Where should results be stored?
:param ngram_path: Where are the ngrams?
:return: Nothing. Dumps results to pickeled file
"""
wordFreqs = defaultdict(lambda: defaultdict(lambda: 0))
coFreqs = defaultdict(lambda: defaultdict(lambda: 0))
wordVols = defaultdict(lambda: defaultdict(lambda: 0))
coVols = defaultdict(lambda: defaultdict(lambda: 0))
file_counter = 0
total_files = len(ngram_chunk) + 1
baseWords = baseWords + ['behebung']
targetWords = targetWords
for ngram_file in ngram_chunk:
file_counter +=1
print 'Analyzing file {0} of {1} in chunk'.format(file_counter, total_files)
cur_file = gzip.open(os.path.join(ngram_path, ngram_file), 'rb')
for line in cur_file:
dat = line.split('\t')
phrase = dat[0].lower()
if any(baseWord in phrase for baseWord in baseWords): # If contains baseword then
if any(targetWord in phrase for targetWord in targetWords): # If also contains target word, then
for bWord in baseWords:
reg = r'\b' + re.escape(bWord) + r'\b'
try:
re.search(reg, phrase).group(0)
wordFreqs[dat[1]][bWord] += int(dat[2])
wordVols[dat[1]][bWord] += int(dat[3].replace('\n', ''))
for tWord in targetWords:
regT = r'\b' + re.escape(tWord) + r'\b'
try:
re.search(regT, phrase).group(0)
wordFreqs[dat[1]][tWord] += int(dat[2])
wordVols[dat[1]][tWord] += int(dat[3].replace('\n', ''))
coFreqs[dat[1]]['_'.join([bWord,tWord])] += int(dat[2])
coVols[dat[1]]['_'.join([bWord,tWord])] += int(dat[3].replace('\n', ''))
except:
continue
except:
continue
else:
for bWord in baseWords:
reg = r'\b' + re.escape(bWord) + r'\b'
try:
re.search(reg, phrase).group(0)
wordFreqs[dat[1]][bWord] += int(dat[2])
wordVols[dat[1]][bWord] += int(dat[3].replace('\n', ''))
except:
continue
elif any(targetWord in phrase for targetWord in targetWords):
for tWord in targetWords:
regT = r'\b' + re.escape(tWord) + r'\b'
try:
print re.search(regT, phrase).group(0)
wordFreqs[dat[1]][tWord] += int(dat[2])
wordVols[dat[1]][tWord] += int(dat[3].replace('\n', ''))
except:
continue
dictFreqs = mergeDicts(dictList=[wordFreqs,coFreqs])
dictVols = mergeDicts(dictList=[wordVols,coVols])
ds = [dictFreqs, dictVols]
print 'Writing {0}'.format(output_path)
dill.dump(ds, open(output_path, 'wb'))
def chunkGrams1(ngram_chunk, baseWords, targetWords, output_path, ngram_path):
"""
This function using exact matching to identify co-occurrences.
:param ngram_chunk: The files to search through for given job
:param baseWords: List of base words
:param targetWords: List of target words
:param output_path: Where should results be stored?
:param ngram_path: Where are the ngrams?
:return: Nothing. Dumps results to pickeled file
"""
wordFreqs = defaultdict(lambda: defaultdict(lambda: 0))
coFreqs = defaultdict(lambda: defaultdict(lambda: 0))
wordVols = defaultdict(lambda: defaultdict(lambda: 0))
coVols = defaultdict(lambda: defaultdict(lambda: 0))
file_counter = 0
total_files = len(ngram_chunk) + 1
for ngram_file in ngram_chunk:
file_counter +=1
print 'Analyzing file {0} of {1} in chunk'.format(file_counter, total_files)
cur_file = gzip.open(os.path.join(ngram_path, ngram_file), 'rb')
for line in cur_file:
dat = line.split('\t')
phrase = dat[0].lower()
if any(baseWord in phrase for baseWord in baseWords): # If contains baseword then
if any(targetWord in phrase for targetWord in targetWords): # If also contains target word, then
for bWord in baseWords:
if bWord in phrase:
wordFreqs[dat[1]][bWord] += int(dat[2])
wordVols[dat[1]][bWord] += int(dat[3].replace('\n', ''))
for tWord in targetWords:
if tWord in phrase:
wordFreqs[dat[1]][tWord] += int(dat[2])
wordVols[dat[1]][tWord] += int(dat[3].replace('\n', ''))
coFreqs[dat[1]]['_'.join([bWord,tWord])] += int(dat[2])
coVols[dat[1]]['_'.join([bWord,tWord])] += int(dat[3].replace('\n', ''))
else:
for bWord in baseWords:
if bWord in phrase:
wordFreqs[dat[1]][bWord] += int(dat[2])
wordVols[dat[1]][bWord] += int(dat[3].replace('\n', ''))
elif any(targetWord in phrase for targetWord in targetWords):
for tWord in targetWords:
if tWord in phrase:
wordFreqs[dat[1]][tWord] += int(dat[2])
wordVols[dat[1]][tWord] += int(dat[3].replace('\n', ''))
dictFreqs = mergeDicts(dictList=[wordFreqs,coFreqs])
dictVols = mergeDicts(dictList=[wordVols,coVols])
ds = [dictFreqs, dictVols]
print 'Writing {0}'.format(output_path)
dill.dump(ds, open(output_path, 'wb'))
def main(ngramSyntax = sys.argv[1]):
"""
This function extracts necessary parameters from the parameters txt file
and distributes the jobs across workers.
:param ngramSyntax:
:return:
"""
pars = []
with open(ngramSyntax, 'rb') as ngs:
for line in ngs:
pars.append(line.replace('\n', '').strip())
ngram_path = pars[0]
nJobs = int(pars[1])
output_dir = pars[2]
baseWordPath = pars[3]
targetWordPath = pars[4]
coType = pars[5]
if coType == '3':
negWordPath = pars[6]
negReg = getNegWords(negWordPath)
baseWords = getWords(baseWordPath)
targetWords = getWords(targetWordPath)
print('Calculating n-gram cooccurrence with parameters:\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}'.format(ngram_path,
nJobs,
output_dir,
baseWordPath,
targetWordPath,
baseWords,
targetWords))
if coType == '1':
print 'Cooccurrence search based on pattern matching (type 1)'
elif coType == '2':
print 'Cooccurrence search based on exact matching (type 2)'
elif coType == '3':
print 'Cooccurrence search based on exact matching with negation exclusion (type 3)'
print 'Negation words: {0}'.format(negWords)
ngram_files = sorted(getFiles(ngram_path=ngram_path))
ngram_chunks = chunks(ngram_files, nJobs=nJobs)
jobN = 0
output_paths = []
output_dir = output_dir
jobs = []
for chunk in ngram_chunks:
jobN += 1
out_path = output_dir + '/job_' + str(jobN) + '_dict.pickle'
output_paths.append(out_path)
if coType == '1':
p = multiprocessing.Process(target=chunkGrams1, args=(chunk,baseWords,targetWords,out_path,ngram_path,))
if coType == '2':
p = multiprocessing.Process(target=chunkGrams2, args=(chunk, baseWords, targetWords, out_path, ngram_path,))
if coType == '3':
p = multiprocessing.Process(target=chunkGrams3, args=(chunk, baseWords, targetWords, out_path, ngram_path,negReg,))
jobs.append(p)
for p in jobs:
p.start()
for p in jobs:
print 'Joining {0} thread'.format(p)
p.join()
return(output_paths)
def getGnDicts(output_paths, ngramSyntax):
"""This function combines the objects saved by the main() function and also
downloads and combines total ngram counts.
"""
pars = []
with open(ngramSyntax, 'rb') as ngs:
for line in ngs:
pars.append(line.replace('\n', '').strip())
output_dir = pars[2]
ngramFreqDicts = []
ngramVolDicts = []
for dicPath in output_paths:
dic = dill.load(open(dicPath, 'rb'))
ngramFreqDicts.append(dic[0])
ngramVolDicts.append(dic[1])
ngramFreqs = mergeDicts(ngramFreqDicts)
ngramFreqsDf = pd.DataFrame.from_dict(ngramFreqs, orient='index')
ngramVols = mergeDicts(ngramVolDicts)
ngramVolsDf = pd.DataFrame.from_dict(ngramVols, orient='index')
url="http://storage.googleapis.com/books/ngrams/books/googlebooks-eng-all-totalcounts-20120701.txt"
gnTotals = gnTotal(url)
gnTotals.set_index('year', inplace=True)
ngramFreqsDf = pd.concat([gnTotals, ngramFreqsDf], axis=1)
ngramVolsDf = pd.concat([gnTotals, ngramVolsDf], axis=1)
print 'Writing Frequencies to: {0}'.format(output_dir + '/ngramFreqsDf.csv')
ngramFreqsDf.to_csv(output_dir + '/ngramFreqsDf.csv', sep=',')
print 'Writing Volumes to: {0}'.format(output_dir + '/ngramVolsDf.csv')
ngramVolsDf.to_csv(output_dir + '/ngramVolsDf.csv', sep=',')
if __name__ == '__main__':
output_paths = main(ngramSyntax = sys.argv[1])
getGnDicts(output_paths=output_paths, ngramSyntax=sys.argv[1])