AI-Rephraser / app.py
imseldrith's picture
Update app.py
49be6a8
raw
history blame
2.3 kB
from flask import Flask, render_template, request
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from summa.summarizer import summarize
from textblob import TextBlob
import spacy
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/paraphrase", methods=["POST"])
def paraphrase():
input_text = request.form["input_text"]
# Option to correct grammar using TextBlob
corrected_text = str(TextBlob(input_text).correct())
# Option to remove special characters
clean_text = ''.join(e for e in corrected_text if e.isalnum() or e.isspace())
# Perform text summarization
summary = summarize(clean_text)
# Perform word tokenization and remove stopwords
stop_words = set(stopwords.words("english"))
words = word_tokenize(summary)
words = [word for word in words if word.lower() not in stop_words]
# Perform lemmatization on the words
lemmatizer = WordNetLemmatizer()
lemmatized_words = [lemmatizer.lemmatize(word, pos=get_wordnet_pos(word)) for word in words]
# Load spaCy's NER model
nlp = spacy.load("en_core_web_sm")
# Use spaCy's NER to identify named entities in the input text
doc = nlp(summary)
entities = []
for ent in doc.ents:
entities.append((ent.text, ent.label_))
# Use spaCy's POS tagging on the input text
pos_tags = []
for token in doc:
pos_tags.append((token.text, token.pos_))
# Use TextBlob to perform sentiment analysis on the input text
sentiment = TextBlob(summary).sentiment.polarity
return render_template("paraphrase.html", input_text=input_text, output_text=' '.join(lemmatized_words), entities=entities, pos_tags=pos_tags, sentiment=sentiment)
def get_wordnet_pos(word):
"""Map POS tag to first character used by WordNetLemmatizer"""
tag = nltk.pos_tag([word])[0][1]
tag = tag[0].upper()
tag_dict = {"J": wordnet.ADJ,
"N": wordnet.NOUN,
"V": wordnet.VERB,
"R": wordnet.ADV}
return tag_dict.get(tag, wordnet.NOUN)
if __name__ == "__main__":
app.run(host="0.0.0.0",port=7860,debug=True)