使用nltk.post_tag时出错

时间:2016-06-06 05:46:44

标签: nltk pos-tagger

我一直在尝试在我的代码中使用nltk.pos_tag,但是当我这样做时我遇到了错误。我已经下载了Penn treebank和max_ent_treebank_pos。但错误仍然存​​在。这是我的代码:

import nltk
from nltk import tag
from nltk import*



a = "Alan Shearer is the first player to score over a hundred Premier League goals."
a_sentences = nltk.sent_tokenize(a)
a_words     = [nltk.word_tokenize(sentence) for sentence in a_sentences]
a_pos       = [nltk.pos_tag(sentence) for sentence in a_words]
print(a_pos)

这是我得到的错误:

"Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
  print (nltk.pos_tag(text))
  File "C:\Python34\lib\site-packages\nltk\tag\__init__.py", line 110, in  pos_tag
tagger = PerceptronTagger()
File "C:\Python34\lib\site-packages\nltk\tag\perceptron.py", line 140, in    __init__
AP_MODEL_LOC = 'file:'+str(find('taggers/averaged_perceptron_tagger/'+PICKLE))
File "C:\Python34\lib\site-packages\nltk\data.py", line 641, in find
raise LookupError(resource_not_found)
LookupError: 
 Resource 'taggers/averaged_perceptron_tagger/averaged_perceptron
 _tagger.pickle' not found.  Please use the NLTK Downloader to
 obtain the resource:  >>> nltk.download()
 Searched in:
   - 'C:\\Users\\T01142/nltk_data'
   - 'C:\\nltk_data'
   - 'D:\\nltk_data'
   - 'E:\\nltk_data'
   - 'C:\\Python34\\nltk_data'
   - 'C:\\Python34\\lib\\nltk_data'
   - 'C:\\Users\\T01142\\AppData\\Roaming\\nltk_data'

3 个答案:

答案 0 :(得分:2)

从python中调用它:

nltk.download(&#39; averaged_perceptron_tagger&#39)

答案 1 :(得分:0)

在Flask服务器中遇到同样的问题。 nltk在服务器配置中使用了不同的路径,因此我反复在nltk.data.path.append("/home/yourusername/whateverpath/")调用之前在服务器代码中添加pos_tag

请注意,此问题有一些复制

How to config nltk data directory from code?

nltk doesn't add $NLTK_DATA to search path?

POS tagging with NLTK. Can't locate averaged_perceptron_tagger

答案 2 :(得分:0)

要解决此错误,请在 python 提示符下运行以下命令:

Stack(
  children: [
    Positioned(
      top: 10.0,
      left: 10.0,
      child: ListView(
         child: Container(
           height: 120.0,
           child: firstStack(),
         )
      ),
   ),
   Positioned(
      bottom: 0.0,
      right: 10.0
      ListView(
         child: Container(
           height: 120.0,
           child: firstStack(),
         )
      ),
   ),
  ]
),