/
search.py
245 lines (211 loc) · 7.86 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
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
238
239
240
241
242
243
244
245
__author__ = 'Nick Roberts -- Steven Bock'
import re
import hashlib
import mechanize
import urlparse
import robotparser
from BeautifulSoup import BeautifulSoup
from tqdm import tqdm
import snowballstemmer
import math
browse = mechanize.Browser()
#GLOBAL VARS
baseUrl = "http://lyle.smu.edu/~fmoore/"
robots = robotparser.RobotFileParser()
robots.set_url(urlparse.urljoin(baseUrl, "robots.txt"))
robots.read()
urlList = [baseUrl]
visitedUrls = [baseUrl]
retrieveLimit = 0
stopWords = ()
outgoingLinks = []
badLinks = []
jpgAmount = 0
words = {}
docIDCounter = 1
documentIDs = []
duplicateDetect = []
duplicateCount = 0
stemmer = snowballstemmer.stemmer("english")
#END GLOBAL VARS
#---------- INPUT -----------
print "Welcome to the project 2 search engine"
temp = input("Please enter page crawl limit: ")
retrieveLimit = int(temp)
stopFile = open("stopwords.txt", "r")
lines = stopFile.read()
stopWords = lines.split()
def cosSim(doc, queryLen, docLen, queryIdf):
temp = 0
tempIndex = 0
for x in doc:
temp += (x*queryIdf[tempIndex])
tempIndex += 1
cosSimNumber = temp / (docLen * queryLen)
return cosSimNumber
def rankResults(query, documentsByWord, docsToSearch, idfDict):
cosSimList = []
querylen = 0
queryIdf = []
docLengths = []
termDocMatrix = [] #words will be in order typed in query
for document in docsToSearch:
tempList = []
for word in query:
temp = 0
for x in documentsByWord[word]: #looking at tuples
if x[0] == document:
temp = x[1]
temp = temp * idfDict[word]
tempList.append(temp)
termDocMatrix.append(tempList)
for word in query:
querylen += pow(idfDict[word], 2)
queryIdf.append(idfDict[word])
querylen = math.sqrt(querylen)
for doc in termDocMatrix:
temp = 0
for x in doc:
temp = temp + pow(x, 2)
temp = math.sqrt(temp)
docLengths.append(temp)
# print termDocMatrix
# print docsToSearch
# print queryIdf
# print querylen
# print docLengths
docLenIndex = 0
for x in termDocMatrix:
cosSimList.append(cosSim(x, querylen, docLengths[docLenIndex], queryIdf))
docLenIndex += 1
resultList = zip(cosSimList, docsToSearch)
resultList.sort()
resultList.reverse()
resultCount = 1
resultLimit = 5
for x in resultList:
print resultCount, ". ", docIdDict[x[1]], " ; Similarity: ", x[0]
resultCount += 1
if resultCount > resultLimit:
break
def union(documents):
union = []
for x in documents.keys():
for y in documents[x]:
union.append(y[0])
return set(union)
def idf(word):
numberOfDocs = len(documentIDs)
numberOfAppearances = len(words[word])
idf = math.log((numberOfDocs/(numberOfAppearances)), 2)
return idf
def getDocs(query):
finalList = {}
for word in query:
tempList = []
for x in words[word]:
tempList.append(x)
finalList[word] = tempList
return finalList
def search(query):
wordsToSearch = query.split()
stemmedQuery = []
idfDict = {}
for x in wordsToSearch:
stemmedQuery.append(stemmer.stemWord(x))
finalQuery = []
for x in stemmedQuery:
if x in words.keys():
finalQuery.append(x)
else:
print x, " was not not found"
finalQuery = set(finalQuery)
if len(finalQuery) == 0:
print "No results"
return;
print "Searching for: ",
for x in finalQuery:
print x, " ",
idfDict[x] = idf(x)
print ""
documentsByWord = getDocs(finalQuery)
docsToSearch = union(documentsByWord)
rankResults(finalQuery, documentsByWord, docsToSearch, idfDict)
print "Crawling Pages, please wait..."
with tqdm(total=retrieveLimit) as progress:
for page in urlList:
if docIDCounter > retrieveLimit:
break #quits crawling if retrieval limit is reached
try:
#---------- Page Crawler (gets words and links from each page ---------
soup = ""
browse.open(page)
if page.endswith(".txt"):
soup = browse.response().read()
else:
soup = BeautifulSoup(browse.response().read()) #if can't parse, assumed to be binary file or 404
soup = soup.getText()
hashTest = hashlib.md5(soup.encode('utf-8')).hexdigest()
if hashTest not in duplicateDetect:
duplicateDetect.append(hashTest)
wordsInPage = soup.split()
if not page.endswith(".txt"):
for link in browse.links():
tempURL = urlparse.urljoin(link.base_url, link.url)
#BELOW: gets rid of duplicate urls resulting from index.html/index.htm
if tempURL.endswith("index.html"):
tempURL = tempURL.replace("index.html", "")
elif tempURL.endswith("index.htm"):
tempURL = tempURL.replace("index.htm", "")
if tempURL not in urlList:
if tempURL.startswith(baseUrl):
if robots.can_fetch("*", "/" + link.url): #checks robots.txt, necessary because of unusual robots.txt location
urlList.append(tempURL)
else:
if tempURL + "/" not in urlList:
outgoingLinks.append(tempURL)
documentIDs.append((docIDCounter, page)) #if an exception hasn't happened by this point, it is safe to assign the docID
progress.update(1)
#-------------- WORD INDEXER ----------------#
for x in wordsInPage: #parse and stem words, add to dictionary
x = x.replace(",", "") #removes commas before checking for stopwords
x = re.sub("[^a-zA-Z]","", x) #removes non-alphabetic characters from words
if x not in stopWords and len(x) > 0:
temp = x
temp = temp.lower()
temp = stemmer.stemWord(temp)
#print temp
if temp not in words.keys():
words[temp] = [(docIDCounter, 1)]
else:
tempPageList = [x[0] for x in words[temp]]
if docIDCounter in tempPageList:
tempIndex = tempPageList.index(docIDCounter)
words[temp][tempIndex] = (docIDCounter, words[temp][tempIndex][1] + 1)
else:
words[temp].append((docIDCounter,1))
docIDCounter += 1 #increments doc ID after successful parsing
#-------------- Binary File Handler ----------------#
else:
duplicateCount += 1
except: #occurs if it is a binary file or non-existent file (this is needed for p2, below if statements are not
#print page
if page.endswith(".jpg"):
jpgAmount += 1 #not needed for p2
if browse.response().code == 404:
badLinks.append(page)
if (docIDCounter < retrieveLimit):
progress.update(retrieveLimit - docIDCounter + 1)
############BEGIN PROJECT 2 Search Component #################################
docIdDict = dict(documentIDs)
print ""
print "Web Crawling Complete, starting search engine"
searchQuery = ""
while True:
searchQuery = raw_input("Please enter query words (separated by spaces) or type Quit: ")
searchQuery = str(searchQuery)
searchQuery = searchQuery.lower()
if (searchQuery == "quit"):
print "Goodbye!"
break
search(searchQuery)