Advertisement
Guest User

Untitled

a guest
May 24th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. def classify_plain_sql(pubid,topn):
  2.     # Create a new connection
  3.     con=connection()
  4.     # Create a cursor on the connection
  5.     cur=con.cursor()
  6.     result = cur.execute("SELECT title, summary FROM articles WHERE id=?", (pubid,))
  7.     for i in result:
  8.         title = i[0]
  9.         summary = i[1]
  10.     words = summary.split(" ")        
  11.     query = '''
  12.    SELECT title, class, subclass, weight
  13.    FROM classes, articles
  14.    where term=? and id=?
  15.    '''
  16.     l1 = []
  17.     for i in words:
  18.         l1.extend(cur.execute(query, [i, pubid]))
  19.     # Sort by class and subclass to replace GROUP BY
  20.     l1.sort(key = lambda tup: (tup[1], tup[2]))
  21.     l2 = []
  22.     index = 0
  23.     for i in range(len(l1)-1) :
  24.     print i
  25.         l2.append([l1[i][0],l1[i][1],l1[i][2],l1[i][3]])
  26.     for i in l2:
  27.     print i
  28.     flag = 1
  29.     while flag == 1 and index < (len(l2)-1):
  30.         if l2[index][1] == l2[index+1][1] and l2[index][2] == l2[index+1][2]:
  31.             l2[index][3] = l2[index][3] + l2[index+1][3]
  32.         del l2[index+1]
  33.     else:
  34.         index = index  + 1
  35.         if index == (len(l2)-1):
  36.         flag = 0
  37.     l2.sort(key = lambda tup: (tup[3]), reverse = True)    
  38.     return [("Title","Class","Subclass", "Sum of Weights")]  + l2[0:(int(topn))]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement