Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vikeshsingh37/6487665e9e12ff008bd155df79f3e4b1 to your computer and use it in GitHub Desktop.
Save vikeshsingh37/6487665e9e12ff008bd155df79f3e4b1 to your computer and use it in GitHub Desktop.
import random
import nltk
from nltk.corpus import wordnet as wn
from snorkel.augmentation import transformation_function
nltk.download("wordnet", quiet=True)
def get_synonyms(word):
"""Get the synonyms of word from Wordnet."""
lemmas = set().union(*[s.lemmas() for s in wn.synsets(word)])
return list(set(l.name().lower().replace("_", " ") for l in lemmas) - {word})
@transformation_function()
def tf_replace_word_with_synonym(x):
"""Try to replace a random word with a synonym."""
words = x.text.lower().split()
idx = random.choice(range(len(words)))
synonyms = get_synonyms(words[idx])
if len(synonyms) > 0:
x.text = " ".join(words[:idx] + [synonyms[0]] + words[idx + 1 :])
return x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment