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是主谓关系。