Guest User

Untitled

a guest
Oct 21st, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.71 KB | None | 0 0
  1. from flask import Flask, jsonify, request, session
  2. import pickle, re
  3.  
  4. app = Flask(__name__)
  5.  
  6. tweety = pickle.load(open('./twitter.pickle'))
  7.  
  8. @app.route('/', methods=['GET'])
  9. def hello_world():
  10.     s = '''
  11.   <form action="/result" method="post">
  12.   <p><input type=text name=ipText>
  13.   <p><input type=submit value=Analyse>
  14.   </form>
  15.   '''
  16.     return s
  17.  
  18. @app.route('/json', methods=['POST'])
  19. def json():
  20.     try:
  21.         text = request.form['ipText']
  22.     text = re.sub(r"[!@#$%\^&\*()\[\]{};:<>.?/\|,\\]+"," ",text)
  23.     text = re.sub(r"'\"","", text)
  24.         test_feats = dict([word, True] for word in text.split())
  25.         test_classify = tweety.prob_classify(test_feats)
  26.         result = tweety.classify(test_feats)
  27.         classes = ['pos', 'neg']
  28.         prob = {}
  29.         for cls in classes:
  30.             prob[cls] = '%0.2f' % round(test_classify.prob(cls), 2)
  31.         hello = {'result': result, 'output': prob}
  32.         return jsonify(hello)
  33.     except KeyError:
  34.         return "Please pass some text to analyze."
  35.  
  36. @app.route('/feedback', methods=['POST'])
  37. def feedback():
  38.     try:
  39.         text = request.form['ipText']
  40.         result = request.form['result']
  41.         print text, result, request.form['feedback']
  42.         if result == 'neg':
  43.             if str(request.form['feedback']) == '1':
  44.                 # add it to file with strong/ideal neg statements
  45.                 logFile = open('neg.txt', 'a')
  46.                 # two blank lines?
  47.                 logFile.write(text+'\n\n')
  48.                 logFile.close()
  49.             elif str(request.form['feedback']) == '-1':
  50.                 '''
  51.                   This is noise, so find a way to remove such content from
  52.                   Tweet logs.
  53.                '''
  54.                 pass
  55.         else :
  56.             if str(request.form['feedback']) == '1':
  57.                 # add it to file with strong/ideal pos statements
  58.                 logFile = open('pos.txt', 'a')
  59.                 # two blank lines?
  60.                 logFile.write(text+'\n\n')
  61.                 logFile.close()
  62.             elif str(request.form['feedback']) == '-1':
  63.                 '''
  64.                   This is noise, so find a way to remove such content from
  65.                   Tweet logs.
  66.                '''
  67.                 pass
  68.         return 'Recorded the feedback.'
  69.     except KeyError:
  70.         return 'Not able to process feedback.'
  71.  
  72. @app.route('/result', methods=['POST'])
  73. def result():
  74.     try:
  75.         text = request.form['ipText']
  76.         ipText = text
  77.     text = re.sub(r"[!@#$%\^&\*()\[\]{};:<>.?/\|,\\]+"," ",text)
  78.     text = re.sub(r"'\"","", text)
  79.         test_feats = dict([word, True] for word in text.split())
  80.         test_classify = tweety.prob_classify(test_feats)
  81.         result = tweety.classify(test_feats)
  82.         classes = ['pos', 'neg']
  83.         prob = {}
  84.         for cls in classes:
  85.             prob[cls] = '%0.2f' % round(test_classify.prob(cls), 2)
  86.         print ipText
  87.         htmlResult = '''<form action="/feedback" method="post">
  88.        <p>Text <input type=text name=ipText value="'''+ipText+'''">
  89.        <p> Result is: <input type=text name=result value='''+result+'''>
  90.        <p> Analysis:<br />
  91.        Positive: <input type=text name=pos value='''+str(prob['pos'])+'''><br />
  92.        Negative: <input type=text name=neg value='''+str(prob['neg'])+'''><br />
  93.        <input type=radio name=feedback value=-1> -1
  94.        <input type=radio name=feedback value=0> 0
  95.        <input type=radio name=feedback value=1> 1
  96.        <p><input type=submit value=Feedback>
  97.        </form>
  98.        '''
  99.         hello = {'result': result, 'output': prob}
  100.         # return jsonify(hello)
  101.         return htmlResult
  102.     except KeyError:
  103.         return "Please pass some text to analyze."
  104.  
  105. if __name__ == '__main__':
  106.     app.run(host='0.0.0.0', debug=True)
Add Comment
Please, Sign In to add comment