Solr java IKAnalyzer词库

首先需要引入一下两个包:

IKAnalyzer2012FF_u1.jar

lucene-core-4.3.1.jar

如果想和服务器上的停词库同步可以把stopword.dic拷贝进来。

别的什么都不需要进行配置,代码如下

/**
 * IK分词器两种写法
 * @author zhouguohui
 *
 */
public class test {
    public static void main( String[] args ){
    	test1();
    	test2();
    }
    
    /**
     * 使用IK实现
     */
    public static void test1(){
    	try {
			String text="基于IK分词器的两种不同写法的分词工具,这是第一种";  
	        //创建分词对象  
	        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();
			
		} catch (IOException e) {
			e.printStackTrace();
		}  
    }
    
    /**
     * 使用lucene实现
     */
    public static void test2(){
    	try{
	       String text="基于IK分词器的两种不同写法的分词工具,这是第二种";  
	        StringReader sr=new StringReader(text);  
            IKSegmenter ik=new IKSegmenter(sr, true);  
	        Lexeme lex=null;  
	        while((lex=ik.next())!=null){  
	           System.out.print(lex.getLexemeText()+"|");  
	       }  
	    } catch (IOException e) {
			e.printStackTrace();
		} 
    }
}

  运行结果为:

基于|ik|分词器|的|两种|不同|写法|的|分词|工具|这是|第一种|
基于|ik|分词器|的|两种|不同|写法|的|分词|工具|这是|第二种|
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值