-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpyngram.py
57 lines (43 loc) · 1.43 KB
/
pyngram.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
from collections import Counter
from datetime import datetime
from itertools import islice
from multiprocessing import Pool,cpu_count
from operator import itemgetter
import sys
def IterRows():
for datarow in datarows:
yield datarow.strip()
def get_ngrams(thestr):
s = ' '.join(thestr.split()).split(' ')
return [tuple(s[i:i+n]) for i in range(len(s)-n+1)]
# Take the input file from the arguments
inputfile = sys.argv[1]
start_time = datetime.now()
print 'reading the data into memory'
infile = open(inputfile, 'r')
datarows = infile.readlines()
infile.close()
# We'll use this later
numrows = len(datarows)
# Take the value of n from the arguments
n = int(sys.argv[2])
pool = Pool(processes=cpu_count())
iterrows = IterRows()
print 'building full %sgram list' % n
# Make a shared counter that all processes can use
counter = Counter()
# Chunk the data into 40000 rows per process at one time. This can be optimized
N = 40000
for i in range(numrows//N+1):
mapper = pool.imap(get_ngrams, islice(iterrows, N))
for ngrams in mapper:
for ngram in ngrams:
counter.update({' '.join(ngram): 1})
print 'writting sorted dict of ngram sums to disk'
outfile = '%sgrams.tsv' % n
f = open(outfile, 'w')
f.write('%sgram\tcount\n' % n)
for gram in sorted(counter.iteritems(), key=itemgetter(1), reverse=True):
f.write('%s\t%s\n' % (gram[0], gram[1]))
f.close()
print 'COMPLETE:', datetime.now() - start_time