使用Stanford Word Segmenter and Stanford Named Entity Recognizer (NER)實現中文命名實體識

一、分詞介紹

斯坦福大學的分詞器,該系統需要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介紹

斯坦福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.sta nford.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! 
這段說明,很清晰,需要將中文分詞的結果作爲NER的輸入,然後才能識別出NER來。
同時便於測試,本Demo使用junit-4.10.jar,下面開始上代碼
[java]  view plain  copy
  1. import edu.stanford.nlp.ie.AbstractSequenceClassifier;   
  2. import edu.stanford.nlp.ie.crf.CRFClassifier;   
  3. import edu.stanford.nlp.ling.CoreLabel;   
  4.   
  5. /**  
  6.  
  7. * <p>  
  8. * ClassName ExtractDemo  
  9. * </p>  
  10. * <p>  
  11. * Description 加載NER模塊  
  12. * </p>  
  13.  
  14. * @author wangxu [email protected]  
  15. * <p>  
  16. * Date 2015年1月8日 下午2:53:45  
  17. * </p>  
  18. * @version V1.0.0  
  19.  
  20. */   
  21. public class ExtractDemo {   
  22. private static AbstractSequenceClassifier<CoreLabel> ner;   
  23. public ExtractDemo() {   
  24. InitNer();   
  25. }   
  26. public void InitNer() {   
  27. String serializedClassifier = "classifiers/chinese.misc.distsim.crf.ser.gz"// chinese.misc.distsim.crf.ser.gz   
  28. if (ner == null) {   
  29. ner = CRFClassifier.getClassifierNoExceptions(serializedClassifier);   
  30. }   
  31. }   
  32.   
  33. public String doNer(String sent) {   
  34. return ner.classifyWithInlineXML(sent);   
  35. }   
  36.   
  37. public static void main(String args[]) {   
  38. String str = "我 去 吃飯 , 告訴 李強 一聲 。";   
  39. ExtractDemo extractDemo = new ExtractDemo();   
  40. System.out.println(extractDemo.doNer(str));   
  41. System.out.println("Complete!");   
  42. }   
  43.   
  44. }   
[java]  view plain  copy
  1. import java.io.File;   
  2. import java.io.IOException;   
  3. import java.util.Properties;   
  4.   
  5. import org.apache.commons.io.FileUtils;   
  6.   
  7. import edu.stanford.nlp.ie.crf.CRFClassifier;   
  8. import edu.stanford.nlp.ling.CoreLabel;   
  9.   
  10. /**  
  11.  
  12. * <p>  
  13. * ClassName ZH_SegDemo  
  14. * </p>  
  15. * <p>  
  16. * Description 使用Stanford CoreNLP進行中文分詞  
  17. * </p>  
  18.  
  19. * @author wangxu [email protected]  
  20. * <p>  
  21. * Date 2015年1月8日 下午1:56:54  
  22. * </p>  
  23. * @version V1.0.0  
  24.  
  25. */   
  26. public class ZH_SegDemo {   
  27. public static CRFClassifier<CoreLabel> segmenter;   
  28. static {   
  29. // 設置一些初始化參數   
  30. Properties props = new Properties();   
  31. props.setProperty("sighanCorporaDict""data");   
  32. props.setProperty("serDictionary""data/dict-chris6.ser.gz");   
  33. props.setProperty("inputEncoding""UTF-8");   
  34. props.setProperty("sighanPostProcessing""true");   
  35. segmenter = new CRFClassifier<CoreLabel>(props);   
  36. segmenter.loadClassifierNoExceptions("data/ctb.gz", props);   
  37. segmenter.flags.setProperties(props);   
  38. }   
  39.   
  40. public static String doSegment(String sent) {   
  41. String[] strs = (String[]) segmenter.segmentString(sent).toArray();   
  42. StringBuffer buf = new StringBuffer();   
  43. for (String s : strs) {   
  44. buf.append(s + " ");   
  45. }   
  46. System.out.println("segmented res: " + buf.toString());   
  47. return buf.toString();   
  48. }   
  49.   
  50. public static void main(String[] args) {   
  51. try {   
  52. String readFileToString = FileUtils.readFileToString(new File("澳門141人食物中毒與進食「問題生蠔」有關.txt"));   
  53. String doSegment = doSegment(readFileToString);   
  54. System.out.println(doSegment);   
  55.   
  56. ExtractDemo extractDemo = new ExtractDemo();   
  57. System.out.println(extractDemo.doNer(doSegment));   
  58.   
  59. System.out.println("Complete!");   
  60. catch (IOException e) {   
  61. e.printStackTrace();   
  62. }   
  63.   
  64. }   
  65. }   
注意一定是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!

轉載:
http://blog.csdn.net/sparkexpert/article/details/49497231

http://blog.csdn.net/jdbc/article/details/51382262
http://blog.csdn.net/haoji007/article/details/52788676