-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
237 lines (212 loc) · 9.35 KB
/
main.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import nltk
import math
import urllib.request
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from bs4 import BeautifulSoup
print("Search Engine using Natural Language Processing in Python")
string = input("Enter the search query : ")
######################################################Performing Tokenization using nltk#########################################
tokens = nltk.word_tokenize(string)
print("Tokenized query is : ")
for i in nltk.word_tokenize(string):
print (i)
######################################################Removing stopwords#########################################################
stop_words = set(stopwords.words("english"))
filtered_string = []
print("\nFiltered string that contains words except articles, pronouns & prepositions : ")
for w in tokens:
if w not in stop_words:
filtered_string.append(w)
print(filtered_string)
###########################################################Performing Stemming using nltk#############################################
ps = PorterStemmer()
stemmed_string = []
print("\nStemmed words which are present in filtered string : ")
for w in filtered_string:
stemmed_string.append(ps.stem(w))
print(stemmed_string)
n = len(stemmed_string)
print("\n\n\n")
############################################################# KMP Algorithm ##########################################################
# Python program for KMP Algorithm
def KMPSearch(pat, text, mydict):
array1 = []
array2 = []
M = len(pat)
N = len(text)
b = 0
p = 0
# create lps[] that will hold the longest prefix suffix
# values for pattern
lps = [0]*M
j = 0 # index for pat[]
# Preprocess the pattern (calculate lps[] array)
computeLPSArray(pat, M, lps)
i = 0 # index for txt[]
while i < N:
if pat[j] == text[i]:
i += 1
j += 1
if j == M:
p += 1
j = lps[j-1]
# mismatch after j matches
elif i < N and pat[j] != text[i]:
# Do not match lps[0..lps[j-1]] characters,
# they will match anyway
if j != 0:
j = lps[j-1]
else:
i += 1
mydict.update({pat:p})
def computeLPSArray(pat, M, lps):
len = 0 # length of the previous longest prefix suffix
lps[0] # lps[0] is always 0
i = 1
# the loop calculates lps[i] for i = 1 to M-1
while i < M:
if pat[i] == pat[len]:
len += 1
lps[i] = len
i += 1
else:
if len != 0:
len = lps[len-1]
# note that we do not increment i here
else:
lps[i] = 0
i += 1
############################################################################################################################################################
print("Searching.........................................")
print("\n")
text_file = open("links.txt","r")
normalizedict = dict()
countelem = [0]*len(stemmed_string)
for link in text_file.readlines():
if (link == '\n'):
pass
else :
tf = []
idf = []
p = 0
url = link
print("-----------------------------------------------------------------------------------------------------")
print("URL of the webpage is : ",url)
try:
resp = urllib.request.urlopen(url)
html = resp.read()
except urllib.error.URLError as e:
contents = e.read()
soup = BeautifulSoup(html,"html.parser") # kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
text = soup.get_text() # get text
lines = (line.strip() for line in text.splitlines()) # break into lines and remove leading and trailing space on each
chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) # break multi-headlines into a line each
text = '\n'.join(chunk for chunk in chunks if chunk) # drop blank lines
mydict = dict()
d = 0
e = 0
for word in stemmed_string:
KMPSearch(word, text, mydict)
for items in mydict.keys():
if mydict[items] == 0:
pass
else:
countelem[p] = countelem[p] + 1
p = p + 1
for i in mydict:
d = d + mydict[i]
e = d*1.0/len(text)
if e == 0:
pass
else:
normalizedict.update({link:e})
print(mydict)
print("Normalized value (sum of values present)/(length of text) is : ",e)
print("-----------------------------------------------------------------------------------------------------")
#################################################### Output on basis of normalization ###############################################################################
print("\n")
#print(normalizedict)
print("\n############### Results ###############")
print("\n")
sorted_list = [k for v,k in sorted([(v,k) for k,v in normalizedict.items()], reverse = True)]
for i in sorted_list:
print(i)
print("\n")
print("###############A document is relevant if normalization is greater than 0.25 and is retrieved if normalization > 0 \n")
#######################################################################################################################################################
#####################################################Calculating Precision ###############################################################################
retrieve = 0
relevant = 0
for i in normalizedict.keys():
if normalizedict[i] >= 0.0015:
relevant = relevant+1
elif normalizedict[i] >= 0:
retrieve = retrieve + 1
if retrieve != 0:
print("###############Precision for the given set of documents is ",relevant/retrieve)
print("\n \n")
print("\n###############Results on the basis of tf*idf ###############\n")
#########################################################################################################################################################
text_file = open("links.txt","r")
score = dict()
for difflink in text_file.readlines():
if (difflink == '\n'):
pass
else :
tf = []
idf = []
tfidf = []
q = 0
diffurl = difflink
print("-----------------------------------------------------------------------------------------------------")
print("URL of given webpage is : ",diffurl)
try:
resp = urllib.request.urlopen(diffurl)
html = resp.read()
except urllib.error.URLError as e:
contents = e.read()
soup = BeautifulSoup(html,"html.parser") # kill all script and style elements
for script in soup(["script", "style"]):
script.extract() # rip it out
text = soup.get_text() # get text
lines = (line.strip() for line in text.splitlines()) # break into lines and remove leading and trailing space on each
chunks = (phrase.strip() for line in lines for phrase in line.split(" ")) # break multi-headlines into a line each
text = '\n'.join(chunk for chunk in chunks if chunk) # drop blank lines
mydict = dict()
d = 0
e = 0
for word in stemmed_string:
KMPSearch(word, text, mydict)
s = sum(mydict.values())
for elements in mydict.keys():
if s != 0 :
tf.append(mydict[elements]/s)
if countelem[q] == 0:
idf.append(0)
else:
idf.append(math.log(8/countelem[q]))
q = q + 1
print("tf = ",tf)
print("idf = ",idf)
for i in range(0,len(tf)):
tfidf.append(tf[i]*idf[i])
print("tfidf = ",tfidf)
e = sum(tfidf)
print("Score for the given link is : ",e)
score.update({difflink:e})
print("-----------------------------------------------------------------------------------------------------")
print("\n")
##################################################### Output on the basis of tf*idf ###############################################################################
#print(score)
print("\n############### Results ###############")
print("\n")
sorted_list = [k for v,k in sorted([(v,k) for k,v in score.items()],reverse = True)]
for i in sorted_list:
print(i)
text_file.close()
##########################################################################################################################################################