lucene3.1.0 简单分词实例

[color=red][b]环境说明[/b][/color] lucene版本3.1.0
分词工具英文版的是用标准版的,即StandardAnalyzer
中文分词是用SmartChineseAnalyzer,lucene包中有
使用的junit4.0测试的


import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.util.Version;
import org.junit.Test;

public class Analyzertest {

//Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_31);

//String text = "我是中国人";
String text = "IndexWriter javadoc a apach2.0.txt";

@Test
public void test ()
{
try {
analyzer(analyzer,text);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void analyzer(Analyzer al, String text) throws Exception
{
TokenStream tokeStream = al.tokenStream("content", new StringReader(text));
//TermAttribute 已过时,文档中推荐使用CharTermAttribute
tokeStream.addAttribute(CharTermAttribute.class);
while(tokeStream.incrementToken())
{

CharTermAttribute ta = tokeStream.getAttribute(CharTermAttribute.class);
System.out.println(ta.toString());
//System.out.println(tokeStream.toString());
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值