-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (59 loc) · 2.07 KB
/
main.py
File metadata and controls
83 lines (59 loc) · 2.07 KB
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
73
74
75
76
77
78
79
80
81
82
83
# Google Project: Auto Complete
import os
try:
import cPickle as pickle
except ModuleNotFoundError:
import pickle
import string
from TrieComplete import TrieAutoComplete, SourceData
comp = TrieAutoComplete()
minimized_db = {}
db = {}
missing_score = {0: 10, 1: 8, 2: 6, 3: 4}
def splitter(n, words):
new_list = []
for i in range(0, len(words)-n+1):
new_list += [" ".join(words[i:i+n])]
return new_list
def all_files(path):
my_lst = []
for filename in os.listdir(path):
if filename.endswith(".pkl") or filename.endswith(".txt"):
my_lst.append(path+'\\'+filename)
else:
my_lst += all_files(path + '\\' + filename)
return my_lst
def init(path_root='./'):
# return
global comp
for file_path in all_files(path_root):
print('file init: ', file_path)
with open(file_path, 'r', encoding="utf8") as f:
line_number = -1
for line in f.readlines():
line_number += 1
sentence = line.rstrip()
words = sentence.translate(str.maketrans('', '', string.punctuation)).lower().split()
for i in range(1, len(words)):
for piece in splitter(i, words):
comp.insert_word(piece, SourceData(file_path, line_number, 0, 0))
def main():
path = input('please enter path of directory to scan text files: ')
init(path)
print('Done!')
while True:
q = input("type word or part of word: ")
q = q.lower()
res = comp.search_prefix(q, '')
print('suggestion auto complete:')
sorted_res = sorted(res, key=lambda x: x[2], reverse=True)
for i, item in enumerate(sorted_res[:10]):
print(f'{i+1}. {item[0]}, score: {item[2]}')
#for source_data in item[1]:
# print('\t', source_data.file_name)
#print('regular res:', res)
# sorted_res = sorted(res, key=lambda x: x.score, reverse=True)
# print('sorted res: ', sorted_res)
# sleep(1)
if __name__ == '__main__':
main()