zoukankan      html  css  js  c++  java
  • 利用IK分词器,自定义分词规则

    IK分词源码下载地址:https://code.google.com/p/ik-analyzer/downloads/list

    lucene源码下载地址:http://www.eu.apache.org/dist/lucene/java/

    下载IK分词源码后,运行出现错误提示:

    Analyzer cannot be resolved to a type
    	TokenStream cannot be resolved to a type
    	OffsetAttribute cannot be resolved to a type
    	OffsetAttribute cannot be resolved to a type
    	CharTermAttribute cannot be resolved to a type
    	CharTermAttribute cannot be resolved to a type
    	TypeAttribute cannot be resolved to a type
    	TypeAttribute cannot be resolved to a type
    

     解决办法:

    在项目project -->clean 下即可

    自定义分词规则步骤:

    里面的例子:

    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.TokenStream;
    import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
    import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
    import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
    import org.wltea.analyzer.lucene.IKAnalyzer;
    public static void main(String[] args) {
    		String testString = "张柏芝士蛋糕房 ZHANG'S CAKE SHOP,网友们Hold不住了:宋祖英语培训班、周渝民政服务中心、容祖儿童医院、吴奇隆胸医院、苏永康复中心、梁朝伟哥专卖、陈冠希望小学、吴彦祖传中医坊、林书豪华酒店";
    		iktest1(testString);
    	}
    	
    	// 实现普通分词
    	public  static Map<String, Object>  iktest1(String testString){
    		Map<String, Object> resultsMap = new HashMap<String, Object>();
    		Analyzer ikAnalyzer = new IKAnalyzer(true);
    		  TokenStream ts = null;
    		  try {
    			   ts = ikAnalyzer.tokenStream("myik", testString);
    			  //词元位置属性
    			  OffsetAttribute offset = ts.addAttribute(OffsetAttribute.class);
    			  //词文本属性
    			  CharTermAttribute term = ts.addAttribute(CharTermAttribute.class);
    			  //词文本属性
    			  TypeAttribute type = ts.addAttribute(TypeAttribute.class);
    			  ts.reset();
    				while (ts.incrementToken()){
    					resultsMap.put("获得分词", term.toString());
    					for (Object obj : resultsMap.entrySet()) {
    			            Entry entry = (Entry) obj;
    			            String key = (String) entry.getKey();
    			            String value = (String) entry.getValue();
    			            System.out.println(key + ":" + value);
    			        }
    					//System.out.println(resultsMap);
    					//  System.out.println(offset.startOffset() + " - " + offset.endOffset() + " : " + term.toString() + " | " + type.type());
    				}
    			  ts.end();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally{
    			if (ts != null){
    				try {
    					ts.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
    		  return resultsMap;
    	}
    	
    	//实现只能分词2
    	public static void testik02(){
    		
    	}
    

    分词结果:

    获得分词:张柏芝
    获得分词:士
    获得分词:蛋糕
    获得分词:房
    获得分词:zhang
    获得分词:s
    获得分词:cake
    获得分词:shop
    获得分词:网友
    获得分词:们
    获得分词:hold
    获得分词:不
    获得分词:住了
    获得分词:宋祖英
    获得分词:语
    获得分词:培训班
    获得分词:周渝民
    获得分词:政
    获得分词:服务中心
    获得分词:容祖儿
    获得分词:童
    获得分词:医院
    获得分词:吴奇隆
    获得分词:胸
    获得分词:医院
    获得分词:苏永康
    获得分词:复
    获得分词:中心
    获得分词:梁朝伟
    获得分词:哥
    获得分词:专卖
    获得分词:陈冠希
    获得分词:望
    获得分词:小学
    获得分词:吴彦祖
    获得分词:传
    获得分词:中医
    获得分词:坊
    获得分词:林
    获得分词:书
    获得分词:豪华酒店

    这样分词不是很智能,分词需要我们自己设置。

    存在的问题 还需要定义歧义字典。但是ik不支持歧义字典

  • 相关阅读:
    node项目发布pm2
    图片地址获取图片信息
    前端文件上传 获取文件大小 获取图片宽高
    前端上传图片预览
    vue按钮防暴力点击
    小程序父子组件之间的通信
    form表单
    es6-函数的扩展
    php的魔术常量以及类的模式方法
    OpenStack笔记
  • 原文地址:https://www.cnblogs.com/zhanggl/p/4773036.html
Copyright © 2011-2022 走看看