Commit 473ef24d authored by Malith Anjana's avatar Malith Anjana

finalize

parent f1100075
from ast import Return
import json import json
from chatterbot import ChatBot from chatterbot import ChatBot
from flask import Flask, jsonify, request from flask import Flask, jsonify, request
...@@ -9,24 +10,53 @@ print('Loading Sentiment Analysis model') ...@@ -9,24 +10,53 @@ print('Loading Sentiment Analysis model')
sa = Sentiment() sa = Sentiment()
app = Flask(__name__) app = Flask(__name__)
# bot = ChatBot('Chatterbot', storage_adapter="chatterbot.storage.SQLStorageAdapter")
# trainer = ChatterBotCorpusTrainer(bot) sentiment_dict = dict()
# trainer.train('./chatterbot_training_dataset/ai.yml', './chatterbot_training_dataset/emotion.yml', './chatterbot_training_dataset/conversational.yml')
#trainer.train('./chatterbot_training_dataset/conversational.yml') score_report = list()
#trainer.train('./chatterbot_training_dataset/emotion.yml')
@app.route('/test', methods=['POST']) @app.route('/test', methods=['POST'])
def index_test(): def index_test():
global sentiment_dict
global score_report
overall_output=dict()
js_text = request.get_json() js_text = request.get_json()
#Initialize the dictionary
if "start" in js_text:
if js_text["start"]:
sentiment_dict=dict()
score_report=list()
else:
return jsonify({"error":"You have to define the start in the payload"})
pred=sa.get_sentiment_prediction(js_text['text']) pred=sa.get_sentiment_prediction(js_text['text'])
new_ma_val = max(pred.items(), key=operator.itemgetter(1))[0] max_emotion = max(pred.items(), key=operator.itemgetter(1))[0]
max_value = max(pred.items(), key=operator.itemgetter(1))[1]
output={"text":js_text['text'], "matchedEmotion":max_emotion,"maximumValue":max_value, "emotionWithAverage":pred}
score_report.append(output)
if max_emotion in sentiment_dict:
sentiment_dict[max_emotion]+=max_value
else:
sentiment_dict[max_emotion]=max_value
sorted_values = {k:v for k,v in sorted(sentiment_dict.items(), key=lambda x: x[1], reverse=True)}
overall_output={"overalEmotion":list(sorted_values.keys())[0],"overalScore":list(sorted_values.values())[0], "otherScores":dict(zip(list(sorted_values.keys())[1:],list(sorted_values.values())[1:]))}
overal_output={"overall":overall_output, "individual": score_report}
overal_res=json.dumps(overal_output)
#arr = [ {'emotion' : i, 'value' : pred[i]} for i in pred] ##clear the dictionary
avg = json.dumps(pred) if "end" in js_text:
if js_text["end"]:
sentiment_dict=dict()
score_report=list()
#response from chatbot return overal_res
# res = str(bot.get_response(js_text['text']))
return avg
app.run(debug=True, port=8080) app.run(debug=True, port=8080)
\ No newline at end of file
...@@ -148,7 +148,7 @@ class Sentiment: ...@@ -148,7 +148,7 @@ class Sentiment:
def load_model_weights(self,model): def load_model_weights(self,model):
model.load_weights('./model_weights/sentiment_weights.h5') model.load_weights('./model_weights/sentiment_weights3.h5')
return model return model
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment