简介:pycorrector是一个开源的中文文本纠错工具。支持中文音似、形似、语法错误纠正,python3开发。pycorrector实现了Kenlm、ConvSeq2Seq、BERT、MacBERT、ELECTRA、ERNIE、Transformer等多种模型的文本纠错,并在SigHAN数据集评估各模型的效果。
历史攻略:
AI语音测试点
Python:2行代码实现文字转语音
项目开源地址:
https://github.com/shibing624/pycorrector
安装:
pip install -U pycorrector
案例1:文本纠错
# -*- coding: utf-8 -*-
# time: 2023/02/18 11:26
# file: demo1.py
# 公众号: 玩转测试开发
import pycorrector
message = "机七学习是人工智能领遇最能体现智能的一个分知"
corrected_sent, detail = pycorrector.message(mes)
print(corrected_sent, detail)
运行结果:
(base) [root@ci4vyvxi572ysx2s write]# python demo1.py
2023-02-18 11:53:18.515 | DEBUG | pycorrector.detector:_initialize_detector:89 - Loaded language model: /root/.pycorrector/datasets/zh_giga.no_cna_cmn.prune01244.klm
机器学习是人工智能领域最能体现智能的一个分知 [('机七', '机器', 0, 2), ('领遇', '领域', 9, 11)]
案例2:成语、专名纠错
# -*- coding: utf-8 -*-
# time: 2023/02/18 11:26
# file: demo2.py
# 公众号: 玩转测试开发
import sys
sys.path.append("..")
from pycorrector.proper_corrector import ProperCorrector
m = ProperCorrector()
x = [
'报应接中迩来',
'今天在拼哆哆上买了点苹果',
]
for i in x:
print(i, ' -> ', m.proper_correct(i))
运行结果:
(base) [root@ci4vyvxi572ysx2s write]# python demo2.py
报应接中迩来 -> ('报应接踵而来', [('接中迩来', '接踵而来', 2, 6)])
今天在拼哆哆上买了点苹果 -> ('今天在拼多多上买了点苹果', [('拼哆哆', '拼多多', 3, 6)])
案例3:英文拼写纠错
# -*- coding: utf-8 -*-
# time: 2023/02/18 11:26
# file: demo3.py
# 公众号: 玩转测试开发
import pycorrector
sent = "what happending? how to speling it, can you gorrect it?"
corrected_text, details = pycorrector.en_correct(sent)
print(sent, '=>', corrected_text)
print(details)
运行结果:
(base) [root@ci4vyvxi572ysx2s write]# python demo3.py
2023-02-18 11:59:02.620 | DEBUG | pycorrector.en_spell:_init:39 - load en spell data: /root/miniconda3/lib/python3.9/site-packages/pycorrector/data/en/en.json.gz, size: 30120
what happending? how to speling it, can you gorrect it? => what happening? how to spelling it, can you correct it?
[('happending', 'happening', 5, 15), ('speling', 'spelling', 24, 31), ('gorrect', 'correct', 44, 51)]
更多使用技巧详见官方文档。