斯坦福大学命名实体识别

一、分词介绍
http://nlp.stanford.edu/software/segmenter.shtml
斯坦福大学的分词器,该系统需要JDK 1.8+,从上面链接中下载stanford-segmenter-2014-10-26,解压之后,如下图所示
,进入data目录,其中有两个gz压缩文件,分别是ctb.gz和pku.gz,其中CTB:宾州大学的中国树库训练资料 ,PKU:中国北京大学提供的训练资料。当然了,你也可以自己训练,一个训练的例子可以在这里面看到http://nlp.stanford.edu/software/trainSegmenter-20080521.tar.gz
二、NER介绍
http://nlp.stanford.edu/software/CRF-NER.shtml
斯坦福NER是采用Java实现,可以识别出(PERSON,ORGANIZATION,LOCATION),使用本软件发表的研究成果需引用下述论文:
Jenny Rose Finkel, Trond Grenager, and Christopher Manning. 2005. Incorporating Non-local Information into Information Extraction Systems by Gibbs Sampling. Proceedings of the 43nd Annual Meeting of the Association for Computational Linguistics (ACL 2005), pp. 363-370.
下载地址在:
http://nlp.stanford.edu/~manning/papers/gibbscrf3.pdf
在NER页面可以下载到两个压缩文件,分别是stanford-ner-2014-10-26和stanford-ner-2012-11-11-chinese
将两个文件解压可看到
,默认NER可以用来处理英文,如果需要处理中文要另外处理。
Included with Stanford NER are a 4 class model trained for CoNLL, a 7 class model trained for MUC, and a 3 class model trained on both data sets for the intersection of those class sets.
3 class: Location, Person, Organization
4 class: Location, Person, Organization, Misc
7 class: Time, Location, Organization, Person, Money, Percent, Date 如上图可以看到针对英文提供了3class、4class、7class,http://nlp.stanford.edu:8080/ner/ 但是中文并没有,这是一个在线演示的地址,可以上去瞧瞧 。

三、分词和NER使用
在Eclipse中新建一个Java Project,将data目录拷贝到项目根路径下,再把stanford-ner-2012-11-11-chinese解压的内容全部拷贝到classifiers文件夹下,将stanford-segmenter-3.5.0加入到classpath之中,将classifiers文件夹拷贝到项目根目录,将stanford-ner-3.5.0.jar和stanford-ner.jar加入到classpath中。最后,去http://nlp.stanford.edu/software/corenlp.shtml下载stanford-corenlp-full-2014-10-31,将解压之后的stanford-corenlp-3.5.0也加入到classpath之中。最后的Eclipse中结构如下:

根据
We also provide Chinese models built from the Ontonotes Chinese named entity data. There are two models, one using distributional similarity clusters and one without. These are designed to be run on word-segmented Chinese. So, if you want to use these on normal Chinese text, you will first need to run Stanford Word Segmenter or some other Chinese word segmenter, and then run NER on the output of that!
Chinese NER
这段说明,很清晰,需要将中文分词的结果作为NER的输入,然后才能识别出NER来。
同时便于测试,本Demo使用junit-4.10.jar,下面开始上代码
[java] view plain copy
import edu.stanford.nlp.ie.AbstractSequenceClassifier;
import edu.stanford.nlp.ie.crf.CRFClassifier;
import edu.stanford.nlp.ling.CoreLabel;

/**
*
*


* ClassName ExtractDemo
*


*


* Description 加载NER模块
*


*
* @author wangxu wangx89@126.com
*


* Date 2015年1月8日 下午2:53:45
*


* @version V1.0.0
*
*/
public class ExtractDemo {
private static AbstractSequenceClassifier ner;
public ExtractDemo() {
InitNer();
}
public void InitNer() {
String serializedClassifier = “classifiers/chinese.misc.distsim.crf.ser.gz”; // chinese.misc.distsim.crf.ser.gz
if (ner == null) {
ner = CRFClassifier.getClassifierNoExceptions(serializedClassifier);
}
}

public String doNer(String sent) {
return ner.classifyWithInlineXML(sent);
}

public static void main(String args[]) {
String str = “我 去 吃饭 , 告诉 李强 一声 。”;
ExtractDemo extractDemo = new ExtractDemo();
System.out.println(extractDemo.doNer(str));
System.out.println(“Complete!”);
}

}
[java] view plain copy
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.io.FileUtils;

import edu.stanford.nlp.ie.crf.CRFClassifier;
import edu.stanford.nlp.ling.CoreLabel;

/**
*
*


* ClassName ZH_SegDemo
*


*


* Description 使用Stanford CoreNLP进行中文分词
*


*
* @author wangxu wangx89@126.com
*


* Date 2015年1月8日 下午1:56:54
*


* @version V1.0.0
*
*/
public class ZH_SegDemo {
public static CRFClassifier segmenter;
static {
// 设置一些初始化参数
Properties props = new Properties();
props.setProperty(“sighanCorporaDict”, “data”);
props.setProperty(“serDictionary”, “data/dict-chris6.ser.gz”);
props.setProperty(“inputEncoding”, “UTF-8”);
props.setProperty(“sighanPostProcessing”, “true”);
segmenter = new CRFClassifier(props);
segmenter.loadClassifierNoExceptions(“data/ctb.gz”, props);
segmenter.flags.setProperties(props);
}

public static String doSegment(String sent) {
String[] strs = (String[]) segmenter.segmentString(sent).toArray();
StringBuffer buf = new StringBuffer();
for (String s : strs) {
buf.append(s + ” “);
}
System.out.println(“segmented res: ” + buf.toString());
return buf.toString();
}

public static void main(String[] args) {
try {
String readFileToString = FileUtils.readFileToString(new File(“澳门141人食物中毒与进食“问题生蚝”有关.txt”));
String doSegment = doSegment(readFileToString);
System.out.println(doSegment);

ExtractDemo extractDemo = new ExtractDemo();
System.out.println(extractDemo.doNer(doSegment));

System.out.println(“Complete!”);
} catch (IOException e) {
e.printStackTrace();
}

}
}
注意一定是JDK 1.8+的环境,最后输出结果如下
loading dictionaries from data/dict-chris6.ser.gz…Done. Unique words in ChineseDictionary is: 423200
done [23.2 sec].
serDictionary=data/dict-chris6.ser.gz
sighanCorporaDict=data
inputEncoding=UTF-8
sighanPostProcessing=true
INFO: TagAffixDetector: useChPos=false | useCTBChar2=true | usePKChar2=false
INFO: TagAffixDetector: building TagAffixDetector from data/dict/character_list and data/dict/in.ctb
Loading character dictionary file from data/dict/character_list
Loading affix dictionary from data/dict/in.ctb
segmented res: 2008年 9月 9日 新华网 9月 8日 信息 : ( 记者 张家伟 ) 澳门 特区 政府 卫生局 疾病 预防 及 控制 中心 8 日 表示 , 目前 累计 有 141 人 在 本地 自助 餐厅 进食 后 出现 食物 中毒 症状 , 其中 大部分 与 进食 “ 问题 生蚝 ” 有关 。 卫生局 最早 在 3 日 公布 说 , 有 14 名 来自 三 个 群体 的 港 澳 人士 8月 27日 至 30日 期间 在 澳门 金沙 酒店 用 餐后 出现 不适 , 患者 陆续 出现 发热 、 呕吐 和 腹泻 等类 诺沃克 样 病毒 感染 的 症状 。 初步 调查 显示 , “ 上述 情况 可能 和 进食 生蚝 有关 ” 。
2008年 9月 9日 新华网 9月 8日 信息 : ( 记者 张家伟 ) 澳门 特区 政府 卫生局 疾病 预防 及 控制 中心 8 日 表示 , 目前 累计 有 141 人 在 本地 自助 餐厅 进食 后 出现 食物 中毒 症状 , 其中 大部分 与 进食 “ 问题 生蚝 ” 有关 。 卫生局 最早 在 3 日 公布 说 , 有 14 名 来自 三 个 群体 的 港 澳 人士 8月 27日 至 30日 期间 在 澳门 金沙 酒店 用 餐后 出现 不适 , 患者 陆续 出现 发热 、 呕吐 和 腹泻 等类 诺沃克 样 病毒 感染 的 症状 。 初步 调查 显示 , “ 上述 情况 可能 和 进食 生蚝 有关 ” 。
Loading classifier from E:\workspaces\EclipseEE4.4\aaaaaa\classifiers\chinese.misc.distsim.crf.ser.gz … done [6.8 sec].
2008年 9月 9日 新华网 9月 8日 信息 : ( 记者 张家伟 ) 澳门 特区 政府 卫生局 疾病 预防 及 控制 中心 8 日 表示 , 目前 累计 有 141 人 在 本地 自助 餐厅 进食 后 出现 食物 中毒 症状 , 其中 大部分 与 进食 “ 问题 生蚝 ” 有关 。 卫生局 最早 在 3 日 公布 说 , 有 14 名 来自 三 个 群体 的 港 澳 人士 8月 27日 至 30日 期间 在 澳门 金沙 酒店 用 餐后 出现 不适 , 患者 陆续 出现 发热 、 呕吐 和 腹泻 等类 诺沃克 样 病毒 感染 的 症状 。 初步 调查 显示 , “ 上述 情况 可能 和 进食 生蚝 有关 ” 。
Complete!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值