-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeasure2.py
58 lines (41 loc) · 1.22 KB
/
measure2.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
from rabinkarp_parallel import RabinKarpParallel
from multiprocessing import Process, Queue, Lock
from doc_handle import DocumentHandle
import sys
def main(lock):
processes = []
R = Queue()
# number of threads
k = 3
prk = RabinKarpParallel()
doc = DocumentHandle()
# open name and content original document
textname, txt = doc.get_txt()
# print txt
# open pattern name
filenames = doc.get_pat()
d = int((len(txt) - 5 + 1) / k + 1)
for i in range(1, len(filenames)):
# open pattern one by one through the loop
patname = filenames[i].replace('\n', '')
with open (patname, 'r') as pattern:
pattern = pattern.read().replace('\n', ' ').replace('\t', ' ')
pattern = pattern.split()
# pattern = doc.wordshingling(pattern)
# print pattern
# print patname
# print pattern
for j in range(k - 1):
p = Process(target=all_position, args=(lock, int(j * d), int((j+1) * d) + 5 - 1, pattern, txt, i, R,))
processes.append(p)
p.start()
p = Process(target=all_position, args=(lock, int(d * (k-1)), len(txt) + 5 - 1, pattern, txt, i, R,))
processes.append(p)
p.start()
def all_position(l, x, y, pat, txt, i, R):
l.acquire()
print pat
l.release()
if __name__ == '__main__':
lock = Lock()
main(lock)