Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def classify_plain_sql(pubid,topn):
- # Create a new connection
- con=connection()
- # Create a cursor on the connection
- cur=con.cursor()
- result = cur.execute("SELECT title, summary FROM articles WHERE id=?", (pubid,))
- for i in result:
- title = i[0]
- summary = i[1]
- words = summary.split(" ")
- query = '''
- SELECT title, class, subclass, weight
- FROM classes, articles
- where term=? and id=?
- '''
- l1 = []
- for i in words:
- l1.extend(cur.execute(query, [i, pubid]))
- # Sort by class and subclass to replace GROUP BY
- l1.sort(key = lambda tup: (tup[1], tup[2]))
- l2 = []
- index = 0
- for i in range(len(l1)-1) :
- print i
- l2.append([l1[i][0],l1[i][1],l1[i][2],l1[i][3]])
- for i in l2:
- print i
- flag = 1
- while flag == 1 and index < (len(l2)-1):
- if l2[index][1] == l2[index+1][1] and l2[index][2] == l2[index+1][2]:
- l2[index][3] = l2[index][3] + l2[index+1][3]
- del l2[index+1]
- else:
- index = index + 1
- if index == (len(l2)-1):
- flag = 0
- l2.sort(key = lambda tup: (tup[3]), reverse = True)
- return [("Title","Class","Subclass", "Sum of Weights")] + l2[0:(int(topn))]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement