package com.cndatacom.lucene.test;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class Test
{
public static void main(String[] args) throws IOException
{
ArrayList<String> analysisWords = new ArrayList<String>();
IKAnalyzer analyzer = new IKAnalyzer();
//基于Lucene实现
StringReader reader = new StringReader("我们是中国人");
TokenStream tokenStream = analyzer.tokenStream("", reader);
tokenStream.addAttribute(CharTermAttribute.class);
CharTermAttribute charTermAttribute = tokenStream.getAttribute(CharTermAttribute.class);
//取出所有分词
while (tokenStream.incrementToken()) {
analysisWords.add(charTermAttribute.toString());
}
reader.close();
System.out.println(analysisWords);
}
}
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class Test
{
public static void main(String[] args) throws IOException
{
ArrayList<String> analysisWords = new ArrayList<String>();
IKAnalyzer analyzer = new IKAnalyzer();
//基于Lucene实现
StringReader reader = new StringReader("我们是中国人");
TokenStream tokenStream = analyzer.tokenStream("", reader);
tokenStream.addAttribute(CharTermAttribute.class);
CharTermAttribute charTermAttribute = tokenStream.getAttribute(CharTermAttribute.class);
//取出所有分词
while (tokenStream.incrementToken()) {
analysisWords.add(charTermAttribute.toString());
}
reader.close();
System.out.println(analysisWords);
}
}