Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -144,42 +144,13 @@ def correct_spelling(text):
|
|
144 |
corrected_words.append(word)
|
145 |
return ' '.join(corrected_words)
|
146 |
|
147 |
-
# Function to replace a word with its synonym
|
148 |
-
def get_wordnet_synonyms(word):
|
149 |
-
"""Get synonyms from WordNet for a given word."""
|
150 |
-
synonyms = set()
|
151 |
-
for syn in wn.synsets(word):
|
152 |
-
for lemma in syn.lemmas():
|
153 |
-
synonyms.add(lemma.name()) # Add synonym to set
|
154 |
-
return list(synonyms)
|
155 |
-
|
156 |
-
def replace_with_synonyms(text):
|
157 |
-
doc = nlp(text)
|
158 |
-
replaced_words = []
|
159 |
-
|
160 |
-
for index, token in enumerate(doc):
|
161 |
-
# Replace every 2nd and 4th word
|
162 |
-
if (index + 1) % 2 == 0 or (index + 1) % 4 == 0:
|
163 |
-
# Get synonyms from WordNet
|
164 |
-
relevant_synonyms = get_wordnet_synonyms(token.text)
|
165 |
-
if relevant_synonyms:
|
166 |
-
# Randomly choose a synonym from the available options
|
167 |
-
synonym = random.choice(relevant_synonyms)
|
168 |
-
# Add the synonym instead of the original word
|
169 |
-
replaced_words.append(synonym.replace('_', ' ')) # Replace underscores with spaces
|
170 |
-
else:
|
171 |
-
replaced_words.append(token.text) # No relevant synonym found
|
172 |
-
else:
|
173 |
-
replaced_words.append(token.text) # Non-replaceable tokens
|
174 |
|
175 |
-
return ' '.join(replaced_words)
|
176 |
|
177 |
|
178 |
# Main function for paraphrasing and grammar correction
|
179 |
def paraphrase_and_correct(text):
|
180 |
# Add synonym replacement here
|
181 |
cleaned_text = remove_redundant_words(text)
|
182 |
-
paraphrased_text = replace_with_synonyms(cleaned_text)
|
183 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
184 |
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
185 |
paraphrased_text = correct_article_errors(paraphrased_text)
|
|
|
144 |
corrected_words.append(word)
|
145 |
return ' '.join(corrected_words)
|
146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
|
|
148 |
|
149 |
|
150 |
# Main function for paraphrasing and grammar correction
|
151 |
def paraphrase_and_correct(text):
|
152 |
# Add synonym replacement here
|
153 |
cleaned_text = remove_redundant_words(text)
|
|
|
154 |
paraphrased_text = capitalize_sentences_and_nouns(cleaned_text)
|
155 |
paraphrased_text = force_first_letter_capital(paraphrased_text)
|
156 |
paraphrased_text = correct_article_errors(paraphrased_text)
|