nltk.Text:没有得到理想的结果

时间:2015-12-11 18:36:44

标签: python nlp nltk

我正在尝试从某些文字中提取关键字

t="""Two Travellers, walking in the noonday sun, sought the shade of a widespreading tree to rest. As they lay looking up among the pleasant leaves, they saw that it was a Plane Tree.

"How useless is the Plane!" said one of them. "It bears no fruit whatever, and only serves to litter the ground with leaves."

"Ungrateful creatures!" said a voice from the Plane Tree. "You lie here in my cooling shade, and yet you say I am useless! Thus ungratefully, O Jupiter, do men receive their blessings!"

Our best blessings are often the least appreciated."""

    text = nltk.Text(word.lower() for word in t.split(" "))
    print text.similar('tree')

但是我得到了

None

为什么会这样?

修改

根据Andy的建议我试过

import nltk
import nltk.text
import nltk.corpus
t="""Two Travellers, walking in the noonday sun, sought the shade of a widespreading tree to rest As they lay looking up among the pleasant leaves, they saw that it was a Plane Tree 
How useless is the Plane!" said one of them. "It bears no fruit whatever, and only serves to litter the ground with leaves."

"Ungrateful creatures!" said a voice from the Plane Tree. "You lie here in my cooling shade, and yet you say I am useless! Thus ungratefully, O Jupiter, do men receive their blessings!"

Our best blessings are often the least appreciated"""
idx = nltk.text.ContextIndex([word.lower( ) for word in t.split(" ")])
print idx.tokens()
idx.similar_words('tree')
for word in nltk.word_tokenize("tree"):
    print idx.similar_words(word)

但是这给了我

['two', 'travellers,', 'walking', 'in', 'the', 'noonday', 'sun,', 'sought', 'the', 'shade', 'of', 'a', 'widespreading', 'tree', 'to', 'rest', 'as', 'they', 'lay', 'looking', 'up', 'among', 'the', 'pleasant', 'leaves,', 'they', 'saw', 'that', 'it', 'was', 'a', 'plane', 'tree', '\nhow', 'useless', 'is', 'the', 'plane!"', 'said', 'one', 'of', 'them.', '"it', 'bears', 'no', 'fruit', 'whatever,', 'and', 'only', 'serves', 'to', 'litter', 'the', 'ground', 'with', 'leaves."\n\n"ungrateful', 'creatures!"', 'said', 'a', 'voice', 'from', 'the', 'plane', 'tree.', '"you', 'lie', 'here', 'in', 'my', 'cooling', 'shade,', 'and', 'yet', 'you', 'say', 'i', 'am', 'useless!', 'thus', 'ungratefully,', 'o', 'jupiter,', 'do', 'men', 'receive', 'their', 'blessings!"\n\nour', 'best', 'blessings', 'are', 'often', 'the', 'least', 'appreciated']
[]

所以单词tree在标记列表中。为什么我没有获得类似功能的任何输出?

0 个答案:

没有答案