{"id":9472,"date":"2025-11-07T14:02:06","date_gmt":"2025-11-07T06:02:06","guid":{"rendered":"\/?p=9472"},"modified":"2025-11-07T14:02:06","modified_gmt":"2025-11-07T06:02:06","slug":"hanlp-%e5%9f%ba%e4%ba%8esvm%e6%94%af%e6%8c%81%e5%90%91%e9%87%8f%e6%9c%ba-%e5%ae%9e%e8%b7%b5-chnsenticorp-%e6%83%85%e6%84%9f%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"\/?p=9472","title":{"rendered":"HanLP \u57fa\u4e8eSVM\u652f\u6301\u5411\u91cf\u673a \u5b9e\u8df5 ChnSentiCorp \u60c5\u611f\u5206\u6790"},"content":{"rendered":"<h2>\u4e00\u3001ChnSentiCorp\u60c5\u611f\u5206\u6790\u9152\u5e97\u8bc4\u8bba\u6570\u636e\u96c6<\/h2>\n<p><code>ChnSentiCorp<\/code>\u60c5\u611f\u5206\u6790\u9152\u5e97\u8bc4\u8bba\u6570\u636e\u96c6\u662f\u4e00\u4e2a\u6807\u6ce8\u4e86\u60c5\u611f\u7684\u6570\u636e\u96c6\uff0c\u5206\u4e86\u8d1f\u9762\u548c\u6b63\u9762\u4e24\u7c7b\uff0c\u4e0b\u8f7d\u5730\u5740\uff1a<\/p>\n<blockquote>\n<p><a href=\"http:\/\/file.hankcs.com\/corpus\/ChnSentiCorp.zip\">http:\/\/file.hankcs.com\/corpus\/ChnSentiCorp.zip<\/a><\/p>\n<\/blockquote>\n<p>\u4e0b\u8f7d\u89e3\u538b\u540e\u7ed3\u6784\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/640-20251107140140841.png\" alt=\"\u56fe\u7247\" \/><\/p>\n<p>\u5b9e\u4f8b\u6570\u636e\u6837\u4f8b\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/640-20251107140140991.png\" alt=\"\u56fe\u7247\" \/><\/p>\n<p>\u4e0b\u9762\u4f7f\u7528 <code>HanLP<\/code> \u57fa\u4e8e <code>SVM<\/code> \u652f\u6301\u5411\u91cf\u673a\u8fdb\u884c\u5206\u7c7b\u8bad\u7ec3\uff0c\u6709\u5173\u4e8e <code>HanLP<\/code> \u73af\u5883\u7684\u642d\u5efa\uff0c\u53ef\u4ee5\u53c2\u8003\u4e0b\u9762\u8fd9\u7bc7\u6587\u7ae0\uff1a<\/p>\n<blockquote>\n<p><a href=\"https:\/\/xiaobichao.blog.csdn.net\/article\/details\/128271909\">https:\/\/xiaobichao.blog.csdn.net\/article\/details\/128271909<\/a><\/p>\n<\/blockquote>\n<h3>\u8bad\u7ec3\u6570\u636e<\/h3>\n<pre><code>public class ClassifySentimentTrain {\n\n    public static void main(String[] args) throws IOException {\n        \/\/\u8bed\u6599\u5e93\u7684\u5730\u5740\n        String dataPath = &quot;F:\/bigdata\/hanlp\/ChnSentiCorp\/ChnSentiCorp\u60c5\u611f\u5206\u6790\u9152\u5e97\u8bc4\u8bba&quot;;\n        \/\/\u6a21\u578b\u4fdd\u5b58\u8def\u5f84\n        String modelPath = &quot;F:\/bigdata\/hanlp\/ChnSentiCorp\/svm-classification-model.ser&quot;;\n        \/\/\u8bad\u7ec3\u6570\u636e\n        trainData(dataPath, modelPath);\n    }\n\n    private static void trainData(String dataPath, String modelPath) throws IOException {\n        File corpusFolder = new File(dataPath);\n        if (!corpusFolder.exists() || !corpusFolder.isDirectory())  {\n            System.err.println(&quot;\u6ca1\u6709\u6587\u672c\u5206\u7c7b\u8bed\u6599&quot;);\n            return;\n        }\n        \/\/ FileDataSet\u7701\u5185\u5b58\uff0c\u53ef\u52a0\u8f7d\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u652f\u6301\u4e0d\u540c\u7684ITokenizer\uff0c\u8be6\u89c1\u6e90\u7801\u4e2d\u7684\u6587\u6863\n        \/\/ \u4f7f\u7528\u524d90% \u7684\u6570\u636e\u4f5c\u4e3a\u8bad\u7ec3\u96c6\n        IDataSet trainingCorpus = new FileDataSet()\n                .setTokenizer(new HanLPTokenizer())\n                .load(dataPath, &quot;UTF-8&quot;, 0.9);\n        \/\/ \u521b\u5efaSVM\u5206\u7c7b\u5668\n        IClassifier classifier = new LinearSVMClassifier();\n        \/\/ \u8bad\u7ec3\u6570\u636e\n        classifier.train(trainingCorpus);\n        \/\/ \u83b7\u53d6\u8bad\u7ec3\u6a21\u578b\n        AbstractModel model = classifier.getModel();\n        \/\/ \u4f7f\u7528\u540e10% \u7684\u6570\u636e\u4f5c\u4e3a\u6d4b\u8bd5\u96c6\n        IDataSet testingCorpus = new MemoryDataSet(model)\n                .load(dataPath, &quot;UTF-8&quot;, -0.1);\n        \/\/ \u8ba1\u7b97\u51c6\u786e\u7387\n        FMeasure result = Evaluator.evaluate(classifier, testingCorpus);\n        System.out.println(&quot;\u6d4b\u8bd5\u96c6\u51c6\u786e\u5ea6\uff1a&quot;);\n        System.out.println(result);\n        \/\/ \u4fdd\u5b58\u6a21\u578b\n        IOUtil.saveObjectTo(model, modelPath);\n    }\n\n}<\/code><\/pre>\n<p>\u67e5\u770b\u8bad\u7ec3\u65e5\u5fd7\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/640-20251107140141096.png\" alt=\"\u56fe\u7247\" \/><\/p>\n<p>\u67e5\u770b\u8bad\u7ec3\u6a21\u578b\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/640-20251107140141227.png\" alt=\"\u56fe\u7247\" \/><\/p>\n<h3>\u6d4b\u8bd5\u6a21\u578b<\/h3>\n<pre><code>public class TestSentimentClassify {\n\n    public static void main(String[] args) {\n        String modelPath = &quot;F:\/bigdata\/hanlp\/ChnSentiCorp\/svm-classification-model.ser&quot;;\n        testModel(modelPath);\n    }\n\n    private static void testModel(String modelPath){\n        LinearSVMModel model = (LinearSVMModel) IOUtil.readObjectFrom(modelPath);\n        IClassifier classifier = new LinearSVMClassifier(model);\n        \/\/ \u6d4b\u8bd5\u5206\u7c7b\n        String text1 = &quot;\u524d\u53f0\u5ba2\u623f\u670d\u52a1\u6001\u5ea6\u975e\u5e38\u597d\uff01\u65e9\u9910\u5f88\u4e30\u5bcc\uff0c\u623f\u4ef7\u5f88\u5e72\u51c0\u3002\u518d\u63a5\u518d\u5389\uff01&quot;;\n        System.out.printf(&quot;\u300a%s\u300b \u5c5e\u4e8e\u5206\u7c7b \u3010%s\u3011\\n&quot;, text1, classifier.classify(text1));\n\n        String text2 = &quot;\u7ed3\u679c\u5927\u5931\u6240\u671b\uff0c\u706f\u5149\u660f\u6697\uff0c\u7a7a\u95f4\u6781\u5176\u72ed\u5c0f\uff0c\u5e8a\u57ab\u8d28\u91cf\u6076\u52a3\uff0c\u623f\u95f4\u8fd8\u4f34\u7740\u4e00\u80a1\u9709\u5473\u3002&quot;;\n        System.out.printf(&quot;\u300a%s\u300b \u5c5e\u4e8e\u5206\u7c7b \u3010%s\u3011\\n&quot;, text2, classifier.classify(text2));\n\n        String text3 = &quot;\u53ef\u5229\u7528\u6587\u672c\u5206\u7c7b\u5b9e\u73b0\u60c5\u611f\u5206\u6790\uff0c\u6548\u679c\u8fd8\u884c&quot;;\n        System.out.printf(&quot;\u300a%s\u300b \u5c5e\u4e8e\u5206\u7c7b \u3010%s\u3011\\n&quot;, text3, classifier.classify(text3));\n\n    }\n\n}<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/640-20251107140141335.png\" alt=\"\u56fe\u7247\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001ChnSentiCorp\u60c5\u611f\u5206\u6790\u9152\u5e97\u8bc4\u8bba\u6570\u636e\u96c6 ChnSentiCorp\u60c5\u611f\u5206\u6790\u9152\u5e97\u8bc4\u8bba\u6570\u636e\u96c6\u662f\u4e00\u4e2a\u6807\u6ce8\u4e86\u60c5\u611f\u7684\u6570\u636e\u96c6\uff0c\u5206\u4e86\u8d1f\u9762\u548c\u6b63   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[225],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9472"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9472"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9472\/revisions"}],"predecessor-version":[{"id":9473,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9472\/revisions\/9473"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9472"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}