這幾天弄一個項目用到jieba中文分詞工具,之前電腦重裝系統,系統內沒了,這下來安裝,碰到了坑,記錄下
文章目錄
環境
- win10
- python3.7
- jieba-0.39
安裝python3.7
- 官網鏈接:https://www.python.org/downloads/windows/
- 選擇對應版本和64/32位的executable Installer進行下載
- 下載完,執行exe
- 安裝時注意:
- 管理員安裝
- 勾選Add python to PATH
- 如果安裝後出現報錯:
0x..... 用戶取消了安裝
,說明沒有其他用戶權限,那是因爲你勾選了Install launcher for all user,如果沒有權限就去掉這個勾吧 - Customize installation從而選擇自己想裝的位置
python -V
查看安裝版本
- 安裝完cmd -> python即可進入命令行編輯模式,敲個print(“abc”)試試。exit(0)進行退出
安裝jieba
這裏選用jieba-0.39安裝
官網下載
https://pypi.org/project/jieba/
- 找到對應版本,截至本文最新的版本是0.42,需要找歷史發佈版本,並下載,是個zip包
- 解壓jieba-0.39.zip到安裝目錄
- 文件路徑欄輸入cmd(不知道啥意思的話,就cmd後cd到剛纔的解壓目錄)
- 執行:python setup.py install
- 沒報錯信息就說明成功
在線安裝jieba
- pip install jieba==0.39(不設置版本的話就安裝最新版)
安裝過程中報錯
pip is configured with locations that require TLS/SSL
如果是linux環境下,需要檢查openssl依賴
查看:rpm -aq|grep openssl
安裝:yum install openssl openssl-devel
若有缺少安裝,在安裝後重新安裝python進行編譯安裝即可
cd Python-3.6.4 ./configure --with-ssl make & make install
- Windows環境解決
解決方法爲到https://slproweb.com/products/Win32OpenSSL.html上下載winopessl,直接下載第一個MSI安裝即可(就3~4m左右)
這裏附上直接下載的鏈接:https://slproweb.com/download/Win64OpenSSL_Light-1_1_1g.msi
下載完安裝即可
測試jieba
testjieba.py
#encoding=utf-8
import jieba
# 全模式
seg_list = jieba.cut("南京市長江大橋",cut_all=True)
print(",".join(seg_list))
# 精確模式
seg_list = jieba.cut("我來到北京天安門",cut_all=False)
print(",".join(seg_list))
# 默認是精確模式
seg_list = jieba.cut("他來到了阿里深研大廈")
print(",".join(seg_list))
# 搜索引擎模式
seg_list = jieba.cut_for_search("明大帥碩士畢業於中國科學院大學,後在家裏深造")
print(",".join(seg_list))
- 執行:cmd -> python testjieba.py
E:\study\anaconda3\pkgs\jieba-0.39>python testjieba.py
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\明柯\AppData\Local\Temp\jieba.cache
Loading model cost 1.103 seconds.
Prefix dict has been built succesfully.
南京,南京市,京市,市長,長江,長江大橋,大橋
我,來到,北京,天安門
他,來到,了,阿里,深研,大廈
明,大帥,碩士,畢業,於,中國,科學,學院,科學院,中國科學院,大學,,,後,在,家裏,深造