pyhanlp是python版封装的的HanLP,项目地址:https://github.com/hankcs/pyhanlp
1.安装pyhanlp:pip install pyhanlp
2.在字典路径下添加自定义的词典:CustomDictionary主词典文本路径是data/dictionary/custom/CustomDictionary.txt,用户可以在此增加自己的词语(不推荐);也可以单独新建一个文本文件,通过配置文件;CustomDictionaryPath=data/dictionary/custom/CustomDictionary.txt; 我的词典.txt;来追加词典(推荐)。
#hanlp --version
jar1.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
4.然后需要删除缓存文件,这样python才会重新加载新增的文件:
#rm -f CustomDictionary.txt.bin
5.测试新增的词典:
python -c "from pyhanlp import *;print(HanLP.segment('codis集群,今日头条'))"
/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 (没有那个文件或目录)
HanLP词性列表:详细的词性列表可以查询hanlp项目网站上内容,更为全面详细!
---------------------