哈工大自然语言处理工具箱之ltp在windows10下的安装使用教程
ltp是哈工大出品的自然语言处理工具箱, pyltp是python下对ltp(c++)的封装.
在linux下我们很容易的安装pyltp, 因为各种编译工具比较方便. 但是在windows下需要安装vs并且还得做一些配置, 因为我服务的人都是在windows下办公, 需要让他们能够在windows下使用ltp, 所以才有了这篇笔记. 我的方案有两个:
- 在win10 的bash下安装ltp, 然后启动ltp的server, 通过http协议来实现在windows下python调用ltp的方法.
- 安装编译好的wheel(目前只有python3.6/3.5 amd64)(我推荐这种方案)
- 我在文章最下面还引用了一种方法, 就是使用官方已经编译好的可执行exe文件, 直接在命令行(如cmd)下调用.
第一种方案: bash下安装
基本环境
- windows 10
- bash for windows
- python 3.6
安装bash on ubuntu on windows
这个大家自行百度, 安装很简单.
安装编译环境
sudo apt install cmake sudo apt install g++
安装过程大概十几分钟.
下载ltp源码
- 下载源码, 这是github地址.
- 解压到你能记住的位置
编译
cd到源码目录, 比如我的目录:
cd /mnt/d/bash-sites/ltp-3.4.0
运行编译命令:
./configure make
编译过程大概花费十几分钟. 现在我的目录里多了一个bin文件夹:
drwxrwxrwx 0 root root 512 jan 31 15:42 ./ drwxrwxrwx 0 root root 512 jan 31 15:30 ../ -rwxrwxrwx 1 root root 800 jan 31 15:30 appveyor.yml* -rwxrwxrwx 1 root root 0 jan 31 15:30 authors* drwxrwxrwx 0 root root 512 jan 31 15:53 bin/ drwxrwxrwx 0 root root 512 jan 31 15:42 build/ -rwxrwxrwx 1 root root 29301 jan 31 15:30 changelog.md* drwxrwxrwx 0 root root 512 jan 31 15:30 cmake/ -rwxrwxrwx 1 root root 1439 jan 31 15:30 cmakelists.txt* drwxrwxrwx 0 root root 512 jan 31 15:30 conf/ -rwxrwxrwx 1 root root 131 jan 31 15:30 configure* -rwxrwxrwx 1 root root 902 jan 31 15:30 copying* drwxrwxrwx 0 root root 512 jan 31 15:30 doc/ -rwxrwxrwx 1 root root 79976 jan 31 15:30 doxyfile* drwxrwxrwx 0 root root 512 jan 31 15:30 examples/ -rwxrwxrwx 1 root root 1028 jan 31 15:30 .gitignore* drwxrwxrwx 0 root root 512 jan 31 15:42 include/ -rwxrwxrwx 1 root root 85 jan 31 15:30 install* drwxrwxrwx 0 root root 512 jan 31 15:53 lib/ -rwxrwxrwx 1 root root 965 jan 31 15:30 makefile* -rwxrwxrwx 1 root root 6639 jan 31 15:30 news.md* -rwxrwxrwx 1 root root 4750 jan 31 15:30 readme.md* drwxrwxrwx 0 root root 512 jan 31 15:30 src/ -rwxrwxrwx 1 root root 3048 jan 31 15:30 subproject.d.json* drwxrwxrwx 0 root root 512 jan 31 15:31 thirdparty/ drwxrwxrwx 0 root root 512 jan 31 15:31 tools/ -rwxrwxrwx 1 root root 1372 jan 31 15:30 .travis.yml*
配置server
一开始我启动server遇到了这个错误.
[info] 2018-01-31 15:54:39 loading segmentor model from "ltp_data/cws.model" ...
[error] 2018-01-31 15:54:39 /mnt/d/bash-sites/ltp-3.4.0/src/ltp/ltpresource.cpp: line 50: loadsegmentorresource(): failed to load segmentor model
[error] 2018-01-31 15:54:39 /mnt/d/bash-sites/ltp-3.4.0/src/ltp/ltp.cpp: line 78: load(): in ltp::wordseg, failed to load segmentor resource
[error] 2018-01-31 15:54:39 /mnt/d/bash-sites/ltp-3.4.0/src/server/ltp_server.cpp: line 172: main(): failed to setup ltp engine.
因为缺少了模型文件, 在这里下载最新的模型文件.
解压到/mnt/d/bash-sites/ltp-3.4.0/ltp_data/
下, 这是ltp默认的数据模型存放位置.
然后就能顺利启动服务器啦.
syd@desktop-j02r2vj:/mnt/d/bash-sites/ltp-3.4.0$ ./bin/ltp_server --port 9090
[info] 2018-01-31 15:56:36 loading segmentor model from "ltp_data/cws.model" ...
[info] 2018-01-31 15:56:36 segmentor model is loaded.
[info] 2018-01-31 15:56:36 loading postagger model from "ltp_data/pos.model" ...
[info] 2018-01-31 15:56:36 postagger model is loaded
[info] 2018-01-31 15:56:36 loading ner resource from "ltp_data/ner.model"
[info] 2018-01-31 15:56:36 ner resource is loaded.
[info] 2018-01-31 15:56:36 loading parser resource from "ltp_data/parser.model"
[info] 2018-01-31 15:56:37 parser is loaded.
[info] 2018-01-31 15:56:37 loading srl resource from "ltp_data/pisrl.model"
[dynet] random seed: 493907432
[dynet] allocating memory: 2000mb
[dynet] memory allocation done.
[info] 2018-01-31 15:56:39 srl resource is loaded.
[info] 2018-01-31 15:56:39 resources loading finished.
[info] 2018-01-31 15:56:39 start listening on port [9090]...
测试
随便写个请求, 看看效果:
import requests import json uri_base = "http://127.0.0.1:9090/ltp" data = {'s': '我认为他叫汤姆去拿外衣和鞋子。', 'x': 'n', 't': 'srl'} response = requests.get(uri_base, data=data) rdata = response.json() print(json.dumps(rdata, indent=4, ensure_ascii=false)) [ [ [ { "arg": [], "cont": "我", "id": 0, "ne": "o", "parent": 1, "pos": "r", "relate": "sbv" }, { "arg": [ { "beg": 0, "end": 0, "id": 0, "type": "a0" }, { "beg": 2, "end": 9, "id": 1, "type": "a1" } ], "cont": "认为", "id": 1, "ne": "o", "parent": -1, "pos": "v", "relate": "hed" }, { "arg": [], "cont": "他", "id": 2, "ne": "o", "parent": 3, "pos": "r", "relate": "sbv" }, { "arg": [ { "beg": 2, "end": 2, "id": 0, "type": "a0" }, { "beg": 4, "end": 4, "id": 1, "type": "a1" }, { "beg": 5, "end": 9, "id": 2, "type": "a2" } ], "cont": "叫", "id": 3, "ne": "o", "parent": 1, "pos": "v", "relate": "vob" }, { "arg": [], "cont": "汤姆", "id": 4, "ne": "s-nh", "parent": 3, "pos": "nh", "relate": "dbl" }, { "arg": [], "cont": "去", "id": 5, "ne": "o", "parent": 6, "pos": "v", "relate": "adv" }, { "arg": [ { "beg": 7, "end": 9, "id": 0, "type": "a1" } ], "cont": "拿", "id": 6, "ne": "o", "parent": 3, "pos": "v", "relate": "vob" }, { "arg": [], "cont": "外衣", "id": 7, "ne": "o", "parent": 6, "pos": "n", "relate": "vob" }, { "arg": [], "cont": "和", "id": 8, "ne": "o", "parent": 9, "pos": "c", "relate": "lad" }, { "arg": [], "cont": "鞋子", "id": 9, "ne": "o", "parent": 7, "pos": "n", "relate": "coo" }, { "arg": [], "cont": "。", "id": 10, "ne": "o", "parent": 1, "pos": "wp", "relate": "wp" } ] ] ]
第二种方案: 安装wheel
下载wheels
下面两个文件针对不同的python版本下载一个即可, 这是我在自己的电脑(win10)上编译的,不知道你的系统是否能用,64bit的windows应该都可以,有问题在下面留言。
注意: 这两个文件的区别是python版本号
安装文件
下载好了以后, 在命令行下, cd到wheel文件所在的目录, 然后使用命令pip install wheel文件名安装.
测试
安装好了以后, 打开python shell, 试用一下.
from pyltp import sentencesplitter sents = sentencesplitter.split('元芳你怎么看?我就趴窗口上看呗!') # 分句 print('\n'.join(sents))
下载models数据
- 下载models链接:https://pan.baidu.com/s/1o9vytmu 密码:5ntf
- 放到任意方便调用的地方即可, 因为程序里需要你自己主动调用的
第三种方案: 直接调用编译好的ltp的可执行文件
可以参考, 但是我在3.4版本中测试不成功, 加载srl资源失败. 但是在3.3.1版本上测试是成功的.
总结
到此这篇关于哈工大自然语言处理工具箱之ltp在windows10下的安装使用教程的文章就介绍到这了,更多相关ltp在windows10下的安装使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!