TypeError:__ init __()得到关键字参数'encoding'的多个值

时间:2018-04-19 05:03:27

标签: python python-2.7 nlp nltk stanford-nlp

from nltk.tag.stanford import CoreNLPNERTagger as POSTagger
english_postagger = POSTagger(RESOURCES_DIR+'jars/english-left3words-distsim.tagger',RESOURCES_DIR+'jars/stanford-postagger.jar', encoding='utf-8')

当我运行上面的代码时,我收到以下错误。如何解决这个问题?

Traceback (most recent call last):
 File "ILPMod.py", line 44, in <module>
 english_postagger = POSTagger(RESOURCES_DIR+'jars/english-left3words-distsim.tagger',RESOURCES_DIR+'jars/stanford-postagger.jar', encoding='utf-8')
TypeError: __init__() got multiple values for keyword argument 'encoding'

1 个答案:

答案 0 :(得分:1)

您作为POSTagger导入的

CoreNLPNERTagger具有与StanfordPOSTagger不同的界面。来自文档:

  

class nltk.tag.stanford.CoreNLPNERTagger(url='http://localhost:9000', encoding='utf8')

     

基地:nltk.tag.stanford.CoreNLPTagger

     

这是CoreNLPTagger的一个子类,它包含了   nltk.parse.CoreNLPParser用于命名实体标记。

>>> from nltk.tag.stanford import CoreNLPNERTagger
>>> CoreNLPNERTagger(url='http://localhost:9000').tag('Rami Eid is studying at Stony Brook University in NY'.split())  [('Rami', 'PERSON'),('Eid', 'PERSON'), ('is', 'O'), ('studying', 'O'), ('at', 'O'), ('Stony', 'ORGANIZATION'), ('Brook', 'ORGANIZATION'), ('University', 'ORGANIZATION'), ('in', 'O'), ('NY', 'O')]

对于CoreNLPNERTagger,您应首先启动一个核心NLP服务器并连接到它:

with CoreNLPServer(port=9000) as server:
    tagger = CoreNLPNERTagger(url=server.url)
    tagger.tag(...)

或者只是按照您当前正在尝试的方式使用StanfordPOSTagger