lolamontes69

Ch4 Ex4-Collective Intelligence (page length)

Jun 27th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. """ Chapter 4 Exercise 4.
  2.    Long/Short Document Search.
  3.  
  4.    Sometimes the length of a page will be a determining factor in whether it is relevant to a particular search application or user. A user may be interested in finding a long article about a difficult subject or a quick reference page for a command-line tool.
  5.    Write a weighting function that will give preference to longer of shorter documents depending upon its parameters.
  6. """
  7.  
  8.     def pagelengthscore(self,rows,smallisbetter=0):
  9.         uniqueurls=set([row[0] for row in rows])
  10.         wordscount=dict([(u,self.con.execute('select count(*) from wordlocation where urlid=%d' % u).fetchone()[0]) for u in uniqueurls])
  11.         return self.normalizescores(wordscount,smallisbetter)
  12.  
  13. """
  14.    usage:
  15.        weights=[(1.0,self.pagelengthscore(rows, 0))]  # for longer pages
  16.        weights=[(1.0,self.pagelengthscore(rows, 1))]  # for shorter pages
Advertisement
Add Comment
Please, Sign In to add comment