将 stanford pos tagger 导入 nltk 时遇到问题

发布于 2024-12-03 12:46:08 字数 472 浏览 2 评论 0原文

这可能是一个非常琐碎的问题。我正在尝试通过 nltk 使用 stanford pos tagger 此处 问题是我的 nltk 库不包含 stanford 模块。所以我将其复制到适当的文件夹中并进行编译。现在,当我尝试运行示例时,会检测到模块,但不会检测到模块内的类。谁能告诉我哪里错了?这可能又是非常愚蠢的。

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

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

This is probably a very trivial question. I am trying to use the stanford pos tagger through nltk given here The problem is that my nltk lib doesnt contain the stanford module. So I copied the same into the appropriate folder and compiled the same. Now when i try to run an example the module is getting detected but not the class inside the module. Can anyone tell me where I am going wrong?? Again this is probably very dumb.

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

I used py_compile to compile the stanford.py file. Am i missing something

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

魂牵梦绕锁你心扉 2024-12-10 12:46:08

您仅导入 stanford。为了访问 StanfordTagger 您需要使用:(

>>> from nltk.tag.stanford import StanfordTagger

假设 `StanfordTagger 没有进一步嵌套在模块中)或通过以下方式访问它:

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

You are only importing stanford. In order to access StanfordTagger you need to use either:

>>> from nltk.tag.stanford import StanfordTagger

(assuming that `StanfordTagger is not further nested in a module) or access it by

>>> st = stanford.StanfordTagger('bidirection-distsim-wsj-0-18.tagger')
[旋木] 2024-12-10 12:46:08

如果您想使用斯坦福解析器,请使用:

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'])]),
树(',', [',']), 树('NP', [树('PRP$', ['我的']), 树('NN',
['名称'])]),树('VP',[树('VBZ',['是']),树('ADJP',[树('JJ',
['Melroy'])])]), Tree('.', ['.'])])]), Tree('ROOT', [Tree('SBARQ',
[树('WHNP',[树('WP',['什么'])]),树('SQ',[树('VBZ',
['是']), 树('NP', [树('PRP$', ['你的']), 树('NN', ['名称'])])]),
树('.', ['?'])])])]

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

注2:

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

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

If you want to use the Stanford parser, use this:

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?"))

Output:

[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'])])]),
Tree('.', ['?'])])])]

Note 1:
In this example both the parser & model jars are in the same folder.

Note 2:

  • File name of stanford parser is: stanford-parser.jar
  • File name of stanford models is: stanford-parser-x.x.x-models.jar

Note 3:
The englishPCFG.ser.gz file can be found inside the models.jar file (/edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz). Please use come archive manager to 'unzip' the models.jar file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文