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));}}

测试结果:

在这里插入图片描述

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...