I can't come up with a stranger problem, guess you'll help me.
for p in wn.synsets('change'):<br>
print(p)
Getting:
Synset('change.n.01')
Synset('change.n.02')
Synset('change.n.03')
Synset('change.n.04')
Synset('change.n.05')
Synset('change.n.06')
Synset('change.n.07')
Synset('change.n.08')
Synset('change.n.09')
Synset('variety.n.06')
Synset('change.v.01')
Synset('change.v.02')
Synset('change.v.03')
Synset('switch.v.03')
Synset('change.v.05')
Synset('change.v.06')
Synset('exchange.v.01')
Synset('transfer.v.06')
Synset('deepen.v.04')
Synset('change.v.10')
For example I have an a string:
a = 'transfer'
I'd like to be able to identify all kinds of synonyms of word 'change' and know f.e. 'transfer' is the one of them. How can I ask my program: "Is 'transfer' is one of the synonyms of 'change'?"
Firstly, wordnet indexes concepts (aka Synsets) and link possible words for each concept, the following code shows the concepts link to the word 'change':
>>> from nltk.corpus import wordnet as wn
>>> wn.synsets('change')
[Synset('change.n.01'), Synset('change.n.02'), Synset('change.n.03'), Synset('change.n.04'), Synset('change.n.05'), Synset('change.n.06'), Synset('change.n.07'), Synset('change.n.08'), Synset('change.n.09'), Synset('variety.n.06'), Synset('change.v.01'), Synset('change.v.02'), Synset('change.v.03'), Synset('switch.v.03'), Synset('change.v.05'), Synset('change.v.06'), Synset('exchange.v.01'), Synset('transfer.v.06'), Synset('deepen.v.04'), Synset('change.v.10')]
A synset has several properties, it has:
Here's how to interface the above properties in NLTK:
>>> wn.synsets('change')[0]
Synset('change.n.01')
>>> wn.synsets('change')[0].offset()
7296428
>>> wn.synsets('change')[0].pos()
u'n'
>>> wn.synsets('change')[0].definition()
u'an event that occurs when something passes from one state or phase to another'
>>> wn.synsets('change')[0].lemma_names()
[u'change', u'alteration', u'modification']
>>> wn.synsets('change')[0].hypernyms()
[Synset('happening.n.01')]
But a synset doesn't necessary have synonym relations. If we define synonyms as words that have similar meaning, it is the words (i.e. lemmas) that have synonymy relations. In addition, the context of the words defines whether a word is a synonym of another. A single word has limited meaning, it's the "concept" that contains meaning and instantiate the meaning through human words. At least that's the typical theory of semantics
So when you want to ask is 'transfer' a synonym of 'change', you have to first:
Then comparison of meaning is possible.
See also:
You need to first get the lemmas then iterate over your lemmas and get the names then check the membership with in operand:
>>> a in [j.name() for i in wn.synsets('change') for j in i.lemmas()]
True
>>> [j.name() for i in wn.synsets('change') for j in i.lemmas()]
[u'change', u'alteration', u'modification', u'change', u'change', u'change', u'change', u'change', u'change', u'change', u'change', u'variety', u'change', u'change', u'alter', u'modify', u'change', u'change', u'alter', u'vary', u'switch', u'shift', u'change', u'change', u'change', u'exchange', u'commute', u'convert', u'exchange', u'change', u'interchange', u'transfer', u'change', u'deepen', u'change', u'change']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With