@Test
//测试中文分词器效果
public void testIkAnalyzer() throws Exception{
Analyzer analyzer = new IKAnalyzer();
String text = "测试测hi实测hi上次嗯好四黑";
TokenStream tokenStream = analyzer.tokenStream("fileName", new StringReader(text));
CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);
tokenStream.reset();
while(tokenStream.incrementToken()){
System.out.println("开始-》"+offsetAttribute.startOffset());
System.out.println(charTermAttribute);
System.out.println("结束-》"+offsetAttribute.endOffset());
}
}
添加 IKAnalyzer.cfg.xml,main.dic,stopword.dic配置文件,其中main.dic用来添加新词,实现解析分词时,某一类特定的词语不分开,当做一个词语(例如“白富美”,“高富帅”等),stopword.dic用来忽略某些词,而 IKAnalyzer.cfg.xml用来加载上述两个配置文件