(根据语料库打印出给定单词的相似单词。)
不过,我想将单词列表存储在一个列表中。但是函数本身返回None
。你知道吗
#text is a variable of nltk.Text module
simList = text.similar("physics")
>>> a = text.similar("physics")
the and a in science this which it that energy his of but chemistry is
space mathematics theory as mechanics
>>> a
>>> a
# a contains no value.
那么我应该修改源函数本身吗?但我不认为这是一个好的做法。那么如何重写该函数,使其返回值呢?你知道吗
编辑引用this thread,我尝试使用ContextIndex
类。但我得到以下错误。你知道吗
File "test.py", line 39, in <module>
text = nltk.text.ContextIndex(word.lower() for word in words) File "/home/kenden/den/codes/nlpenv/local/lib/python2.7/site-packages/nltk/text.py", line 56, in __init__
for i, w in enumerate(tokens)) File "/home/kenden/den/codes/nlpenv/local/lib/python2.7/site-packages/nltk/probability.py", line 1752, in __init__
for (cond, sample) in cond_samples: File "/home/kenden/den/codes/nlpenv/local/lib/python2.7/site-packages/nltk/text.py", line 56, in <genexpr>
for i, w in enumerate(tokens)) File "/home/kenden/den/codes/nlpenv/local/lib/python2.7/site-packages/nltk/text.py", line 43, in _default_context
right = (tokens[i+1].lower() if i != len(tokens) - 1 else '*END*') TypeError: object of type 'generator' has no len()
这是我的第39行测试.py你知道吗
text = nltk.text.ContextIndex(word.lower() for word in words)
我怎样才能解决这个问题?你知道吗
出现此错误是因为
ContextIndex
构造函数试图获取标记列表的len()
(参数tokens
)。但实际上是作为生成器传递的,因此会出现错误。为了避免出现问题,只需传递一个真实的列表,例如:相关问题 更多 >
编程相关推荐