How to get all the meanings of a word using python NLTK?

跟風遠走 提交于 2019-12-02 18:28:13

问题


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.


回答1:


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')]


来源:https://stackoverflow.com/questions/21318791/how-to-get-all-the-meanings-of-a-word-using-python-nltk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!