三元组是斯坦福自然语言解析中比较基础的一部分,主要过程是将每一个句子从文中提取出来以后,将句子中符合三元组(subject+relation+object)的成分提取出来,其中每个triple中有四个成员变量:
1、confidence:表示该三元组符合subject+relation+object程度的置信度
2、subject:三元组中的主体,表示动作的发出者
3、relation:三元组中表示subject和object的联系
4、object:三元组中表示relation动作的承受者
首先对工具入口StanfordCoreNLP进行实例化pipeline,并在管道中添加分析三元组所需要的功能,用Property类来实现。我这里定义了一个TripleBean类,每个三元组类中含有TripleSubject、TripleRelation、以及TripleObject、其中每个类中都包含该单词的词性、命名实体,以一个TripleElement作为例子
package edu.stu.common;
public class TripleElement {
private String element;
private String nameentity;
private String partofspeech;
public TripleElement(String element ,String nameentity ,String partofspeech)\\实例化一个TripleElement对象
{
this.element = element;
this.nameentity = nameentity;
this.partofspeech = partofspeech;
}
public String getElement()\\元素的单词本身
{
return this.element;
}
public String getPartOfSpeech()\\元素的词性
{
return this.partofspeech;
}
public String getNameEntity()\\元素的命名实体识别
{
return this.nameentity;
}
public String toString()\\输出该对象时,输出对象中的单词
{
return this.getElement();
}
}
`
“`
package edu.stanford.nlp.pipeline.tool;
public class Triple
{
private StanfordCoreNLP pipeline = null;
private String content = null;
private Annotation doc = null;
private Collection<RelationTriple> triples = null;
private Collection<TripleBean> triplebeans = null;
public Triple(File filename)
{
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, depparse, natlog, openie");
triplebeans = new ArrayList();
this.pipeline = new StanfordCoreNLP(props);
this.content = new ReadFile(filename).getContent();
this.doc = new Annotation(this.content); // 用字符串初始化一个annotation类型
pipeline.annotate(doc);
}
public Triple(String content)
{
Properties props = new Properties(); //props是一个类似map的结构
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, depparse, natlog, openie");
triplebeans = new ArrayList();
this.pipeline = new StanfordCoreNLP(props);
this.content = content;
this.doc = new Annotation(this.content); // 用字符串初始化一个annotation类型
pipeline.annotate(doc);
convertToTripleBean();
}