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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -145,6 +145,14 @@ def correct_spelling(text):
145
  return ' '.join(corrected_words)
146
 
147
  # Function to replace a word with its synonym
 
 
 
 
 
 
 
 
148
  def replace_with_synonyms(text):
149
  doc = nlp(text)
150
  replaced_words = []
@@ -152,23 +160,21 @@ def replace_with_synonyms(text):
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:
166
- replaced_words.append(token.text) # No synonyms available
167
  else:
168
  replaced_words.append(token.text) # Non-replaceable tokens
169
 
170
  return ' '.join(replaced_words)
171
 
 
172
  # Main function for paraphrasing and grammar correction
173
  def paraphrase_and_correct(text):
174
  # Add synonym replacement here
 
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 = []
 
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