0

我已经在 mac 上安装了一个 Apache 服务器,并以cherrypy 作为我的 web 框架。我打算公开某些涉及 nltk 的服务。我在做同样的事情时收到 500 个内部错误。互联网上的搜索将“AttributeError:'module'对象没有属性'word'”解释为循环依赖问题。我怀疑系统python和nltk的版本差异。

我是 Python 新手,我的调试技能真的很差。请帮忙

import cherrypy
import nltk

class HelloWorld :
    def index(self) :
        return "Hello world !"
    index.exposed=True

    def printHappy(self,age) :
        return age
    printHappy.exposed=True

    def fNouns(self,string):
        text = nltk.word.tokenize(string)
        nounTag=nltk.pos_Tag(text)
        return  nounTag
    fNouns.exposed=True

cherrypy.quickstart(HelloWorld())

当我尝试使用它访问它时http://localhost:8080/fNouns/hey,我收到以下错误(但是http://localhost:8080/printHappy/1234有效!!)

500 Internal Server Error

服务器遇到了一个意外情况,导致它无法完成请求。

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
    response.body = self.handler()
  File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/lib/encoding.py", line 188, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cpdispatch.py", line 34, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "hello.py", line 14, in fNouns
    text = nltk.word.tokenize(string)
AttributeError: 'module' object has no attribute 'word'
4

1 回答 1

0

您输入了错误的函数名称。nltk.word.tokenize实际上叫做nltk.word_tokenize. 你像这样使用它:

In [1]: from nltk import word_tokenize

In [2]: word_tokenize('i like cats and dogs')
Out[2]: ['i', 'like', 'cats', 'and', 'dogs']

如果您使用错误的名称,则会收到以下错误:

In [3]: import nltk

In [4]: nltk.word.tokenize('i like cats and dogs')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/<ipython-input-4-a0785dff62d6> in <module>()
----> 1 nltk.word.tokenize('i like cats and dogs')

AttributeError: 'module' object has no attribute 'word'
于 2013-06-19T11:56:44.983 回答