Nltk: コロケーション関数はエラーを返します

作成日 2019年05月15日  ·  10コメント  ·  ソース: nltk/nltk

本の第1章を読んでいたところ、コロケーション関数がエラーを返しました。 collocation_list関数が導入されたため、text.pyの440行目は冗長であるようです。 text.pyの現在の440行目と441行目を書き直すことで問題を修正しました。

古いコード:
collocation_strings = [w1 + '' + w2 for w1、w2 in self.collocation_list(num、window_size)] *
print(tokenwrap(collocation_strings、separator = ";"))

新しいコード:
print(tokenwrap(self.collocation_list()、separator = ";"))

bug goodfirstbug resolved text

最も参考になるコメント

また、 .collocations()にも問題がありますが、 .collocation_list()機能します。

全てのコメント10件

バグを提起してくれた@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およびsent1、...、sent9を読み込んでいます
テキストまたは文の名前を入力して表示します。
「texts()」または「sents()」と入力して、資料を一覧表示します。
text1:ハーマン・メルヴィル1851年の白鯨
text2:Jane Austen1811による分別と多感
text3:創世記
text4:就任アドレスコーパス
text5:チャットコーパス
text6:モンティパイソンと聖杯
text7:ウォールストリートジャーナル
text8:パーソナルコーパス
text9:Gによって木曜日だった男。 K。 チェスタートン1908
トレースバック(最後の最後の呼び出し):
ファイル "c:\ Users \ Adam.vscode \ extensions \ ms-python.python-2019.6.24221 \ pythonFiles \ ptvsd_launcher.py"、43行目
main(ptvsdArgs)
ファイル "c:\ Users \ Adam.vscode \ extensions \ ms-python.python-2019.6.24221 \ pythonFiles \ lib \ python \ ptvsd__main__。py"、行434、メイン
run()
run_fileのファイル "c:\ Users \ Adam.vscode \ extends \ ms-python.python-2019.6.24221 \ pythonFiles \ lib \ python \ ptvsd__main __。py"、312行目
runpy.run_path(target、run_name = '__ main __')
run_pathのファイル "c:\ users \ adam \ appdata \ local \ programs \ python \ python37-32 \ Lib \ runpy.py"、行263
pkg_name = pkg_name、script_name = fname)
_run_module_codeのファイル "c:\ users \ adam \ appdata \ local \ programs \ python \ python37-32 \ Lib \ runpy.py"、行96
mod_name、mod_spec、pkg_name、script_name)
ファイル "c:\ users \ adam \ appdata \ local \ programs \ python \ python37-32 \ Lib \ runpy.py"、行85、_run_code
exec(code、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 for w1、w2 in self.collocation_list(num、window_size)
ファイル "C:\ Users \ Adam.virtualenvs \ natlang-9ek-vNym \ lib \ site-packages \ nltk \ text.py"、行444、
w1 + "" + w2 for w1、w2 in self.collocation_list(num、window_size)
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によると、私はNLTK3.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 for w1、w2 in text.collocation_list(num、window_size)]

次のように:
collocation_strings = [w for w in text.collocation_list(num、window_size)]

こっちも一緒。 nltkブックを操作すると、collocations()でエラーが発生しますが、collocation_list()は機能します。

このページは役に立ちましたか?
0 / 5 - 0 評価