-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallelized_stemmer.py
73 lines (56 loc) · 2.53 KB
/
parallelized_stemmer.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
#!/usr/bin/python3
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
import re
from dask import delayed
import time
from multiprocessing import Process
class ParallelStemmer():
DELIMETERS = "\"", "\'", "{", "}", "(", ")", "[", "]", ">", "<", "_", "=", "+", "|", "\\", ":", ";", " ", ",", ".", "/", "?", "~", "!", "@", "#", "$", "%", "^", "&", "*", "\r", "\n", "\t", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
def __init__(self):
self.list_of_sentences = open("./resources/dataset_teks.txt").readlines()
def tokenizing(self, text):
regexPattern = '|'.join(map(re.escape, self.DELIMETERS))
texts = re.split(regexPattern, text.lower())
return texts
def content_len(self):
return len(self.list_of_sentences)
def stem(self):
factory = StemmerFactory()
stemmer = factory.create_stemmer()
self.execute_stem(stemmer, self.list_of_sentences, 0, self.content_len())
def parallel_stem(self, total_process):
factory = StemmerFactory()
stemmer = factory.create_stemmer()
d = int(self.content_len()/total_process)
processes = []
start_time = time.time()
for i in range(total_process - 1):
p = Process(target=self.parallelized_execute_stem, args=(stemmer, self.list_of_sentences, int(i * d), int((i+1) * d)))
processes.append(p)
p.start()
p = Process(target=self.parallelized_execute_stem, args=(stemmer, self.list_of_sentences, int(d * (total_process - 1)), self.content_len))
processes.append(p)
p.start()
for pr in processes:
pr.join()
end_time = time.time()
print("elapsed time in seconds", end_time - start_time)
def execute_stem(self, stemmer, list_of_sentences, start, end):
start_time = time.time()
index = int(start)
end_index = int(end)
for i in range(index, end_index):
words = self.tokenizing(list_of_sentences[i])
for word in words:
if word:
print(i, stemmer.stem(word))
end_time = time.time()
print("elapsed time in seconds", end_time - start_time)
def parallelized_execute_stem(self, stemmer, list_of_sentences, start, end):
index = int(start)
end_index = int(end)
for i in range(index, end_index):
words = self.tokenizing(list_of_sentences[i])
for word in words:
if word:
print(i, stemmer.stem(word))