首页 > 代码库 > stanford trex使用方法

stanford trex使用方法


一、stanford trex 简介

http://nlp.stanford.edu/software/tregex.shtml



                 A java program for identifying patterns in trees
                 Like regular expressions for strings, based on tgrep syntax
                 Simple example: NP < NN

               

将包导入Java工程,下载地址     

http://download.csdn.net/detail/u014112584/7536563


程序代码测试:

                         

import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;
import edu.stanford.nlp.trees.tregex.tsurgeon.Tsurgeon;
import edu.stanford.nlp.trees.tregex.tsurgeon.TsurgeonPattern;

//http://codego.net/196118/
//https://deerchao.net/tutorials/regex/regex.htm#mission 正则表达式入门教程
public class Demo1 {
	public static void main(String []args){
		Tree t = Tree.valueOf("(ROOT(NP(NP (DT An) (NNP NP))(PP (IN over)(NP(NP (DT an) (NNP NN))(SBAR(WHNP (WDT that))(S(VP (VBZ is)(PP (IN over)(NP (NN dog))))))))(. .)))");
		String s = "NP < NN | < NNS";
		Tree argTree =Tree.valueOf("(NN arg0)");
		//输出匹配的内容
		TregexPattern p = TregexPattern.compile(s);
		TregexMatcher m = p.matcher(t);
		
		/*
		TsurgeonPattern surgery = Tsurgeon.parseOperation("");
		Tsurgeon.processPattern (p,surgery,t).pennPrint();
		
		 Tree conjSubTree = m.getNode("conj");
		   System.out.println(conjSubTree.value());
		   */
		while (m.find()) {
			System.out.println("源树型结构!\n");
			t.pennPrint();//打印原树形
			System.out.println("匹配的部分:");
			m.getMatch().pennPrint();//打印匹配后的树型
		      m.getMatch().removeChild(0);//剔除匹配后的树叶
		      System.out.println("剔除匹配后的子叶:");
		      m.getMatch().pennPrint();
		      System.out.println("增加匹配后的树:");
		      m.getMatch().addChild(argTree);//增加匹配后的树,完成替换
		      //m.getMatch().pennPrint();
			  t.pennPrint();
			  //System.out.println(" \n");
		   // m.getMatch().pennPrint();
		    String le=m.getMatch().toString();
		   // System.out.println(le);//替换后匹配的东西
		//TsurgeonPattern surgery = Tsurgeon.parseOperation("ok");
		//.processPattern(p, surgery, t).pennPrint();
		//Tsurgeon.processPattern(m.getMatch(), p, t);
		//TsurgeonPattern surgery = Tsurgeon.parseOperation("");
		//m.getMatch().removeChild(0);
		//m.getMatch().addChild(argTree);
		//m.getNode(le);
		   //System.out.println("OK");
		}
	}
}
代码二:

                 

import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;
import edu.stanford.nlp.trees.tregex.tsurgeon.Tsurgeon;
import edu.stanford.nlp.trees.tregex.tsurgeon.TsurgeonPattern;

//http://codego.net/196118/
//https://deerchao.net/tutorials/regex/regex.htm#mission 正则表达式入门教程
public class Demo2 {
	public static void main(String []args){
		Tree t = Tree.valueOf("(ROOT(NP(NP (DT An) (NNP NP))(PP (IN over)(NP(NP (DT an) (NNP NN))(SBAR(WHNP (WDT that))(S(VP (VBZ is)(PP (IN over)(NP (NN dog))))))))(. .)))");
		String s = "NP < NN | < NNS";
		//Tree argTree =Tree.valueOf("(ROOT(NP (NN arg0)))");
		//输出匹配的内容
		TregexPattern p = TregexPattern.compile(s);
		TregexMatcher m = p.matcher(t);
		
		
		//TsurgeonPattern surgery = Tsurgeon.parseOperation("");
		//Tsurgeon.processPattern (p,surgery,t).pennPrint();
		/*
		 Tree conjSubTree = m.getNode("conj");
		   System.out.println(conjSubTree.value());
		   */
		//TsurgeonPattern surgery = Tsurgeon.parseOperation("excise remove remove");
		//Tsurgeon.processPattern (p,surgery,t).pennPrint();
		
		while (m.find()) {
		m.getMatch().pennPrint();
		String le=m.getMatch().toString();
		
	
		//m.getMatch().removeChild(0);
		//m.getMatch().addChild(argTree);
		//m.getNode(le);
		// Tree xx=m; 
		// System.out.println(xx.value());//输出要找的那部分
		   //System.out.println("OK");
		}
		/*
	Tree t = Tree.valueOf("(ROOT(NP(NP (DT An) (NNP NP))(PP (IN over)(NP(NP (DT an) (NNP NN))(SBAR(WHNP (WDT that))(S(VP (VBZ is)(PP (IN over)(NP (NN dog))))))))(. .)))");
	TregexPattern pat = TregexPattern.compile("NP < (NN < dog) $ (VP <<# (barks > VBZ))");
	TsurgeonPattern surgery = Tsurgeon.parseOperation("excise remove remove");
	Tsurgeon.processPattern (pat,surgery,t).pennPrint();
	*/
	}
}