python定义词典_pyhanlp用户自定义词典添加

原标题:pyhanlp用户自定义词典添加

pyhanlp是python版封装的的HanLP,项目地址:https://github.com/hankcs/pyhanlp

经过测试,HanLP比nltk在中文分词和实体识别方面都更好用。

如何向pyhanlp添加自定义的词典?以python 2.7.9为例:

1.安装pyhanlp:pip install pyhanlp

2.在字典路径下添加自定义的词典:CustomDictionary主词典文本路径是data/dictionary/custom/CustomDictionary.txt,用户可以在此增加自己的词语(不推荐);也可以单独新建一个文本文件,通过配置文件;CustomDictionaryPath=data/dictionary/custom/CustomDictionary.txt; 我的词典.txt;来追加词典(推荐)。

具体绝对路径可用hanlp --version获取:

#hanlp --version

jar 1.6.3: /usr/local/lib/python2.7/site-packages/pyhanlp/static/hanlp-1.6.3.jar

data 1.6.2: /usr/local/lib/python2.7/site-packages/pyhanlp/static/data

config : /usr/local/lib/python2.7/site-packages/pyhanlp/static/hanlp.properties

#cat /usr/local/lib/python2.7/site-packages/pyhanlp/static/hanlp.properties | grep "CustomDictionaryPath"

3.建议在该路径下添加自己的词典文件例如 我的词典.txt,并将其加入上面的properties文件里的CustomDictionaryPath下面。

#cat 我的词典.txt

codis集群 nz 1000

今日头条 nz 1000

第一列为词条,第二列为词性(默认为n),第三列为词频

4.然后需要删除缓存文件,这样python才会重新加载新增的文件:

#rm -f CustomDictionary.txt.bin

5.测试新增的词典:

python -c "from pyhanlp import *;print(HanLP.segment('codis集群,今日头条'))"

五月 16, 2018 4:43:14 下午 com.hankcs.hanlp.corpus.io.IOUtil readBytes

警告: 读取/usr/local/lib/python2.7/site-packages/pyhanlp/static/data/dictionary/custom/CustomDictionary.txt.bin时发生异常java.io.FileNotFoundException: /usr/local/lib/python2.7/site-packages/pyhanlp/static/data/dictionary/custom/CustomDictionary.txt.bin (没有那个文件或目录)

报这个错误没有关系,只是个warning,重新加载缓存文件而已。

备注:

HanLP词性列表:https://blog.csdn.net/a13662080711/article/details/80106759

文章来源于明月三千里68的博客返回搜狐,查看更多

责任编辑:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值