当前位置:   article > 正文

Python:使用pycorrector处理错字、纠正

pycorrector

简介:pycorrector是一个开源的中文文本纠错工具。支持中文音似、形似、语法错误纠正,python3开发。pycorrector实现了Kenlm、ConvSeq2Seq、BERT、MacBERT、ELECTRA、ERNIE、Transformer等多种模型的文本纠错,并在SigHAN数据集评估各模型的效果。

历史攻略:

AI语音测试点

Python:2行代码实现文字转语音

项目开源地址:

https://github.com/shibing624/pycorrector
  • 1

安装:

pip install -U pycorrector
  • 1

案例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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

运行结果:

(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)]
  • 1
  • 2
  • 3

案例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))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

运行结果:

(base) [root@ci4vyvxi572ysx2s write]# python demo2.py 
报应接中迩来  ->  ('报应接踵而来', [('接中迩来', '接踵而来', 2, 6)])
今天在拼哆哆上买了点苹果  ->  ('今天在拼多多上买了点苹果', [('拼哆哆', '拼多多', 3, 6)])
  • 1
  • 2
  • 3

案例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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

运行结果:

(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)]
  • 1
  • 2
  • 3
  • 4

更多使用技巧详见官方文档。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/347849
推荐阅读
相关标签
  

闽ICP备14008679号