当前位置:   article > 正文

IK分词器原理

ik分词器原理

1、词典加载

  1. private void loadMainDict() {
  2. // 建立一个主词典实例
  3. _MainDict = new DictSegment((char) 0);
  4. // 读取主词典文件
  5. Path file = PathUtils.get(getDictRoot(), Dictionary.PATH_DIC_MAIN);
  6. loadDictFile(_MainDict, file, false, "Main Dict");
  7. // 加载扩展词典
  8. this.loadExtDict();
  9. // 加载远程自定义词库
  10. this.loadRemoteExtDict();
  11. }

2、词典数据结构

  1. public class DictSegment {
  2. //数组大小上限
  3. private static final int ARRAY_LENGTH_LIMIT = 3;
  4. //当前节点上存储的字符
  5. private Character nodeChar;
  6. //Map存储结构
  7. private Map<Character , DictSegment> childrenMap;
  8. //数组方式存储结构
  9. private DictSegment[] childrenArray;
  10. }

根据ARRAY_LENGTH_LIMIT作为阈值来存储方式,如果当子节点数,不太于阈值时,采用数组的方式childrenArray来存储,当子节点数大于阈值时,采用Map的方式childrenMap来存储。查找的时候hashmao可以获得O(1)的算法复杂度。

3、填充字典树

  1. /**
  2. * 加载填充词典片段
  3. * @param charArray
  4. * @param begin
  5. * @param length
  6. * @param enabled
  7. */
  8. private synchronized void fillSegment(char[] charArray, int begin, int length, int enabled) {
  9. //获取字典表中的汉字对象
  10. Character beginChar = new Character(charArray[begin]);
  11. Character keyChar = charMap.get(beginChar);
  12. //字典中没有该字,则将其添加入字典
  13. if (keyChar == null) {
  14. charMap.put(beginChar, beginChar);
  15. keyChar = beginChar;
  16. }
  17. //搜索当前节点的存储,查询对应keyChar的keyChar,如果没有则创建
  18. DictSegment ds = lookforSegment(keyChar, enabled);
  19. if (ds != null) {
  20. //处理keyChar对应的segment
  21. if (length > 1) {
  22. //词元还没有完全加入词典树
  23. ds.fillSegment(charArray, begin + 1, length - 1, enabled);
  24. } else if (length == 1) {
  25. //已经是词元的最后一个char,设置当前节点状态为enabled,
  26. //enabled=1表明一个完整的词,enabled=0表示从词典中屏蔽当前词
  27. ds.nodeState = enabled;
  28. }
  29. }
  30. }

4、查询字典树

  1. /**
  2. * 匹配词段
  3. * @param charArray
  4. * @param begin
  5. * @param length
  6. * @param searchHit
  7. * @return Hit
  8. */
  9. Hit match(char[] charArray, int begin, int length, Hit searchHit) {
  10. if (searchHit == null) {
  11. //如果hit为空,新建
  12. searchHit = new Hit();
  13. //设置hit的其实文本位置
  14. searchHit.setBegin(begin);
  15. } else {
  16. //否则要将HIT状态重置
  17. searchHit.setUnmatch();
  18. }
  19. //设置hit的当前处理位置
  20. searchHit.setEnd(begin);
  21. Character keyChar = new Character(charArray[begin]);
  22. DictSegment ds = null;
  23. //引用实例变量为本地变量,避免查询时遇到更新的同步问题
  24. DictSegment[] segmentArray = this.childrenArray;
  25. Map<Character, DictSegment> segmentMap = this.childrenMap;
  26. //STEP1 在节点中查找keyChar对应的DictSegment
  27. if (segmentArray != null) {
  28. //在数组中查找
  29. DictSegment keySegment = new DictSegment(keyChar);
  30. int position = Arrays.binarySearch(segmentArray, 0, this.storeSize, keySegment);
  31. if (position >= 0) {
  32. ds = segmentArray[position];
  33. }
  34. } else if (segmentMap != null) {
  35. //在map中查找
  36. ds = (DictSegment) segmentMap.get(keyChar);
  37. }
  38. //STEP2 找到DictSegment,判断词的匹配状态,是否继续递归,还是返回结果
  39. if (ds != null) {
  40. if (length > 1) {
  41. //词未匹配完,继续往下搜索
  42. return ds.match(charArray, begin + 1, length - 1, searchHit);
  43. } else if (length == 1) {
  44. //搜索最后一个char
  45. if (ds.nodeState == 1) {
  46. //添加HIT状态为完全匹配
  47. searchHit.setMatch();
  48. }
  49. if (ds.hasNextNode()) {
  50. //添加HIT状态为前缀匹配
  51. searchHit.setPrefix();
  52. //记录当前位置的DictSegment
  53. searchHit.setMatchedDictSegment(ds);
  54. }
  55. return searchHit;
  56. }
  57. }
  58. //STEP3 没有找到DictSegment, 将HIT设置为不匹配
  59. return searchHit;
  60. }

5、切分词元

Lexeme(词元),就可以理解为是一个词语或个单词。Lexeme类实现Comparable的,起始位置靠前的优先,长度较长的优先,这可以用来决定一个词在一条分词结果的词元链中的位置

  1. public int compareTo(Lexeme other) {
  2. //起始位置优先
  3. if(this.begin < other.getBegin()){
  4. return -1;
  5. }else if(this.begin == other.getBegin()){
  6. //词元长度优先
  7. if(this.length > other.getLength()){
  8. return -1;
  9. }else if(this.length == other.getLength()){
  10. return 0;
  11. }else {//this.length < other.getLength()
  12. return 1;
  13. }
  14. }else{//this.begin > other.getBegin()
  15. return 1;
  16. }
  17. }

IK中默认用到三个子分词器,分别是LetterSegmenter(字母分词器),CN_QuantifierSegment(量词分词器),CJKSegmenter(中日韩分词器)。

CN_QuantifierSegmenter的词典来源两个地方:1.quantifier.dic文件,包含量词 2.数词直接写到ChnNumberChars类中了,内容如下:"一二两三四五六七八九十零壹贰叁肆伍陆柒捌玖拾百千万亿拾佰仟萬億兆卅廿"

LetterSegmenter分别有三个类似的处理器:字母、数字、字母和数字的组合。

输入词循环单字经过分词器,匹配的结果一共三种UNMATCH(未匹配),MATCH(匹配), PREFIX(前缀匹配),Match指完全匹配已经到达叶子节点,而PREFIX是指当前对上所经过的匹配路径存在,但未到达到叶子节点。此外一个词也可以既是MATCH也可以是PREFIX。前缀匹配的都被存入了tempHit中去。而完整匹配的都存入context中保存。

6、歧义判断(选择最优路径)

IKArbitrator(歧义分析裁决器)是处理歧义的主要类。ik_smart模式会进行歧义判断,ik_max_word会返回所有的分词词元。

ik解决歧义流程:

1.贪心选择其中不相交的分词结果,存放到分词候选结果集option中

2.把存在歧义的词,也就是和option中的词相交的词放入conflickStack中

3.从conflictStack中选出一个歧义词c,从option结尾回滚option词元链,直到能放下词c

4.从词c的位置执行forwardPath,生成一个可选分词结果集

5.直到conflictStack中的所有歧义词处理完毕

通过上面方式生成分词结果集,对结果集排序,取最优:

1)比较有效文本长度

2)比较词元个数,越少越好

3)路径跨度越大越好

4)根据统计学结论,逆向切分概率高于正向切分,因此位置越靠后的优先

5)词长越平均越好

6)词元位置权重比较

  1. public int compareTo(LexemePath o) {
  2. //比较有效文本长度
  3. if(this.payloadLength > o.payloadLength){
  4. return -1;
  5. }else if(this.payloadLength < o.payloadLength){
  6. return 1;
  7. }else{
  8. //比较词元个数,越少越好
  9. if(this.size() < o.size()){
  10. return -1;
  11. }else if (this.size() > o.size()){
  12. return 1;
  13. }else{
  14. //路径跨度越大越好
  15. if(this.getPathLength() > o.getPathLength()){
  16. return -1;
  17. }else if(this.getPathLength() < o.getPathLength()){
  18. return 1;
  19. }else {
  20. //根据统计学结论,逆向切分概率高于正向切分,因此位置越靠后的优先
  21. if(this.pathEnd > o.pathEnd){
  22. return -1;
  23. }else if(pathEnd < o.pathEnd){
  24. return 1;
  25. }else{
  26. //词长越平均越好
  27. if(this.getXWeight() > o.getXWeight()){
  28. return -1;
  29. }else if(this.getXWeight() < o.getXWeight()){
  30. return 1;
  31. }else {
  32. //词元位置权重比较
  33. if(this.getPWeight() > o.getPWeight()){
  34. return -1;
  35. }else if(this.getPWeight() < o.getPWeight()){
  36. return 1;
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }
  43. return 0;
  44. }

7、输出阶段

  1. Lexeme getNextLexeme(){
  2. //从结果集取出,并移除第一个Lexme
  3. Lexeme result = this.results.pollFirst();
  4. while(result != null){
  5. //数量词合并
  6. this.compound(result);
  7. if(Dictionary.getSingleton().isStopWord(this.segmentBuff , result.getBegin() , result.getLength())){
  8. //是停止词继续取列表的下一个
  9. result = this.results.pollFirst();
  10. }else{
  11. //不是停止词, 生成lexeme的词元文本,输出
  12. result.setLexemeText(String.valueOf(segmentBuff , result.getBegin() , result.getLength()));
  13. break;
  14. }
  15. }
  16. return result;
  17. }

最后输出阶段做了以下结果处理:

1.smart模式进行英文数字+中文数字,英文数词+中文量词合并

2.移除停用词

8、根据业务需求进行增加优化

字典树生成时候可以对每个dictSegment,给出词性分或者热度分。再歧义裁决时候考虑进去。

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号