package com.haha.test;
import java.io.IOException;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class Test2 {
public static void main(String[] args) throws IOException {
String text="基于java语言开发的轻量级的中文分词工具包";
//创建分词对象
Analyzer anal=new IKAnalyzer(true);
StringReader reader=new StringReader(text);
//分词
TokenStream ts=anal.tokenStream("", reader);
CharTermAttribute term=ts.getAttribute(CharTermAttribute.class);
//遍历分词数据
while(ts.incrementToken()){
System.out.print(term.toString()+"|");
}
reader.close();
System.out.println();
}
}
package com.haha.test;
import java.io.IOException;
import java.io.StringReader;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
public class Test3 {
public static void main(String[] args) throws IOException {
String text="基于java语言开发的轻量级的中文分词工具包";
StringReader sr=new StringReader(text);
IKSegmenter ik=new IKSegmenter(sr, true);
Lexeme lex=null;
while((lex=ik.next())!=null){
System.out.print(lex.getLexemeText()+"|");
}
}
}
package IKAnalyzerTest;
import java.io.IOException;
import java.io.StringReader;
import org.wltea.analyzer.IKSegmentation;
import org.wltea.analyzer.Lexeme;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws IOException{
String str = "从那时起,我就不是坏人了";
StringReader reader = new StringReader(str);
IKSegmentation ik = new IKSegmentation(reader,true);//当为true时,分词器进行最大词长切分
Lexeme lexeme = null;
while((lexeme = ik.next())!=null)
System.out.println(lexeme.getLexemeText());
}
}