2013-06-19 21 views
0

我已經安裝了CherryPy的在Mac上的Apache服務器作爲我的網絡framework.I打算暴露NLTK涉及某些服務時給出了一個錯誤。我也遇到了500個內部錯誤。在互聯網上搜索解釋「AttributeError的:‘模塊’對象有沒有屬性‘詞’」,在系統Python和NLTK作爲循環依賴issue.I嫌疑版本的區別。CherryPy的使用NLTK(自然語言工具包)

我是新來的Python和我的調試技巧真的poor.Kindly幫助

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' 
+0

你需要'進口nltk.word'。 – Blender

+0

感謝Blender.Still沒有解決。在python終端上,我可以導入nltk,但我得到這個:Traceback(最近調用最後一個): 文件「hello.py」,第2行,在 import nltk.word 導入錯誤:沒有模塊名爲話,如果我使用進口nltk.word – user2261494

回答

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' 
+0

謝謝mbatchkarov.It worked.Also它pos_tag,而不是真正pos_Tag.Didn't懷疑這是因爲,當我用文字蟒蛇控制檯沒有拋出了一個問題.tokenize。真的很感謝你的幫助。 – user2261494