中文命名實體識別

1、分詞介紹

http://nlp.stanford.edu/software/segmenter.shtmlhtml

斯坦福大學的分詞器,該系統須要JDK 1.8+,從上面連接中下載stanford-segmenter-2014-10-26,解壓以後,以下圖所示java

,進入data目錄,其中有兩個gz壓縮文件,分別是ctb.gz和pku.gz,其中CTB:賓州大學的中國樹庫訓練資料 ,PKU:中國北京大學提供的訓練資料。固然了,你也能夠本身訓練,一個訓練的例子能夠在這裏面看到http://nlp.stanford.edu/software/trainSegmenter-20080521.tar.gzapache

2、NER介紹

http://nlp.stanford.edu/software/CRF-NER.shtmlapp

斯坦福NER是採用Java實現,能夠識別出(PERSON,ORGANIZATION,LOCATION),使用本軟件發表的研究成果需引用下述論文:ide

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. 測試

下載地址在:ui

http://nlp.stanford.edu/~manning/papers/gibbscrf3.pdfspa

在NER頁面能夠下載到兩個壓縮文件,分別是stanford-ner-2014-10-26和stanford-ner-2012-11-11-chinese.net

將兩個文件解壓可看到orm

,默認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,下面開始上代碼

view plaincopy to clipboardprint?

  1. 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 wangx89@126.com * <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!"); } }   

view plaincopy to clipboardprint?

  1. 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 wangx89@126.com * <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& lt;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(); } } }   

注意必定是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].

<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!

相關文章
相關標籤/搜索