mac os 10.14 安裝pyltp

該方法我試了好多次,纔在mac上面安裝好。

1.語言技術平臺(Language Technology Platform,LTP)是哈工大社會計算與信息檢索研究中心歷時十年開發的一整套中文語言處理系統。LTP制定了基於XML的語言處理結果表示,並在此基礎上提供了一整套自底向上的豐富而且高效的中文語言處理模塊(包括詞法、句法、語義等6項中文處理核心技術),以及基於動態鏈接庫(Dynamic Link Library, DLL)的應用程序接口、可視化工具,並且能夠以網絡服務(Web Service)的形式進行使用。具體網址如下:http://ltp.ai/index.html

2.操作環境:
操作系統:mac os 10.14
python版本: python3.6

3.安裝pyltp
首先你要下載兩個東西:
(1)先將pyltp整體git下來:
打開命令行終端:

$ git clone https://github.com/HIT-SCIR/pyltp

(2)將這部分下載下來,將下載的文件放在 剛剛下載的pyltp下面,並解壓命名爲ltp
https://github.com/HIT-SCIR/ltp/releases

在這裏插入圖片描述

然後ls一下,發現當前目錄裏多了這個pyltp的項目,然後執行以下代碼:

$ git submodule init
$ git submodule update

之後進入這個pyltp文件夾,使用管理員權限執行setup.py文件:

$ cd pyltp
$ sudo python setup.py install

遇到報錯:

error: command 'clang++' failed with exit status 1

Mac的OSX系統的C語言,編譯器用的是Clang。既然報錯與clang有關,應該是xcode command tools出現問題,網上搜了一下其它文章,大部分人也都是通過更新安裝xcode解決的。

打開mac終端,執行

xcode-select --install

然後點擊安裝;安裝成功以後檢查一下:

bogon:~ mbp$ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

再次執行setup.py文件。

$ sudo python setup.py install

這時候,會遇到報錯,
請更改pyltp文件夾下setup.py文件 :
第121行的框線處,與你的操作系統當前保持一致即可。

然後再運行

$ sudo python setup.py install

仍然報錯!!!

In file included from /Users/unm/other/ltp-3.4.0/thirdparty/eigen/unsupported/Eigen/CXX11/Tensor:143:
/Users/unm/other/ltp-3.4.0/thirdparty/eigen/unsupported/Eigen/CXX11/src/Tensor/TensorStorage.h:39:7: error:
class template partial specialization is not more specialized than the
primary template [-Winvalid-partial-specialization]
class TensorStorage<T, FixedDimensions, Options_>
^
/Users/unm/other/ltp-3.4.0/thirdparty/eigen/unsupported/Eigen/CXX11/src/Tensor/TensorStorage.h:34:63: note:
template is declared here
template<typename T, typename Dimensions, int Options_> class TensorStorage;
^
1 error generated.

好吧,我又改了一個地方:
去ltp文件夾下面:

user⁩/mbp⁩/pyltp⁩/ltp⁩/⁨thirdparty⁩/eigen⁩/⁨unsupported⁩/Eigen⁩/CXX11⁩/src⁩/Tensor⁩

修改文件 tensorStorage.h 34行,用

template<typename T, typename Dimensions, int Options_, typename empty = void> class TensorStorage;`

替換原來的

template<typename T, typename Dimensions, int Options_> class TensorStorage;

然後就完全安裝好了這個包了。

然後運行

$ sudo python setup.py install

大功告成。!!

Processing dependencied for pyplt==0.2.1
Finshed processing dependencies for pyplt==0.2.1

(3)然後import一下這個包試一下:

bogon:~ mbp$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyltp

顯示成功了。

最後一步,去官網下載ltp模型文件: ltp_data_v3.4.0.zip ,解壓以後重新命名爲ltp_model,
至於放在哪個文件夾下面不影響,只是在加載的時候要找得到
http://ltp.ai/

>>> from pyltp import Parser
>>> parser = Parser()
>>> parser.load('ltp_model/parser.model')
>>> a = ['媽媽', ' 愛', ' 女兒']
>>> b = ['n', 'v', 'd']
>>> arc = parser.parse(a, b)
>>> arc[0].relation
'SBV'

解析出來的第一個結果的relation是主謂關係。