OpenNLP Tokenizers分割一个输入字符序列为tokens。Tokens通常是一个单词,标点符号,数字等等。
Pierre Vinken, 61 years old, will join the board as a nonexecutive director Nov. 29.
Mr. Vinken is chairman of Elsevier N.V., the Dutch publishing group.
Rudolph Agnew, 55 years old and former chairman of Consolidated Gold Fields
PLC, was named a director of this British industrial conglomerate.
The following result shows the individual tokens in a whitespace separated representation.
Pierre Vinken , 61 years old , will join the board as a nonexecutive director Nov. 29 .
Mr. Vinken is chairman of Elsevier N.V. , the Dutch publishing group .
Rudolph Agnew , 55 years old and former chairman of Consolidated Gold Fields PLC ,
was named a nonexecutive director of this British industrial conglomerate .
A form of asbestos once used to make Kent cigarette filters has caused a high
percentage of cancer deaths among a group of workers exposed to it more than 30 years ago ,
researchers reported .
OpenNLP提供了多个Tokenizer实现:
大多数词性标注(part-of-speech taggings),句法分析(parsers)等,以这种方式使用文本tokenized工作。确保你的tokenizer产生期望的tokens类型,使用later文本处理组件是非常重要的。
使用OpenNLP(和其他许多系统),tokenization是一个两个阶段的处理:首先,识别句子边界,然后识别其中每一个句子的tokens。
Tokenizers可以通过它定义的API集成到一个应用程序。WhitespaceTokenizer的共享示例可以通过静态字段WhitespaceTokenizer.INSTANCE得到。SimpleTokenizer的共享实例可以使用同样的方式从SimpleTokenizer.INSTANCE得到。在实例化TokenizerME(learnable Tokenizer)前,必须先创建一个Token模型。下面的代码示例展示了怎样加载一个模型。
InputStream modelIn = new FileInputStream("en-token.bin");
try {
TokenizerModel model = new TokenizerModel(modelIn);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (modelIn != null) {
try {
modelIn.close();
}
catch (IOException e) {
}
}
}
在模型加载后,可以实例化TokenizerME。
Tokenizer tokenizer = new TokenizerME(model);
Tokenizer提供两个Tokenize方法,两个方法都期望一个包含未被Tokenized的文本的输入String对象。如果可能最好是一个句子,但是取决于learnable Tokenizer的训练,这不是必须的。第一个返回一个String数组,数组中每一个String是一个token。
String tokens[] = tokenizer.tokenize("An input sample sentence.");
输出是一个包含这些tokens的数组。
"An", "input", "sample", "sentence", "."
第二个方法,TokenizePos方法返回一个Span数组,每一个Span包含这个输入String的tokens的开始和结束字符偏移量。
Span tokenSpans[] = tokenizer.tokenizePos("An input sample sentence.");
这个tokenSpans数组有5个元素。调用Span.getCoveredText得到一个span的文本,它得到一个Span和输入的文本。TokenizerME可以输出被检测的tokens的概率。getTokenProbabilities 方法必须立即调用,在tokenize的方法被调用之后。
TokenizerME tokenizer = ...
String tokens[] = tokenizer.tokenize(...);
double tokenProbs[] = tokenizer.getTokenProbabilities();
tokenProbs 数组的每一个token在线包括一个double值,这个值大小从0到1,1是最大的可能的概率,0是最小的可能的概率值。
Tokenizer提供了API来训练新的tokenization模型。训练需要三个基本步骤:
下面的示例代码解释了这三步:
Charset charset = Charset.forName("UTF-8");
ObjectStream<String> lineStream = new PlainTextByLineStream(new FileInputStream("en-sent.train"),
charset);
ObjectStream<TokenSample> sampleStream = new TokenSampleStream(lineStream);
TokenizerModel model;
try {
model = TokenizerME.train("en", sampleStream, true, TrainingParameters.defaultParams());
}
finally {
sampleStream.close();
}
OutputStream modelOut = null;
try {
modelOut = new BufferedOutputStream(new FileOutputStream(modelFile));
model.serialize(modelOut);
} finally {
if (modelOut != null)
modelOut.close();
}