-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_struct.py
47 lines (40 loc) · 1.3 KB
/
data_struct.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
class Document:
def __init__(self, text, url):
self.text = text
self.url = url
self.total_term = 0
self.f_max = 0
self.vector = None
class Term:
def __init__(self):
self.doc_count = {}
def add_match(self, hsh_doc):
try: self.doc_count[hsh_doc] += 1
except KeyError: self.doc_count[hsh_doc] = 1
def match_by_document(self, hsh_doc):
try: return self.doc_count[hsh_doc]
except: return 0
@property
def match_len(self):
return len(self.doc_count)
import progressbar
def get_progressbar(N, name = ""):
return progressbar.ProgressBar(
maxval=N,
widgets=[progressbar.Bar('#', '[', ']'),
name,
progressbar.Percentage()])
def pretty(dict_, *texts):
result = list(texts)
for key in dict_:
for i, text in enumerate(texts):
lower_text : str = text.lower()
index = 0
while True:
try:
index = lower_text.index(key, index)
result[i] = text[0:index] + "\33[47m{}\033[00m".format(text[index: index + len(key)]) + text[index + len(key) + 1: ]
index += len(key)
except ValueError:
break
return tuple(result)