我是NLP的新手,正在阅读一篇关于搭配的文章。下面是使用nltk从文本生成搭配的代码片段。在实现代码时,我得到了下面分享的错误。我不能明白我做错了什么。我还参考了官方文档nltk/collocation,方法是不同的,我在遵循它时遇到了困难。
from nltk.corpus import genesis
tokens = genesis.words('english-kjv.txt')
gen_text = nltk.Text(tokens)
gen_text.collocations()---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-115-a1efe81f5efd> in <module>
3 tokens = genesis.words('english-kjv.txt')
4 gen_text = nltk.Text(tokens)
----> 5 gen_text.collocations()
~\anaconda3\lib\site-packages\nltk\text.py in collocations(self, num, window_size)
442
443 collocation_strings = [
--> 444 w1 + " " + w2 for w1, w2 in self.collocation_list(num, window_size)
445 ]
446 print(tokenwrap(collocation_strings, separator="; "))
~\anaconda3\lib\site-packages\nltk\text.py in <listcomp>(.0)
442
443 collocation_strings = [
--> 444 w1 + " " + w2 for w1, w2 in self.collocation_list(num, window_size)
445 ]
446 print(tokenwrap(collocation_strings, separator="; "))
ValueError: too many values to unpack (expected 2)发布于 2020-12-19 23:58:51
请参阅this问题。使用gen_text.collocation_list()可能会解决这个问题。
https://stackoverflow.com/questions/65371626
复制相似问题