当前位置:网站首页>使用哈工大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
[['他', '叫汤姆去', '拿', '外衣', '。']]
边栏推荐
- mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
- 高阶组件使用
- POJ 1026 Cipher (置换群)
- 使用JMeter进行MySQL的压力测试
- blocking non-blocking poll mechanism asynchronous
- Regarding the missing json converter, the error message is: No converter found for return value of type
- L2 applications from a product perspective: why is it a playground?
- 商城限时秒杀功能系统
- 【勇敢饭饭,不怕刷题之链表】链表中有环的问题
- How can an organization judge the success of data governance?
猜你喜欢
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
第5章相似矩阵及二次型(4)
[Brave food, not afraid of the linked list of brushing questions] Merging of ordered linked lists
接口定义与实现
动作捕捉系统用于室内组合定位技术研究
C#实战:基于ItextSharp技术标签生成小工具
金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
快速上手,征服三种不同分布式架构调用方案
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
2022年裁员潮,失业程序员何去何从?
随机推荐
HDU 1520 Anniversary party (tree dp)
2023版揽胜运动曝光,安全、舒适一个不落
让软件飞——“X+”技术揭秘
What is affecting MySQL performance?
4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
Break through the dimensional barriers and let the dolls around you move on the screen!
8月份DB-Engines 数据库排行榜最新战况
How can an organization judge the success of data governance?
mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
mysql5.7 installation and deployment - yum installation
三相380V整流后的电压
SQL优化最强总结 (建议收藏~)
Redis设计与实现
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
flask-restplus接口地址404问题
MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
老板加薪!看我做的WPF Loading!!!
OSSCore 开源解决方案介绍
为什么Redis很快
程序员追求技术夯实基础学习路线建议