gpt4 book ai didi

machine-learning - openNLP 对内容进行分类始终返回第一类别

转载 作者:行者123 更新时间:2023-11-30 08:37:13 24 4
gpt4 key购买 nike

我正在使用 openNLP 库进行测试,以实现内容分类的自动化,但我遇到了麻烦。我正在使用此代码,它始终返回我的训练数据中的第一个类别,我正在传递来自任何新闻网站的完整文章。

    public void trainModel() {
try {
InputStreamFactory inputStreamFactory = new MarkableFileInputStreamFactory( new File("C:\\Users\\emehm\\Desktop\\data\\training_data.txt") );
ObjectStream<String> lineStream = new PlainTextByLineStream(inputStreamFactory, "UTF-8");
ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(lineStream);

DoccatModel model = DocumentCategorizerME.train("en", sampleStream, TrainingParameters.defaultParams(), new DoccatFactory());
DocumentCategorizerME myCategorizer = new DocumentCategorizerME(model);
double[] outcomes = myCategorizer.categorize( new String[]{ this.getFileContent() });
String category = myCategorizer.getBestCategory(outcomes);
Map<String, Double> map = myCategorizer.scoreMap(new String[]{ this.getFileContent() });
System.out.println(category);
} catch (IOException e) {
// Failed to read or parse training data, training failed
e.printStackTrace();
}
}

public String getFileContent() throws IOException {
InputStream is = new FileInputStream("C:\\Users\\emehm\\Desktop\\data\\statija.txt");
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
String line = buf.readLine();
StringBuilder sb = new StringBuilder();
while (line != null) {
sb.append(line).append("\n");
line = buf.readLine();
}
buf.close();
return sb.toString();
}

训练数据:http://pastebin.com/ZhxswkvJ

我正在使用的文章:http://pastebin.com/xtABGcbh

它总是返回列表中的第一个类别,我想知道我缺少什么?当我调试它时,它会为所有这些返回 0.25 分数,并出于某种原因选择第一个。当我测试一个单词时,我猜它可以工作,但它不适用于一篇文章。

最佳答案

输入需要分成单独的单词,即用空格分隔。

更改此:double[] results = myCategorizer.categorize( new String[]{ this.getFileContent() });

对此:double[] results = myCategorizer.categorize( this.getFileContent().split("") );

之后,你应该会有更好的结果。值得注意的是,有效性与模型的质量相关。

关于machine-learning - openNLP 对内容进行分类始终返回第一类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42908442/

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