Solr 5.x集成中文分词word

  1. 使用标准分词器,如图:

  2. 使用word分词器

    1. 下载word-1.3.jar,注意solr的版本和word分词的版本
    2. 将文件word-1.3.jar拷贝至文件夹C:\workspace\Tomcat7.0\webapps\solr\WEB-INF\lib\下
    3. 修改如下文件C:\workspace\solr_home\solr\mysolr\conf\schema.xml

        在schema节点下添加如下节点:

    <fieldType name="word_cn" class="solr.TextField">

    <analyzer type="index">

    <tokenizer class="org.apdplat.word.solr.ChineseWordTokenizerFactory"/>

    </analyzer>

    <analyzer type="query">

    <tokenizer class="org.apdplat.word.solr.ChineseWordTokenizerFactory"/>

    </analyzer>

    </fieldType>

    如图:

    1. 添加分词字段

      <field name="content_wordsplit" type="word_cn" indexed="true" stored="true" multiValued="true"/>

    2. 重启tomcat
    3. 验证分词

    4. 发现同程被分词分开了,需要将"同程"添加到词库中
    5. 编辑C:\workspace\solr_home\solr\mysolr\conf\schema.xml文件,修改如下:

      <fieldType name="word_cn" class="solr.TextField">

      <analyzer type="index">

      <tokenizer class="org.apdplat.word.solr.ChineseWordTokenizerFactory"conf="C:/workspace/solr_home/solr/mysolr/conf/word.local.conf"/>

      </analyzer>

      <analyzer type="query">

      <tokenizer class="org.apdplat.word.solr.ChineseWordTokenizerFactory"conf="C:/workspace/solr_home/solr/mysolr/conf/word.local.conf"/>

      </analyzer>

      </fieldType>

    6. 在文件夹C:\workspace\solr_home\solr\mysolr\conf\下新建文件word.local.conf
    7. 从github中复制word.conf的配置内容,复制dic.txt,stopwords.txt
    8. 修改word.local.conf文件

      dic.path=classpath:dic.txt,classpath:custom_dic,C:/workspace/solr_home/solr/mysolr/conf/word_dic.txt

      stopwords.path=classpath:stopwords.txt,classpath:custom_stopwords_dic,C:/workspace/solr_home/solr/mysolr/conf/word_stopwords.txt

       

      修改后的word.local.conf全部内容如下:

      #是否启用自动检测功能,如:用户自定义词典、停用词词典

      auto.detect=true

      #词典机制实现类,词首字索引式前缀树

      #dic.class=org.apdplat.word.dictionary.impl.DictionaryTrie

      #前缀树词首字索引分配空间大小,如过小则会导致碰撞增加,减小查询性能

      dictionary.trie.index.size=24000

      #双数组前缀树,速度稍快一些,内存占用稍少一些

      #但功能有限,不支持动态增减单个词条,也不支持批量增减词条

      #只支持先clear()后addAll()的动态改变词典方式

      dic.class=org.apdplat.word.dictionary.impl.DoubleArrayDictionaryTrie

      #双数组前缀树预先分配空间大小,如不够则逐渐递增10%

      double.array.dictionary.trie.size=2600000

      #词典,多个词典之间逗号分隔开

      #如:dic.path=classpath:dic.txt,classpath:custom_dic,d:/dic_more.txt,d:/DIC,D:/DIC2

      #自动检测词库变化,包含类路径下的文件和文件夹、非类路径下的绝对路径和相对路径

      #HTTP资源:dic.path=http://localhost:8080/word_web/resources/dic.txt

      dic.path=classpath:dic.txt,classpath:custom_dic,C:/workspace/solr_home/solr/mysolr/conf/word_dic.txt

      #是否利用多核提升分词速度

      parallel.seg=true

      #词性标注数据:part.of.speech.dic.path=http://localhost:8080/word_web/resources/part_of_speech_dic.txt

      part.of.speech.dic.path=classpath:part_of_speech_dic.txt

      #词性说明数据:part.of.speech.des.path=http://localhost:8080/word_web/resources/part_of_speech_des.txt

      part.of.speech.des.path=classpath:part_of_speech_des.txt

      #二元模型路径

      #HTTP资源:bigram.path=http://localhost:8080/word_web/resources/bigram.txt

      bigram.path=classpath:bigram.txt

      bigram.double.array.trie.size=5300000

      #三元模型路径

      #HTTP资源:trigram.path=http://localhost:8080/word_web/resources/trigram.txt

      trigram.path=classpath:trigram.txt

      trigram.double.array.trie.size=9800000

      #是否启用ngram模型,以及启用哪个模型

      #可选值有:no(不启用)、bigram(二元模型)、trigram(三元模型)

      #如不启用ngram模型

      #则双向最大匹配算法、双向最大最小匹配算法退化为:逆向最大匹配算法

      #则双向最小匹配算法退化为:逆向最小匹配算法

      ngram=bigram

      #停用词词典,多个词典之间逗号分隔开

      #如:stopwords.path=classpath:stopwords.txt,classpath:custom_stopwords_dic,d:/stopwords_more.txt

      #自动检测词库变化,包含类路径下的文件和文件夹、非类路径下的绝对路径和相对路径

      #HTTP资源:stopwords.path=http://localhost:8080/word_web/resources/stopwords.txt

      stopwords.path=classpath:stopwords.txt,classpath:custom_stopwords_dic,C:/workspace/solr_home/solr/mysolr/conf/word_stopwords.txt

      #用于分割词的标点符号,目的是为了加速分词,只能为单字符

      #HTTP资源:punctuation.path=http://localhost:8080/word_web/resources/punctuation.txt

      punctuation.path=classpath:punctuation.txt

      #分词时截取的字符串的最大长度

      intercept.length=16

      #百家姓,用于人名识别

      #HTTP资源:surname.path=http://localhost:8080/word_web/resources/surname.txt

      surname.path=classpath:surname.txt

      #数量词

      #HTTP资源:quantifier.path=http://localhost:8080/word_web/resources/quantifier.txt

      quantifier.path=classpath:quantifier.txt

      #是否启用人名自动识别功能

      person.name.recognize=true

      #是否保留空白字符

      keep.whitespace=false

      #是否保留标点符号,标点符号的定义见文件:punctuation.txt

      keep.punctuation=false

      #将最多多少个词合并成一个

      word.refine.combine.max.length=3

      #对分词结果进行微调的配置文件

      word.refine.path=classpath:word_refine.txt

      #同义词词典

      word.synonym.path=classpath:word_synonym.txt

      #反义词词典

      word.antonym.path=classpath:word_antonym.txt

      #lucene、solr、elasticsearch、luke等插件是否启用标注

      tagging.pinyin.full=false

      tagging.pinyin.acronym=false

      tagging.synonym=false

      tagging.antonym=false

      #是否启用识别工具,来识别文本(英文单词、数字、时间等)

      recognition.tool.enabled=true

      #如果你想知道word分词器的词典中究竟加载了哪些词

      #可在配置项dic.dump.path中指定一个文件路径

      #word分词器在加载词典的时候,顺便会把词典的内容写到指定的文件路径

      #可指定相对路径或绝对路径

      #如:

      #dic.dump.path=dic.dump.txt

      #dic.dump.path=dic.dump.txt

      #dic.dump.path=/Users/ysc/dic.dump.txt

      dic.dump.path=

      #redis服务,用于实时检测HTTP资源变更

      #redis主机

      redis.host=localhost

      #redis端口

      redis.port=6379

    9. 修改文件C:/workspace/solr_home/solr/mysolr/conf/word_dic.txt,添加字库:同程
    10. 重启tomcat
    11. 验证分词结果,如图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
word分词是一个Java实现的中文分词组件,提供了多种基于词典的分词算法,并利用ngram模型来消除歧义。 能准确识别英文、数字,以及日期、时间等数量词,能识别人名、地名、组织机构名等未登录词。 同时提供了Lucene、Solr、ElasticSearch插件。 分词使用方法: 1、快速体验 运行项目根目录下的脚本demo-word.bat可以快速体验分词效果 用法: command [text] [input] [output] 命令command的可选值为:demo、text、file demo text 杨尚川是APDPlat应用级产品开发平台的作者 file d:/text.txt d:/word.txt exit 2、对文本进行分词 移除停用词:List words = WordSegmenter.seg("杨尚川是APDPlat应用级产品开发平台的作者"); 保留停用词:List words = WordSegmenter.segWithStopWords("杨尚川是APDPlat应用级产品开发平台的作者"); System.out.println(words); 输出: 移除停用词:[杨尚川, apdplat, 应用级, 产品, 开发平台, 作者] 保留停用词:[杨尚川, 是, apdplat, 应用级, 产品, 开发平台, 的, 作者] 3、对文件进行分词 String input = "d:/text.txt"; String output = "d:/word.txt"; 移除停用词:WordSegmenter.seg(new File(input), new File(output)); 保留停用词:WordSegmenter.segWithStopWords(new File(input), new File(output)); 4、自定义配置文件 默认配置文件为类路径下的word.conf,打包在word-x.x.jar中 自定义配置文件为类路径下的word.local.conf,需要用户自己提供 如果自定义配置和默认配置相同,自定义配置会覆盖默认配置 配置文件编码为UTF-8 5、自定义用户词库 自定义用户词库为一个或多个文件夹或文件,可以使用绝对路径或相对路径 用户词库由多个词典文件组成,文件编码为UTF-8 词典文件的格式为文本文件,一行代表一个词 可以通过系统属性或配置文件的方式来指定路径,多个路径之间用逗号分隔开 类路径下的词典文件,需要在相对路径前加入前缀classpath: 指定方式有三种: 指定方式一,编程指定(高优先级): WordConfTools.set("dic.path", "classpath:dic.txt,d:/custom_dic"); DictionaryFactory.reload();//更改词典路径之后,重新加载词典 指定方式二,Java虚拟机启动参数(中优先级): java -Ddic.path=classpath:dic.txt,d:/custom_dic 指定方式三,配置文件指定(低优先级): 使用类路径下的文件word.local.conf来指定配置信息 dic.path=classpath:dic.txt,d:/custom_dic 如未指定,则默认使用类路径下的dic.txt词典文件 6、自定义停用词词库 使用方式和自定义用户词库类似,配置项为: stopwords.path=classpath:stopwords.txt,d:/custom_stopwords_dic 7、自动检测词库变化 可以自动检测自定义用户词库和自定义停用词词库的变化 包含类路径下的文件和文件夹、非类路径下的绝对路径和相对路径 如: classpath:dic.txtclasspath:custom_dic_dir, d:/dic_more.txt,d:/DIC_DIR,D:/DIC2_DIR,my_dic_dir,my_dic_file.txt classpath:stopwords.txtclasspath:custom_stopwords_dic_dir, d:/stopwords_more.txt,d:/STOPWORDS_DIR,d:/STOPWORDS2_DIR,stopwords_dir,remove.txt 8、显式指定分词算法 对文本进行分词时,可显式指定特定的分词算法,如: WordSegmenter.seg("APDPlat应用级产品开发平台", SegmentationA
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值