1、例子:
分词串:这是一个中文分词的例子
采用智能分词
有重合元素即为相交
词元链:2--4 相交 词元:2 - 3 3-5 3-4 不相交 4-6
2-4和3-5相交,3-5又和4-6相交
词元链LexemePath crossPath对象:
pathBegin : 2
pathEnd : 6
payloadLength : 4
lexeme : 2-4 : 一个 : CN_WORD
lexeme : 2-3 : 一 : TYPE_CNUM
lexeme : 3-5 : 个中 : CN_WORD
lexeme : 3-4 : 个 : COUNT
lexeme : 4-6 : 中文 : CN_WORD
//候选路径集合,第一个2--6作为最优结果
TreeSet<LexemePath> pathOptions = new TreeSet<LexemePath>();
[pathBegin : 2
pathEnd : 6
payloadLength : 4
lexeme : 2-4 : null : CN_WORD
lexeme : 4-6 : null : CN_WORD
, pathBegin : 3
pathEnd : 6
payloadLength : 3
lexeme : 3-4 : null : COUNT
lexeme : 4-6 : null : CN_WORD
, pathBegin : 2
pathEnd : 5
payloadLength : 3
lexeme : 2-3 : null : TYPE_CNUM
lexeme : 3-5 : null : CN_WORD
, pathBegin : 3
pathEnd : 5
payloadLength : 2
lexeme : 3-5 : null : CN_WORD
]
歧义处理结果:
pathBegin : 2
pathEnd : 6
payloadLength : 4
lexeme : 2-4 : 一个 : CN_WORD
lexeme : 4-6 : 中文 : CN_WORD
2、源码分析歧义处理
从头遍历词元链crossPath,生成一条新的不包含相交的词元的链(新的无歧义词元组合),并找到所有相交的Lexeme栈lexemeStack。
遍历lexemeStack把相交的Lexeme作为起始位置,生成新的无歧义词元组合。
把无歧义词元组合加入候选路径集合TreeSet<LexemePath> pathOptions排序,排序规则包括“有效文本长度”等。把第一个作为结果。
//对当前的crossPath进行歧义处理 QuickSortSet.Cell headCell = crossPath.getHead(); LexemePath judgeResult = this.judge(headCell, crossPath.getPathLength()); TreeSet<LexemePath> pathOptions使用LexemePath的compareTo方法排序,pathOptions.first()作为最优结果。 /** * 歧义识别 * @param lexemeCell 歧义路径链表头 * @param fullTextLength 歧义路径文本长度 * @param option 候选结果路径 * @return */ private LexemePath judge(QuickSortSet.Cell lexemeCell , int fullTextLength){ //候选路径集合 TreeSet<LexemePath> pathOptions = new TreeSet<LexemePath>(); //候选结果路径 LexemePath option = new LexemePath(); //对crossPath进行一次遍历,同时返回本次遍历中有冲突的Lexeme栈。 //zw:链中第一个元素为2-4 : null : CN_WORD 所以相交于 2-3 : null : TYPE_CNUM , 3-5 : null : CN_WORD , 3-4 : null : COUNT Stack<QuickSortSet.Cell> lexemeStack = this.forwardPath(lexemeCell , option); //当前词元链并非最理想的,加入候选路径集合 pathOptions.add(option.copy()); //存在歧义词,处理 QuickSortSet.Cell c = null; while(!lexemeStack.isEmpty()){ c = lexemeStack.pop(); //回滚词元链 this.backPath(c.getLexeme() , option); //从歧义词位置开始,递归,生成可选方案 this.forwardPath(c , option); pathOptions.add(option.copy()); } //返回集合中的最优方案 return pathOptions.first(); } /** * 向前遍历,添加词元,构造一个无歧义词元组合 * @param LexemePath path * @return */ private Stack<QuickSortSet.Cell> forwardPath(QuickSortSet.Cell lexemeCell , LexemePath option){ //发生冲突的Lexeme栈 Stack<QuickSortSet.Cell> conflictStack = new Stack<QuickSortSet.Cell>(); QuickSortSet.Cell c = lexemeCell; //迭代遍历Lexeme链表 while(c != null && c.getLexeme() != null){ if(!option.addNotCrossLexeme(c.getLexeme())){ //词元交叉,添加失败则加入lexemeStack栈 conflictStack.push(c); } c = c.getNext(); } return conflictStack; } /** * 回滚词元链,直到它能够接受指定的词元 * @param lexeme * @param l */ private void backPath(Lexeme l , LexemePath option){ while(option.checkCross(l)){ option.removeTail(); } } /** * 向LexemePath追加不相交的Lexeme * @param lexeme * @return */ boolean addNotCrossLexeme(Lexeme lexeme){ if(this.isEmpty()){ this.addLexeme(lexeme); this.pathBegin = lexeme.getBegin(); this.pathEnd = lexeme.getBegin() + lexeme.getLength(); this.payloadLength += lexeme.getLength(); return true; }else if(this.checkCross(lexeme)){ return false; }else{ this.addLexeme(lexeme); this.payloadLength += lexeme.getLength(); Lexeme head = this.peekFirst(); this.pathBegin = head.getBegin(); Lexeme tail = this.peekLast(); this.pathEnd = tail.getBegin() + tail.getLength(); return true; } } /** * Lexeme链(路径) */ class LexemePath extends QuickSortSet implements Comparable<LexemePath> public int compareTo(LexemePath o) { //比较有效文本长度 if(this.payloadLength > o.payloadLength){ return -1; }else if(this.payloadLength < o.payloadLength){ return 1; }else{ //比较词元个数,越少越好 if(this.size() < o.size()){ return -1; }else if (this.size() > o.size()){ return 1; }else{ //路径跨度越大越好 if(this.getPathLength() > o.getPathLength()){ return -1; }else if(this.getPathLength() < o.getPathLength()){ return 1; }else { //根据统计学结论,逆向切分概率高于正向切分,因此位置越靠后的优先 if(this.pathEnd > o.pathEnd){ return -1; }else if(pathEnd < o.pathEnd){ return 1; }else{ //词长越平均越好 if(this.getXWeight() > o.getXWeight()){ return -1; }else if(this.getXWeight() < o.getXWeight()){ return 1; }else { //词元位置权重比较 if(this.getPWeight() > o.getPWeight()){ return -1; }else if(this.getPWeight() < o.getPWeight()){ return 1; } } } } } } return 0; }
相关推荐
多个版本ik分词器源码
IKAnalyzer 源码解析,三个核心模块,词典加载、分词处理、歧义处理,分词处理和歧义处理都有例子进行解析。
IK3.2.8源码分析后的整理文档,包括原理、结构等。(原创)
IKAnalyzer 分词源码,有案例,支持jdk1.6
IK分词器源码,可以研究IK分词器原理,也可以修改源码达到自己的业务需求。
IK 分词器源码 此版本IK Analyzer 2012FF版,支持4Lucene.0以上 , 适合大家学习,开发自己的分词器,或参与该项目开发
IKAnalyzer 源码解析,三个核心模块,词典加载、分词处理、歧义处理,分词处理和歧义处理都有例子进行解析。
IK分词全名为IK Analyzer,是由java编写的中文分词工具包,目前在lucene以及solr中用的比较多,本系列的文章主要对ik的核心源码进行解析讲解配套的资源,http://blog.csdn.net/a925907195
IKAnalyzer2012源码
linux版本的elasticsearch-6.4.3-全套(elasticsearch安装包+源码+ik分词器+kibana)
SpringBoot集成Solr全文检索,示例工程源码,ik分词插件
IKAnalyzer2012_u6中文分词器以及手册正式版 Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2017/10/29 1:41 doc -a---- 2017/10/29 1:41 414 IKAnalyzer.cfg.xml -a---- 2017/10/29 1...
ikanalyzer源码,解析ikanalyzer的实现过程,了解ikanalyzer的原理
网上的包基本都没有智能分词了,下载下来基本也是缺的,此包已经包含智能分词,并附上相应的IKAnalyzer源码包,可以自行修改相关字典
IK算法源码
IKAnalyzer3.2.8分词器 源码,用户lucene索引开发中
支持停止词,扩展词热更新,将分析器自定义为ik_smart_custom和ik_max_word_custom了,使用idea导入项目,mvn clear complile package 打包完成,使用release中的zip文件部署,记得修改db.properties中的数据库连接...
在2012版本中,IK实现了简单的分词 歧义排除算法,标志着IK分词器从单纯的词典分词向模拟语义分词衍化。 IK Analyzer 2012特性: 采用了特有的“正向迭代最细粒度切分算法“,支持细粒度和智能分词两种切分模式; ...
solr5的ik中文分词器源码,解压后需要自己打包成jar包