'''
This file uses some state-of-art methods to generate summaries. 
Methods are as follows:
LSA, TextRank, LexRank. 
For detailed information about each algorithm, please refer to:

Generally, we uses these methods to generate summaries of comments from summary table,
in which, summaries generated by Random and LeadBased method have already been there.

Note: By default, LSA (mid = 3)

Author: Wenjun Wang
Date: June 3rd, 2015
'''

from __future__ import absolute_import
from __future__ import division, print_function, unicode_literals

from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer
from sumy.nlp.stemmers import Stemmer
from sumy.utils import get_stop_words

import MySQLdb as mdb
import sys 
import math
import nltk

LANGUAGE = "english"
PERCENT = 0.1
stemmer = Stemmer(LANGUAGE)

'''
walk_through - go through top 50 entities from a specific source
'''
def walk_through(db,cur):
	sent_tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
	getorig = 'SELECT summary.cid, comment.body FROM summary,comment\
	WHERE summary.cid = comment.cid AND mid = 1';
	cur.execute(getorig)
	rows = cur.fetchall()
	for row in rows:
		cid = row[0]
		comment = row[1]
		sentence_list = sent_tokenizer.tokenize(comment)
		num = int(math.ceil(PERCENT*len(sentence_list)))
		parser = PlaintextParser.from_string(comment, Tokenizer(LANGUAGE))
		LSAstr = lsa(comment,parser,num)
		#print(LSAstr)
		insert = 'INSERT INTO summary(cid,mid,score,body) VALUES\
		('+ str(cid) +',3,-1,"' + LSAstr + '")'
		cur.execute(insert)
		db.commit()

def lsa(comment,parser,num):
	summarizer = LsaSummarizer(stemmer)
	summarizer.stop_words = get_stop_words(LANGUAGE)

	LSAstr = ''
	for sentence in summarizer(parser.document,num):
		LSAstr += str(sentence)

	return LSAstr

def main():
	if len(sys.argv) < 4:
		print("Usage: python sumy.py db usr pwd")
		sys.exit(1)
	database = sys.argv[1]
	usr = sys.argv[2]
	pwd = sys.argv[3]

	db = mdb.connect(host="localhost",user=usr,passwd=pwd,db=database)
	cur = db.cursor()

	walk_through(db,cur)

	cur.close()
	db.close()

if __name__ == "__main__":
	main()
