java classifier4j_Classifier4J的中文支持

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 tokens = new Vector();

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只定义了英文中的停用词,对于中文而言,庖丁分词的词典中已经包含了停用词。

Java项目中引入XGBoost(eXtreme Gradient Boosting)的库,如xgboost4j和xgboost4j-spark,通常是为了利用XGBoost的梯度提升机器学习算法。以下是如何添加这两个依赖的步骤: 1. **xgboost4j** (Java原生接口): - 如果你在Maven项目中,你需要添加`xgboost4j`和`xgboost4j-native`库到pom.xml文件中: ```xml <dependencies> ... <dependency> <groupId>ml.dmlc</groupId> <artifactId>xgboost4j</artifactId> <version>1.4.0</version> </dependency> <dependency> <groupId>ml.dmlc</groupId> <artifactId>xgboost4j-native</artifactId> <classifier>linux-x64</classifier> <version>1.4.0</version> </dependency> <!-- 根据你的操作系统调整classifier --> ... </dependencies> ``` - 如果使用Gradle,可以在build.gradle文件中添加类似的内容: ```groovy implementation 'ml.dmlc:xgboost4j:1.4.0' natives 'ml.dmlc:xgboost4j-native:1.4.0:linux-x64' // 或者根据你的系统调整 ``` 2. **xgboost4j-spark** (Spark集成): - 对于Spark项目,除了基本的xgboost4j依赖,还需要添加`xgboost4j-spark`依赖,它提供了XGBoost与Spark的交互支持: ```xml <dependency> <groupId>ml.dmlc</groupId> <artifactId>xgboost4j-spark_2.12</artifactId> <version>1.4.0</version> </dependency> ``` - 或者在Gradle中: ```groovy implementation 'ml.dmlc:xgboost4j-spark_2.12:1.4.0' ``` 注意这里的`_2.12`表示兼容的是Scala 2.12版本的Spark,如果你的项目使用其他版本,请相应地替换。 确保下载了对应的JAR包或从Maven仓库获取,然后将它们添加到项目的类路径中才能正常使用XGBoost的API。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值