问题描述
我想在输入查询中使用特定单词的所有含义.
I want to use all meanings of a particular word in an input query.
例如:
假设我的输入查询是: 狗在吠叫"
Suppose my input query is:"Dog is barking at tree"
在这里,我想以以下格式获取 TREE 和 BARK 一词的所有含义:
Here I want to get all meanings of the word TREE and BARK in the following format:
tree#n#01,tree#n#02 ...,依此类推.树皮#n#01,树皮#n#02 ...等等
tree#n#01,tree#n#02... and so on. bark#n#01,bark#n#02... and so on
我正在使用POS标记相应地提取名词,动词,形容词和副词同义词集.如果将bark用作动词(如我们的输入查询中所用),则只有相关的含义应显示为bark#v#01,bark#v#02 ...
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...
请帮助我使用Python解决此问题.我正在使用Python NLTK模块进行自然语言处理.
Please help me to solve this using Python. I am using Python NLTK module for natural language processing.
推荐答案
要知道哪个单词具有相同/相似的pos标签,可以使用惯用语
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')]
这篇关于如何使用python NLTK获取单词的所有含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!