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 |
import edu.stanford.nlp.ie.AbstractSequenceClassifier; import edu.stanford.nlp.ie.crf.CRFClassifier; import edu.stanford.nlp.ling.CoreLabel; /** * * <p> * ClassName ExtractDemo * </p> * <p> * Description 加载NER模块 * </p> * * @author wangxu [email protected] * <p> * Date 2015年1月8日 下午2:53:45 * </p> * @version V1.0.0 * */ public class ExtractDemo { private static AbstractSequenceClassifier<CoreLabel> 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!"); } } |
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; /** * * <p> * ClassName ZH_SegDemo * </p> * <p> * Description 使用Stanford CoreNLP进行中文分词 * </p> * * @author wangxu [email protected] * <p> * Date 2015年1月8日 下午1:56:54 * </p> * @version V1.0.0 * */ public class ZH_SegDemo { public static CRFClassifier<CoreLabel> 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<CoreLabel>(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(); } } } |
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].
<MISC>2008年 9月 9日 新华网 9月 8日</MISC> 信息 : ( 记者 <PERSON>张家伟</PERSON> ) <GPE>澳门</GPE> <LOC>特区</LOC> <ORG>政府 卫生局 疾病 预防 及 控制 中心</ORG> <MISC>8 日</MISC> 表示 , 目前 累计 有 141 人 在 本地 自助 餐厅 进食 后 出现 食物 中毒 症状 , 其中 大部分 与 进食 “ 问题 生蚝 ” 有关 。 <ORG>卫生局</ORG> 最早 在 3 日 公布 说 , 有 14 名 来自 <MISC>三</MISC> 个 群体 的 <GPE>港 澳</GPE> 人士 <MISC>8月 27日 至 30日</MISC> 期间 在 <GPE>澳门</GPE> 金沙 酒店 用 餐后 出现 不适 , 患者 陆续 出现 发热 、 呕吐 和 腹泻 等类 诺沃克 样 病毒 感染 的 症状 。 初步 调查 显示 , “ 上述 情况 可能 和 进食 生蚝 有关 ” 。
Complete!
|