斯坦福自然语言处理(1)——三元组的使用

三元组是斯坦福自然语言解析中比较基础的一部分,主要过程是将每一个句子从文中提取出来以后,将句子中符合三元组(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();
    }
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值