Nltk: função de colocações retorna erro

Criado em 15 mai. 2019  ·  10Comentários  ·  Fonte: nltk/nltk

Eu estava lendo o capítulo 1 do livro e a função de colocações retorna um erro. Parece que a linha 440 em text.py é redundante, já que a função collocation_list foi introduzida. Corrigi o problema reescrevendo a linha 440 e a linha 441 atuais em text.py.

código antigo:
collocation_strings = [w1 + '' + w2 para w1, w2 em self.collocation_list (num, window_size)] *
imprimir (tokenwrap (collocation_strings, separator = ";"))

novo Código:
imprimir (tokenwrap (self.collocation_list (), separator = ";"))

bug goodfirstbug resolved text

Comentários muito úteis

Também ainda está tendo problemas com .collocations() , mas .collocation_list() funciona.

Todos 10 comentários

Obrigado @martinevanschouwenburg por levantar o bug!

Sim, parece que a lista de colocação é necessária. Para replicar o bug:

$ 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)

Ainda estou vendo esse erro também ao ler o capítulo 1 do livro.

* Exemplos introdutórios para o livro NLTK *
Carregando texto1, ..., texto9 e enviado1, ..., enviado9
Digite o nome do texto ou frase para visualizá-lo.
Digite: 'textos ()' ou 'sents ()' para listar os materiais.
texto1: Moby Dick de Herman Melville 1851
text2: Sense and Sensibility por Jane Austen 1811
text3: O Livro do Gênesis
text4: Corpus de endereço inaugural
text5: Chat Corpus
text6: Monty Python e o Santo Graal
text7: Wall Street Journal
texto8: Personals Corpus
text9: O homem que era quinta-feira por G. K. Chesterton 1908
Traceback (última chamada mais recente):
Arquivo "c: \ Users \ Adam.vscode \ extensions \ ms-python.python-2019.6.24221 \ pythonFiles \ ptvsd_launcher.py", linha 43, em
main (ptvsdArgs)
Arquivo "c: \ Users \ Adam.vscode \ extensions \ ms-python.python-2019.6.24221 \ pythonFiles \ lib \ python \ ptvsd__main __. Py", linha 434, no principal
corre()
Arquivo "c: \ Users \ Adam.vscode \ extensions \ ms-python.python-2019.6.24221 \ pythonFiles \ lib \ python \ ptvsd__main __. Py", linha 312, em run_file
runpy.run_path (target, run_name = '__ main__')
Arquivo "c: \ users \ adam \ appdata \ local \ programs \ python \ python37-32 \ Lib \ runpy.py", linha 263, em run_path
pkg_name = pkg_name, script_name = fname)
Arquivo "c: \ users \ adam \ appdata \ local \ programs \ python \ python37-32 \ Lib \ runpy.py", linha 96, em _run_module_code
mod_name, mod_spec, pkg_name, script_name)
Arquivo "c: \ users \ adam \ appdata \ local \ programs \ python \ python37-32 \ Lib \ runpy.py", linha 85, em _run_code
exec (código, run_globals)
Arquivo "c: \ Users \ Adam \ Documents \ code \ python \ natlang \ natlang.py", linha 4, em
text4.collocations ()
Arquivo "C: \ Users \ Adam.virtualenvs \ natlang-9ek-vNym \ lib \ site-packages \ nltk \ text.py", linha 444, em colocações
w1 + "" + w2 para w1, w2 em self.collocation_list (num, window_size)
Arquivo "C: \ Users \ Adam.virtualenvs \ natlang-9ek-vNym \ lib \ site-packages \ nltk \ text.py", linha 444, em
w1 + "" + w2 para w1, w2 em self.collocation_list (num, window_size)
ValueError: muitos valores para desempacotar (esperado 2)

@networkjr Também posso confirmar isso. Talvez a correção no # 2227 ainda não tenha sido enviada ao PyPi?

@networkjr é o mesmo com o pacote Anaconda

Estou trabalhando no livro NLTK, sou completamente novo em NLTK e relativamente novo em Python - e estou recebendo o mesmo erro.

$ 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)

De acordo com meu Pipfile.lock , estou usando NLTK 3.4.5, que acredito ser o lançamento mais recente.

Existe uma solução para esse problema?

Isso foi corrigido no # 2377 e deve ser corrigido no próximo lançamento do NLTK em breve.

Caso contrário, se você não puder esperar =)

pip install -U https://github.com/nltk/nltk/archive/develop.zip

Eu ainda tenho o mesmo erro após atualizar o cntk com
pip install -U https://github.com/nltk/nltk/archive/develop.zip

Versão cnkt atual '3.4.5'

Como posso corrigir isso?

Muito Obrigado.

Também ainda está tendo problemas com .collocations() , mas .collocation_list() funciona.

Substitua na linha 444 em /nltk/text.py:
collocation_strings = [w1 + "" + w2 para w1, w2 em text.collocation_list (num, window_size)]

com o seguinte:
collocation_strings = [w para w em text.collocation_list (num, window_size)]

O mesmo aqui. Trabalhar com o livro nltk dá erro para collocations () enquanto collocation_list () funciona.

Esta página foi útil?
0 / 5 - 0 avaliações