赞
踩
//数据取自mysql
public void TrainHanlpModel(Map<String, String[]> map) throws IOException {
IClassifier classifier = new NaiveBayesClassifier();
classifier.train(map);
NaiveBayesModel model = (NaiveBayesModel) classifier.getModel();
IOUtil.saveObjectTo(model, path);
}
获取path
public static IClassifier classifier; { path=getInputPath()+"\\emtionModel.ser"; } private String getInputPath() { File path = null; try { path = new File(ResourceUtils.getURL("classpath:").getPath()); } catch (FileNotFoundException e) { e.printStackTrace(); } if (!path.exists()) path = new File(""); File input = new File(path.getAbsolutePath(), "model/"); if (!input.exists()) input.mkdirs(); return input.getAbsolutePath(); }
// 这里没使用啥注入, 纯粹写写demo测试使用。。
public String hello(String txt) throws SQLException {
// 读取model
ConnectionMysql connectionMysql = new ConnectionMysql();
connectionMysql.Connection();
//String ret= connectionMysql.GetModelStr();
String ret="";
GetHanlpTest glt = new GetHanlpTest();
if(ret!=null||ret.length()>0)
{
IClassifier classifier =glt.GetIClassifier(glt.GetNaiveBayesModelByStr());
ret= classifier.classify(txt);
}
return ret;
}
获取mode 方法
IOUtil这个包为hanlp自带。
public NaiveBayesModel GetNaiveBayesModelByStr()
{
NaiveBayesModel model= new NaiveBayesModel();
model=(NaiveBayesModel)IOUtil.readObjectFrom(path);
return model;
}
欢迎关注微信公众号
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。