哈⼯⼤⾃然语⾔处理⼯具箱之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
安ash on ubuntu on windows
这个⼤家⾃⾏百度, 安装很简单.
安装编译环境
sudo apt install cmake
sudo apt install g++
安装过程⼤概⼗⼏分钟.
下载ltp源码
下载源码, 这是github地址.
解压到你能记住的位置
编译
cd到源码⽬录, ⽐如我的⽬录:
电脑python安装教程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:l*
-
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:*
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 .l*
配置server
⼀开始我启动server遇到了这个错误.
[INFO] 2018-01-31 15:54:39 Loading segmentor model from "ltp_del" ...
[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_del" ...
[INFO] 2018-01-31 15:56:36 segmentor model is loaded.
[INFO] 2018-01-31 15:56:36 Loading postagger model from "ltp_del" ...
[INFO] 2018-01-31 15:56:36 postagger model is loaded
[INFO] 2018-01-31 15:56:36 Loading NER resource from "ltp_del"
[INFO] 2018-01-31 15:56:36 NER resource is loaded.
[INFO] 2018-01-31 15:56:36 Loading parser resource from "ltp_del"
[INFO] 2018-01-31 15:56:37 parser is loaded.
[INFO] 2018-01-31 15:56:37 Loading srl resource from "ltp_del"
[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 = "127.0.0.1:9090/ltp"
data = {'s': '我认为他叫汤姆去拿外⾐和鞋⼦。', 'x': 'n', 't': 'srl'}
response = (uri_base, data=data)
svg是图片格式吗rdata = response.json()
print(json.dumps(rdata, indent=4, ensure_ascii=False))
[
[
[
{
"arg": [],c语言最简单游戏代码
"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"
},
ppt模板免费下载网站公司{
"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应该都可以,有问题在下⾯留⾔。
pyltp-0.2.1-cp35-cp35m-win_amd64.whl
pyltp-0.2.1-cp36-cp36m-win_amd64.whl
注意: 这两个⽂件的区别是python版本号
安装⽂件
下载好了以后, 在命令⾏下, cd到wheel⽂件所在的⽬录, 然后使⽤命令pip install wheel⽂件名安装.
测试
安装好了以后, 打开python shell, 试⽤⼀下.
from pyltp import SentenceSplitter
sents = SentenceSplitter.split('元芳你怎么看?我就趴窗⼝上看呗!') # 分句
print('\n'.join(sents))
下载models数据
下载models链接:pan.baidu/s/1o9vytmU密码:5ntf
放到任意⽅便调⽤的地⽅即可,因为程序⾥需要你⾃⼰主动调⽤的
第三种⽅案: 直接调⽤编译好的ltp的可执⾏⽂件
可以参考, 但是我在3.4版本中测试不成功, 加载srl资源失败. 但是在3.3.1版本上测试是成功的.
总结
到此这篇关于哈⼯⼤⾃然语⾔处理⼯具箱之ltp在windows10下的安装使⽤教程的⽂章就介绍到这了,更多相关ltp在windows10下的安装使⽤内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。