-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearcherAlgorithm.py
104 lines (74 loc) · 3.06 KB
/
SearcherAlgorithm.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
import wikipedia
from linkFinder import *
# Checks to see if the destination page is in the passed in list
def findLink(links, wantedLink):
for x in range(len(links)):
if links[x].lower() == wantedLink.title.lower():
return x
return -1
###############################################################################
# Traverses links across layers of Wiki pages
def layerSearch(previousList, counter, dest):
global masterList
toBeList = []
counter += 1
for x in range(len(previousList)):
try:
nextElem = wikipedia.page(previousList[x])
except wikipedia.exceptions.WikipediaException:
continue
currentList = getLinks(nextElem.title, masterList)
for y in range(len(currentList)):
associateTo(currentList[y],nextElem.title)
if(findLink(currentList, dest) >= 0 ):
return counter
for i in currentList:
if i not in masterList:
toBeList.append(i)
masterList.append(i)
layerSearch(toBeList, counter, dest)
###############################################################################
def associateTo(ancestor, predecessor):
reverseAssociations[ancestor] = predecessor
###############################################################################
def acquireAllAssociations(ancestor,counter, listStrings):
for x in reverseAssociations:
if x.lower() == ancestor.lower() and counter != 0:
listStrings = listStrings + [reverseAssociations[x]]
counter = counter - 1
return acquireAllAssociations(reverseAssociations[x], counter, listStrings)
return listStrings
###############################################################################
def main(begin, end):
# TODO: FIX DISAMBIGUATION ERRORS ###########################################
try:
start = wikipedia.page(begin) if begin != "" else wikipedia.page(wikipedia.random())
except wikipedia.exceptions.PageError:
return ("\nSorry the start page %s doesn't exist" %begin)
try:
dest = wikipedia.page(end) if end != "" else wikipedia.page(wikipedia.random())
except wikipedia.exceptions.PageError:
return ("\nSorry the ending page %s doesn't exist" %end)
#############################################################################
previousList = []
listOfConnections = []
currentList = getLinks(start.title, masterList)
result = 1;
for y in range(len(currentList)):
associateTo(currentList[y],start.title)
if(findLink(currentList, dest) >= 0 ):
listOfConnections = acquireAllAssociations(dest.title,
result, listOfConnections)
else:
previousList = currentList
for i in previousList:
if i not in masterList:
masterList.append(i)
result = layerSearch(previousList, result, dest)
listOfConnections = acquireAllAssociations(dest.title,
result, listOfConnections)
return list(reversed([dest.title] + listOfConnections))
###############################################################################
# Globals
masterList = []
reverseAssociations = {}