Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # PR(X) = (1-0.85) * 0.85(ETC)
- class page:
- def __init__(self, name, links=None):
- if links is None:
- links = []
- self.name = name
- self.__links = links
- self.parent = False
- self.__latestPointsTo = []
- def __contains__(self, item):
- return item.name in self.__links
- def getInbounds(self):
- inboundPages = []
- for applicantPage in self.parent.getPages():
- if applicantPage == self:
- continue
- if self in applicantPage:
- inboundPages.append(applicantPage)
- self.__latestPointsTo = inboundPages
- return inboundPages
- def __len__(self):
- return len(self.__links)
- def __repr__(self):
- return f"Name:{self.name}\nPoints To:{','.join(self.__links)}\nPointed To By:{','.join([v.name for v in self.__latestPointsTo])}"
- class index:
- def __init__(self, data = []):
- self.__data = data
- self.__pageRank = {}
- def index(self, page):
- self.__data.append(page)
- self.__pageRank[page] = 1
- page.parent = self
- def getPage(self, pageName):
- for v in self.__data:
- if v.name == pageName:
- return v
- return False
- def optimise(self):
- for indexedPage in self.__data:
- finalParameter = 0
- inboundPages = indexedPage.getInbounds()
- for v in inboundPages:
- finalParameter += (self.__pageRank[v] / len(v))
- self.__pageRank[indexedPage] = (1-0.85) * 0.85 * (finalParameter)
- def getPages(self):
- for v in self.__data:
- yield v
- @property
- def searchResults(self):
- record = False
- recordPage = False
- for v in self.getPages():
- if (not record) or self.__pageRank[v] > record:
- record = self.__pageRank[v]
- recordPage = v
- return recordPage
- searchEngine = index()
- searchEngine.index(page("google", ["youtube", "wiki"]))
- searchEngine.index(page("youtube", ["wiki", "youtube", "google"]))
- searchEngine.index(page("wiki", ["google", "youtube"]))
- for i in range(500):
- searchEngine.optimise()
- print(searchEngine.searchResults)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement