Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, jsonify, request, session
- import pickle, re
- app = Flask(__name__)
- tweety = pickle.load(open('./twitter.pickle'))
- @app.route('/', methods=['GET'])
- def hello_world():
- s = '''
- <form action="/result" method="post">
- <p><input type=text name=ipText>
- <p><input type=submit value=Analyse>
- </form>
- '''
- return s
- @app.route('/json', methods=['POST'])
- def json():
- try:
- text = request.form['ipText']
- text = re.sub(r"[!@#$%\^&\*()\[\]{};:<>.?/\|,\\]+"," ",text)
- text = re.sub(r"'\"","", text)
- test_feats = dict([word, True] for word in text.split())
- test_classify = tweety.prob_classify(test_feats)
- result = tweety.classify(test_feats)
- classes = ['pos', 'neg']
- prob = {}
- for cls in classes:
- prob[cls] = '%0.2f' % round(test_classify.prob(cls), 2)
- hello = {'result': result, 'output': prob}
- return jsonify(hello)
- except KeyError:
- return "Please pass some text to analyze."
- @app.route('/feedback', methods=['POST'])
- def feedback():
- try:
- text = request.form['ipText']
- result = request.form['result']
- print text, result, request.form['feedback']
- if result == 'neg':
- if str(request.form['feedback']) == '1':
- # add it to file with strong/ideal neg statements
- logFile = open('neg.txt', 'a')
- # two blank lines?
- logFile.write(text+'\n\n')
- logFile.close()
- elif str(request.form['feedback']) == '-1':
- '''
- This is noise, so find a way to remove such content from
- Tweet logs.
- '''
- pass
- else :
- if str(request.form['feedback']) == '1':
- # add it to file with strong/ideal pos statements
- logFile = open('pos.txt', 'a')
- # two blank lines?
- logFile.write(text+'\n\n')
- logFile.close()
- elif str(request.form['feedback']) == '-1':
- '''
- This is noise, so find a way to remove such content from
- Tweet logs.
- '''
- pass
- return 'Recorded the feedback.'
- except KeyError:
- return 'Not able to process feedback.'
- @app.route('/result', methods=['POST'])
- def result():
- try:
- text = request.form['ipText']
- ipText = text
- text = re.sub(r"[!@#$%\^&\*()\[\]{};:<>.?/\|,\\]+"," ",text)
- text = re.sub(r"'\"","", text)
- test_feats = dict([word, True] for word in text.split())
- test_classify = tweety.prob_classify(test_feats)
- result = tweety.classify(test_feats)
- classes = ['pos', 'neg']
- prob = {}
- for cls in classes:
- prob[cls] = '%0.2f' % round(test_classify.prob(cls), 2)
- print ipText
- htmlResult = '''<form action="/feedback" method="post">
- <p>Text <input type=text name=ipText value="'''+ipText+'''">
- <p> Result is: <input type=text name=result value='''+result+'''>
- <p> Analysis:<br />
- Positive: <input type=text name=pos value='''+str(prob['pos'])+'''><br />
- Negative: <input type=text name=neg value='''+str(prob['neg'])+'''><br />
- <input type=radio name=feedback value=-1> -1
- <input type=radio name=feedback value=0> 0
- <input type=radio name=feedback value=1> 1
- <p><input type=submit value=Feedback>
- </form>
- '''
- hello = {'result': result, 'output': prob}
- # return jsonify(hello)
- return htmlResult
- except KeyError:
- return "Please pass some text to analyze."
- if __name__ == '__main__':
- app.run(host='0.0.0.0', debug=True)
Add Comment
Please, Sign In to add comment