synonym.py 877 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import nltk
  2. from nltk.corpus import wordnet
  3. import os
  4. from nltk.corpus.reader import WordNetError
  5. print("Loading nltk", os.path.dirname(__file__) + '/../../nltk_data/')
  6. nltk.data.path.append(os.path.dirname(__file__) + '/../../nltk_data/')
  7. def get_synonyms(word):
  8. synonyms = []
  9. try:
  10. synsets_words = wordnet.synsets(word)
  11. except WordNetError:
  12. return []
  13. for syn in synsets_words:
  14. for lemma in syn.lemmas():
  15. synonyms.append(lemma.name())
  16. return synonyms
  17. def get_antonyms(word):
  18. antonyms = []
  19. try:
  20. synsets_words = wordnet.synsets(word)
  21. except WordNetError:
  22. return []
  23. for syn in synsets_words:
  24. for lemma in syn.lemmas():
  25. if lemma.antonyms():
  26. for antonym in lemma.antonyms():
  27. antonyms.append(antonym.name())
  28. return antonyms