-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalsearchEngine.py
184 lines (154 loc) · 4.84 KB
/
FinalsearchEngine.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
#Finishing the search engine python program by saurabh saxena(copyright 2012)
#Motivation stanford lectures and research paper by Larry Page and Sergey Bin
import urllib
def get_page(url):
try:
return urllib.urlopen(url).read()
except:
return ""
crawl_depth = 1
split_list = " ,!-<>"
def dfs(graph, node, visited):
if node in visited:
return []
visited.append(node)
for neighbor in graph[node]:
for visit in dfs(graph, neighbor, visited):
if visit not in visited:
visited.append(visit)
return visited
def reachable(graph, node):
return dfs(graph, node, [])
def edit_distance(s,t):
if len(s) == 0:
return len(t)
if len(t) == 0:
return len(s)
if s[0] == t[0]:
return edit_distance(s[1:], t[1:])
else:
return min( 1 + edit_distance(s[1:], t[1:]),
1 + edit_distance(s, t[1:]),
1 + edit_distance(s[1:], t))
def multi_lookup(index, keywords):
if not keywords:
return [ ]
activepos = lookup(index, keywords[0])
for keyword in keywords[1:]:
newactivepos = [ ]
nexturlpos = lookup(index, keyword)
if nexturlpos:
for pos in activepos:
if [pos[0], pos[1] + 1] in nexturlpos:
newactivepos.append([pos[0], pos[1] + 1])
activepos = newactivepos
result = []
for pos in activepos:
result.append(pos[0])
return result
def split_string(source, splitlist = split_list):
output = []
atsplit = True
for char in source:
if char in splitlist:
atsplit = True
elif atsplit:
output.append(char)
atsplit = False
else:
output[-1] += char
return output
def lucky_search(index, ranks, keyword):
pages = lookup(index, keyword)
if not pages:
return None
best_page = pages[0]
for candidate in pages:
if ranks[candidate] > ranks[best_page]:
best_page = candidate
return best_page
def quicksort_pages(pages, ranks):
if not pages or len(pages) <= 1:
return pages
else:
pivot = ranks[pages[0]]
worse = []
better = []
for page in pages[1:]:
if ranks[page] <= pivot:
worse.append(page)
else:
better.append(page)
return quicksort_pages(better, ranks) + [pages[0]]
def ordered_search(index, ranks, keyword):
pages = lookup(index, keyword)
return quicksort_pages(pages, ranks)
def compute_ranks(graph):
d = 0.8 # damping factor
numloops = 10
ranks = {}
npages = len(graph)
for page in graph:
ranks[page] = 1.0 / npages
for i in range(0, numloops):
newranks = {}
for page in graph:
newrank = (1 - d) / npages
for node in graph:
if page in graph[node]:
newrank = newrank + d * (ranks[node] / len(graph[node]))
newranks[page] = newrank
ranks = newranks
return ranks
def crawl_web(seed, max_depth = crawl_depth):
tocrawl = [[seed, 0]]
crawled = []
graph = {}
index = {}
while tocrawl:
page, depth = tocrawl.pop()
if page not in crawled and depth <= max_depth:
content = get_page(page)
add_page_to_index(index, page, content)
outlinks = get_all_links(content)
graph[page] = outlinks
for link in outlinks:
tocrawl.append([link, depth + 1])
crawled.append(page)
return index, graph
def get_next_target(page):
start_link = page.find('<a href=')
if start_link == -1:
return None, 0
start_quote = page.find('"', start_link)
end_quote = page.find('"', start_quote + 1)
url = page[start_quote + 1:end_quote]
return url, end_quote
def get_all_links(page):
links = []
while True:
url, endpos = get_next_target(page)
if url:
links.append(url)
page = page[endpos:]
else:
break
return links
def add_page_to_index(index, url, content):
words = split_string(content)
position = 0
for word in words:
add_to_index(index, word, url, position)
position = position + 1
def add_to_index(index, keyword, url, position):
if keyword in index:
index[keyword].append([url, position])
else:
index[keyword] = [[url, position]]
def lookup(index, keyword):
if keyword in index:
return index[keyword]
else:
return None
index, graph = crawl_web('http://')
ranks = compute_ranks(graph)