-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
71 lines (45 loc) · 1.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask, request, render_template
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import os
import pandas as pd
import numpy as np
import nltk
#!from nltk.corpus import punkt
from string import punctuation
import re
from nltk.corpus import stopwords
nltk.download('stopwords')
set(stopwords.words('english'))
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('index.html')
@app.route('/', methods=['POST'])
def my_form_post():
stop_words = stopwords.words('english')
#!convert to lowercase
text1 = request.form['text1'].lower()
text_final = ''.join(c for c in text1 if not c.isdigit())
#*remove punctuations
#^text3 = ''.join(c for c in text2 if c not in punctuation)
#?remove stopwords
processed_doc1 = ' '.join([word for word in text_final.split() if word not in stop_words])
sanalyzer = SentimentIntensityAnalyzer()
dd = sanalyzer.polarity_scores(text=processed_doc1)
dd['text'] = processed_doc1
#*create a dataframe
df = pd.DataFrame(dd, index=[0])
#!create a vectorizer
vectorizer = TfidfVectorizer()
#*create a matrix
matrix = vectorizer.fit_transform(df['text'])
#&create a similarity matrix
sim_matrix = cosine_similarity(matrix)
#?create a similarity matrix
sim_matrix = sim_matrix.flatten()
compound = round((1 + dd['compound'])/2, 2)
return render_template('index.html', final=compound, text1=text_final,text2=dd['pos'],text5=dd['neg'],text4=compound,text3=dd['neu'], text6=sim_matrix)
if __name__ == "__main__":
app.run(debug=True, host="127.0.0.1", port=5002, threaded=True, processes=1)