sashtech commited on
Commit
4527fa4
·
verified ·
1 Parent(s): d655287

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
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
- words = text.split()
150
  replaced_words = []
151
-
152
- for word in words:
153
- synonyms = wordnet.synsets(word)
154
- if synonyms:
155
- # Take the first synonym if available
156
- synonym = synonyms[0].lemmas()[0].name()
157
- # Replace the word with its synonym if it's different
158
- if synonym.lower() != word.lower():
159
- replaced_words.append(synonym.replace('_', ' '))
 
 
 
 
 
 
160
  else:
161
- replaced_words.append(word)
162
  else:
163
- replaced_words.append(word)
164
 
165
  return ' '.join(replaced_words)
166
 
167
  # Main function for paraphrasing and grammar correction
168
  def paraphrase_and_correct(text):
169
- paraphrased_text = replace_with_synonyms(text) # Add synonym replacement here
170
- cleaned_text = remove_redundant_words(paraphrased_text)
 
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)