Question

I want to use all meanings of a particular word in an input query.

For example:

Suppose my input query is: "Dog is barking at tree"

Here I want to get all meanings of the word TREE and BARK in the following format:

tree#n#01,tree#n#02... and so on. bark#n#01,bark#n#02... and so on

I am using POS tagging to extract noun,verb,adjective and adverb synset accordingly. If bark is used as verb (as used in our input query) then only the related meanings should be displayed as bark#v#01,bark#v#02...

Please help me to solve this using Python. I am using Python NLTK module for natural language processing.

Was it helpful?

Solution

To know which word have the same/similar pos tag, you can use the idiomatic

>>> from nltk.tag import pos_tag
>>> sent = "dog is barking at tree"
>>> [i for i in pos_tag(sent.split()) if i[1] == "NN"]
[('dog', 'NN'), ('tree', 'NN')]

Then to get the possible synsets for a word, simply do:

>>> from nltk.corpus import wordnet as wn
>>> wn.synsets('dog')
[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]

Most probably the solution you're looking for is:

>>> from nltk.corpus import wordnet as wn
>>> from nltk.tag import pos_tag
>>> sent = "dog is barking at tree"
>>> for i in [i[0] for i in pos_tag(sent.split()) if i[1].lower()[0] == 'n']:
...     print wn.synsets(i); print
... 
[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]

[Synset('tree.n.01'), Synset('tree.n.02'), Synset('tree.n.03'), Synset('corner.v.02'), Synset('tree.v.02'), Synset('tree.v.03'), Synset('tree.v.04')]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top