|
| 1 | +from flask import Flask, request, jsonify |
| 2 | +from textblob import TextBlob |
| 3 | +from ekphrasis.classes.preprocessor import TextPreProcessor |
| 4 | +from ekphrasis.classes.tokenizer import SocialTokenizer |
| 5 | +from ekphrasis.dicts.emoticons import emoticons |
| 6 | +from ekphrasis.classes.spellcorrect import SpellCorrector |
| 7 | + |
| 8 | +text_processor = TextPreProcessor( |
| 9 | + normalize = [ |
| 10 | + 'url', |
| 11 | + 'email', |
| 12 | + 'percent', |
| 13 | + 'money', |
| 14 | + 'phone', |
| 15 | + 'user', |
| 16 | + 'time', |
| 17 | + 'url', |
| 18 | + 'date', |
| 19 | + 'number', |
| 20 | + ], |
| 21 | + annotate = { |
| 22 | + 'hashtag', |
| 23 | + 'allcaps', |
| 24 | + 'elongated', |
| 25 | + 'repeated', |
| 26 | + 'emphasis', |
| 27 | + 'censored', |
| 28 | + }, |
| 29 | + fix_html = True, |
| 30 | + segmenter = 'twitter', |
| 31 | + corrector = 'twitter', |
| 32 | + unpack_hashtags = True, |
| 33 | + unpack_contractions = True, |
| 34 | + spell_correct_elong = False, |
| 35 | + tokenizer = SocialTokenizer(lowercase = True).tokenize, |
| 36 | + dicts = [emoticons], |
| 37 | +) |
| 38 | + |
| 39 | +sp = SpellCorrector(corpus = 'english') |
| 40 | +app = Flask(__name__) |
| 41 | + |
| 42 | + |
| 43 | +def process_text(string): |
| 44 | + return ' '.join( |
| 45 | + [ |
| 46 | + sp.correct(c) |
| 47 | + for c in text_processor.pre_process_doc(string) |
| 48 | + if '<' not in c |
| 49 | + and '>' not in c |
| 50 | + and c not in ',!;:{}\'"!@#$%^&*(01234567890?/|\\' |
| 51 | + ] |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +@app.route('/', methods = ['GET']) |
| 56 | +def hello(): |
| 57 | + return 'Hello!' |
| 58 | + |
| 59 | + |
| 60 | +@app.route('/classify', methods = ['GET']) |
| 61 | +def classify(): |
| 62 | + text = request.args.get('text') |
| 63 | + result = TextBlob(process_text(text)) |
| 64 | + return jsonify( |
| 65 | + { |
| 66 | + 'polarity': result.sentiment.polarity, |
| 67 | + 'subjectivity': result.sentiment.subjectivity, |
| 68 | + } |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +application = app |
0 commit comments