java - 使用 .prop 文件以编程方式训练 NER 模型

标签 java named-entity-recognition stanford-nlp

我一直在使用属性文件训练我的 ner 模型,如教程中所示 LINK .我正在使用相同的 Prop 文件,但是当我不明白如何以编程方式进行时。

props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, sentiment, regexner");
props.setProperty("ner.model", "resources/NER.prop");

prop文件如下图所示:

# location of the training file
trainFile = nerTEST.tsv
# location where you would like to save (serialize) your
# classifier; adding .gz at the end automatically gzips the file,
# making it smaller, and faster to load
serializeTo = resources/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

# This specifies the order of the CRF: order 1 means that features
# apply at most to a class pair of previous class and current class
# or current class and next class.
maxLeft=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
# word character ngrams will be included up to length 6 as prefixes
# and suffixes only
useNGrams=true
noMidNGrams=true
maxNGramLeng=6
usePrev=true
useNext=true
useDisjunctive=true
useSequences=true
usePrevSequences=true
# the last 4 properties deal with word shape features
useTypeSeqs=true
useTypeSeqs2=true
useTypeySequences=true
wordShape=chris2useLC

错误:

 java.io.StreamCorruptedException: invalid stream header: 23206C6F
....
..
Caused by: java.io.IOException: Couldn't load classifier from resources/NER.prop

来自关于 SO 的另一个问题,我知道你直接提供了模型文件。但是,我们如何在属性文件的帮助下做到这一点?

最佳答案

您应该从命令行运行此命令:

java -cp "*" edu.stanford.nlp.ie.crf.CRFClassifier -prop NER.prop

如果你想在 Java 代码中运行它,你可以这样做:

String[] args = new String[]{"-props", "NER.prop"};
CRFClassifier.main(args);

.prop 文件是指定用于训练模型的设置的文件。您的代码试图将 .prop 文件作为模型本身加载,这导致了错误。

做任何一个都会在 resources/ner-model.ser.gz 生成最终模型

关于java - 使用 .prop 文件以编程方式训练 NER 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38073043/

相关文章:

python - spaCy,准备训练数据 : doc. char_span 返回 'None'

python - 如何测试 stanfordnlp 是否在 GPU 上运行?

java - 使用斯坦福解析器获得句子的 K 个最佳解析

java - Hadoop wordcount - 减少方法将 1 个文件结果打印到单个 1 行

java - 解析数组中已经存在的json数据?

java - HashSet<List<Integer>> 时间复杂度

java - 在 OpenNLP 中训练命名实体

java - 如何使用 PowerMockito 完全模拟一个类?

python - NLTK : combining stanford tagger and personal tagger

python - 为什么 CoreNLP ner tagger 和 ner tagger 将分开的数字连接在一起?