我正在阅读本书的第1章,并置函数返回错误。 似乎text.py中的第440行是多余的,因为已经引入了collocation_list函数。 我通过重写text.py中的当前行440和441解决了该问题。
旧代码:
collocation_strings = [w1 +''+ w2 for w1,self中的w2.collocation_list(num,window_size)] *
打印(tokenwrap(collocation_strings,分隔符=“;”))
新代码:
打印(tokenwrap(self.collocation_list(),分隔符=“;”))
感谢@martinevanschouwenburg提出的错误!
是的,看起来需要搭配列表。 复制错误:
$ python3
Python 3.6.4rc1 (v3.6.4rc1:3398dcb14f, Dec 5 2017, 00:58:30)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from nltk.book import *
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908
>>> text4.collocations()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/nltk/text.py", line 440, in collocations
collocation_strings = [w1 + ' ' + w2 for w1, w2 in self.collocation_list(num, window_size)]
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/nltk/text.py", line 440, in <listcomp>
collocation_strings = [w1 + ' ' + w2 for w1, w2 in self.collocation_list(num, window_size)]
ValueError: too many values to unpack (expected 2)
在阅读本书的第1章时,我仍然会看到此错误。
* NLTK书的入门示例*
加载text1,...,text9和send1,...,sent9
输入文本或句子的名称以进行查看。
输入:“ texts()”或“ sents()”以列出材料。
text1:赫尔曼·梅尔维尔(Mount Dick),赫尔曼·梅尔维尔(Herman Melville)1851年
text2:简·奥斯丁的感性和敏感性1811
text3:创世记
text4:就职演说语料库
text5:聊天语料库
text6:Monty Python和圣杯
text7:《华尔街日报》
text8:交友语料库
text9:G的《星期四的人》。 。 切斯特顿1908
追溯(最近一次通话):
在第43行中输入文件“ c:\ Users \ Adam.vscode \ extensions \ ms-python.python-2019.6.24221 \ pythonFiles \ ptvsd_launcher.py”
主要(ptvsdArgs)
主文件434行中的文件“ c:\ Users \ Adam.vscode \ extensions \ ms-python.python-2019.6.24221 \ pythonFiles \ lib \ python \ ptvsd__main __。py”
跑()
在run_file中的第312行,文件“ c:\ Users \ Adam.vscode \ extensions \ ms-python.python-2019.6.24221 \ pythonFiles \ lib \ python \ ptvsd__main __。py”
runpy.run_path(target,run_name ='__ main__')
文件“ c:\ users \ adam \ appdata \ local \ programs \ python \ python37-32 \ Lib \ runpy.py”,行263,在run_path中
pkg_name = pkg_name,script_name = fname)
文件“ c:\ users \ adam \ appdata \ local \ programs \ python \ python37-32 \ Lib \ runpy.py”,第96行,在_run_module_code中
mod_name,mod_spec,pkg_name,script_name)
文件“ c:\ users \ adam \ appdata \ local \ programs \ python \ python37-32 \ Lib \ runpy.py”,第85行,使用_run_code
exec(代码,run_globals)
文件“ c:\ Users \ Adam \ Documents \ code \ python \ natlang \ natlang.py”,第4行,在
text4.collocations()
文件“ C:\ Users \ Adam.virtualenvs \ natlang-9ek-vNym \ lib \ site-packages \ nltk \ text.py”,行444,并置
w1 +“” + w2代表self.collocation_list(num,window_size)中的w1,w2
文件“ C:\ Users \ Adam.virtualenvs \ natlang-9ek-vNym \ lib \ site-packages \ nltk \ text.py”,行444,在
w1 +“” + w2表示self.collocation_list(num,window_size)中的w1,w2
ValueError:太多值无法解包(预期2)
@networkjr我也可以确认。 也许#2227中的修复程序尚未推送到PyPi?
@networkjr与Anaconda包相同
我正在研究NLTK,这对NLTK来说是全新的,而对于Python来说则是相当新的-我也遇到了同样的错误。
$ python
Python 3.7.2 (default, Feb 14 2019, 11:13:53)
[Clang 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> from nltk.book import *
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: 'texts()' or 'sents()' to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908
>>> text4.collocations()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/george/code/nltk/py3env/lib/python3.7/site-packages/nltk/text.py", line 444, in collocations
w1 + " " + w2 for w1, w2 in self.collocation_list(num, window_size)
File "/Users/george/code/nltk/py3env/lib/python3.7/site-packages/nltk/text.py", line 444, in <listcomp>
w1 + " " + w2 for w1, w2 in self.collocation_list(num, window_size)
ValueError: too many values to unpack (expected 2)
根据我的Pipfile.lock
我使用的是NLTK 3.4.5,我认为这是最新版本。
有解决此问题的方法吗?
此问题已在#2377上修复,应尽快在下一个NLTK版本中修复。
否则,如果您不能等待=)
pip install -U https://github.com/nltk/nltk/archive/develop.zip
更新cntk后,我仍然有相同的错误pip install -U https://github.com/nltk/nltk/archive/develop.zip
当前cnkt版本'3.4.5'
我该如何解决?
非常感谢。
.collocations()
仍然有问题,但.collocation_list()
可以工作。
替换为/nltk/text.py中的第444行:
collocation_strings = [w1 +“” + w2表示文本中的w1,w2。collocation_list(num,window_size)]
具有以下内容:
collocation_strings = [在text.collocation_list(num,window_size)中w为w]
同样在这里。 遍历nltk书会导致collocations()错误,而collocation_list()有效。
最有用的评论
.collocations()
仍然有问题,但.collocation_list()
可以工作。