OpenNLP-Document Categorizer-如何基于状态对文档进行分类;文档的语言不是英语,还是默认功能?

时间:2018-12-24 12:58:33

标签: java nlp text-classification naivebayes opennlp

我想使用OpenNLP的文档分类器根据其状态对文档进行分类:预打开,打开,锁定,关闭等。

我有5个课程,我使用的是朴素贝叶斯算法,我的训练集中有60个文档,并使用1个截止参数对我的集合进行了1000次迭代训练。

但是没有成功,当我测试它们时,我没有得到好的结果。我在想也许是因为文档的语言(不是英语),或者也许我应该以某种方式将状态添加为功能。我已经在分类程序中设置了默认功能,而且我对它们也不是很熟悉。

结果应该被锁定,但其归类为已打开。

InputStreamFactory in=null;
try {
in= new MarkableFileInputStreamFactory(new 
File("D:\\JavaNlp\\doccategorizer\\doccategorizer.txt"));
}
catch (FileNotFoundException e2) {
System.out.println("Creating new input stream");
e2.printStackTrace();
}

ObjectStream lineStream=null;
ObjectStream sampleStream=null;

try {
lineStream = new PlainTextByLineStream(in, "UTF-8");
sampleStream = new DocumentSampleStream(lineStream);            
}
catch (IOException e1) {
System.out.println("Document Sample Stream");
e1.printStackTrace();
}


TrainingParameters params = new TrainingParameters();
params.put(TrainingParameters.ITERATIONS_PARAM, 1000+"");
params.put(TrainingParameters.CUTOFF_PARAM, 1+"");
params.put(AbstractTrainer.ALGORITHM_PARAM, 
NaiveBayesTrainer.NAIVE_BAYES_VALUE);


DoccatModel model=null;
try {
model = DocumentCategorizerME.train("en", sampleStream, params, new 
DoccatFactory());
} 
catch (IOException e) 
{
System.out.println("Training...");
e.printStackTrace();
}


System.out.println("\nModel is successfully trained.");


BufferedOutputStream modelOut=null;

try {
modelOut = new BufferedOutputStream(new 
FileOutputStream("D:\\JavaNlp\\doccategorizer\\classifier-maxent.bin"));
} 
catch (FileNotFoundException e) {

System.out.println("Creating output stream");
e.printStackTrace();
}
try {
model.serialize(modelOut);
}
catch (IOException e) {

System.out.println("Serialize...");
e.printStackTrace();
}
System.out.println("\nTrained model is kept in: 
"+"model"+File.separator+"en-cases-classifier-maxent.bin");

DocumentCategorizer doccat = new DocumentCategorizerME(model);
String[] docWords = "Some text here...".replaceAll("[^A-Za-z]", " ").split(" ");
double[] aProbs = doccat.categorize(docWords);


System.out.println("\n---------------------------------\nCategory : 
Probability\n---------------------------------");
for(int i=0;i<doccat.getNumberOfCategories();i++){
System.out.println(doccat.getCategory(i)+" : "+aProbs[i]);
}
System.out.println("---------------------------------");

System.out.println("\n"+doccat.getBestCategory(aProbs)+" : is the category 
for the given sentence");

results results2

有人可以建议我如何很好地对文档进行分类,例如我应该先添加语言检测器还是添加新功能?

预先感谢

1 个答案:

答案 0 :(得分:1)

默认情况下,文档分类器采用文档文本并形成一袋单词。包中的每个单词都成为一个特色。只要该语言可以由英语令牌生成器(默认情况下也是空白令牌生成器)进行令牌化,我就会猜想这不是您的问题。我会检查您用于训练数据的数据格式。格式应如下:

category<tab>document text

文本应为一行。可在http://opennlp.apache.org/docs/1.9.0/manual/opennlp.html#tools.doccat.training.tool

中找到文档分类器的opennlp文档。

如果您可以提供一两行训练数据来帮助检查格式,将会很有帮助。

编辑:另一个潜在问题。 60个文档可能不足以训练一个好的分类器,特别是如果您的词汇量很大。另外,即使这不是英语,也请告诉我它不是多种语言。最后,文档文本是分类文档的最佳方法吗?文档本身的元数据会产生更好的功能吗?

希望有帮助。