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

首次处理英文语料,需要进行一些基础的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":"."}]

时间: 2024-08-03 21:20:44

将Stanford CoreNLP的解析结果构造为json格式的相关文章

WebLoad 解析服务器返回的JSON格式内容

服务器返回Json格式的响应内容经常是以 String (txt) 形式返回给客户端.客户端需要把 文本形式的内容还原为Json格式以进一步做处理(如,取得返回内容的一个值作为下个请求的一个输入).这就要用到 一个函数 evel(). 具体做法如下: Parsing the JSON Response 1. In the InitAgenda() function in the Agenda, define the global variable values of the SaveSource

python解析url返回的json格式数据

1.python代码# --*-- coding=utf-8 --*--import urllib2import urllibimport json weatherHtml = urllib.urlopen('http://songsearch.kugou.com/song_search_v2?keyword=周杰伦&pagesize=1')#通过urllib模块中的urlopen的方法打开urlweatherHtml1 = weatherHtml.read()#通过read方法获取返回数据pr

android 解析文章,通过JSON格式请求传递 的好文章,这里记录一下

http://blog.sina.com.cn/s/blog_8d955f8c0100xv7i.html http://blog.163.com/zhangzheming_282/blog/static/117920962012112725957443/ http://blog.163.com/zhangzheming_282/blog/static/11792096201211272536399/

利用 js-xlsx 实现 Excel 文件导入并解析Excel数据成json格式的数据并且获取其中某列数据

演示效果参考如下:XML转JSON 另一个搭配SQL实现:http://sheetjs.com/sexql/index.html 详细介绍: 1.首先需要导入js <script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script> 2.导入文件框 这里importExcel(this)是方法,名字可以自己定义. <input type="file"onchang

android学习二十三(解析json格式数据)

前面我们已经掌握了xml格式数据的解析方式,那么接下来我们学习下如何解析json格式的数据.比起xml,json的主要优势在于它体积更小,在网络上传输的时候可以更省流量.但缺点在于,它的语义性差,看起来不如xml直观. 开始之前,先在自己的电脑apache-tomcat-6.0.39\webapps\ROOT路径目录下新建一个get_data.json的文件,然后编辑这个文件,并加入如下json格式的内容: [{"id":"5","version"

Stanford Corenlp学习笔记——词性标注

使用Stanford Corenlp对中文进行词性标注 语言为Scala,使用的jar的版本是3.6.0,而且是手动添加jar包,使用sbt添加其他版本的时候出现了各种各样的问题 添加的jar包有5个 代码 import edu.stanford.nlp.pipeline.{Annotation, StanfordCoreNLP} /** * Created by common on 17-5-13. */ object NLPLearning { def main(args: Array[St

stanford corenlp自定义切词类

stanford corenlp的中文切词有时不尽如意,那我们就需要实现一个自定义切词类,来完全满足我们的私人定制(加各种词典干预).上篇文章<IKAnalyzer>介绍了IKAnalyzer的自由度,本篇文章就说下怎么把IKAnalyzer作为corenlp的切词工具. <stanford corenlp的TokensRegex>提到了corenlp的配置CoreNLP-chinese.properties,其中customAnnotatorClass.segment就是用于指定

Eclipse下使用Stanford CoreNLP的方法

源码下载地址:CoreNLP官网. 目前release的CoreNLP version 3.5.0版本仅支持java-1.8及以上版本,因此有时需要为Eclipse添加jdk-1.8配置,配置方法如下: 首先,去oracle官网下载java-1.8,下载网址为:java下载,安装完成后. 打开Eclipse,选择Window -> Preferences -> Java –> Installed JREs 进行配置: 点击窗体右边的“add”,然后添加一个“Standard VM”(应该

C#深入解析Json格式内容

继上一篇<浅谈C#手动解析Json格式内容>我又来分析加入了一些功能让 这个解析类更实用 本章节最会开放我最终制作成功的Anonymous.Json.dll这个解析库 需要的拿走~ 功能继上一篇增加了许多上一篇只是讲述了  解析的步骤但是 至于一些扩展的功能却没有涉及 本文将继续讲解 1.如何将json转换为一个类或者结构 甚至属性 2.如何将一个类或者结构甚至属性转换为json 就这两点就已经很头疼了 诶 废话不多说进入正题 上一篇一直有个很神秘的JsonObject没有讲解 现在先来揭开J