Apache OpenNLP 2.5.12 常用工具与 API 详解

Apache OpenNLP 2.5.12 常用工具与 API 详解:CLI 工具全列表与推理工具实测(SimpleTokenizer/TokenizerME/SentenceDetector/POSTagger/TokenNameFinder/LemmatizerME/LanguageDetector)、训练器工具(含 TokenizerTrainer 自建语料实测)与 Java API 速查(TokenizerME/POSTaggerME/NameFinderME/Span),含注意事项与 FAQ,全程实测。

Apache OpenNLPApache OpenNLP2026/10/12

Apache OpenNLP 2.5.12 常用工具与 API 详解

目标:详解 Apache OpenNLP 2.5.12 的 CLI 工具与 Java API 使用。OpenNLP 提供 bin/opennlp 命令行工具(分词/分句/词性标注/命名实体/词形还原/语言检测/文档分类等推理工具 + 各任务训练器)与 opennlp-tools jar 的 Java API(TokenizerME/POSTaggerME/NameFinderME 等)。本文对每个常用工具给出用法、命令与实测回显,并速查核心 Java API 类。所有命令与输出均在 Ubuntu 22.04.5 + JDK 17 + OpenNLP 2.5.12 环境实测。

一、环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(jammy)
  • Java:OpenJDK 17.0.20.1
  • OpenNLP 版本:2.5.12(安装目录 /opt/opennlp-2.5.12)
  • CLI 入口:/opt/opennlp-2.5.12/bin/opennlp
  • 模型目录:/opt/opennlp/models/
  • 训练数据目录:/opt/opennlp/train/
  • 演示账号:root

二、工具概览

./bin/opennlp 主命令打印全部工具(实测回显原文):

OpenNLP 2.5.12.
Usage: opennlp TOOL
where TOOL is one of:

  Doccat                            Learned document categorizer
  DoccatTrainer                     Trainer for the learnable document categorizer
  DoccatEvaluator                   Measures the performance of the Doccat model with the reference data
  DoccatCrossValidator              K-fold cross validator for the learnable Document Categorizer
  DoccatConverter                   Converts 20newsgroup data format to native OpenNLP format
  LanguageDetector                  Learned language detector
  LanguageDetectorTrainer           Trainer for the learnable language detector
  LanguageDetectorConverter         Converts leipzig data format to native OpenNLP format
  LanguageDetectorCrossValidator    K-fold cross validator for the learnable Language Detector
  LanguageDetectorEvaluator         Measures the performance of the Language Detector model with the reference data
  DictionaryBuilder                 Builds a new dictionary
  SimpleTokenizer                   Character class tokenizer
  TokenizerME                       Learnable tokenizer
  TokenizerTrainer                  Trainer for the learnable tokenizer
  TokenizerMEEvaluator              Measures the performance of the learnable tokenizer
  TokenizerCrossValidator           K-fold cross validator for the learnable tokenizer
  TokenizerConverter                Converts external data formats (irishsentencebank,ad,pos,masc,conllx,namefinder,parse,conllu) to native OpenNLP format
  DictionaryDetokenizer             Normalizes tokenized text according to rules defined in a detokenizer dictionary
  SentenceDetector                  Learnable sentence detector
  SentenceDetectorTrainer           Trainer for the learnable sentence detector
  SentenceDetectorEvaluator         Measures the performance of the learnable sentence detector
  SentenceDetectorCrossValidator    K-fold cross validator for the learnable sentence detector
  SentenceDetectorConverter         Converts external data formats (nkjp,irishsentencebank,ad,pos,masc,conllx,namefinder,parse,moses,conllu,letsmt) to native OpenNLP format
  TokenNameFinder                   Learnable name finder
  TokenNameFinderTrainer            Trainer for the learnable name finder
  TokenNameFinderEvaluator          Measures the performance of the NameFinder model with the reference data
  TokenNameFinderCrossValidator     K-fold cross validator for the learnable Name Finder
  TokenNameFinderConverter          Converts external data formats (evalita,ad,conll03,bionlp2004,conll02,masc,muc6,ontonotes,brat) to native OpenNLP format
  CensusDictionaryCreator           Converts 1990 US Census names into a dictionary
  POSTagger                         Learnable part of speech tagger
  POSTaggerTrainer                  Trains a model for the part-of-speech tagger
  POSTaggerEvaluator                Measures the performance of the POS tagger model with the reference data
  POSTaggerCrossValidator           K-fold cross validator for the learnable POS tagger
  POSTaggerConverter                Converts external data formats (ad,masc,conllx,parse,ontonotes,conllu) to native OpenNLP format
  LemmatizerME                      Learnable lemmatizer
  LemmatizerTrainerME               Trainer for the learnable lemmatizer
  LemmatizerEvaluator               Measures the performance of the Lemmatizer model with the reference data
  ChunkerME                         Learnable chunker
  ChunkerTrainerME                  Trainer for the learnable chunker
  ChunkerEvaluator                  Measures the performance of the Chunker model with the reference data
  ChunkerCrossValidator             K-fold cross validator for the chunker
  ChunkerConverter                  Converts ad data format to native OpenNLP format
  Parser                            Performs full syntactic parsing
  ParserTrainer                     Trains the learnable parser
  ParserEvaluator                   Measures the performance of the Parser model with the reference data
  ParserConverter                   Converts external data formats (ontonotes,frenchtreebank) to native OpenNLP format
  BuildModelUpdater                 Trains and updates the build model in a parser model
  CheckModelUpdater                 Trains and updates the check model in a parser model
  TaggerModelReplacer               Replaces the tagger model in a parser model
  EntityLinker                      Links an entity to an external data set
  NGramLanguageModel                Gives the probability and most probable next token(s) of a sequence of tokens in a language model

All tools print help when invoked with help parameter
Example: opennlp SimpleTokenizer help

工具分类表:

类别工具
分词SimpleTokenizer、TokenizerME(+Trainer/Evaluator/CrossValidator/Converter)
分句SentenceDetector(+Trainer/Evaluator/CrossValidator/Converter)
词性标注POSTagger(+Trainer/Evaluator/CrossValidator/Converter)
命名实体TokenNameFinder(+Trainer/Evaluator/CrossValidator/Converter)
词形还原LemmatizerME(+TrainerME/Evaluator)
语言检测LanguageDetector(+Trainer/Converter/CrossValidator/Evaluator)
文档分类Doccat(+Trainer/Evaluator/CrossValidator/Converter)
其他DictionaryBuilder、DictionaryDetokenizer、ChunkerME、Parser、EntityLinker、NGramLanguageModel、CensusDictionaryCreator
Java APIopennlp-tools jar:TokenizerME/POSTaggerME/NameFinderME 等(见六)

💡 说明:所有工具以 help 参数打印各自帮助(Example: opennlp SimpleTokenizer help);推理工具从标准输入管道读取文本(< sentences / < documents),模型文件作为位置参数传入。

三、前置条件

已按《Ubuntu 22.04 安装 Apache OpenNLP 2.5.12 指南》完成安装与模型下载:

cd /opt/opennlp-2.5.12 && ./bin/opennlp
ls -lh /opt/opennlp/models/

模型清单(实测回显):

-rw-r--r-- 1 root root 5.0M en-ner-person.bin
-rw-r--r-- 1 root root  97K en-sent.bin
-rw-r--r-- 1 root root 430K en-token.bin
-rw-r--r-- 1 root root 509K opennlp-en-ud-ewt-lemmas-1.3-2.5.4.bin
-rw-r--r-- 1 root root 1.2M opennlp-en-ud-ewt-pos-1.3-2.5.4.bin
-rw-r--r-- 1 root root  20K opennlp-en-ud-ewt-sentence-1.3-2.5.4.bin
-rw-r--r-- 1 root root 319K opennlp-en-ud-ewt-tokens-1.3-2.5.4.bin
-rw-r--r-- 1 root root 141K langdetect-183.bin

四、推理 CLI 工具详解

各推理工具用法行(实测,./bin/opennlp <Tool> help):

Usage: opennlp SimpleTokenizer < sentences
Usage: opennlp TokenizerME model < sentences
Usage: opennlp SentenceDetector model < sentences
Usage: opennlp POSTagger model < sentences
Usage: opennlp TokenNameFinder model1 model2 ... modelN < sentences
Usage: opennlp LemmatizerME model < sentences
Usage: opennlp Doccat model < documents
Usage: opennlp LanguageDetector model < documents

4.1 SimpleTokenizer(字符类分词)

无需模型,按字符类别切分:

cd /opt/opennlp-2.5.12
printf "Pierre Vinken, 61 years old, will join the board as a nonexecutive director on November 29. Mr. Vinken is chairman of Elsevier N.V., the Dutch publishing group.\n" | ./bin/opennlp SimpleTokenizer

实测回显:

Pierre Vinken , 61 years old , will join the board as a nonexecutive director on November 29 . Mr . Vinken is chairman of Elsevier N . V . , the Dutch publishing group .
Average: 111.1 sent/s

Total: 1 sent
Runtime: 0.009s
Execution time: 0.030 seconds

4.2 TokenizerME(学习型分词器)

需要分词模型(实测 en-token.bin,传统 demo 模型):

cd /opt/opennlp-2.5.12
printf "Pierre Vinken, 61 years old, will join the board as a nonexecutive director on November 29. Mr. Vinken is chairman of Elsevier N.V., the Dutch publishing group.\n" | ./bin/opennlp TokenizerME /opt/opennlp/models/en-token.bin

实测回显:

Loading Tokenizer model ...
done (0.151s)

Pierre Vinken , 61 years old , will join the board as a nonexecutive director on November 29 . Mr. Vinken is chairman of Elsevier N.V. , the Dutch publishing group .
Average: 100.0 sent/s

Total: 1 sent
Runtime: 0.01s
Execution time: 0.179 seconds

💡 说明:与 SimpleTokenizer 的差异——SimpleTokenizer 把 Mr. 切成 Mr 与 .、把 N.V. 切成 N . V .;TokenizerME 学习后把 Mr.、N.V. 保留为单个 token。

4.3 SentenceDetector(分句)

实测 en-sent.bin(demo 模型):

cd /opt/opennlp-2.5.12
printf "Pierre Vinken, 61 years old, will join the board as a nonexecutive director on November 29. Mr. Vinken is chairman of Elsevier N.V., the Dutch publishing group.\n" | ./bin/opennlp SentenceDetector /opt/opennlp/models/en-sent.bin

实测回显:

Loading Sentence Detector model ...
done (0.066s)

Pierre Vinken, 61 years old, will join the board as a nonexecutive director on November 29.
Mr. Vinken is chairman of Elsevier N.V., the Dutch publishing group.
Average: 500.0 sent/s

Total: 2 sent
Runtime: 0.004s
Execution time: 0.086 seconds

💡 说明:把 1 段输入按句号切分为 2 句,句号后换行输出(Total: 2 sent)。

4.4 POSTagger(词性标注)

实测 UD 词性模型(opennlp-en-ud-ewt-pos-1.3-2.5.4.bin):

cd /opt/opennlp-2.5.12
printf "Pierre Vinken, 61 years old, will join the board as a nonexecutive director on November 29. Mr. Vinken is chairman of Elsevier N.V., the Dutch publishing group.\n" | ./bin/opennlp POSTagger /opt/opennlp/models/opennlp-en-ud-ewt-pos-1.3-2.5.4.bin

实测回显:

Loading POS Tagger model ...
done (0.266s)

Pierre_PROPN Vinken,_PROPN 61_NUM years_NOUN old,_PUNCT will_AUX join_VERB the_DET board_NOUN as_ADP a_DET nonexecutive_ADJ director_NOUN on_ADP November_PROPN 29._PUNCT Mr._PROPN Vinken_PROPN is_AUX chairman_NOUN of_ADP Elsevier_ADJ N.V.,_PROPN the_DET Dutch_PROPN publishing_VERB group._PUNCT
Average: 21.3 sent/s

Total: 1 sent
Runtime: 0.047s
Execution time: 0.365 seconds

💡 说明:输出为 token_TAG 形式;UD 标签集——PROPN(专有名词)、NUM(数词)、NOUN(名词)、ADJ(形容词)、AUX(助动词)、VERB(动词)、DET(限定词)、ADP(介词)、PUNCT(标点)。

4.5 TokenNameFinder(命名实体识别)

用法支持多模型并列(model1 model2 ... modelN);实测人名模型 en-ner-person.bin:

cd /opt/opennlp-2.5.12
printf "Pierre Vinken is the chairman of Elsevier N.V. He was born in Amsterdam in 1931. Alice Smith works at Google in New York.\n" | ./bin/opennlp TokenNameFinder /opt/opennlp/models/en-ner-person.bin

实测回显:

Loading Token Name Finder model ...
done (0.639s)

<START:person> Pierre Vinken <END> is the chairman of Elsevier N.V. He was born in Amsterdam in 1931. Alice Smith works at Google in New York.
Average: 17.5 sent/s

Total: 1 sent
Runtime: 0.057s
Execution time: 0.750 seconds

💡 说明:命中的实体用 <START:person> ... <END> 标记包裹;en-ner-person.bin 只识别人名(person),识别机构/地点需 en-ner-organization.bin、en-ner-location.bin(同一模型站)。

4.6 LemmatizerME(词形还原)

⚠️ 注意:输入必须是 word_TAG(下划线连接词与词性)的 POSSample 格式;纯文本输入会报 Invalid format 错误。

cd /opt/opennlp-2.5.12
printf "The_DET dogs_NOUN are_AUX running_VERB quickly_ADV through_ADP the_DET fields_NOUN ._PUNCT\n" | ./bin/opennlp LemmatizerME /opt/opennlp/models/opennlp-en-ud-ewt-lemmas-1.3-2.5.4.bin

实测回显:

Loading Lemmatizer model ...
done (0.144s)

The     DET     the
dogs    NOUN    dog
are     AUX     be
running VERB    run
quickly ADV     quickly
through ADP     through
the     DET     the
fields  NOUN    field
.       PUNCT   .

Average: 62.5 sent/s

Total: 1 sent
Runtime: 0.016s
Execution time: 0.176 seconds

💡 说明:三列分别为「词 / 词性 / 词元(lemma)」——dogs→dog、are→be、running→run、fields→field;词元与词相同的原样回显。

4.7 LanguageDetector(语言检测)

实测 langdetect-183.bin:

cd /opt/opennlp-2.5.12
printf "This is an English sentence.\n" | ./bin/opennlp LanguageDetector /opt/opennlp/models/langdetect-183.bin

实测回显:

Loading Language Detector model ...
done (0.683s)

eng     This is an English sentence.

Average: 62.5 doc/s

Total: 1 doc
Runtime: 0.016s
Execution time: 0.726 seconds

💡 说明:输出第一列 eng 为 ISO 639-3 语言代码(eng=英语),Total: 1 doc 表明按文档(doc)计数。

4.8 Doccat(文档分类)

用法:opennlp Doccat model < documents。

⚠️ 说明:本文未单独采集 Doccat 运行回显(需文档分类模型);用法行与主帮助中的工具说明为实测原文,使用方式与 LanguageDetector 一致(模型 + 标准输入管道)。

4.9 其余工具

DictionaryDetokenizer、ChunkerME、Parser、EntityLinker、NGramLanguageModel、DictionaryBuilder、CensusDictionaryCreator 等工具在主帮助中列出(见二)。

⚠️ 说明:以上工具本次未采集独立运行回显;按需用 ./bin/opennlp <Tool> help 查看各自参数。

五、训练器工具详解

训练器统一形态:opennlp <Trainer> -lang ... -model ... -data ... [-params ...] [-encoding ...](通用参数含义见 5.1 实测与下文 Usage 行;各训练器专属参数表见《Apache OpenNLP 2.5.12 配置参数详解》篇)。

5.1 TokenizerTrainer(含自建语料实测)

训练数据格式:每行一句话、词间以字面 <SPLIT> 分隔(UTF-8)。实测语料 /opt/opennlp/train/en-token-sample.train(16 句 × 12 遍 = 192 行),首行:

This<SPLIT>is<SPLIT>a<SPLIT>sample<SPLIT>sentence<SPLIT>for<SPLIT>tokenizer<SPLIT>training<SPLIT>.

⚠️ 排障记录:若用「每行一个词」或「空格分隔」作训练数据,只会产生 NO_SPLIT 一种 outcome,训练报 ERROR: Not enough training data(InsufficientTrainingDataException: Training data must contain more than one outcome)。

训练命令:

cd /opt/opennlp-2.5.12
./bin/opennlp TokenizerTrainer -lang en -cutoff 1 -iterations 100 -model /opt/opennlp/models/en-token-custom.bin -data /opt/opennlp/train/en-token-sample.train -encoding UTF-8

实测回显(节选):

Indexing events with TwoPass using cutoff of 1
Computing event counts...
done. 9504 events
Indexing...
done.
Sorting and merging events...
done. Reduced 9504 events to 792.
Done indexing in 0.36 s.
Incorporating indexed data for training...
done.
        Number of Event Tokens: 792
         Number of Outcomes: 2
         Number of Predicates: 2461
...done.
Computing model parameters ...
Performing 100 iterations.
1: (7716/9504) Log Likelihood : -6587.670804041749
2: (7836/9504) Log Likelihood : -4108.5332447883475
...
100: (9504/9504) Log Likelihood : -176.4261324887696
Training finished after completing 100 iterations successfully.
Writing tokenizer model ...
done (0.083s)

Wrote tokenizer model to path: /opt/opennlp/models/en-token-custom.bin
Execution time: 0.807 seconds

自训模型推理验证(en-token-custom.bin,24K):

cd /opt/opennlp-2.5.12
printf "This is a sample sentence for the custom trained tokenizer model.\n" | ./bin/opennlp TokenizerME /opt/opennlp/models/en-token-custom.bin

实测回显:

Loading Tokenizer model ...
done (0.051s)

This is a sample sentence for the cus tom trained tokenizer model .
Average: 47.6 sent/s

Total: 1 sent
Runtime: 0.021s
Execution time: 0.111 seconds

⚠️ 注意:小样本自训模型把 custom 切成了 cus 与 tom(训练语料仅 16 句,效果有限);生产应使用更大语料训练。

5.2 POSTaggerTrainer

Usage(实测 help 首行):

Usage: opennlp POSTaggerTrainer[.ad|.masc|.conllx|.parse|.ontonotes|.conllu] [-factory factoryName] [-resources resourcesDir] [-featuregen featuregenFile] [-dict dictionaryPath] [-tagDictCutoff tagDictCutoff] [-params paramsFile] -lang language -model modelFile -data sampleData [-encoding charsetName]

参数速查:-factory 工厂类;-resources 资源目录;-featuregen 特征生成器描述文件;-dict XML 标签词典文件;-tagDictCutoff 标签词典 cutoff(指定时创建/扩展可变 TagDictionary);-params 训练参数文件;-lang/-model/-data/-encoding 为通用参数。

⚠️ 说明:本次未单独采集 POS 训练运行回显,命令形态与 TokenizerTrainer 一致(见 5.1)。

5.3 TokenNameFinderTrainer

Usage(实测 help 首行):

Usage: opennlp TokenNameFinderTrainer[.evalita|.ad|.conll03|.bionlp2004|.conll02|.masc|.muc6|.ontonotes|.brat] [-factory factoryName] [-resources resourcesDir] [-type modelType] [-featuregen featuregenFile] [-nameTypes types] [-sequenceCodec codec] [-params paramsFile] -lang language -model modelFile -data sampleData [-encoding charsetName]

参数速查:-type 模型类型;-nameTypes 训练使用的 name 类型;-sequenceCodec 编码 name span 的序列编码器;其余同 POSTaggerTrainer。

⚠️ 说明:本次未单独采集 NER 训练运行回显。

5.4 SentenceDetectorTrainer

Usage(实测 help 首行):

Usage: opennlp SentenceDetectorTrainer[.nkjp|.irishsentencebank|.ad|.pos|.masc|.conllx|.namefinder|.parse|.moses|.conllu|.letsmt] [-factory factoryName] [-abbDict path] [-eosChars string] [-useTokenEnd useTokenEnd] [-params paramsFile] -lang language -model modelFile -data sampleData [-encoding charsetName]

参数速查:-abbDict XML 缩写词典;-eosChars 句末(EOS)字符;-useTokenEnd 是否检测测试数据中下一句的起始索引。

⚠️ 说明:本次未单独采集分句训练运行回显。

5.5 DoccatTrainer

Usage(实测 help 首行):

Usage: opennlp DoccatTrainer[.20newsgroup] [-factory factoryName] [-featureGenerators fg] [-params paramsFile] -lang language -model modelFile -data sampleData [-encoding charsetName]

参数速查:-featureGenerators 逗号分隔的特征生成器类,缺省使用词袋(Bag of words)。

⚠️ 说明:本次未单独采集文档分类训练运行回显。

六、Java API 详解

opennlp-tools-2.5.12.jar 提供全部核心类(jar 内确认存在):

包类作用
opennlp.tools.tokenizeTokenizerModel、TokenizerME、Tokenizer、SimpleTokenizer分词模型与分词器
opennlp.tools.postagPOSModel、POSTaggerME词性标注模型与标注器
opennlp.tools.namefindTokenNameFinderModel、NameFinderME命名实体模型与识别器
opennlp.tools.utilSpan文本区间(词元 span、实体 span)

6.1 opennlp.tools.tokenize(分词 API)

import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.tokenize.TokenizerME;

TokenizerModel model = new TokenizerModel(new FileInputStream("/opt/opennlp/models/en-token.bin"));
TokenizerME tokenizer = new TokenizerME(model);
String[] tokens = tokenizer.tokenize("Pierre Vinken, 61 years old.");
  • TokenizerModel:从 .bin 加载分词模型。
  • TokenizerME:学习型分词器,tokenize(String) 返回 String[]。
  • Tokenizer:分词接口;SimpleTokenizer 为其字符类实现(无需模型)。

6.2 opennlp.tools.postag(词性标注 API)

import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;

POSModel model = new POSModel(new FileInputStream("/opt/opennlp/models/opennlp-en-ud-ewt-pos-1.3-2.5.4.bin"));
POSTaggerME tagger = new POSTaggerME(model);
String[] tags = tagger.tag(tokens);   // tokens 来自分词器,与输出一一对应
  • POSModel:加载词性模型;POSTaggerME.tag(String[]) 返回与输入等长的词性数组。

6.3 opennlp.tools.namefind(命名实体 API)

import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.util.Span;

TokenNameFinderModel model = new TokenNameFinderModel(new FileInputStream("/opt/opennlp/models/en-ner-person.bin"));
NameFinderME finder = new NameFinderME(model);
Span[] spans = finder.find(tokens);
double[] probs = finder.probs(spans);
for (int i = 0; i < spans.length; i++) {
    Span span = spans[i];
    System.out.println("类型=" + span.getType() + " 概率=" + probs[i] + " 实体="
            + String.join(" ", Arrays.copyOfRange(tokens, span.getStart(), span.getEnd())));
}
  • TokenNameFinderModel:加载 NER 模型;NameFinderME.find(String[]) 返回 Span[](span.getType() 为实体类型,如 person)。
  • finder.probs(Span[]) 返回每个实体的概率(与 spans 等长)。
  • Span.getStart()/getEnd() 为实体在 token 数组中的起止下标(左闭右开),用 Arrays.copyOfRange 还原实体文本。

6.4 opennlp.tools.util.Span

Span 表示文本区间:namefind 结果以 Span 承载实体位置与类型(getType());CLI 的 <START:person> ... <END> 标记即 Span 序列化后的等价物。

💡 说明:完整可运行示例(分词 + 词性标注 + 命名实体三步串联,含 pom 与运行回显)见《Apache OpenNLP 2.5.12 构建并运行示例应用指南》篇;上述片段即该工程实测所用 API。

七、注意事项

⚠️ 注意:

  1. CLI 推理工具一律从标准输入读文本(< sentences / < documents),模型文件作为位置参数传入。
  2. LemmatizerME 输入必须是 word_TAG 格式(见 4.6),纯文本报 Invalid format。
  3. TokenNameFinder 支持多模型并列(model1 model2 ... modelN),可一次标注多类实体。
  4. 训练数据格式必须满足各训练器要求(Tokenizer 见 5.1),否则报 ERROR: Not enough training data。
  5. 词性模型分 UD 标签集与 Penn Treebank 标签集两类,混用不影响推理但标签体系不同(见安装指南「版本选型说明」)。
  6. 自训模型质量取决于语料规模与迭代次数;小语料实测效果有限(见 5.1 自训模型推理回显)。

八、常见问题

Q1:工具怎么查看帮助? ./bin/opennlp <Tool> help(如 ./bin/opennlp TokenizerTrainer help);./bin/opennlp 打印全部工具列表。

Q2:LemmatizerME 报 Invalid format? 输入须为 word_TAG 的 POSSample 格式(如 running_VERB),纯文本不行。

Q3:自训模型效果差? 训练语料过小(实测 16 句的模型把 custom 切成 cus/tom)。加大语料规模、提高迭代次数或改用 MAXENT_QN 算法(相关内容见《Apache OpenNLP 2.5.12 配置参数详解》篇)。

Q4:识别不出其他实体类型? en-ner-person.bin 只识别人名;机构/地点需 en-ner-organization.bin、en-ner-location.bin(同一模型站下载),并用多模型参数并列传入(model1 model2 ... modelN)。

Q5:CLI 内存高? 用 JAVA_HEAP 限制堆:JAVA_HEAP=256m ./bin/opennlp TokenizerME /opt/opennlp/models/en-token.bin < /tmp/input.txt;另开终端 ps -ef | grep "[o]pennlp.tools.cmdline.CLI" | grep -o "\-Xmx[^ ]*" 复核生效值(实测 -Xmx256m)。

九、参考链接

  • Apache OpenNLP 官方文档:https://opennlp.apache.org/docs/
  • Apache OpenNLP 下载目录:https://dlcdn.apache.org/opennlp/
  • demo 模型站:https://opennlp.sourceforge.net/models-1.5/
  • UD 模型目录:https://dlcdn.apache.org/opennlp/models/ud-models-1.3/