使用nltk(自然语言工具包)时Cherrypy出错

时间:2013-06-19 06:24:31

标签: python nltk

我已经在mac上安装了Apache服务器,并且我的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'

1 个答案:

答案 0 :(得分: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'