from scraper import main as scraper_main
from predictor import StaticPredictor, loadTrainedStaticPredictor
from srs import settings
from utilities import loadScraperDataFromDB, Sentence
from swnModel import get_sentiment_score_for_sentences, get_ftScore_ftSenIdx_dicts
from sentiment_plot import box_plot
from database import upsert_contents_for_product_id, update_for_product_id
import os

def get_ft_dicts_from_contents(contents, staticPredictor):
	sentences = []
	for cont in contents:
		sentences.append(Sentence(content=cont))

	staticPredictor.predict_for_sentences(sentences)	
	get_sentiment_score_for_sentences(sentences)
	return get_ftScore_ftSenIdx_dicts(sentences)
	

def fill_in_db(product_id):
	
	# fetch product info from db 
	prod1_contents, prod1_ft_score_dict, prod1_ft_senIdx_dict = loadScraperDataFromDB(product_id)

	if len(prod1_contents) == 0: # not in db yet
		# scrape contents
		prod1_contents = scraper_main(product_id)
		
		# classify, sentiment score
		staticPredictor = loadTrainedStaticPredictor()
		prod1_ft_score_dict, prod1_ft_senIdx_dict = \
		get_ft_dicts_from_contents(prod1_contents, staticPredictor)
		
		# insert new entry
		upsert_contents_for_product_id(product_id, prod1_contents, \
			prod1_ft_score_dict, prod1_ft_senIdx_dict)
	
	elif len(prod1_ft_score_dict) == 0 or len(prod1_ft_senIdx_dict) == 0:
		
		# classify, sentiment score
		staticPredictor = loadTrainedStaticPredictor()
		prod1_ft_score_dict, prod1_ft_senIdx_dict = \
		get_ft_dicts_from_contents(prod1_contents, staticPredictor)
		
		# update old entry
		update_for_product_id(product_id, prod1_ft_score_dict, prod1_ft_senIdx_dict)
	else:
		print "Filled in db before for {0}".format(product_id)
	
def plot(product_id):
	_, prod1_ft_score_dict, _ = loadScraperDataFromDB(product_id)
	plot_folder = settings['sentiment_plot']
	figure_file_path = os.path.join(plot_folder, product_id + '_boxplot.png')
	box_plot(prod1_ft_score_dict, figure_file_path, product_id)

def main(product_id):
	fill_in_db(product_id)
	plot(product_id)

if __name__ == '__main__':
	product_id = 'B00HZE2PYI'
	main(product_id)
@@ -33,17 +33,38 @@
 per_main
+, createAmazonScraper
 %0A
-
 from pre
@@ -700,32 +700,47 @@
 %0Adef fill_in_db(
+amazonScraper, 
 product_id):%0A%09%0A%09
@@ -959,24 +959,39 @@
 craper_main(
+amazonScraper, 
 product_id)%0A
@@ -1314,16 +1314,31 @@
 dx_dict)
+%0A%0A%09%09return True
 %0A%09%0A%09elif
@@ -1697,16 +1697,31 @@
 dx_dict)
+%0A%0A%09%09return True
 %0A%09else:%0A
@@ -1776,16 +1776,32 @@
 duct_id)
+%0A%0A%09%09return False
 %0A%09%0Adef p
@@ -2081,16 +2081,43 @@
 ct_id):%0A
+%09a = createAmazonScraper()%0A
 %09fill_in
@@ -2120,16 +2120,19 @@
 l_in_db(
+a, 
 product_

modify fill_in_db so that