如何解析句子列表?
我想使用斯坦福 NLP 解析器解析句子列表。 我的列表是一个 ArrayList,如何使用 LexicalizedParser 解析所有列表?
我想从每个句子中得到这种形式:
Tree parse = (Tree) lp1.apply(sentence);
I want to parse a list of sentences with the Stanford NLP parser.
My list is an ArrayList
, how can I parse all the list with LexicalizedParser
?
I want to get from each sentence this form:
Tree parse = (Tree) lp1.apply(sentence);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然人们可以深入研究文档,但我将在这里提供代码,特别是因为链接移动和/或死亡。这个特定的答案使用整个管道。如果对整个管道不感兴趣,我将在一秒钟内提供替代答案。
下面的例子是使用Stanford pipeline的完整方式。如果对共指解析不感兴趣,请从第 3 行代码中删除
dcoref
。因此,在下面的示例中,如果您只是将其输入到文本正文(文本变量)中,管道就会为您执行句子分割(ssplit 注释器)。只有一句话吗?好吧,没关系,您可以将其作为文本变量输入。Although one can dig into the documentation, I am going to provide code here on SO, especially since links move and/or die. This particular answer uses the whole pipeline. If not interested in the whole pipeline, I will provide an alternative answer in just a second.
The below example is the complete way of using the Stanford pipeline. If not interested in coreference resolution, remove
dcoref
from the 3rd line of code. So in the example below, the pipeline does the sentence splitting for you (the ssplit annotator) if you just feed it in a body of text (the text variable). Have just one sentence? Well, that is ok, you can feed that in as the text variable.实际上,斯坦福大学 NLP 的文档提供了如何解析句子的示例。
您可以在此处找到文档
Actually documentation from Stanford NLP provide sample of how to parse sentences.
You can find the documentation here
因此,正如所承诺的,如果您不想访问完整的斯坦福管道(尽管我相信这是推荐的方法),您可以直接使用 LexicalizedParser 类。在这种情况下,您将下载最新版本的Stanford Parser(而其他版本将使用CoreNLP工具)。确保除了解析器 jar 之外,您还拥有要使用的相应解析器的模型文件。示例代码:
请注意,这适用于解析器的 3.3.1 版本。
So as promised, if you don't want to access the full Stanford pipeline (although I believe that is the recommended approach), you can work with the LexicalizedParser class directly. In this case, you would download the latest version of Stanford Parser (whereas the other would use CoreNLP tools). Make sure that in addition to the parser jar, you have the model file for the appropriate parser you want to work with. Example code:
Note this works for version 3.3.1 of the parser.