File size: 2,292 Bytes
97d70c2
 
 
 
 
 
 
 
b8c9b58
a233401
d4399fb
97d70c2
7609276
97d70c2
d4399fb
97d70c2
b8c9b58
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
69
70
71
from flask import Flask, render_template, request
from textblob import TextBlob
import re
import nltk
from nltk.translate.bleu_score import sentence_bleu
from nltk.corpus import wordnet
from nltk.corpus import sentiwordnet as swn
from nltk.sentiment import SentimentIntensityAnalyzer

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']
    input_text = re.sub(r'[^\w\s]', '', input_text) # remove special characters

    # Correct grammar using TextBlob
    input_text = str(TextBlob(input_text).correct())

    # Summarize the text using TextBlob
    summarized_text = str(TextBlob(input_text).summarize())

    # Paraphrase the text
    paraphrased_text = generate_paraphrase(input_text)

    # Emotion detection
    emotion = detect_emotion(input_text)

    # Named Entity Recognition
    entities = nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(input_text)))

    # Part-of-Speech Tagging
    pos_tags = nltk.pos_tag(nltk.word_tokenize(input_text))

    # Sentiment Analysis
    sentiment = SentimentIntensityAnalyzer().polarity_scores(input_text)

    return render_template('index.html', input_text=input_text, summarized_text=summarized_text, paraphrased_text=paraphrased_text, entities=entities, pos_tags=pos_tags, sentiment=sentiment, emotion=emotion)

def generate_paraphrase(text):
    # Use TextBlob to generate paraphrased text
    paraphrased_text = str(TextBlob(text).words)

    # Custom synonyms
    custom_synonyms = [('happy', 'joyful'), ('sad', 'unhappy')]
    for syn in custom_synonyms:
        paraphrased_text = paraphrased_text.replace(syn[0], syn[1])

    return paraphrased_text

def detect_emotion(text):
    # Use SentiWordNet to detect emotion in text
    emotions = []
    words = nltk.word_tokenize(text)
    for word in words:
        synset = swn.senti_synsets(word)
        if len(synset) > 0:
            emotions.append(synset[0].pos_score() - synset[0].neg_score())
    if emotions:
        emotion = max(emotions)
    else:
        emotion = 0

    return 'positive' if emotion > 0 else 'negative' if emotion < 0 else 'neutral'

if __name__ == '__main__':
    app.run(host="0.0.0.0",port=7860,debug=True)