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

后端 未结 1 1050
眼角桃花
眼角桃花 2021-01-28 17:14

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\"

相关标签:
1条回答
  • 2021-01-28 17:54

    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')]
    
    0 讨论(0)
提交回复
热议问题