lolamontes69

Ch4 Ex2-Collective Intelligence (booleans in query)

Jun 25th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.90 KB | None | 0 0
  1. """ Chapter 4 Exercise 2
  2.  
  3.    Boolean operations.
  4.    Many search engines support Boolean queries, which allow users to construct searches like "python OR pearl."
  5.    An OR search can work by doing the queries separately and combining the results,
  6.    but what about "python AND (program OR code)."?
  7.    Modify the query methods to support some basic Boolean operations.
  8.  
  9.    Accepted methods
  10.    1: foo and bar
  11.    2: foo or bar  3: foo and (bar or blah)  4: (bar or blah) and foo
  12.    6: foo not bar
  13.    
  14. """
  15.     def and_op_query(self,q):
  16.         wordids = []
  17.         q1 = q.split(' ')
  18.         for q2 in q1:
  19.             if q2 != '':
  20.                 passthescores = {}
  21.                 rows,words=self.getmatchrows(q2)
  22.                 if rows == "Nothing":
  23.                     wordids = passthescores = "Nothing"
  24.                     return wordids, passthescores
  25.                 scores=self.getscoredlist(rows,wordids)
  26.                 for a in scores:
  27.                     passthescores[a] = scores[a]
  28.                 wordids += words
  29.         return wordids, passthescores
  30.  
  31.     def query(self,q):
  32.         passthescores = {}
  33.         or_list = []
  34.  
  35.         # Just in case an ignored word gets put into the query
  36.         for a in ignorewords:        
  37.             if (a!='and') and (a!='or'): q = q.replace(' '+a+' ',' ')
  38.  
  39.         # An A AND B query that needs both in it
  40.         if (' or ' not in q)  and (' and ' in q):
  41.             q1 = q.replace(' and ',' ')
  42.             wordids, passthescores = self.and_op_query(q1)
  43.             if (wordids == 'Nothing'):
  44.                 return ['%s Not in database' %q]
  45.  
  46.         # AND/OR's are treated like AND's
  47.         elif (' and ' in q) and (' or ' in q):
  48.             wordids = []
  49.             and_list = q.split(' and ')
  50.             for and1 in range(len(and_list)):
  51.                 if ' or ' in and_list[and1]:
  52.                     if and1 > 0:
  53.                         or_list = and_list[and1].replace('(','').replace(')','').split(' or ')
  54.                         for or1 in or_list:
  55.                             q1 = and_list[and1-1]+' '+or1
  56.                             words, q1dic = self.and_op_query(q1)
  57.                             if words == 'Nothing': pass
  58.                             else:
  59.                                 wordids += words
  60.                                 for res in q1dic:
  61.                                     passthescores[res] = q1dic[res]
  62.                     if and1 == 0:
  63.                         or_list = and_list[and1].replace('(','').replace(')','').split(' or ')
  64.                         for or1 in or_list:
  65.                             q1 = or1+' '+and_list[and1+1]
  66.                             words, q1dic = self.and_op_query(q1)
  67.                             if words == 'Nothing': pass
  68.                             else:
  69.                                 wordids += words
  70.                                 for res in q1dic:
  71.                                     passthescores[res] = q1dic[res]
  72.             if (len(wordids) == 0) : return ['%s Not in database' %q]
  73.  
  74.         elif (' or ' in q):
  75.             # An A OR B is treated like a normal query so just remove the word OR
  76.             q1 = q.replace('(','').replace(')','').replace(' or ',' ')
  77.             rows,wordids=self.getmatchrows(q1)
  78.             if rows == "Nothing": return ['%s Not in database' %q]
  79.             scores=self.getscoredlist(rows,wordids)
  80.             for a in scores:
  81.                 passthescores[a] = scores[a]
  82.  
  83.         # So I wrote simple NOT method anyway
  84.         elif (' not ' in q):
  85.             passscores1 = {}
  86.             q1 = q.split(' not ')
  87.             for q2 in range(len(q1)):
  88.                 if q2 == 0:
  89.                     wordids, passthescores = self.and_op_query(q1[q2])
  90.                     if (wordids == 'Nothing'):
  91.                         return ['%s Not in database' %q]
  92.  
  93.                 elif (q2 > 0) and (q1[q2]!=''):
  94.                     wordids1, passscores1 = self.and_op_query(q1[q2])
  95.                     if (wordids == 'Nothing'):
  96.                         pass
  97.                     else:
  98.                         for notdic in passscores1:
  99.                             if notdic in passthescores:
  100.                                 passthescores.pop(notdic)
  101.             if len(passthescores)==0:
  102.                 return ['%s Not in database' %q]
  103.  
  104.         else:
  105.             # if this is just a normal query pass normally
  106.             rows,wordids=self.getmatchrows(q)
  107.             if rows == "Nothing": return ['%s Not in database' %q]
  108.             scores=self.getscoredlist(rows,wordids)
  109.             for a in scores:
  110.                 passthescores[a] = scores[a]
  111.  
  112.         # return top ten scores
  113.         rankedscores=sorted([(score,url) for (url,score) in passthescores.items()],reverse=1)
  114.         for (score,urlid) in rankedscores[0:10]:
  115.             print '%f\t%s' % (score,self.geturlname(urlid))
  116.         return wordids,[r[1] for r in rankedscores[0:10]]
Advertisement
Add Comment
Please, Sign In to add comment