Stanford Named Entity Recognizer (NER)是斯坦福大学自然语言研究小组发布的成果之一,
其主页是:http://nlp.stanford.edu/software/CRF-NER.shtml
Stanford NER 是一个Java实现的命名实体识别(以下简称NER)程序。NER将文本中的实体按类标记出来,例如人名,公司名,地区,基因和蛋白质的名字等。
NER基于一个训练而得的Model工作,用于训练的数据即大量人工标记好的文本,理论上用于训练的数据量越大,NER的识别效果就越好。
斯坦福小组给出了三个训练好的Model:
- Location, Person, Organization
- Location, Person, Organization, Misc
- Time, Location, Organization, Person, Money, Percent, Date
但不幸的是,这三个Model都不能被扩展,用于训练的数据也不公开,所以想要一个适应自己需求的Model我们只能从头训练。
下面我们就用一个简单的例子来摸索一下如何训练一个新的Model并用它尝试识别几个简单的句子。
首先从主页下载Stanford Named Entity Recognizer:http://nlp.stanford.edu/software/stanford-ner-2013-11-12.zip 解压即可。
文中提到的训练数据、配置文件、Model和完整工程可从网盘下载:http://pan.baidu.com/s/1xNAqD
准备训练数据
这是本例所使用的训练数据,包含标记好的两句话:Today is Friday. Tomorrow is 11/30.
如图,句子中的每个单词独立成行,Tab后跟该单词的类别,默认为O。例中,我们标记了Friday为WEEK,11/30为DATE,其余均为默认。
训练获得Model
官方说虽然所有的参数都可以通过命令行的方式指定,但更推荐用配置文件的方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #location of the training file trainFile = testdata . tsv #location where you would like to save (serialize to) your #classifier; adding .gz at the end automatically gzips the file, #making it faster and smaller serializeTo = ner - model . ser . gz #structure of your training file; this tells the classifier #that the word is in column 0 and the correct answer is in #column 1 map = word = 0 , answer = 1 #these are the features we'd like to train with #some are discussed below, the rest can be #understood by looking at NERFeatureFactory useClassFeature = true useWord = true useNGrams = true #no ngrams will be included that do not contain either the #beginning or end of the word noMidNGrams = true useDisjunctive = true maxNGramLeng = 6 usePrev = true useNext = true useSequences = true usePrevSequences = true maxLeft = 1 #the next 4 deal with word shape features useTypeSeqs = true useTypeSeqs2 = true useTypeySequences = true wordShape = chris2useLC |
其中trainFile =testdata.tsv指定了我们用于训练的数据,serializeTo = ner-model.ser.gz指定了输出model的名字,其余参数也均有注释说明。
我们将该配置文件austen.prop和训练数据testdata.tsv都放到stanford-ner-2013-11-12文件夹中。
cmd中将当前目录切换到stanford-ner-2013-11-12文件夹,并执行命令:
java -cp stanford-ner.jar edu.stanford.nlp.ie.crf.CRFClassifier -prop austen.prop
执行成功后,我们就可以看到目录下多了ner-model.ser.gz,这就是通过我们的训练数据得到的model。
尝试识别
将刚刚生成的ner-model.ser.gz放到工程根目录下,在代码中获取以后,尝试用它解析我们的两个输入:
1. The next day is Friday
2. Today is 11/29
运行后得到:
The/O next/O day/O is/O Friday/WEEK
Today/O is/O 11/29/DATE
可以看到测试数据中的Friday和11/29都被识别了出来。
注意:在运行这个例子的时候有可能不成功,不成功的原因可能是因为JDK的版本问题,在主页中有一句话“ Stanford NER requires Java v1.8+.”,因此,JDK的版本需要1.8,看看自己的JDK的版本是否符合。