当前位置:网站首页>使用哈工大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
[['他', '叫汤姆去', '拿', '外衣', '。']]
边栏推荐
- JWT 实现登录认证 + Token 自动续期方案
- OneFlow source code parsing: operator instructions executed in a virtual machine
- 这些年我开源的几个小项目
- Chapter 22 Source Code File REST API Reference (4)
- Gartner再次重申了“数据编织”的重要价值
- [Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
- 杭电多校-Loop-(不确定性贪心+线段树)
- owl.carousel poster card Slider carousel switch
- Several small projects that I have open sourced over the years
- 蔚来-软件开发工程师一面记录
猜你喜欢

从脚本到剪辑,影像大师亲授的后期制作秘籍

Research on motion capture system for indoor combined positioning technology

「业务架构」介绍BPMN第二部分-泳道

第2章-矩阵及其运算-矩阵创建(1)

js guessing game source code

MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细

Introduction to cross-end development of Taro applet

In August the DB list latest scores - database Engines

Emulate stm32 directly with proteus - the programmer can be completely discarded

4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
随机推荐
开发模式对测试的影响
JWT implements login authentication + Token automatic renewal scheme
这些年我开源的几个小项目
LeetCode_628_三个数的最大乘积
Redis (six) - transaction and lock mechanism of Redis6 (unfinished, to be supplemented)
In August the DB list latest scores - database Engines
C#实战:基于ItextSharp技术标签生成小工具
2022年裁员潮,失业程序员何去何从?
flask-restplus接口地址404问题
电脑怎么设置屏幕息屏时间(日常使用分享)
【小程序 | 启航篇】一文打通任督二脉
Text selection rounded style border-radius
振弦传感器及核心VM系列振弦采集模块
基于UiAutomator2+PageObject模式开展APP自动化测试实战
第5章相似矩阵及二次型(4)
Programmers pursue technology to consolidate basic learning route suggestions
金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
Gartner再次重申了“数据编织”的重要价值
短视频软件开发——平台同质化如何破局
GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%
