使用斯坦福CoreNLP进行关系抽取

5
我正在尝试使用Stanford CoreNLP库从自然语言内容中提取信息。我的目标是从句子中提取“主语-谓语-宾语”(简化)对。以以下句子为例:John Smith only eats an apple and a banana for lunch. He's on a diet and his mother told him that it would be very healthy to eat less for lunch. John doesn't like it at all but since he's very serious with his diet, he doesn't want to stop. 我想要的结果如下所示: - John Smith - 吃 - 只有一个苹果和一个香蕉的午餐 - 他 - 是 - 节食 - 他的母亲 - 告诉 - 他 - 减少午餐摄入量非常健康 - 约翰 - 不喜欢 - 它(一点也不) - 他 - 对他的节食非常认真
如何实现这一点?或者更具体地说,如何解析依赖树(或更适合的树),以获得上述指定的结果?如果能提供任何提示、资源或代码片段,将不胜感激。顺便提一下:我成功地用其代表性提及替换了代词,这会将“他”和“他的”更改为相应的实体(在这种情况下为约翰·史密斯)。
2个回答

5

斯坦福CoreNLP工具包提供了依赖解析器。

首先,这里有一个链接描述了树中边的类型:

http://universaldependencies.github.io/docs/

您可以使用该工具包生成依赖树的多种方式。

这是一些示例代码,可用于帮助您开始:

import java.io.*;
import java.util.*;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.util.*;
import edu.stanford.nlp.semgraph.*;
import edu.stanford.nlp.trees.TreeCoreAnnotations.*;

public class DependencyTreeExample {

    public static void main (String[] args) throws IOException {

        // set up properties
        Properties props = new Properties();
        props.setProperty("ssplit.eolonly","true");
        props.setProperty("annotators",
                "tokenize, ssplit, pos, depparse");
        // set up pipeline
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        // get contents from file
        String content = new Scanner(new File(args[0])).useDelimiter("\\Z").next();
        System.out.println(content);
        // read in a product review per line
        Annotation annotation = new Annotation(content);
        pipeline.annotate(annotation);

        List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
            System.out.println("---");
            System.out.println("sentence: "+sentence);
            SemanticGraph tree = sentence.get(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation.class);
            System.out.println(tree.toString(SemanticGraph.OutputFormat.READABLE));
        }


    }

}

说明:

  • 将以下内容复制并粘贴到DependencyTreeExample.java文件中
  • 将该文件放在目录stanford-corenlp-full-2015-04-20下
  • 运行命令javac -cp "*:." DependencyTreeExample.java进行编译
  • 将您的句子一句话一行地添加到名为dependency_sentences.txt的文件中
  • 运行命令java -cp "*:." DependencyTreeExample dependency_sentences.txt

输出示例:

sentence: John doesn't like it at all.
dep                 reln                gov                 
---                 ----                ---                 
like-4              root                root                
John-1              nsubj               like-4              
does-2              aux                 like-4              
n't-3               neg                 like-4              
it-5                dobj                like-4              
at-6                case                all-7               
all-7               nmod:at             like-4              
.-8                 punct               like-4 

这将打印出依赖关系解析结果。通过使用SemanticGraph对象,您可以编写代码来查找所需的模式。
在此示例中,您会注意到“like”使用“nsubj”指向“John”,并使用“dobj”将其指向“it”。
有关参考,请查看edu.stanford.nlp.semgraph.SemanticGraph: http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/semgraph/SemanticGraph.html

谢谢您的回答。我该如何使用“SemanticGraph tree”来按照我在问题中指定的格式对结果进行格式化? - qsi

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接