当前位置:网站首页>使用哈工大LTP测试分词并且增加自定义字典
使用哈工大LTP测试分词并且增加自定义字典
2022-08-10 10:56:00 【GIS从业者】
1、github下载源码
https://github.com/HIT-SCIR/ltp
2、拷贝测试代码测试
详细说明下拷贝测试代码
https://github.com/HIT-SCIR/ltp/blob/master/docs/quickstart.rst
from ltp import LTP
ltp = LTP()
segment, _ = ltp.seg(["他叫汤姆去拿外衣。"])
# [['他', '叫', '汤姆', '去', '拿', '外衣', '。']]

报错了
稍微修改下代码,添加前面两行
import sys,os
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
from ltp import LTP
ltp = LTP()
segment, _ = ltp.seg(["他叫汤姆去拿外衣。"])
# [['他', '叫', '汤姆', '去', '拿', '外衣', '。']]
print(segment)
执行过程中,缺少什么包就安装什么包就行,我的少了这些
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple packaging
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple transformers --user
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy==1.17.3
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pygtrie
执行成功,等待下载训练包,然后测试结果成功
Ignored unknown kwarg option direction
[['他', '叫', '汤姆', '去', '拿', '外衣', '。']]
3、升级测试,增加自定义字典
(1)、脚本中增加
仅仅为了测试,增加的自定义字典是随便写的
import sys,os
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
from ltp import LTP
ltp = LTP()
# 也可以在代码中添加自定义的词语
ltp.add_words(words=["叫汤姆去"], max_window=4)
segment, _ = ltp.seg(["他叫汤姆去拿外衣。"])
print(segment)
(2)、文件中增加
其中’user_dict.txt’文件我是放在了这里

文本记得用utf-8编码保存
import sys,os
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '/' + '..'))
from ltp import LTP
ltp = LTP()
root_path=os.path.abspath(os.path.dirname(__file__) + '/' + '..')
# user_dict.txt 是词典文件, max_window是最大前向分词窗口
ltp.init_dict(path=os.path.join(root_path,"dict",'user_dict.txt'), max_window=4)
segment, _ = ltp.seg(["他叫汤姆去拿外衣。"])
print(segment)
读取utf-8编码的文本时,读取第一个文本会出现乱码问题
代码有个地方(“.\ltp-master\ltp\algorithms\maximum_forward_matching.py”)稍微修改了下,修改之后就正常了
测试结果成功
Ignored unknown kwarg option direction
[['他', '叫汤姆去', '拿', '外衣', '。']]
边栏推荐
- 首次入选OSDI顶会!腾讯提出超大规模推荐系统的模型低延时更新方案
- Codeforces 814 C. An impassioned circulation of affection (dp)
- 【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
- bus event bus use
- ENVI 5.3软件安装包和安装教程
- 程序员追求技术夯实基础学习路线建议
- STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
- 【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
- JWT 实现登录认证 + Token 自动续期方案
- Cybersecurity Notes 5 - Digital Signatures
猜你喜欢
随机推荐
What is affecting MySQL performance?
Research on motion capture system for indoor combined positioning technology
ISO9001在讲什么?过程方法和风险思维
OSSCore 开源解决方案介绍
TCP/IP笔记
Some tips for using Unsafe
The brave rice rice, does not fear the brush list of 】 list has a ring
为什么Redis很快
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
2022年裁员潮,失业程序员何去何从?
Codeforces 814 C. An impassioned circulation of affection (dp)
What is an abstract class
POJ 1026 Cipher (置换群)
关于“码农”的一点自嘲解构
Three-phase 380V rectified voltage
what is rtems
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
HCIP ---- VLAN
Short video software development - how to break the platform homogenization
Regarding the missing json converter, the error message is: No converter found for return value of type










