/**
* 建立分类器
*/
OnlineTrainer trainer = new OnlineTrainer(af);
Linear pclassifier = trainer.train(trainset);
pp.removeTargetPipe();
pclassifier.setPipe(pp);
af.setStopIncrement(true);
//将分类器保存到模型文件
pclassifier.saveTo(modelFile);
pclassifier = null;
//从模型文件读入分类器
Linear cl =Linear.loadFrom(modelFile);
//性能评测
Evaluation eval = new Evaluation(testset);
eval.eval(cl,1);
/**
* 测试
*/
System.out.println("类别 : 文本内容");
System.out.println("===================");
for(int i=0;i<testset.size();i++){
Instance data = testset.getInstance(i);
Integer gold = (Integer) data.getTarget();
String pred_label = cl.getStringLabel(data);
String gold_label = cl.getLabel(gold);
if(pred_label.equals(gold_label))
System.out.println(pred_label+" : "+testset.getInstance(i).getSource());
else
System.err.println(gold_label+"->"+pred_label+" : "+testset.getInstance(i).getSource());
}
/**
* 分类器使用
*/
String str = "韦德:不拿冠军就是失败 詹皇:没拿也不意味失败";
System.out.println("============\n分类:"+ str);
Pipe p = cl.getPipe();
Instance inst = new Instance(str);
try {
//特征转换
p.addThruPipe(inst);
} catch (Exception e) {
e.printStackTrace();
}
String res = cl.getStringLabel(inst);
System.out.println("类别:"+ res);
//清除模型文件
(new File(modelFile)).deleteOnExit();
System.exit(0);
}