Stanford parser入门2:中文句法路径提取

1.代码

//调用stanford parser进行句法分析,提取节点间路径
import java.io.IOException;
import java.util.List;

import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.trees.Tree;

public class ParserTest2 {

	public static void main(String[] args) throws IOException {
		String grammar = "edu/stanford/nlp/models/lexparser/chineseFactored.ser.gz";
//		String grammar = "edu/stanford/nlp/models/lexparser/chinesePCFG.ser.gz";
		String[] options = {};
		LexicalizedParser lp = LexicalizedParser.loadModel(grammar, options);
		String line = "质量 太 差 了";
		String word1 = "质量";
		String word2 = "差";
		Tree parse = lp.parse(line);
		parse.pennPrint();
		List<Tree> nodelist = parse.preOrderNodeList();
		Tree node1=parse,node2=parse;
		for(Tree nodet:nodelist)
		{
			if(nodet.label().toString().equals(word1)){
				node1 = nodet;
				//System.out.println(nodet.label().toString());
			}
			if(nodet.label().toString().equals(word2)){
				node2 = nodet;
				//System.out.println(nodet.label().toString());
			}
		}
		List<Tree> nodepath = parse.pathNodeToNode(node1, node2);
		String nodepathstr="";
		for(Tree nodet:nodepath)
		{
			if(nodet!=null)
				nodepathstr += nodet.label().toString() + ",";
		}
		System.out.println(nodepathstr);
		System.out.println("---end---");
	}
}

2.结果

(ROOT
  (IP
    (IP
      (NP (NN 质量))
      (VP
        (ADVP (AD 太))
        (VP (VA 差) (AS 了))))
    (PU ,)
    (CP
      (IP
        (VP (VV 用)
          (VP
            (ADVP (AD 不))
            (PP (P 到)
              (NP (NN 半天)))
            (ADVP (AD 就))
            (VP (VE 没)
              (NP (NN 电))))))
      (SP 了))))
质量,NN,NP,IP,VP,VP,VA,差,
---end---

3.参考资料

https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/Tree.html

https://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/ling/Label.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值