Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -146,28 +146,35 @@ def correct_spelling(text):
|
|
146 |
|
147 |
# Function to replace a word with its synonym
|
148 |
def replace_with_synonyms(text):
|
149 |
-
|
150 |
replaced_words = []
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
else:
|
161 |
-
replaced_words.append(
|
162 |
else:
|
163 |
-
replaced_words.append(
|
164 |
|
165 |
return ' '.join(replaced_words)
|
166 |
|
167 |
# Main function for paraphrasing and grammar correction
|
168 |
def paraphrase_and_correct(text):
|
169 |
-
|
170 |
-
cleaned_text = remove_redundant_words(
|
|
|
171 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
172 |
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
173 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
|
|
146 |
|
147 |
# Function to replace a word with its synonym
|
148 |
def replace_with_synonyms(text):
|
149 |
+
doc = nlp(text)
|
150 |
replaced_words = []
|
151 |
+
for token in doc:
|
152 |
+
if token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"}: # Limit to specific POS
|
153 |
+
synonyms = wordnet.synsets(token.text)
|
154 |
+
if synonyms:
|
155 |
+
# Select synonyms with the same part of speech
|
156 |
+
relevant_synonyms = [syn for syn in synonyms if syn.pos() == token.pos_.lower()]
|
157 |
+
if relevant_synonyms:
|
158 |
+
# Randomly choose a synonym from the relevant options
|
159 |
+
synonym = random.choice(relevant_synonyms).lemmas()[0].name().replace('_', ' ')
|
160 |
+
if synonym.lower() != token.text.lower(): # Avoid replacing with the same word
|
161 |
+
replaced_words.append(synonym)
|
162 |
+
else:
|
163 |
+
replaced_words.append(token.text)
|
164 |
+
else:
|
165 |
+
replaced_words.append(token.text) # No relevant synonym found
|
166 |
else:
|
167 |
+
replaced_words.append(token.text) # No synonyms available
|
168 |
else:
|
169 |
+
replaced_words.append(token.text) # Non-replaceable tokens
|
170 |
|
171 |
return ' '.join(replaced_words)
|
172 |
|
173 |
# Main function for paraphrasing and grammar correction
|
174 |
def paraphrase_and_correct(text):
|
175 |
+
# Add synonym replacement here
|
176 |
+
cleaned_text = remove_redundant_words(text)
|
177 |
+
paraphrased_text = replace_with_synonyms(cleaned_text)
|
178 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
179 |
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
180 |
paraphrased_text = correct_article_errors(paraphrased_text)
|