Pages

Mar 13, 2015

[Python] Find synonyms by using WordNet, NLTK

WordNet is a great reference when you want to find synonyms of a word. For more information, please see the website: http://wordnet.princeton.edu/

The following Python code is a simple example.
#Python 3
#Find synonyms of a word
>>> from nltk.corpus import wordnet as wn
>>> syn = wn.synsets("car")
>>> syn
[Synset('car.n.01'), Synset('car.n.02'), Synset('car.n.03'), Synset('car.n.04'), Synset('cable_car.n.01')]
>>> syn[0].lemma_names()
['car', 'auto', 'automobile', 'machine', 'motorcar']
#Note that synonyms are always in singular form.
#If you want to compare words, need to deal with the words that are in plural form.
view raw WordNet.py hosted with ❤ by GitHub

WordNet Interface: http://www.nltk.org/howto/wordnet.html

No comments:

Post a Comment