sashtech commited on
Commit
7e6f7b9
·
verified ·
1 Parent(s): 6e564ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -148,19 +148,18 @@ def correct_spelling(text):
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:
 
148
  def replace_with_synonyms(text):
149
  doc = nlp(text)
150
  replaced_words = []
151
+
152
+ for index, token in enumerate(doc):
153
+ # Replace every 2nd and 4th word
154
+ if (index + 1) % 2 == 0 or (index + 1) % 4 == 0:
155
+ # Check if the token has synonyms in the dictionary
156
+ if token.text in synonyms_dict:
157
+ relevant_synonyms = synonyms_dict[token.text]
158
  if relevant_synonyms:
159
+ # Randomly choose a synonym from the available options
160
+ synonym = random.choice(relevant_synonyms)
161
+ # Add the synonym instead of the original word
162
+ replaced_words.append(synonym)
 
 
163
  else:
164
  replaced_words.append(token.text) # No relevant synonym found
165
  else: