Classifier4J是一个轻量级的分类工具,支持贝叶斯分类、向量空间模型、信息摘要等。然而它却不支持中文,异常信息大致如下:

Exception in thread "main" java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:813)
at java.util.HashMap$ValueIterator.next(HashMap.java:839)
at java.util.Collections.max(Collections.java:657)

主要原因在于Classifier4J自带的DefaultTokenizer使用正则表达式“\W”进行分词,这种方式对英文还好,因为英文有着天然的分隔符,然而对中文则是不适用的。因而我们需要自己实现Classifier4J对中文的支持,分词工具选用庖丁分词。在包 net.sf.classifier4J中加入以下类:

package net.sf.classifier4J;

import java.io.IOException;
import java.io.StringReader;
import java.util.Vector; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.TermAttribute; import net.paoding.analysis.analyzer.PaodingAnalyzer; /**
* @author hongyu
*/
public class PaodingTokenizer implements ITokenizer { private Analyzer paoding; public PaodingTokenizer() {
paoding = new PaodingAnalyzer();
} @Override
public String[] tokenize(String input) {
if(input != null) {
StringReader inputReader = new StringReader(input);
TokenStream ts = paoding.tokenStream("", inputReader);
TermAttribute termAtt = (TermAttribute)ts.getAttribute(TermAttribute.class); Vector<String> tokens = new Vector<String>();
try {
while(ts.incrementToken()) {
tokens.add(termAtt.term());
}
return tokens.toArray(new String[0]);
} catch (IOException e) {
return new String[0];
}
} else {
return new String[0];
}
} }

net.sf.classifier4J.Utilities的第二个构造方法修改如下:

    public static Map getWordFrequency(String input, boolean caseSensitive) {
//return getWordFrequency(input, caseSensitive, new DefaultTokenizer(), new DefaultStopWordsProvider());
return getWordFrequency(input, caseSensitive, new PaodingTokenizer(), new DefaultStopWordsProvider());
}

net.sf.classifier4J.vector.VectorClassifier中第一个构造方法第一行做如下修改:

        //tokenizer = new DefaultTokenizer();
tokenizer = new PaodingTokenizer();

另外还有一些其他小的bug:

1,为了能够正确处理查询字符串出现在首部的情况,SimpleClassifier最后一个方法修改如下:

    public double classify(String input) {
if ((input != null) && (input.indexOf(searchWord) >= 0)) {
return 1;
} else {
return 0;
}
}

2,为了能够正确的对中文信息提取摘要,Utilities的getSentences方法修改如下:

    public static String[] getSentences(String input) {
if (input == null) {
return new String[0];
} else {
// split on a ".", a "!", a "?" followed by a space or EOL
//return input.split("(\\.|!|\\?)+(\\s|\\z)");
return input.split("(\\。|\\.|!|\\?)+(\\s|\\z)?");
}
}

3,中文句子一般以句号结尾,因而SimpleSummariser中第122行修改为:

result.append("。");

以下是几个简单的测试类:

1,基本分类器:

public class BasicUsage {

	public static void main(String args[]) throws Exception {

		SimpleClassifier classifier = new SimpleClassifier();
classifier.setSearchWord("中华");
String sentence = "中华人民共和国"; System.out.println("The string '" + sentence +
"' contains the word '中华': " + classifier.isMatch(sentence));
System.out.println("The match rate is: " + classifier.classify(sentence));
} }

运行结果:

The string '中华人民共和国' contains the word '中华': true
The match rate is: 1.0

2,贝叶斯分类器:

public class Bayesian {

	public static void main(String args[]) throws Exception {

		IWordsDataSource wds = new SimpleWordsDataSource();
IClassifier classifier = new BayesianClassifier(wds);
System.out.println( "Matches = " + classifier.classify("中华人民共和国") );
} }

运行结果:

Matches = 0.5

3,信息摘要:

public class Summariser {

	public static void main(String args[]) {

		String input = "中华人民共和国简称中国,位于欧亚大陆东部,太平洋西岸。中国具有五千年的文明史,是世界四大文明古国之一。";
ISummariser summariser = new SimpleSummariser(); String result = summariser.summarise(input, 1);
System.out.println(result);
} }

运行结果:

中华人民共和国简称中国,位于欧亚大陆东部,太平洋西岸。

4,向量空间模型:

public class Vector {

	public static void main(String args[]) throws Exception {
TermVectorStorage storage = new HashMapTermVectorStorage();
VectorClassifier vc = new VectorClassifier(storage); vc.teachMatch("草本","含羞草");
double result = vc.classify("草本", "含羞草");
System.out.println(result);
} }

运行结果:

0.9999999999999998

最后,Classifier4J只定义了英文中的停用词,对于中文而言,庖丁分词的词典中已经包含了停用词。

Classifier4J的中文支持的更多相关文章

  1. CentOS安装中文支持

    部分文档突然成乱码了. 解决方法: 1.安装中文支持包 # yum groupinstall "Chinese Support" 2 修改# /etc/sysconfig/i18n ...

  2. linux环境下安装sphinx中文支持分词搜索(coreseek+mmseg)

     linux环境下安装sphinx中文支持分词搜索(coreseek+mmseg) 2013-11-10 16:51:14 分类: 系统运维 为什么要写这篇文章? 答:通过常规的三大步(./confi ...

  3. 移动开发之浅析cocos2d-x的中文支持问题

    题记:这阵子一直在学习cocos2d-x,其跨平台的特性确实让人舒爽,引擎的框架概念也很成熟,虽然相应的第三方工具略显单薄,但也无愧是一件移动开发的利器啊,有兴趣的朋友有时间就多了解吧. 使用引擎的过 ...

  4. Jupyter Notebook PDF输出的中文支持

    Jupyter Notebook是什么 Jupyter Notebook是ipython Notebook 的升级.Jupyter能够将实时代码,公式,可视化图表以Cell的方式组织在一起,形成一个对 ...

  5. inux 安装中文支持包及中文字符集配置 +i18n

    由于某些原因系统安装时未安装中文支持,导致后续应用出现中文方块乱码现象, 解决方法很简单,当然不是重装,只需以下三步即可搞定. .安装中文包: yum -y groupinstall chinese- ...

  6. linux 安装中文支持包及中文字符集配置

    由于某些原因系统安装时未安装中文支持,导致后续应用出现中文方块乱码现象,解决方法很简单,当然不是重装,只需以下三步即可搞定. 1.安装中文包: #yum -y groupinstall chinese ...

  7. OpenReports中文支持方案

    此文章在<OpenReports中文支持完全解决方案.doc>的基础上做优化,并贴出代码.已测试通过. 一.主要解决的问题 1 页面显示支持中文 2 与服务器或数据库的交互支持中文 3 查 ...

  8. 解决Boost.Regex对中文支持不好的问题

    解决Boost.Regex对中文支持不好的问题 - k.m.Cao - 博客频道 - CSDN.NET 解决Boost.Regex对中文支持不好的问题 k.m.Caov0.1   问题的提出: Boo ...

  9. centos安装中文支持(转)

    安装中文支持包. yum install fonts-chineseyum install fonts-ISO8859-2 -------- 一.安装中文支持方法1.在安装光盘中找到一下包进行安装.r ...

随机推荐

  1. C语言之宏

    所谓的宏就是一种预处理命令,什么是与处理呢?即在编译过程之前先对程序代码做出的必要的转换处理.宏有两个作用: 1.当遇到需要将程序某个特定的数量在程序中出现的所有实例通通加以修改时,程序只需改动一处即 ...

  2. Calculate drive total/free/available space

    using System; using System.Collections.Generic; using System.IO; using System.Text; namespace Consol ...

  3. Python [Leetcode 121]Best Time to Buy and Sell Stock

    题目描述: Say you have an array for which the ith element is the price of a given stock on day i. If you ...

  4. HiveQL 与 SQL的异同

    1 select 别名 (1)别名一定要加as 例:select ID as stuID from students (2) Hive QL不支持在group by, order by 中使用sele ...

  5. Clusterware后台进程

    Clusterware由若干进程组成,其中最重要的是CRSD,CSSD,EVMD   在Clusterware安装的最后阶段,会要求在每个节点执行root.sh脚本,这个脚本实际的作用就是在/etc/ ...

  6. Android 一步一步教你使用ViewDragHelper

    在自定义viewgroup的时候 要重写onInterceptTouchEvent和onTouchEvent 这2个方法 是非常麻烦的事情,好在谷歌后来 推出了ViewDragHelper这个类.可以 ...

  7. 基数排序/Go实现

    package main import ( "fmt" ) type Radix struct { length int //序列中最大数的位数 radix [][]int //0 ...

  8. 打造万能的ListView GridView 适配器

    转载:http://blog.csdn.net/lmj623565791/article/details/38902805/ 通用的ViewHolder 首先分析下ViewHolder的作用,通过co ...

  9. Linux下安装Vapor

    1.官网下载Vapor软件(二进制安装文件) 注:注意版本,linux下可以在终端输入-uname -l 查看系统版本 2.cd到Vapor软件所在目录 3.解压:1)gunzip vapor-*** ...

  10. 【LeetCode】198 - House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...