hankcs / hanlp-lucene-plugin

HanLP中文分词Lucene插件,支持包括Solr在内的基于Lucene的系统
http://www.hankcs.com/nlp/segment/full-text-retrieval-solr-integrated-hanlp-chinese-word-segmentation.html
Apache License 2.0
296 stars 99 forks source link

配置自定义词典部分有效部分无效 #39

Closed yooopan closed 6 years ago

yooopan commented 6 years ago

注意事项

请确认下列注意事项:

版本号

当前最新版本号是:1.6.7 我使用的版本是:1.6.7

我的问题

solr中使用自定义词库,部分词分词成功,部分词分不出来。

场景:医疗搜索

步骤1: 定义了一个hanlp分词的type,其中customDictionaryPath指定了我的自定义词典路径,同时设置enableCustomDictionaryForcing为true:

   <fieldType name="hanlp_syn_disease" class="solr.TextField" positionIncrementGap="100">
          <analyzer type="index">
               <tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" customDictionaryPath="/Users/pan/cn_dic/disease.txt"  enableIndexMode="true" enableNameRecognize="true" enableOrganizationRecognize="true" enableCustomDictionaryForcing="true"/>
               <filter class="solr.SynonymFilterFactory" synonyms="synonyms_disease.txt" ignoreCase="true" expand="true"/>
          </analyzer>
          <analyzer type="query">
          <!-- 切记不要在query中开启index模式 -->
               <tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" enableIndexMode="false" enableNameRecognize="true" enableOrganizationRecognize="true"/>
          </analyzer>
    </fieldType>

词典第110行有一个:腰部脊柱和骨盆其他和未特指部位的扭伤和劳损:

screen shot 2018-08-30 at 3 49 05 pm

solr中的分词效果:

screen shot 2018-08-30 at 3 49 40 pm

这个效果还是比较满意的。

第345行有一个:短暂性全面遗忘

screen shot 2018-08-30 at 3 51 14 pm

但是在solr中分不出来了:

screen shot 2018-08-30 at 3 52 02 pm

我的困惑是为什么同一个文件,有些能够分词成功,有些分不出来。

已经参考 https://github.com/hankcs/HanLP/issues/789

知道自定义词典上不能保证词典中的词一定能切分出来。但是为什么有些词是可以分出来的,有些却不能。

请指教。

yooopan commented 6 years ago

还有,在成功加载用户词典的情况下,比如“新生儿颅内出血”,如何分出:

新生儿颅内出血

而不是:

新生儿颅内出血   新生儿  新生  生儿  颅内出血  颅内 内出血  出血

简单的来说,如何只保留自定义词库中的结果?

hankcs commented 6 years ago

第一个问题:

  1. 请使用1.6.8(重要)
  2. 请确认词典中“短暂性全面遗忘”前后是否有不可见的字符?

你可以用Java调试:

        Segment segment = HanLP.newSegment();
        // 从文件加载
        HanLP.Config.CustomDictionaryPath = new String[]{"/Users/pan/cn_dic/disease.txt"};
        // 或者动态加载
        CustomDictionary.insert("短暂性全面遗忘");
        System.out.println(segment.seg("短暂性全面遗忘"));

输出:

[短暂性全面遗忘/nz]

第二个问题,用xml配置文件关闭索引时的indexmode:

enableIndexMode="false"