Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1964835
  • 博文数量: 606
  • 博客积分: 9991
  • 博客等级: 中将
  • 技术积分: 5725
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-17 19:07
文章分类

全部博文(606)

文章存档

2011年(10)

2010年(67)

2009年(155)

2008年(386)

分类: Java

2009-05-18 15:19:54

加lucene-core-2.3.2.jar和lucene-Analyzer.jar以及IKAnalyzer.jar这几个包,有问题call我

import java.io.Reader;
import java.io.StringReader;



import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.StopFilter;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.mira.lucene.analysis.IK_CAnalyzer;


public class TestJeAnalyzer {
private static String testString1 = "冗长的代码常常是复杂性的标志,会导致代码难以测试和维护.";
public static void testStandard(String testString) throws Exception{
Analyzer analyzer = new StandardAnalyzer();
Reader r = new StringReader(testString);
StopFilter sf = (StopFilter) analyzer.tokenStream("", r);
System.err.println("=====standard analyzer====");
System.err.println("分析方法:默认没有词只有字");
Token t;
while ((t = sf.next()) != null) {
System.out.println(t.termText());
}
}
public static void testCJK(String testString) throws Exception{
Analyzer analyzer = new CJKAnalyzer();
Reader r = new StringReader(testString);
StopFilter sf = (StopFilter) analyzer.tokenStream("", r);
System.err.println("=====cjk analyzer====");
System.err.println("分析方法:交叉双字分割");
Token t;
while ((t = sf.next()) != null) {
System.out.println(t.termText());
}
}
public static void testChiniese(String testString) throws Exception{
Analyzer analyzer = new ChineseAnalyzer();
Reader r = new StringReader(testString);
TokenFilter tf = (TokenFilter) analyzer.tokenStream("", r);
System.err.println("=====chinese analyzer====");
System.err.println("分析方法:基本等同StandardAnalyzer");
Token t;
while ((t = tf.next()) != null) {
System.out.println(t.termText());
}
}

public static void testJe(String testString) throws Exception{
// Analyzer analyzer = new MIK_CAnalyzer();
Analyzer analyzer = new IK_CAnalyzer();
Reader r = new StringReader(testString);
TokenStream ts = (TokenStream)analyzer.tokenStream("", r);
System.err.println("=====je analyzer====");
System.err.println("分析方法:字典分词,正反双向,具体不明");
Token t;
while ((t = ts.next()) != null) {
System.out.println(t.termText());
}
}
public static void main(String[] args) throws Exception{
// String testString = testString1;
String testString = testString1;
System.out.println(testString);

testStandard(testString);
testCJK(testString);
// testPaoding(testString);

testChiniese(testString);
testJe(testString);
}

}
阅读(1399) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~