File size: 2,298 Bytes
97d70c2
 
49be6a8
 
 
 
 
 
b8c9b58
a233401
d4399fb
49be6a8
7609276
49be6a8
d4399fb
49be6a8
b8c9b58
49be6a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97d70c2
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
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)