學習筆記(35)-安裝pyhanlp


安裝pyhanlp

pyhanlp是java寫的,外層封裝了python。

對於新手,在使用的時候稍有難度。

1. 下載源碼

https://github.com/hankcs/pyhanlp

git clone https://github.com/hankcs/pyhanlp.git

2. 創建虛機

python3 -m venv env
source env/bin/activate

3. 安裝pyhanlp

cd pyhanlp
pip install -e .

以下是日志

Obtaining file:///Users/huihui/git/pyhanlp
Collecting jpype1==0.7.0 (from pyhanlp==0.1.62)
  Using cached https://files.pythonhosted.org/packages/28/63/784834e8a24ec2e1ad7f703c3dc6c6fb372a77cc68a2fdff916e18a4449e/JPype1-0.7.0.tar.gz
Installing collected packages: jpype1, pyhanlp
  Running setup.py install for jpype1 ... done
  Running setup.py develop for pyhanlp
Successfully installed jpype1-0.7.0 pyhanlp
You are using pip version 19.0.3, however version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

嘗試調用,加載詞典

(env) huihui@192 pyhanlp % python

Python 3.7.3 (default, Nov 15 2019, 04:04:52) 
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyhanlp
下載 http://hanlp.com/static/release/hanlp-1.7.6-release.zip 到 /Users/huihui/git/pyhanlp/pyhanlp/static/hanlp-1.7.6-release.zip
100.00%, 1 MB, 514 KB/s, 還有 0 分  0 秒   
下載 https://file.hankcs.com/hanlp/data-for-1.7.zip 到 /Users/huihui/git/pyhanlp/pyhanlp/static/data-for-1.7.6.zip
0.38%, 2 MB, 795 KB/s, 還有 13 分 37 秒   下載失敗 https://file.hankcs.com/hanlp/data-for-1.7.zip 由於 timeout('The read operation timed out')
請參考 https://github.com/hankcs/pyhanlp 執行手動安裝.
或手動下載 https://file.hankcs.com/hanlp/data-for-1.7.zip 到 /Users/huihui/git/pyhanlp/pyhanlp/static/data-for-1.7.6.zip
是否前往 https://github.com/hankcs/pyhanlp ?(y/n)y
(env) huihui@192 pyhanlp % 

4. 下載詞典文件

手動下載。

詞典文件有668M,下載之后按照上述提示放置目錄,並解壓。

5.測試pyhanlp

測試例子1,在命令行

(env) huihui@192 pyhanlp % python
Python 3.7.3 (default, Nov 15 2019, 04:04:52) 
[Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyhanlp import *
>>> 
>>> print(HanLP.segment('出事了電腦'))
[出事/vi, 了/ule, 電腦/n]
>>> 

測試例子2,在PyCharm

選擇剛才創建的虛機

from pyhanlp import *

print(HanLP.segment('你好,歡迎在Python中調用HanLP的API'))
for term in HanLP.segment('下雨天地面積水'):
    print('{}\t{}'.format(term.word, term.nature)) # 獲取單詞與詞性

print(HanLP.segment('你好,歡迎在Python中調用HanLP的API'))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM