0

我正在尝试使用 NLTK 创建分散图。据我所知,我正在按照指示进行操作。当我运行他们的示例调用 NLTK 附带的示例文本时,它可以工作。当我调用自己的文本文件时,出现上述错误。

矿:

>>> text11 = "Text_test.txt"
>>> text11.dispersion_plot(["semiosis", "dialectic", "essentially", "icon", "logo"])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'dispersion_plot'

他们的示例代码:

text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"])

感谢您的任何建议/帮助!

4

1 回答 1

3

请注意,您必须在对其进行标记后将其转换为 NLTK Text 对象。此外,您text11在代码中使用的变量是 string "Text_test.txt",而不是文件中名为Text_test.txt.

假如说

  1. 你已经安装了,这是matplotlib工作所必需的numpydispersion_plot
  2. 你的文件在/home/myfile.txt
  3. 您的文件是简单的文本,就像他们使用的一样

那么这应该这样做

# from Ch. 3
f=open('/home/myfile.txt','rU')    # open the file
raw = f.read()                     # read the text
tokens = nltk.word_tokenize(raw)   # tokenize it
mytext = nltk.Text(tokens)         # turn text into a NLTK Text object

# from Ch. 1
mytext.dispersion_plot(["semiosis", "dialectic", "essentially", "icon", "logo"])
于 2013-10-20T16:38:46.617 回答