Spaces:
Running
Running
| 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__) | |
| def index(): | |
| return render_template('index.html') | |
| 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) |