HanLP 基于SVM支持向量机 训练 文本分类
创始人
2024-04-21 08:22:44
0

一、HanLP 基于SVM支持向量机分类器

上篇文章通过朴素贝叶斯文本分类器,训练测试了 搜狗文本分类语料库迷你版 ,本篇继续测试SVM支持向量机分类器。

由于HanLP 官方给出的 SVM 分类器依赖了第三方库,没有集成在主项目中,需要拉取 text-classification-svm 项目:

git clone https://github.com/hankcs/text-classification-svm.git

拉取项目后,发现 pom 依赖的 hanlp 版本是 portable-1.5.2,前面我们用的都是portable-1.8.3,这里就保持保本一致,将 pom 中 hannlp 的版本升级为 portable-1.8.3
在这里插入图片描述
升级后会有两点不兼容,需要修改 LinearSVMClassifier 类:

在这里插入图片描述

然后将该项目打包到本地 Maven 中方便其他项目使用:

mvn clean install -DskipTests

下面在自己的 Maven 项目中添加依赖:


com.hankcs.nlptext-classification1.0.1


de.bwaldvogelliblinear1.95

有关于 HanLP 环境的搭建,可以参考下面这篇文章:

https://xiaobichao.blog.csdn.net/article/details/128271909

SVM 文本分类器,利用liblinear实现HanLP中的文本分类接口,因此对预料库的要求还是 将数据集根据分类放到不同的目录中:

在这里插入图片描述
下面还是使用 搜狗文本分类语料库迷你版 进行测试。

准备语料库

下载数据集:

http://file.hankcs.com/corpus/sogou-text-classification-corpus-mini.zip

下载加压后数据格式:

在这里插入图片描述
文本样例:

在这里插入图片描述

训练数据

public class ClassifyTrain {public static void main(String[] args) throws IOException {//语料库的地址String dataPath = "F:/bigdata/hanlp/sogou-text-classification-corpus-mini/搜狗文本分类语料库迷你版";//模型保存路径String modelPath = "F:/bigdata/hanlp/sogou-text-classification-corpus-mini/svm-classification-model.ser";//训练数据trainData(dataPath, modelPath);}private static void trainData(String dataPath, String modelPath) throws IOException {File corpusFolder = new File(dataPath);if (!corpusFolder.exists() || !corpusFolder.isDirectory())  {System.err.println("没有文本分类语料");return;}// FileDataSet省内存,可加载大规模数据集,支持不同的ITokenizer,详见源码中的文档// 使用前90% 的数据作为训练集IDataSet trainingCorpus = new FileDataSet().setTokenizer(new HanLPTokenizer()).load(dataPath, "UTF-8", 0.9);// 创建SVM分类器IClassifier classifier = new LinearSVMClassifier();// 训练数据classifier.train(trainingCorpus);// 获取训练模型AbstractModel model = classifier.getModel();// 使用后10% 的数据作为测试集IDataSet testingCorpus = new MemoryDataSet(model).load(dataPath, "UTF-8", -0.1);// 计算准确率FMeasure result = Evaluator.evaluate(classifier, testingCorpus);System.out.println("测试集准确度:");System.out.println(result);// 保存模型IOUtil.saveObjectTo(model, modelPath);}}

查看训练日志:

在这里插入图片描述

查看训练模型:

在这里插入图片描述

测试模型

public class TestClassify {public static void main(String[] args) {String modelPath = "F:/bigdata/hanlp/sogou-text-classification-corpus-mini/svm-classification-model.ser";testModel(modelPath);}private static void testModel(String modelPath){LinearSVMModel model = (LinearSVMModel) IOUtil.readObjectFrom(modelPath);IClassifier classifier = new LinearSVMClassifier(model);// 测试分类String text1 = "研究生考录模式亟待进一步专业化";System.out.printf("《%s》 属于分类 【%s】\n", text1, classifier.classify(text1));String text2 = "C罗获2018环球足球奖最佳球员 德尚荣膺最佳教练";System.out.printf("《%s》 属于分类 【%s】\n", text2, classifier.classify(text2));String text3 = "英国造航母耗时8年仍未服役 被中国速度远远甩在身后";System.out.printf("《%s》 属于分类 【%s】\n", text3, classifier.classify(text3));String text4 = "如果真想用食物解压,建议可以食用燕麦";System.out.printf("《%s》 属于分类 【%s】\n", text4, classifier.classify(text4));}}

测试结果:

在这里插入图片描述

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...