Spaces:
Running
Running
Commit
·
49be6a8
1
Parent(s):
167ca4c
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,68 @@
|
|
1 |
from flask import Flask, render_template, request
|
2 |
-
from textblob import TextBlob
|
3 |
-
import re
|
4 |
-
import nltk
|
5 |
-
from nltk.translate.bleu_score import sentence_bleu
|
6 |
from nltk.corpus import wordnet
|
7 |
-
from nltk.
|
8 |
-
from nltk.
|
|
|
|
|
|
|
|
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
-
@app.route(
|
13 |
def index():
|
14 |
-
return render_template(
|
15 |
|
16 |
-
@app.route(
|
17 |
def paraphrase():
|
18 |
-
input_text = request.form[
|
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 |
-
return 'positive' if emotion > 0 else 'negative' if emotion < 0 else 'neutral'
|
69 |
-
|
70 |
-
if __name__ == '__main__':
|
71 |
app.run(host="0.0.0.0",port=7860,debug=True)
|
|
|
1 |
from flask import Flask, render_template, request
|
|
|
|
|
|
|
|
|
2 |
from nltk.corpus import wordnet
|
3 |
+
from nltk.tokenize import word_tokenize
|
4 |
+
from nltk.corpus import stopwords
|
5 |
+
from nltk.stem import WordNetLemmatizer
|
6 |
+
from summa.summarizer import summarize
|
7 |
+
from textblob import TextBlob
|
8 |
+
import spacy
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
+
@app.route("/")
|
13 |
def index():
|
14 |
+
return render_template("index.html")
|
15 |
|
16 |
+
@app.route("/paraphrase", methods=["POST"])
|
17 |
def paraphrase():
|
18 |
+
input_text = request.form["input_text"]
|
19 |
+
|
20 |
+
# Option to correct grammar using TextBlob
|
21 |
+
corrected_text = str(TextBlob(input_text).correct())
|
22 |
+
|
23 |
+
# Option to remove special characters
|
24 |
+
clean_text = ''.join(e for e in corrected_text if e.isalnum() or e.isspace())
|
25 |
+
|
26 |
+
# Perform text summarization
|
27 |
+
summary = summarize(clean_text)
|
28 |
+
|
29 |
+
# Perform word tokenization and remove stopwords
|
30 |
+
stop_words = set(stopwords.words("english"))
|
31 |
+
words = word_tokenize(summary)
|
32 |
+
words = [word for word in words if word.lower() not in stop_words]
|
33 |
+
|
34 |
+
# Perform lemmatization on the words
|
35 |
+
lemmatizer = WordNetLemmatizer()
|
36 |
+
lemmatized_words = [lemmatizer.lemmatize(word, pos=get_wordnet_pos(word)) for word in words]
|
37 |
+
|
38 |
+
# Load spaCy's NER model
|
39 |
+
nlp = spacy.load("en_core_web_sm")
|
40 |
+
|
41 |
+
# Use spaCy's NER to identify named entities in the input text
|
42 |
+
doc = nlp(summary)
|
43 |
+
entities = []
|
44 |
+
for ent in doc.ents:
|
45 |
+
entities.append((ent.text, ent.label_))
|
46 |
+
|
47 |
+
# Use spaCy's POS tagging on the input text
|
48 |
+
pos_tags = []
|
49 |
+
for token in doc:
|
50 |
+
pos_tags.append((token.text, token.pos_))
|
51 |
+
|
52 |
+
# Use TextBlob to perform sentiment analysis on the input text
|
53 |
+
sentiment = TextBlob(summary).sentiment.polarity
|
54 |
+
|
55 |
+
return render_template("paraphrase.html", input_text=input_text, output_text=' '.join(lemmatized_words), entities=entities, pos_tags=pos_tags, sentiment=sentiment)
|
56 |
+
|
57 |
+
def get_wordnet_pos(word):
|
58 |
+
"""Map POS tag to first character used by WordNetLemmatizer"""
|
59 |
+
tag = nltk.pos_tag([word])[0][1]
|
60 |
+
tag = tag[0].upper()
|
61 |
+
tag_dict = {"J": wordnet.ADJ,
|
62 |
+
"N": wordnet.NOUN,
|
63 |
+
"V": wordnet.VERB,
|
64 |
+
"R": wordnet.ADV}
|
65 |
+
return tag_dict.get(tag, wordnet.NOUN)
|
66 |
+
|
67 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
68 |
app.run(host="0.0.0.0",port=7860,debug=True)
|