-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.py
89 lines (89 loc) · 2.24 KB
/
Search.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
def WordsList(text):
Words = text.replace('\n','. ').split(' ')
n = []
for x in Words:
if not x in n:
n.append(x.replace('.','').replace('(','').replace(')','').replace('/',''))
Words = n
del n
return Words
def WordsCorrection(Words,inp):
Result = []
for argument in inp.split(' '):
score = []
for a in Words:
Score = 0
for b in a:
if b.lower() in argument.lower():
Score += 1
else:
Score -= 5
score.append(Score)
BestScore = 0
for x in score:
if x>BestScore:
BestScore = x
Result.append(Words[score.index(BestScore)])
return ' '.join(Result)
def listsentences(path):
try:
Data = open(path,'r').read()
except Exception as e:
open(path,'a')
Data = 'No Data'
a = []
b = ''
ie = True
for x in range(len(Data)-(Data[-1] == ".")):
if Data[x] == '[':
ie = False
if ie:
b += Data[x]
if Data[x] == ']':
ie = True
if "." == Data[x]:
if (Data[x+2] == Data[x+2].upper() or Data[x+1] == Data[x+1].upper()) and (not Data[x+2] in '1234567890' or not Data[x+1] in '1234567890'):
a.append(b[(b[0] == ' '):].replace('\n',' ').replace(' ',' '))
b = ''
a.append(b[(b[0] == ' '):].replace('\n',' ').replace(' ',' '))
return a
def searchresult(arg,data):
a = data
n = []
for c in a:
score = 0
text = []
for x in c.split(' '):
if (x.lower() in [u.lower() for u in arg.split(' ')]) and not x.lower() in text:
score += 5
else:
score -= 3
text.append(x.lower())
if arg.lower().replace(' ','') in c.lower().replace(' ',''):
score += 20
n.append(score/(len(c)+0.1))
best = -999
num = 0
for x in range(len(n)):
if n[x] > best:
best = n[x]
num = x
text = []
b = 1
while 100>len('\n\n'.join(a[num:num+b])):
b += 1
for x in '\n\n'.join(a[num:num+b]).split(' '):
if True in [x.lower() in u.lower() or u.lower() in x.lower() for u in arg.split(' ')]:
text.append(f"*{x}")
else:
text.append(x)
return ' '.join(text)
while 1:
inp=input(">")
print()
correction = WordsCorrection(WordsList(open('a.txt','r').read()),inp)
#if inp.lower() != correction.lower():
#print()
#print('Did you mean:',correction)
#print()
print('Jawab:\n',searchresult(inp,listsentences("a.txt")))