imseldrith commited on
Commit
52b0385
·
1 Parent(s): d27e2cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -38
app.py CHANGED
@@ -1,54 +1,125 @@
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request
2
+ from nltk.tokenize import word_tokenize
3
+ from nltk.corpus import wordnet
4
+ from nltk import pos_tag, ne_chunk
5
+ import textblob
6
+ from polyglot.detect import Detector
7
  import numpy as np
8
+ from keras.models import load_model
 
9
 
 
10
  app = Flask(__name__)
11
+ model = load_model("emotion_detector.h5")
12
 
13
+ @app.route("/")
 
14
  def index():
15
+ return render_template("index.html")
16
 
17
+ @app.route("/paraphrase", methods=["POST"])
 
18
  def paraphrase():
19
+ input_text = request.form["input_text"]
20
+ options = request.form.getlist("options")
21
 
22
+ # Remove special characters
23
+ if "remove_special_characters" in options:
24
+ input_text = remove_special_characters(input_text)
25
+
26
+ # Correct grammar
27
+ if "correct_grammar" in options:
28
+ input_text = correct_grammar(input_text)
29
 
30
+ # Summarize text
31
+ if "summarize_text" in options:
32
+ input_text = summarize_text(input_text)
33
 
34
+ # Multilingual support
35
+ target_language = request.form.get("target_language")
36
+ if target_language:
37
+ input_text = translate(input_text, target_language)
38
 
39
+ # Custom synonyms
40
+ custom_synonyms = request.form.getlist("custom_synonyms")
41
+ for word, synonym in custom_synonyms:
42
+ input_text = replace_word(input_text, word, synonym)
43
 
44
+ # Output customization
45
+ input_text = customise_output(input_text, options)
46
 
47
+ # Integration with other NLP tools
48
+ named_entities = get_named_entities(input_text)
49
+ part_of_speech = get_part_of_speech(input_text)
50
+ sentiment = get_sentiment(input_text)
51
 
52
+ # Emotion detector
53
+ emotion = detect_emotion(input_text)
54
+ input_text = adjust_tone(input_text, emotion)
 
 
 
 
 
55
 
56
+ return render_template("index.html", paraphrased_text=input_text, named_entities=named_entities, part_of_speech=part_of_speech, sentiment=sentiment)
 
57
 
58
+ def remove_special_characters(input_text):
59
+ # Code to remove special characters
60
+ return input_text
61
+
62
+ def summarize_text(input_text):
63
+ # Code to summarize the text
64
+ return input_text
65
+
66
+ def detect_language(input_text):
67
+ detector = Detector(input_text)
68
+ language = detector.language.code
69
+ return language
70
+
71
+ def translate(input_text, target_language):
72
+ blob = textblob.TextBlob(input_text)
73
+ translated_text = blob.translate(to=target_language)
74
+ return translated_text
75
+
76
+ def get_synonyms(word):
77
+ synonyms = []
78
+ for syn in wordnet.synsets(word):
79
+ for lemma in syn.lemmas():
80
+ synonyms.append(lemma.name())
81
+ return synonyms
82
+
83
+ def replace_word(input_text, word, synonym):
84
+ words = word_tokenize(input_text)
85
+ words = [synonym if w == word else w for w in words]
86
+ input_text = " ".join(words)
87
+ return input_text
88
+
89
+ def customise_output(input_text, options):
90
+ # Code to customise output based on options
91
+ return input_text
92
+
93
+ def get_named_entities(input_text):
94
+ named_entities = ne_chunk(pos_tag(word_tokenize(input_text)))
95
+ return named_entities
96
+
97
+ def get_part_of_speech(input_text):
98
+ pos = pos_tag(word_tokenize(input_text))
99
+ return pos
100
+
101
+ def get_sentiment(input_text):
102
+ blob = textblob.TextBlob(input_text)
103
+ sentiment = blob.sentiment.polarity
104
+ return sentiment
105
+
106
+ def correct_grammar(input_text):
107
+ blob = textblob.TextBlob(input_text)
108
+ corrected_text = str(blob.correct())
109
+ return corrected_text
110
+
111
+ def detect_emotion(input_text):
112
+ words = word_tokenize(input_text)
113
+ words = [w.lower() for w in words]
114
+ words = [w for w in words if w.isalpha()]
115
+ input_text = " ".join(words)
116
+ input_text = np.array([input_text])
117
+ sentiment = model.predict(input_text, batch_size=1, verbose=0)[0]
118
+ return sentiment
119
+
120
+ def adjust_tone(input_text, emotion):
121
+ # Code to adjust tone based on emotion
122
+ return input_text
123
+
124
+ if __name__ == "__main__":
125
+ app.run(debug=True,port=7860,host="0.0.0.0")