【问题标题】:trouble importing stanford pos tagger into nltk将 stanford pos tagger 导入 nltk 时遇到问题
【发布时间】:2011-09-08 08:01:59
【问题描述】:

这可能是一个非常琐碎的问题。我正在尝试通过给定here 的 nltk 使用 stanford pos tagger 问题是我的 nltk 库不包含 stanford 模块。因此,我将其复制到相应的文件夹中并进行了相同的编译。现在,当我尝试运行一个示例时,模块被检测到,而不是模块内的类。谁能告诉我哪里出错了??同样,这可能非常愚蠢。

>>> from nltk.tag import stanford 
>>> st = StanfordTagger('bidirection-distsim-wsj-0-18.tagger')

我使用 py_compile 来编译 stanford.py 文件。我错过了什么

【问题讨论】:

    标签: python nltk stanford-nlp


    【解决方案1】:

    您只是在导入stanford。要访问StanfordTagger,您需要使用:

    >>> from nltk.tag.stanford import StanfordTagger
    

    (假设`StanfordTagger 没有进一步嵌套在模块中)或通过

    >>> st = stanford.StanfordTagger('bidirection-distsim-wsj-0-18.tagger')
    

    【讨论】:

    • 天哪,我怎么忘记了!!不过需要弄清楚这个其他错误。
    • 你编译了nltk/tag目录nltk/internals目录吗? py_compile我从来不用,python一直自动为我处理,所以我的知识在这里有些局限。
    • 这确实是与第一个问题不同的问题。通过更改问题的内容,使其不再解释您最初的问题,并且不再对未来的用户有帮助。请(至少)包括最初的问题。
    • 是的,对不起,我现在才意识到......会改变
    • 我得到 TypeError: Can't instantiate abstract class StanfordTagger with abstract methods _cmd
    【解决方案2】:

    如果你想使用斯坦福解析器,使用这个:

    import os
    from nltk.parse import stanford
    os.environ['STANFORD_PARSER'] = '/folder/with/standford/jars'
    os.environ['STANFORD_MODELS'] = '/folder/with/standford/jars'
    
    parser = stanford.StanfordParser(model_path="/location/of/the/englishPCFG.ser.gz")
    print parser.raw_batch_parse(("Hello, My name is Melroy.", "What is your name?"))
    

    输出:

    [Tree('ROOT', [Tree('S', [Tree('INTJ', [Tree('UH', ['Hello'])]), Tree(',', [',']), Tree('NP', [Tree('PRP$', ['My']), Tree('NN', ['name'])]), Tree('VP', [Tree('VBZ', ['is']), Tree('ADJP', [Tree('JJ', ['Melroy'])])]), Tree('.', ['.'])])]), Tree('ROOT', [Tree('SBARQ', [Tree('WHNP', [Tree('WP', ['What'])]), Tree('SQ', [Tree('VBZ', ['is']), Tree('NP', [Tree('PRP$', ['your']), Tree('NN', ['name'])])]), 树('.', ['?'])])])]

    注意 1: 在这个例子中,解析器和模型 jar 都在同一个文件夹中。

    注2:

    • stanford解析器的文件名是:stanford-parser.jar
    • stanford 模型的文件名是:stanford-parser-x.x.x-models.jar

    注 3: 可以在 models.jar 文件 (/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz) 中找到 englishPCFG.ser.gz 文件。请使用存档管理器“解压缩”models.jar 文件。

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      相关资源
      最近更新 更多