【问题标题】:how to generate collocations from text如何从文本生成搭配
【发布时间】:2020-12-19 23:58:51
【问题描述】:

我是 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)

【问题讨论】:

  • 该代码适用于我,但看起来您需要传递 numwindow_size。你用的是什么版本的nltk?我用的是3.5。您可以通过打印nltk.__version__ 找到它
  • 我使用的是 '3.4.5' 版本。我试图传递默认值,即 num=20 & window_size=2。仍然出现同样的错误。

标签: python nlp nltk


【解决方案1】:

请参阅this 问题。使用gen_text.collocation_list() 可能会解决问题。

【讨论】:

  • 感谢分享解决方案,它对我有用。
猜你喜欢
  • 1970-01-01
  • 2011-12-27
  • 2020-10-17
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多