斯坦福词性标注器给出属性错误

问题描述 投票:0回答:2

我尝试了不同的教程来学习在 Python 中使用斯坦福词性标注器。目前,我正在使用斯坦福标记器使用以下代码进行词性标记。然而,我得到了

AttributeError
。我的代码如下:

import nltk
from nltk.tag.stanford import StanfordPOSTagger
english_postagger = StanfordPOSTagger('/home/szk/Downloads/NL2API/NL2API/tutorials/postags/stanford-postagger-2018-10-16/models/english-bidirectional-distsim.tagger', '/home/szk/Downloads/NL2API/NL2API/tutorials/postags/stanford-postagger-2018-10-16/stanford-postagger.jar')
english_postagger.tag('this is stanford postagger in nltk for python users'.split())

错误轨迹如下:

Traceback (most recent call last):
  File "stanfordpostag.py", line 4, in <module>
    english_postagger.tag('this is stanford postagger in nltk for python users'.split())
  File "/home/szk/Downloads/NL2API/NL2API/newv/local/lib/python2.7/site-packages/nltk/tag/stanford.py", line 93, in tag
    return sum(self.tag_sents([tokens]), [])
  File "/home/szk/Downloads/NL2API/NL2API/newv/local/lib/python2.7/site-packages/nltk/tag/stanford.py", line 116, in tag_sents
    cmd, classpath=self._stanford_jar, stdout=PIPE, stderr=PIPE
  File "/home/szk/Downloads/NL2API/NL2API/newv/local/lib/python2.7/site-packages/nltk/internals.py", line 112, in java
    subprocess_output_dict = {'pipe': subprocess.PIPE, 'stdout': subprocess.STDOUT, 'devnull': subprocess.DEVNULL}
AttributeError: 'module' object has no attribute 'DEVNULL'

希望有人能提供解决方案。

python stanford-nlp pos-tagger
2个回答
1
投票

我不确定为什么这不起作用 - 它仍然意味着 - 但从 NLTK 版本 3.2.3 开始,使用此处讨论的较新的斯坦福 CoreNLP 服务器接口(出于速度和可扩展性原因)会更好: https://github.com/nltk/nltk/wiki/Stanford-CoreNLP-API-in-NLTK .

所以你可以尝试一下。可以遵循这些说明,但在所有地方都替换为当前 2018-10-05 CoreNLP 版本,而不是说明中引用的先前版本。


0
投票

几年后,Python 的 stanza 包出现了。当然,另一种选择是通过 NLTK 请参阅此处的说明,但斯坦福NLP 建议使用 Stanza(请参阅此处)。所以我决定使用 stanza,并惊讶于它的易用性。而且,它不使用太多 CPU 或有其他大要求。

import stanza 
stanza.download('de') # download German model

model = stanza.Pipeline('de', processors = 'tokenize, pos')
#initialise German neural pipeline 
doc = model('Ich hoffe es gibt nicht wieder einen blöden Kommentar weil ich einen weiblichen Vornamen habe und wenig Reputationspunkte.')  
print(*[f'word: {word.text}\tupos: {word.upos}' for sent in doc.sentences for word in sent.words], sep='\n')  

Stanza 为不同的语言提供不同的模型(标记化、PoS、NER、情感)。请在此处查看详细信息。与 TreeTagger 相比,它绝对更容易安装。我正在将其性能与 Spacy 和 HanTa 进行比较。我尚未证实的印象是,它在德语方面比上述两个表现更好。

快乐标记!

© www.soinside.com 2019 - 2024. All rights reserved.