lolamontes69

Ch4 Ex6-Collective Intelligence (Inbound link searching)

Jun 27th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. """ Chapter 4 Exercise 6: Inbound link searching.
  2.  
  3.    Your code can rank items based on the text of the inbound links, but they must already be results based on the content of the page. Sometimes the most relevant page doesn't contain the query text at all, but rather a lot of links with text pointing to it - this is often the case with links to images.
  4.  
  5.    Modify the search code to include results where an inbound link contains some of the search terms.
  6.  
  7.    Note: inboundlinks has no word positions unlike row in rows so I included the word positions from their parent. Thus the children may become as important as their parents.
  8. """
  9.  
  10.     def query(self,q):
  11.         rows,wordids=self.getmatchrows(q.lower())
  12.         if rows == "Nothing":
  13.             return ['%s Not in database' %q]
  14.  
  15.         inboundlinks = []
  16.         for row in rows:
  17.             instance = (self.con.execute('select toid from link where fromid=%d' % row[0]).fetchone()[0],)+row[1:]
  18.             if instance not in inboundlinks: inboundlinks.append(instance)
  19.         for inbound in inboundlinks: rows.append(inbound)
  20.        
  21.         scores=self.getscoredlist(rows,wordids)
  22.         rankedscores=sorted([(score,url) for (url,score) in scores.items()],reverse=1)
  23.         for (score,urlid) in rankedscores[0:10]:
  24.             print '%f\t%s' % (score,self.geturlname(urlid))
  25.         return wordids,[r[1] for r in rankedscores[0:10]]
Advertisement
Add Comment
Please, Sign In to add comment