def replace_score(predictor_name = 'MaxEntropy'): from database import update_score_for_product_id, select_ft_score from predictor import loadTrainedPredictor from srs_local import get_ft_dicts_from_contents ''' this function replaces all scores stored in the db with scores by vanderSentiment ''' res = select_ft_score() predictor = loadTrainedPredictor(predictor_name) # print res[1]["ft_score"] for r in res: product_id = r["product_id"] prod_contents = r["contents"] prod_ft_score_dict, prod_ft_senIdx_dict = get_ft_dicts_from_contents(prod_contents, predictor) update_score_for_product_id(product_id, prod_ft_score_dict, prod_ft_senIdx_dict)
def calculate_ft_dict_for_all_products(predictor_name): predictor = loadTrainedPredictor() client, db = connect_to_db() db_product_collection = db.product_collection cursor = db_product_collection.find() i=0 for product in cursor: i+=1 product_id = product['product_id'] prod_contents = product['contents'] prod_ft_score_dict, prod_ft_senIdx_dict = get_ft_dicts_from_contents(prod_contents, predictor) query = {"product_id": product_id} update_field = { "ft_score": prod_ft_score_dict, "ft_senIdx": prod_ft_senIdx_dict } db_product_collection.update(query, {"$set": update_field}, True) if i%10 == 0: print i client.close()