Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
General case synonym matching using nltk.
from nltk.corpus import wordnet
from nltk.stem.wordnet import WordNetLemmatizer
import itertools
def Synonym_Checker(word1, word2):
"""Checks if word1 and word2 and synonyms. Returns True if they are, otherwise False"""
equivalence = WordNetLemmatizer()
word1 = equivalence.lemmatize(word1)
word2 = equivalence.lemmatize(word2)
word1_synonyms = wordnet.synsets(word1)
word2_synonyms = wordnet.synsets(word2)
scores = [i.wup_similarity(j) for i, j in list(itertools.product(word1_synonyms, word2_synonyms))]
max_index = scores.index(max(scores))
best_match = (max_index/len(word1_synonyms), max_index % len(word1_synonyms)-1)
word1_set = word1_synonyms[best_match[0]].lemma_names
word2_set = word2_synonyms[best_match[1]].lemma_names
match = False
match = [match or word in word2_set for word in word1_set][0]
return match
print Synonym_Checker("tomato", "Lycopersicon_esculentum")
@siddhant3s

This comment has been minimized.

Show comment Hide comment
@siddhant3s

siddhant3s Aug 19, 2014

word1_set = word1_synonyms[best_match[0]].lemma_names should be word1_set = word1_synonyms[best_match[0]].lemma_names() Notice the parens at the end ()

word1_set = word1_synonyms[best_match[0]].lemma_names should be word1_set = word1_synonyms[best_match[0]].lemma_names() Notice the parens at the end ()

@hyp3rv1p3r

This comment has been minimized.

Show comment Hide comment
@hyp3rv1p3r

hyp3rv1p3r Apr 9, 2015

best_match should contain integers but often it contains floats so the code fails when you call word1_synonyms[best_match[0]] or word2_synonyms[best_match[0]].

best_match should contain integers but often it contains floats so the code fails when you call word1_synonyms[best_match[0]] or word2_synonyms[best_match[0]].

@ShanikaEdiriweera

This comment has been minimized.

Show comment Hide comment
@ShanikaEdiriweera

ShanikaEdiriweera Sep 1, 2016

When I run this code I get this error:

Traceback (most recent call last):
  File "/home/shanika//Lemmatizer.py", line 76, in <module>
    print Synonym_Checker("tomato", "Lycopersicon_esculentum")
  File "/home/shanika/Lemmatizer.py", line 72, in Synonym_Checker
    match = [match or word in word2_set for word in word1_set][0]
TypeError: 'instancemethod' object is not iterable

ShanikaEdiriweera commented Sep 1, 2016

When I run this code I get this error:

Traceback (most recent call last):
  File "/home/shanika//Lemmatizer.py", line 76, in <module>
    print Synonym_Checker("tomato", "Lycopersicon_esculentum")
  File "/home/shanika/Lemmatizer.py", line 72, in Synonym_Checker
    match = [match or word in word2_set for word in word1_set][0]
TypeError: 'instancemethod' object is not iterable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment