【问题标题】:Setting NLTK with Stanford NLP (both StanfordNERTagger and StanfordPOSTagger) for Spanish使用斯坦福 NLP(StanfordNERTagger 和 StanfordPOSTagger)为西班牙语设置 NLTK
【发布时间】:2015-12-02 07:28:35
【问题描述】:

NLTK 文档在此集成方面相当差。我followed的步骤是:

然后在ipython 控制台中:

在 [11] 中:导入 nltk

In [12]: nltk.__version__
Out[12]: '3.1'

In [13]: from nltk.tag import StanfordNERTagger

然后

st = StanfordNERTagger('/home/me/stanford/stanford-postagger-full-2015-04-20.zip', '/home/me/stanford/stanford-spanish-corenlp-2015-01-08-models.jar')

但是当我尝试运行它时:

st.tag('Adolfo se la pasa corriendo'.split())
Error: no se ha encontrado o cargado la clase principal edu.stanford.nlp.ie.crf.CRFClassifier

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-14-0c1a96b480a6> in <module>()
----> 1 st.tag('Adolfo se la pasa corriendo'.split())

/home/nanounanue/.pyenv/versions/3.4.3/lib/python3.4/site-packages/nltk/tag/stanford.py in tag(self, tokens)
     64     def tag(self, tokens):
     65         # This function should return list of tuple rather than list of list
---> 66         return sum(self.tag_sents([tokens]), [])
     67 
     68     def tag_sents(self, sentences):

/home/nanounanue/.pyenv/versions/3.4.3/lib/python3.4/site-packages/nltk/tag/stanford.py in tag_sents(self, sentences)
     87         # Run the tagger and get the output
     88         stanpos_output, _stderr = java(cmd, classpath=self._stanford_jar,
---> 89                                                        stdout=PIPE, stderr=PIPE)
     90         stanpos_output = stanpos_output.decode(encoding)
     91 

/home/nanounanue/.pyenv/versions/3.4.3/lib/python3.4/site-packages/nltk/__init__.py in java(cmd, classpath, stdin, stdout, stderr, blocking)
    132     if p.returncode != 0:
    133         print(_decode_stdoutdata(stderr))
--> 134         raise OSError('Java command failed : ' + str(cmd))
    135 
    136     return (stdout, stderr)

OSError: Java command failed : ['/usr/bin/java', '-mx1000m', '-cp', '/home/nanounanue/Descargas/stanford-spanish-corenlp-2015-01-08-models.jar', 'edu.stanford.nlp.ie.crf.CRFClassifier', '-loadClassifier', '/home/nanounanue/Descargas/stanford-postagger-full-2015-04-20.zip', '-textFile', '/tmp/tmp6y169div', '-outputFormat', 'slashTags', '-tokenizerFactory', 'edu.stanford.nlp.process.WhitespaceTokenizer', '-tokenizerOptions', '"tokenizeNLs=false"', '-encoding', 'utf8']

StandfordPOSTagger 也是如此

注意:我需要这将是西班牙语版本。 注意:我在 python 3.4.3

运行这个

【问题讨论】:

  • 应该是“Download & extract the stanford NER package”,你忘记了“extract”部分;P
  • 简短的问题,您在哪里找到您发布的说明的文档?是github.com/nltk/nltk/wiki/…吗?如果没有,您介意发布链接和/或在 NLTK 上创建问题,以便开发人员做出适当的更改吗?
  • 我会根据您对问题提出的建议进行更改,谢谢

标签: python python-3.x nlp nltk stanford-nlp


【解决方案1】:

试试:

# StanfordPOSTagger
from nltk.tag.stanford import StanfordPOSTagger
stanford_dir = '/home/me/stanford/stanford-postagger-full-2015-04-20/'
modelfile = stanford_dir + 'models/english-bidirectional-distsim.tagger'
jarfile = stanford_dir + 'stanford-postagger.jar'

st = StanfordPOSTagger(model_filename=modelfile, path_to_jar=jarfile)


# NERTagger
stanford_dir = '/home/me/stanford/stanford-ner-2015-04-20/'
jarfile = stanford_dir + 'stanford-ner.jar'
modelfile = stanford_dir + 'classifiers/english.all.3class.distsim.crf.ser.gz'

st = StanfordNERTagger(model_filename=modelfile, path_to_jar=jarfile)

有关使用斯坦福工具的 NLTK API 的详细信息,请查看:https://github.com/nltk/nltk/wiki/Installing-Third-Party-Software#stanford-tagger-ner-tokenizer-and-parser

注意: NLTK API 用于各个斯坦福工具,如果您使用的是 Stanford Core NLP,最好按照http://www.eecs.qmul.ac.uk/~dm303/stanford-dependency-parser-nltk-and-anaconda.html 上的@dimazest 说明进行操作


已编辑

至于西班牙语 NER 标记,我强烈建议您使用 Stanford Core NLP (http://nlp.stanford.edu/software/corenlp.shtml) 而不是使用 Stanford NER 包 (http://nlp.stanford.edu/software/CRF-NER.shtml)。并按照@dimazest 解决方案进行 JSON 文件读取。

或者,如果您必须使用 NER 包,您可以尝试按照https://github.com/alvations/nltk_cli 的说明进行操作(免责声明:此 repo 与 NLTK 官方无关)。在 unix 命令行上执行以下操作:

cd $HOME
wget http://nlp.stanford.edu/software/stanford-spanish-corenlp-2015-01-08-models.jar
unzip stanford-spanish-corenlp-2015-01-08-models.jar -d stanford-spanish
cp stanford-spanish/edu/stanford/nlp/models/ner/* /home/me/stanford/stanford-ner-2015-04-20/ner/classifiers/

然后在python中:

# NERTagger
stanford_dir = '/home/me/stanford/stanford-ner-2015-04-20/'
jarfile = stanford_dir + 'stanford-ner.jar'
modelfile = stanford_dir + 'classifiers/spanish.ancora.distsim.s512.crf.ser.gz'

st = StanfordNERTagger(model_filename=modelfile, path_to_jar=jarfile)

【讨论】:

  • 感谢您的回答和信息。你能调整一下使用西班牙语标注器的路径吗?
  • 遗憾的是,stanford-ner-2015-04-20.zip 中没有西班牙语 NER 标记器,我将尝试使用 @dimazest 指令找出使用 corenlp 的快速方法。
  • @stanfordnlphelp 看起来不像 POS 标注器和解析器,它有 stanford-postagger-full-2015-04-20.zipstanford-parser-full-2015-04-20.zip,NER 标注器确实有一个 stanford-ner-full-2015-04-20.zip,对吗?
  • CoreNLP 使用 NER 包作为其管道的一部分,对吗?不要认为使用 CoreNLP 或直接使用 NER 工具会有什么不同。
  • 是的,我认为它正在筹备中。但是不,CoreNLP 的工作方式不同(在 API 意义上),而且维护得更好。
【解决方案2】:

错误在于为 StanfordNerTagger 函数编写的参数。

第一个参数应该是模型文件或您正在使用的分类器。您可以在斯坦福 zip 文件中找到该文件。例如:

    st = StanfordNERTagger('/home/me/stanford/stanford-postagger-full-2015-04-20/classifier/tagger.ser.gz', '/home/me/stanford/stanford-spanish-corenlp-2015-01-08-models.jar')

【讨论】:

    【解决方案3】:

    词性标注器

    为了将 StanfordPOSTagger for Spanish 与 python 一起使用,您必须安装包含西班牙语模型的 Stanford 标记器。

    在本例中,我将标记器下载到 /content 文件夹

    cd /content
    wget https://nlp.stanford.edu/software/stanford-tagger-4.1.0.zip
    unzip stanford-tagger-4.1.0.zip
    

    解压后,我在 /content 中有一个文件夹 stanford-postagger-full-2020-08-06, 所以我可以使用标记器:

    from nltk.tag.stanford import StanfordPOSTagger
    
    stanford_dir = '/content/stanford-postagger-full-2020-08-06'
    modelfile = f'{stanford_dir}/models/spanish-ud.tagger'
    jarfile =   f'{stanford_dir}/stanford-postagger.jar'
    
    st = StanfordPOSTagger(model_filename=modelfile, path_to_jar=jarfile)
    

    要检查一切是否正常,我们可以这样做:

    >st.tag(["Juan","Medina","es","un","ingeniero"])
    
    >[('Juan', 'PROPN'),
     ('Medina', 'PROPN'),
     ('es', 'AUX'),
     ('un', 'DET'),
     ('ingeniero', 'NOUN')]
    

    NER 标记器

    在这种情况下,需要分别下载 NER 核心和西班牙模型。

    cd /content
    #download NER core
    wget https://nlp.stanford.edu/software/stanford-ner-4.0.0.zip
    unzip stanford-ner-4.0.0.zip
    #download spanish models
    wget http://nlp.stanford.edu/software/stanford-spanish-corenlp-2018-02-27-models.jar
    unzip stanford-spanish-corenlp-2018-02-27-models.jar -d stanford-spanish
    #copy only the necessary files
    cp stanford-spanish/edu/stanford/nlp/models/ner/* stanford-ner-4.0.0/classifiers/
    rm -rf stanford-spanish stanford-ner-4.0.0.zip stanford-spanish-corenlp-2018-02-27-models.jar
    

    在 python 上使用它:

    from nltk.tag.stanford import StanfordNERTagger
    stanford_dir = '/content/stanford-ner-4.0.0/'
    jarfile = f'{stanford_dir}/stanford-ner.jar'
    modelfile = f'{stanford_dir}/classifiers/spanish.ancora.distsim.s512.crf.ser.gz'
    
    st = StanfordNERTagger(model_filename=modelfile, path_to_jar=jarfile)
    

    要检查一切是否正常,我们可以这样做:

    >st.tag(["Juan","Medina","es","un","ingeniero"])
    
    >[('Juan', 'PERS'),
     ('Medina', 'PERS'),
     ('es', 'O'),
     ('un', 'O'),
     ('ingeniero', 'O')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      相关资源
      最近更新 更多