package com.zhangzhanlei.lucene;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.tokenattributes.CharTermAttribute;
import org.apache.lucene.util.Version;
public class SmartChineseAnalyzer_T
{
@SuppressWarnings("resource")
public List<String> analyzerCnStr(String str)
{
List<String> result = new ArrayList<String>();
Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_46,true);
try
{
TokenStream tokenStream = analyzer.tokenStream("field", str);
CharTermAttribute term = tokenStream.addAttribute(CharTermAttribute.class);
tokenStream.reset();
while(tokenStream.incrementToken())
{
result.add(term.toString());
}
tokenStream.end();
tokenStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return result;
}
/**
* @param args
*/
public static void main(String[] args)
{
SmartChineseAnalyzer_T analyzer = new SmartChineseAnalyzer_T();
List<String> l = analyzer.analyzerCnStr("对于Lucene4.3开发首先摆在我们面前的第一个必须要解决的问题,就是关于中文分词的问题,因为Lucene毕竟是国外的大牛们开发的,显然会比较侧重英文文章,不过还好,在Lucene的下载包里同步了SmartCN的分词器针对中文发行的,每一次Lucene有新的版本发行,这个包同时更新");
System.out.println(l);
}
}
[对于, lucen, 4, 3, 开发, 首先, 摆, 在, 我们, 面前, 的, 第一, 个, 必须, 要, 解决, 的, 问题, 就, 是, 关于, 中文, 分, 词, 的, 问题, 因为, lucen, 毕竟, 是, 国外, 的, 大, 牛, 们, 开发, 的, 显然, 会, 比较, 侧重, 英文, 文章, 不过, 还, 好, 在, lucen, 的, 下载, 包, 里, 同步, 了, smartcn, 的, 分词, 器, 针对, 中文, 发行, 的, 每, 一, 次, lucen, 有, 新, 的, 版本, 发行, 这个, 包, 同时, 更, 新]