Created
March 10, 2018 10:12
-
-
Save balachandrapai/fc07382ef5dd360727b14be49ad93fec to your computer and use it in GitHub Desktop.
Using synsets, finding synonyms and antonyms, word similarity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from nltk.corpus import wordnet | |
syns = wordnet.synsets("program") | |
print(syns[0].name()) | |
#plan.n.01 | |
print(syns[0].lemmas()[0].name()) | |
#plan | |
print(syns[0].definition()) | |
#a series of steps to be carried out or goals to be accomplished | |
print(syns[0].examples()) | |
#['they drew up a six-step plan', 'they discussed plans for a new bond issue'] | |
##The lemmas will be synonyms, | |
##and then you can use .antonyms to find the antonyms to the lemmas | |
synonyms = [] | |
antonyms = [] | |
for syn in wordnet.synsets("good"): | |
for l in syn.lemmas(): | |
synonyms.append(l.name()) | |
if l.antonyms(): | |
antonyms.append(l.antonyms()[0].name()) | |
print(set(synonyms)) | |
print(set(antonyms)) | |
#compare the similarity of two words and their tenses | |
#we use Wu and Palmer method for semantic related-ness | |
w1 = wordnet.synset('ship.n.01') | |
w2 = wordnet.synset('boat.n.01') | |
print(w1.wup_similarity(w2)) | |
w1 = wordnet.synset('ship.n.01') | |
w2 = wordnet.synset('car.n.01') | |
print(w1.wup_similarity(w2)) | |
w1 = wordnet.synset('ship.n.01') | |
w2 = wordnet.synset('cat.n.01') | |
print(w1.wup_similarity(w2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment