剛開始用都不知道要怎么安裝,包括什么文件。百度了下發現正常安裝就可以用,不用下多余的東西,但是但是但是但是但是但是但是但是
用pycharm安裝不行,pip安裝不行,也是見鬼了。
解決方法:
1、手動安裝JPype1
pip install D:\soft\JPype1-0.6.2-cp36-cp36m-win_amd64.whl
這個安裝包可以在這里下 https://www.lfd.uci.edu/~gohlke/pythonlibs/#jpype
注意如果出現 JPype1-0.6.3-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform.是版本的問題,找到自己對應的版本
可看如下搭配更換版本:
cp27 → CPython 2.7 cp34 → CPython 3.4 cp35 → CPython 3.5 cp36 → CPython 3.6 and win32 → 32-bit version of ms-windows win_amd64 → 64-bit version of ms-windows
2、安裝pyhanlp
pip install pyhanlp
3、找到pyhanlp安裝目錄
進入到如 。。。。\python\Lib\site-packages\pyhanlp\static目錄
找到hanlp.properties文件修改root路徑把上面青色路徑加到root中(絕對路徑)
4、測試是否可以用(cmd中輸入hanlp segment 如果出現如下輸出就證明可以用了)
c:\>hanlp segment
歡迎新老師
歡迎/v新/a老/a師n
----------------------------------------------------------------------------------------------------如果可以用了就不用再看下面的了-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5、按照上面步驟不可用,接着這么做
https://pan.baidu.com/s/1UGHNatDivCuSaqiPvb29GA
用上面的網址下載解壓找到里面的hanlp-1.7.2.jar和data文件夾
覆蓋到。。。。\python\Lib\site-packages\pyhanlp\static路徑中去
6.檢查是否可用
終於可以用了 :)
7、以上是在cmd中測試的可以運行,但是到了pycharm中就不行了
調試代碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- from jpype import * startJVM(getDefaultJVMPath(), "-Djava.class.path=G:\TextAnalysis\libs\hanlp-portable-1.3.1.jar;G:\TextAnalysis\libs") HanLP = JClass('com.hankcs.hanlp.HanLP') my_words = u'HanLP是一個致力於向生產環境普及NLP技術的開源Java工具包' # 標准分詞 print(HanLP.segment(my_words).toString()) #索引分詞 IndexTokenizer = JClass('com.hankcs.hanlp.tokenizer.IndexTokenizer') print(IndexTokenizer.segment(my_words).toString()) # 關鍵詞提取 document = u"JPype是一個能夠讓 Python 代碼方便地調用 Java 代碼的工具," \ u"JPype並沒有像IKVM(一款可以在.NET環境中運行JAVA代碼的工具)那樣實現自己的JVM," \ u"而是以pipe方式調用原生JVM。如果要使用JPype就需要先安裝JDK。" print(HanLP.extractKeyword(document, 3).toString()) # 自動摘要 print(HanLP.extractSummary(document, 2).toString()) shutdownJVM()
報錯,還是一串亂碼。。。。。。。。。。。。
然后到cmd中運行報錯的中文是:::
十月 31, 2018 8:31:35 下午 com.hankcs.hanlp.HanLP$Config <clinit> 嚴重: 沒有找到hanlp.properties,可能會導致找不到data ========Tips======== 請將hanlp.properties放在下列目錄: Web項目則請放到下列目錄: Webapp/WEB-INF/lib Webapp/WEB-INF/classes Appserver/lib JRE/lib 並且編輯root=PARENT/path/to/your/data 現在HanLP將嘗試從D:\pythonworkspace2\source\a讀取data…… 十月 31, 2018 8:31:35 下午 com.hankcs.hanlp.corpus.io.IOUtil readBytes 警告: 讀取data/dictionary/CoreNatureDictionary.txt.bin時發生異常java.io.FileNotFoundException: data\dictionary\CoreNatureDictionary.txt.bin (系統找不到指定的路徑。) 十月 31, 2018 8:31:35 下午 com.hankcs.hanlp.dictionary.CoreDictionary load 警告: 核心詞典data/dictionary/CoreNatureDictionary.txt不存在!java.io.FileNotFoundException: data\dictionary\CoreNatureDictionary.txt (系統找不到指定的路徑。) Traceback (most recent call last): File "t2.py", line 14, in <module> print(HanLP.segment(my_words).toString()) jpype._jexception.ExceptionInInitializerErrorPyRaisable: java.lang.ExceptionInInitializerError
解決:
1、配置hanlp.properties環境變量
2、把5中下載的data文件夾復制到項目中去
3、修改hanlp.properties文件,將目錄改為 root=。。/你的項目目錄/data(這一步我還沒用,就已經可以正常運行程序了)
參考:
https://blog.csdn.net/wenwen360360/article/details/80971071
https://blog.csdn.net/qq_34333481/article/details/89206083
