gpt4 book ai didi

java - 无法使用 opennlp 和 java 来训练 location.bin

转载 作者:太空宇宙 更新时间:2023-11-04 14:51:27 24 4
gpt4 key购买 nike

我正在尝试使用 java 中的 opennlp 来训练 en-ner-location.bin 文件,事情是我得到了以下格式的训练文本文件 <START:location> Fontana <END>
<START:location> Palo Verde <END>
<START:location> Picacho <END>

我使用以下代码训练了该文件

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collections;
import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.NameSample;
import opennlp.tools.namefind.NameSampleDataStream;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.tokenize.Tokenizer;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
import opennlp.tools.util.Span;

public class TrainNames {
@SuppressWarnings("deprecation")
public void TrainNames() throws IOException{
File fileTrainer=new File("citytrain.txt");
File output=new File("en-ner-location.bin");
ObjectStream<String> lineStream = new PlainTextByLineStream(new FileInputStream(fileTrainer), "UTF-8");
ObjectStream<NameSample> sampleStream = new NameSampleDataStream(lineStream);
System.out.println("lineStream = " + lineStream);
TokenNameFinderModel model = NameFinderME.train("en", "location", sampleStream, Collections.<String, Object>emptyMap(), 1, 0);

BufferedOutputStream modelOut = null;
try {
modelOut = new BufferedOutputStream(new FileOutputStream(output));
model.serialize(modelOut);
} finally {
if (modelOut != null)
modelOut.close();
}
}
}

我没有收到任何错误或警告,但是当我尝试从这样的字符串中获取城市名称时 cnt="John 计划在 UC Fontana 专攻电气工程并在 IBM 寻求职业生涯。";它返回整个字符串谁能告诉我为什么...??

最佳答案

欢迎来到SO!看起来您需要有关每个位置注释的更多上下文。我相信现在 openNLP 认为你正在训练它来查找单词(任何单词),因为你的训练数据只有一个单词。您需要注释整个句子中的位置,并且您将需要至少几百个样本才能开始看到良好的结果。

另请参阅此答案: How I train an Named Entity Recognizer identifier in OpenNLP?

关于java - 无法使用 opennlp 和 java 来训练 location.bin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23724377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com