-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorlak_search1.py
130 lines (119 loc) · 5.47 KB
/
torlak_search1.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import codecs
from nltk import word_tokenize, sent_tokenize
import eel
from all_data import data
from texts import texts
@eel.expose
def get_data(query):
answer = []
query = query.lower()
query_list = query.split()
for file, sentences in data.items():
for index, sent in enumerate(sentences):
for item in query_list:
tokens_list = sent.lower().split()
for token in tokens_list:
if token.startswith(item):
if item in sent.lower():
s = ''
sent_list = word_tokenize(sent)
for word in sent_list:
if word in [',', '.', '!', '?', ':', '_', '-', ';']:
s = s + word
elif word.lower().startswith(item) or word.lower() == item:
s += " "
s += f'<span style="color:red;">{word}</span> '
else:
s += " "
s += f'<span>{word}</span>'
b = f"<div id='{file}_sent_{index}' class='sentence {index}'><span class='{file} link' onclick='openFile(event)'> {file}</span>: {s} <span class='expand {file} {index}' onclick='expand(event)'>[expand context]</span> </div>"
answer.append(b)
return answer
@eel.expose
def expand_context(file, index, query):
print(file)
print(index)
b = ''
index = int(index)
sentences = data[file]
relevant_sentences = sentences
if 5 <= index <= len(sentences) - 5:
relevant_sentences = sentences[index - 5: index + 4]
elif index < 5:
relevant_sentences = sentences[: index + 7]
elif index > len(sentences) - 5:
relevant_sentences = sentences[index - 8:]
for i, sent in enumerate(relevant_sentences):
s = ''
sent_list = word_tokenize(sent)
for word in sent_list:
if word in [',', '.', '!', '?', ':', '_', '-', ';']:
s = s + word
else:
if word.startswith(query):
s += " "
s += f'<span style="color: red">{word}</span>'
else:
s += " "
s += f'<span>{word}</span>'
b += f"<span>{s}</span> "
return f"<span class='{file} link' onclick='openFile(event)'> {file}</span>" + " " + b + "</div>"
@eel.expose
def open_file(file):
print(file)
text = texts[file]
text_list = text.split('\n')
a = ''
for t in text_list:
a += t
a += "<br/>"
return "<button onclick='back()' >Back</button><br/>" + a
@eel.expose
def get_data_with_context(query):
print(query)
answer = []
query = query.lower()
query_list = query.split()
for file, sentences in data.items():
for index, sent in enumerate(sentences):
for item in query_list:
tokens_list = sent.lower().split()
for token in tokens_list:
if token.startswith(item):
if item in sent.lower():
s = ''
sent_list = word_tokenize(sent)
for word in sent_list:
if word in [',', '.', '!', '?', ':', '_', '-', ';']:
s = s + word
elif word.lower().startswith(item) or word.lower() == item:
s += " "
s += f'<span style="color:red;">{word}</span> '
else:
s += " "
s += f'<span>{word}</span>'
b = f"<div id='{file}_sent_{index}' class='sentence {index}'><span class='{file} link' onclick='openFile(event)'> {file}</span>: {s} <span class='expand {file} {index}' onclick='expand(event)'>[expand context]</span> </div>"
relevant_sentences = sentences
answ = f"<div id='{file}_sent_{index}' class='sentence {index}'><span class='{file} link' onclick='openFile(event)'> {file}</span>:"
if 5 <= index <= len(sentences) - 5:
relevant_sentences = sentences[index - 5: index + 4]
elif index < 5:
relevant_sentences = sentences[: index + 7]
elif index > len(sentences) - 5:
relevant_sentences = sentences[index - 8:]
for j, sen in enumerate(relevant_sentences):
string = ''
sen_list = word_tokenize(sen)
for token in sen_list:
if token in [',', '.', '!', '?', ':', '_', '-', ';']:
string = string + token
else:
string += " "
string += f'<span>{token}</span>'
answ += f"<span>{string}</span> "
answ += "</div>"
answer.append(answ)
return answer
eel.init('web')
eel.start('main.html', size=(700, 700))