分词器之IK Analyzer

IK Analyzer 是基于Lucene的中文分词框架。

一:下载分词器

 IK Analyzer 2012FF 百度网盘下载地址:http://pan.baidu.com/s/1boFDd9d

IKAnalyzer-5.0 百度网盘下载地址:http://pan.baidu.com/s/1dE5zBrV

二:IK Analyzer 分词器使用

 在项目中引入如下:

a,IKAnalyzer2012FF_u1.jar 或者IKAnalyzer-5.0.jar

b,IKAnalyzer.cfg.xml

c,mydict.dic

d,ext_stopword.dic

示例代码: Lucene的版本是6.1.0

package com.lucene.analyzer;

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;

/**
 * 分词器测试类
 * @author luorufeng
 * @date  2016年8月2日
 */
public class TestAnalyzer {
	public static void main(String[] args) throws IOException {
//		Analyzer analyzer = new MyIKAnalyzer();//庖丁分词器  2012  
		//http://blog.sina.com.cn/s/blog_69a69e1a0102w8br.html
		Analyzer analyzer = new IKAnalyzer();//庖丁分词器        

		String text = "lucene是一个很高大上、很牛逼的java搜索引擎工具包,大家快点来学习";
		testAnalyzer(analyzer,text);
	}
	
	/**
	 * 分词器测试方法
	 * @author luorufeng
	 * @date  2016年8月2日
	 * @param analyzer
	 * @param text
	 * @throws IOException 
	 */
	public static void testAnalyzer(Analyzer analyzer,String text) throws IOException{
		System.out.println("当前分词器:"+analyzer.getClass().getSimpleName());
		//分词  
		TokenStream tokenStream = analyzer.tokenStream("content", new StringReader(text));
		CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
		tokenStream.reset();
		//遍历
		while(tokenStream.incrementToken()){
			System.out.print(charTermAttribute+"|");
		}

		tokenStream.end();
		tokenStream.close();
	}
}
 

运行后的运行结果:

 当前分词器:IKAnalyzer

加载扩展词典:mydict.dic

加载扩展停止词典:ext_stopword.dic

lucene|是|一个|一|个|很高|高大上|高大|上|很|牛逼|java|搜索引擎|搜索|索引|引擎|工具包|工具|包|大家|快点|点来|学习|

相关推荐