将Stanford CoreNLP的解析结果构造为json格式

7 篇文章 0 订阅
1 篇文章 0 订阅

首次处理英文语料,需要进行一些基础的NLP处理,首选工具当然是Stanford CoreNLP。由于Stanford CoreNLP官方示例的解析结果不宜直接使用,所以我在它的基础上进行修改,最终将解析结果转为json格式,并依照哈工大ltp的解析结果的格式,将依存句法的解析结果也添加到json中。

1、Stanford CoreNLP的安装

最新版的Stanford CoreNLP仅支持jdk1.8,这比较奇葩,因为目前多数机器的jdk还只是1.6或1.7,最以我下载了支持jdk1.6的最后一个版本,地址:http://nlp.stanford.edu/software/stanford-corenlp-full-2014-08-27.zip 。下载完成后,将解压后的所有内容放到(Eclipse)项目的根目录下,通过Build Path将所有的jar包添加到项目库中,即可完成安装配置。解压后的目录中有一个名为StanfordCoreNlpDemo.java的示例文件,简洁地展示了如何使用此工具,但是它使用的结果显示方式是prettyPrint,这种结果只便于人来看,而不便于机器来获取。所以我以 http://www.cnblogs.com/tec-vegetables/p/4153144.html所示的例子来基础来改写代码。
2、代码,有详细解释
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.json.JSONArray;
import edu.stanford.nlp.dcoref.CorefChain;
import edu.stanford.nlp.ling.CoreAnnotations.CharacterOffsetBeginAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.CharacterOffsetEndAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.LemmaAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.NamedEntityTagAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.PartOfSpeechAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TextAnnotation;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.semgraph.SemanticGraph;
import edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation;
import edu.stanford.nlp.util.CoreMap;

public class TestCoreNLP 
{
	//参数text为需要处理的句子
	public static void run(String text)
	{
		//创建一个corenlp对象,设置需要完成的任务。
		//tokenize: 分词;ssplit:分句;pos:词性标注;lemma:获取词原型;parse:句法解析(含依存句法);dcoref:同义指代    	
		Properties props = new Properties();
		props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
		StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

		// 创建一个基于参数句子的标注对象
		Annotation document = new Annotation(text);

		// 将上述标注对象将对corenlp进行处理
		pipeline.annotate(document);

		// 获取处理结果
		List<CoreMap> sentences = document.get(SentencesAnnotation.class);

		//遍历所有句子,输出每一句的处理结果        
		for(CoreMap sentence: sentences)
		{
			//遍历句子中每一个词,获取其解析结果并构造json数据
			JSONArray jsonSent = new JSONArray(); //创建一个json数组,用于保存当前句子的最终所有解析结果
			int id=1;//当前词在句子中的id,从1开始,因为原始的解析结果就是从1开始的。

			//先获取当前句子的依存句法分析结果			
			SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
			//遍历每一个词
			for (CoreLabel token: sentence.get(TokensAnnotation.class))
			{
				//获取每个词的分析结果				
				Map mapWord = new HashMap();//创建一个map对象,用于保存当前词的解析结果
				mapWord.put("id", id);// 添加id值
				mapWord.put("cont", token.get(TextAnnotation.class));//添加词内容
				mapWord.put("pos", token.get(PartOfSpeechAnnotation.class));//添加词性标注值
				mapWord.put("ner", token.get(NamedEntityTagAnnotation.class));//添加实体识别值
				mapWord.put("lemma", token.get(LemmaAnnotation.class));//添加词原型
				mapWord.put("charBegin",token.get(CharacterOffsetBeginAnnotation.class));//添加词在句子中的起始位置
				mapWord.put("charEnd",token.get(CharacterOffsetEndAnnotation.class));//添加词在句子中的结束位置
				
				//查找每个词对应的依存关系。由于原始的解析结果中,依存关系是单独地集中在另一个字符串变量中的,形如: 依存关系名(被依赖词-被依赖词id,依赖词-依赖词id)\n 依存关系名(被依赖词-被依赖词id,依赖词-依赖词id)\n......需要对其进行解析,这里采用的方法是依据\n进行分割,然后再用正则表达式进行匹配,来逐一获取每一个词的依赖词和依存关系名
				int flag=0;//设置标志位,用于保存当前词的依存关系是否已经处理过,0未处理,1已处理
				String[] dArray= (dependencies.toString(SemanticGraph.OutputFormat.LIST)).split("\n");//根据\n进行分割,结果保存为字符串数组
				for (int i=0;i<dArray.length;i++) //遍历字符串数组
				{
					if(flag==1) //检查当前词的依存关系是否已经处理过,如果已处理,则直接退出遍历过程
						break; 
					ArrayList dc=getDependencyContnet(dArray[i]);//获取数组中第i项,并从中获取依存关系名,被依赖词id和依赖词id,放到一个ArrayList中
					if( Integer.parseInt(String.valueOf(dc.get(2)))==id) //如果当前词id等于当前依存关系中的依赖词id,则说明找到对应的关系结构
					{
						mapWord.put("relation",dc.get(0));//添加依存关系名
						mapWord.put("parent",dc.get(1));//添加被依赖词id
						flag=1; // 将当前词依存关系标志设为1
						break;//退出遍历
					}

				}

				jsonSent.add( mapWord );//将上述结果全部添加到当前句中
				id++;//词id自增
			}
			System.out.println(jsonSent);
			//            // 获取并打印句法解析树
			//            Tree tree = sentence.get(TreeAnnotation.class);
			//            System.out.println("\n"+tree.toString());  

			//            // 获取并打印依存句法的结果
			//			System.out.println("\nDependency Graph:\n " +dependencies.toString(SemanticGraph.OutputFormat.LIST));

			//            // 获取并打印实体指代结果
			//            Map<Integer, CorefChain> graph =  document.get(CorefChainAnnotation.class);
			//    	    System.out.println(graph);
		}
	}

	//解析依存关系值的方法。如,从root(abc-1, efg-3)中获取一个ArrayList,值为[root,1,3]
	public static ArrayList getDependencyContnet(String sent)
	{
		String str=sent;
		ArrayList result=new ArrayList();
		String patternName="(.*)\\(";
		String patternGid="\\(.*-([0-9]*)\\,";
		String patternDid=".*-([0-9]*)\\)";
		Pattern r = Pattern.compile(patternName);
		Matcher m = r.matcher(str);
		if(m.find())
		{
			result.add(m.group(1));
		}
		r=Pattern.compile(patternGid);
		m = r.matcher(str);
		if(m.find())
		{
			result.add(m.group(1));
		}
		r=Pattern.compile(patternDid);
		m = r.matcher(str);
		if(m.find())
		{
			result.add(m.group(1));
		}
		return (result);
	}
}

以“Beijing is the capital of China.”为例,结果为:
[{"id":1,"lemma":"Beijing","relation":"nsubj","parent":"4","ner":"LOCATION","charEnd":7,"cont":"Beijing","charBegin":0,"pos":"NNP"},{"id":2,"lemma":"be","relation":"cop","parent":"4","ner":"O","charEnd":10,"cont":"is","charBegin":8,"pos":"VBZ"},{"id":3,"lemma":"the","relation":"det","parent":"4","ner":"O","charEnd":14,"cont":"the","charBegin":11,"pos":"DT"},{"id":4,"lemma":"capital","relation":"root","parent":"0","ner":"O","charEnd":22,"cont":"capital","charBegin":15,"pos":"NN"},{"id":5,"lemma":"of","ner":"O","charEnd":25,"cont":"of","charBegin":23,"pos":"IN"},{"id":6,"lemma":"China","relation":"prep_of","parent":"4","ner":"LOCATION","charEnd":31,"cont":"China","charBegin":26,"pos":"NNP"},{"id":7,"lemma":".","ner":"O","charEnd":32,"cont":".","charBegin":31,"pos":"."}]


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值