-
Notifications
You must be signed in to change notification settings - Fork 4
/
manager.py
49 lines (36 loc) · 911 Bytes
/
manager.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
import os
import sys
import time
from collections import defaultdict
from multiprocessing import Process, Lock, Manager
processes = [ ]
file = open('article', mode = 'r')
lines = file.read().split('.')
file.close()
manager = Manager()
bag = manager.dict()
lock = manager.Lock()
delims = manager.list([',', ';', ':', '-', '.'])
def bag_of_words(line, l, d, delims):
my_dict = defaultdict(int)
for d in delims:
line = line.replace(d, ' ')
for word in line.split():
my_dict[word] += 1
lock.acquire()
for key, value in my_dict.iteritems():
try:
bag[key] += value
except KeyError:
bag[key] = value
lock.release()
time1 = time.time()
for line in lines:
p = Process(target=bag_of_words, args=(line, lock, bag, delims,))
processes.append(p)
p.start()
for p in processes:
p.join()
time2 = time.time() - time1
print bag
print("Time = {0}".format(time2))