2014-06-13 120 views

回答

1

我不認爲有一個明確的方式來得到你需要的東西,除非你可以更具體地定義它。例如:

>>> from nltk.corpus import wordnet as wn 
>>> blue = wn.synsets('blue')[0] 
>>> cat = wn.synsets('cat')[0] 
>>> blue.definition() 
u'blue color or pigment; resembling the color of the clear sky in the daytime' 
>>> cat.definition() 
u'feline mammal usually having thick soft fur and no ability to roar: domestic cats; wildcats' 

有時候,你很幸運,去了上位的一個或兩個級別:

>>> blue.hypernyms() 
[Synset('chromatic_color.n.01')] 
>>> blue.hypernyms()[0].hypernyms() 
[Synset('color.n.01')] 

有時你不得不去多級了上位得到你想要的東西。

>>> cat.hypernyms() 
[Synset('feline.n.01')] 
>>> cat.hypernyms()[0].hypernyms() 
[Synset('carnivore.n.01')] 
>>> cat.hypernyms()[0].hypernyms()[0].hypernyms() 
[Synset('placental.n.01')] 
>>> cat.hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms() 
[Synset('mammal.n.01')] 
>>> cat.hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms() 
[Synset('vertebrate.n.01')] 
>>> cat.hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms() 
[Synset('chordate.n.01')] 
>>> cat.hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms()[0].hypernyms() 
[Synset('animal.n.01')] 

和獲取到最高上位詞水平使得沒有太大意義:

>>> blue.root_hypernyms() 
[Synset('entity.n.01')] 
>>> cat.root_hypernyms() 
[Synset('entity.n.01')] 

有時候就是沒有上位詞,你去到:

>>> happy = wn.synsets('happy')[0] 
>>> happy.definition() 
u'enjoying or showing or marked by joy or pleasure' 
>>> happy.hypernyms() 
[] 
>>> happy.root_hypernyms() 
[Synset('happy.a.01')]