Spaces:
Running
Running
Commit
·
6e9bab7
1
Parent(s):
2474c1f
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,54 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
from summa.summarizer import summarize
|
| 7 |
from textblob import TextBlob
|
| 8 |
-
import spacy
|
| 9 |
|
|
|
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
def index():
|
| 14 |
-
return render_template(
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
def paraphrase():
|
| 18 |
-
|
| 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 |
-
|
| 58 |
-
|
| 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 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import required libraries
|
| 2 |
+
import nltk
|
| 3 |
+
import re
|
| 4 |
+
import numpy as np
|
| 5 |
+
from flask import Flask, request, render_template
|
|
|
|
| 6 |
from textblob import TextBlob
|
|
|
|
| 7 |
|
| 8 |
+
# Initialize the Flask application
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
+
# Define the root route
|
| 12 |
+
@app.route('/')
|
| 13 |
def index():
|
| 14 |
+
return render_template('index.html')
|
| 15 |
|
| 16 |
+
# Define the route for paraphrasing text
|
| 17 |
+
@app.route('/paraphrase', methods=['POST'])
|
| 18 |
def paraphrase():
|
| 19 |
+
# Get the input text from the form
|
| 20 |
+
input_text = request.form['input_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Correct grammar using TextBlob
|
| 23 |
+
corrected_text = TextBlob(input_text).correct()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Remove special characters using regular expressions
|
| 26 |
+
cleaned_text = re.sub('[^A-Za-z0-9]+', ' ', corrected_text)
|
| 27 |
+
|
| 28 |
+
# Summarize the text using TextBlob
|
| 29 |
+
summarized_text = TextBlob(cleaned_text).summarize()
|
| 30 |
+
|
| 31 |
+
# Perform Part-of-Speech (POS) tagging using NLTK
|
| 32 |
+
pos_tagged_text = nltk.pos_tag(summarized_text.words)
|
| 33 |
+
|
| 34 |
+
# Perform Named Entity Recognition (NER) using NLTK
|
| 35 |
+
ner_tagged_text = nltk.ne_chunk(pos_tagged_text)
|
| 36 |
+
|
| 37 |
+
# Perform sentiment analysis using TextBlob
|
| 38 |
+
sentiment = TextBlob(summarized_text).sentiment
|
| 39 |
+
|
| 40 |
+
# Perform emotion detection and adjust the tone of the paraphrased text
|
| 41 |
+
emotion = ""
|
| 42 |
+
if sentiment.polarity >= 0.5:
|
| 43 |
+
emotion = "Positive"
|
| 44 |
+
elif sentiment.polarity > 0 and sentiment.polarity < 0.5:
|
| 45 |
+
emotion = "Neutral"
|
| 46 |
+
else:
|
| 47 |
+
emotion = "Negative"
|
| 48 |
+
|
| 49 |
+
# Render the results template with the paraphrased text and analysis results
|
| 50 |
+
return render_template('results.html', paraphrased_text=summarized_text, pos_tagged_text=pos_tagged_text, ner_tagged_text=ner_tagged_text, sentiment=sentiment, emotion=emotion)
|
| 51 |
+
|
| 52 |
+
# Run the Flask application
|
| 53 |
+
if __name__ == '__main__':
|
| 54 |
+
app.run(debug=True,host="0.0.0.0",port=7860)
|