-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathporterStem.py
More file actions
158 lines (138 loc) · 4.32 KB
/
porterStem.py
File metadata and controls
158 lines (138 loc) · 4.32 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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()
def get_base(word, suf):
suflen = word.rfind(suf)
base = word[:suflen]
return base
def replacer(word, suf1, suf2):
base = get_base(word, suf1)
base += suf2
return base
def isvowel(l):
letter = l.lower()
if (letter == "a" or letter == "e" or letter == "i" or letter == "o" or letter == "u"):
return True
else:
return False
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)
newIndex=dict()
for word in allwords:
if word.endswith('sses'):
new = replacer(word, 'sses', 'ss')
if new in newIndex:
newList = newIndex[new]
oldList = index[word]
for i in oldList:
newList.append(i)
newList=list(dict.fromkeys(newList))
newIndex[new] = newList
else:
newIndex[new] = index[word]
elif word.endswith('ies'):
new = replacer(word, 'ies', 'i')
if new in newIndex:
newList = newIndex[new]
oldList = index[word]
for i in oldList:
newList.append(i)
newList=list(dict.fromkeys(newList))
newIndex[new] = newList
else:
newIndex[new] = index[word]
elif word.endswith('ss'):
new = word
if new in newIndex:
newList = newIndex[new]
oldList = index[word]
for i in oldList:
newList.append(i)
newList=list(dict.fromkeys(newList))
newIndex[new] = newList
else:
newIndex[new] = index[word]
elif word.endswith('s'):
new = replacer(word, 's', '')
checkVowel = isvowel(new)
if(checkVowel == False):
if new in newIndex:
newList = newIndex[new]
oldList = index[word]
for i in oldList:
newList.append(i)
newList=list(dict.fromkeys(newList))
newIndex[new] = newList
else:
newIndex[new] = index[word]
else:
newIndex[word] = index[word]
else:
newIndex[word] = index[word]
print("===================After stemming===============")
for x, y in newIndex.items():
print(x, ' ', y)
with open(r'C:\Users\VEDANK\Desktop\Output\index2.txt', 'w') as f:
print(newIndex, file=f)
query = input("Enter query: ")
query = query.split()
newQuery=""
docs=[]
for word in query:
if word.endswith('sses'):
word = replacer(word, 'sses', 'ss')
if word in newIndex.keys():
docs = docs + newIndex[word]
print(word)
elif word.endswith('ies'):
word = replacer(word, 'ies', 'i')
if word in newIndex.keys():
docs = docs + newIndex[word]
print(word)
elif word.endswith('ss'):
new = word
if word in newIndex.keys():
docs = docs + newIndex[word]
print(word)
elif word.endswith('s'):
word = replacer(word, 's', '')
print(word)
checkVowel = isvowel(word)
if(checkVowel == False):
if word in newIndex.keys():
docs = docs + newIndex[word]
print(word)
else:
word = word
else:
if word in newIndex:
docs = docs + newIndex[word]
docs=list(dict.fromkeys(docs))
print(docs)