-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermuterm.py
More file actions
107 lines (82 loc) · 2.46 KB
/
permuterm.py
File metadata and controls
107 lines (82 loc) · 2.46 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import re
dir = r"C:\Users\VEDANK\Desktop\Files"
count = 0
files = []
allwords = []
for r, d, f in os.walk(dir):
for file in f:
files.append(os.path.join(r, file))
count += 1
print(count)
index = dict()
for f in files:
file = open(f, 'r')
text = file.read().lower()
words = text.split()
for x in words:
allwords.append(x)
allwords = list(dict.fromkeys(allwords))
for key in allwords:
postList = []
for f in files:
file = open(f, 'r')
text = file.read().lower()
# found = re.search(' ' + key + ' ', ' ' + text + ' ')
if(f' {key} ' in f' {text} '):
postList.append(os.path.basename(f))
index[key] = postList
for x, y in index.items():
print(x, ' ', y)
with open(r'C:\Users\VEDANK\Desktop\Output\Index1.txt', 'w') as f:
print(index, file=f)
permuterm = dict()
def rotate(lst, n):
return lst[-n:] + lst[:-n]
for key in allwords:
nkey = key + "$"
for i in range(len(nkey), 0,-1):
pkey = rotate(nkey, i)
permuterm[pkey] = key
print("---------Permuterm-----------")
for x, y in permuterm.items():
print(x, ' ', y)
with open(r'C:\Users\VEDANK\Desktop\Output\PermuTerm.txt', 'w') as f:
print(permuterm, file=f)
def rotatequery(lst, n):
checkWord = lst[-n:] + lst[:-n]
if checkWord.endswith('*'):
return checkWord
else:
pass;
docs = []
def matchPrefix(rotateWord,matchWord):
permutermList = []
for pk in permuterm:
if pk.startswith(rotateWord):
rex = re.compile("%s" %matchWord)
match = permuterm[pk]
if rex.findall(match):
permutermList.append(permuterm[pk])
return permutermList
def processQuery(rotateWord,matchWord):
permutermList = matchPrefix(rotateWord,matchWord)
print(permutermList)
for term in permutermList:
templist = index[term]
for val in templist:
docs.append(val)
query = input("Enter query: ")
query = query.split()
for word in query:
matchWord = word.replace("*", ".*")
word = word.split('*')
newWord = word[0] + '*' + word[len(word)-1]
print(newWord)
newWord = newWord.split('*')
rotateWord = newWord[1] + "$" + newWord[0]
print(rotateWord + "*")
processQuery(rotateWord,matchWord)
docs=list(dict.fromkeys(docs))
print("---------------")
print(docs)