Advertisement
Alexplazz

Untitled

Mar 15th, 2024
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.26 KB | None | 0 0
  1. # PR(X) = (1-0.85) * 0.85(ETC)
  2.  
  3. class page:
  4.     def __init__(self, name, links=None):
  5.         if links is None:
  6.             links = []
  7.  
  8.         self.name = name
  9.         self.__links = links
  10.         self.parent = False
  11.         self.__latestPointsTo = []
  12.  
  13.     def __contains__(self, item):
  14.         return item.name in self.__links
  15.  
  16.     def getInbounds(self):
  17.         inboundPages = []
  18.         for applicantPage in self.parent.getPages():
  19.             if applicantPage == self:
  20.                 continue
  21.  
  22.             if self in applicantPage:
  23.                 inboundPages.append(applicantPage)
  24.         self.__latestPointsTo = inboundPages
  25.         return inboundPages
  26.  
  27.     def __len__(self):
  28.         return len(self.__links)
  29.  
  30.     def __repr__(self):
  31.         return f"Name:{self.name}\nPoints To:{','.join(self.__links)}\nPointed To By:{','.join([v.name for v in self.__latestPointsTo])}"
  32. class index:
  33.     def __init__(self, data = []):
  34.         self.__data = data
  35.         self.__pageRank = {}
  36.  
  37.     def index(self, page):
  38.         self.__data.append(page)
  39.         self.__pageRank[page] = 1
  40.         page.parent = self
  41.  
  42.     def getPage(self, pageName):
  43.         for v in self.__data:
  44.             if v.name == pageName:
  45.                 return v
  46.         return False
  47.  
  48.     def optimise(self):
  49.         for indexedPage in self.__data:
  50.             finalParameter = 0
  51.             inboundPages = indexedPage.getInbounds()
  52.  
  53.             for v in inboundPages:
  54.                 finalParameter += (self.__pageRank[v] / len(v))
  55.  
  56.             self.__pageRank[indexedPage] = (1-0.85) * 0.85 * (finalParameter)
  57.  
  58.     def getPages(self):
  59.         for v in self.__data:
  60.             yield v
  61.  
  62.     @property
  63.     def searchResults(self):
  64.         record = False
  65.         recordPage = False
  66.         for v in self.getPages():
  67.             if (not record) or self.__pageRank[v] > record:
  68.                 record = self.__pageRank[v]
  69.                 recordPage = v
  70.         return recordPage
  71.  
  72. searchEngine = index()
  73. searchEngine.index(page("google", ["youtube", "wiki"]))
  74. searchEngine.index(page("youtube", ["wiki", "youtube", "google"]))
  75. searchEngine.index(page("wiki", ["google", "youtube"]))
  76.  
  77. for i in range(500):
  78.     searchEngine.optimise()
  79.  
  80. print(searchEngine.searchResults)
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement