利用IK分词器,自定义分词规则
2015-08-31 14:45
381 查看
IK分词源码下载地址:https://code.google.com/p/ik-analyzer/downloads/list
lucene源码下载地址:http://www.eu.apache.org/dist/lucene/java/
下载IK分词源码后,运行出现错误提示:
解决办法:
在项目project -->clean 下即可
自定义分词规则步骤:
里面的例子:
分词结果:
获得分词:张柏芝
获得分词:士
获得分词:蛋糕
获得分词:房
获得分词:zhang
获得分词:s
获得分词:cake
获得分词:shop
获得分词:网友
获得分词:们
获得分词:hold
获得分词:不
获得分词:住了
获得分词:宋祖英
获得分词:语
获得分词:培训班
获得分词:周渝民
获得分词:政
获得分词:服务中心
获得分词:容祖儿
获得分词:童
获得分词:医院
获得分词:吴奇隆
获得分词:胸
获得分词:医院
获得分词:苏永康
获得分词:复
获得分词:中心
获得分词:梁朝伟
获得分词:哥
获得分词:专卖
获得分词:陈冠希
获得分词:望
获得分词:小学
获得分词:吴彦祖
获得分词:传
获得分词:中医
获得分词:坊
获得分词:林
获得分词:书
获得分词:豪华酒店
这样分词不是很智能,分词需要我们自己设置。
存在的问题 还需要定义歧义字典。但是ik不支持歧义字典
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不支持歧义字典
相关文章推荐
- 基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系
- OC之NSString
- android学习笔记之Intent与BroadcastReceiver
- BZOJ 3668([Noi2014]起床困难综合症-2进制拆分)
- 15. jQuery - 删除元素
- spring错误-在spring里面找不到org.springframework.cache.ehcache.EhCacheManagerFactoryBean
- MFC MDI应用程序 自定义标题
- Java的注释说明
- mongodb的基本操作
- android 选中效果xml文件
- 阿里无线前端招聘内容一览(2)
- STL - 迭代器 - 安插型迭代器
- 单击操作即将鼠标移动至某个对象
- 用POI导出excel时,较长的数字不想被自动变为科学计数法的解决方式
- 命令模式
- ios8下,alertController之初体验
- linux下sed的使用
- LeetCode OJ 之 Binary Tree Paths(二叉树路径)
- android 使用百度云推送
- poj 1273 Drainage Ditches