日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文本纠错pycorrector

發(fā)布時(shí)間:2024/7/5 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文本纠错pycorrector 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文鏈接:https://blog.csdn.net/javastart/article/details/107428483

這一段時(shí)間再研究身份證和面單識別項(xiàng)目,總發(fā)現(xiàn)一些識別準(zhǔn)確率問題,在想辦法提高識別率,突然看了一篇文字糾錯(cuò)方面資料,可以發(fā)現(xiàn)與ocr結(jié)合。開始收集糾錯(cuò)方面的框架,感覺網(wǎng)上評價(jià)pycorrector 這個(gè)項(xiàng)目不錯(cuò)

pycorrector

中文文本糾錯(cuò)工具。音似、形似錯(cuò)字(或變體字)糾正,可用于中文拼音、筆畫輸入法的錯(cuò)誤糾正。python3.6開發(fā)。

pycorrector依據(jù)語言模型檢測錯(cuò)別字位置,通過拼音音似特征、筆畫五筆編輯距離特征及語言模型困惑度特征糾正錯(cuò)別字。

Demo

https://www.borntowin.cn/product/corrector

Question

中文文本糾錯(cuò)任務(wù),常見錯(cuò)誤類型包括:

  • 諧音字詞,如 配副眼睛-配副眼鏡
  • 混淆音字詞,如 流浪織女-牛郎織女
  • 字詞順序顛倒,如 伍迪艾倫-艾倫伍迪
  • 字詞補(bǔ)全,如 愛有天意-假如愛有天意
  • 形似字錯(cuò)誤,如 高梁-高粱
  • 中文拼音全拼,如 xingfu-幸福
  • 中文拼音縮寫,如 sz-深圳
  • 語法錯(cuò)誤,如 想象難以-難以想象

當(dāng)然,針對不同業(yè)務(wù)場景,這些問題并不一定全部存在,比如輸入法中需要處理前四種,搜索引擎需要處理所有類型,語音識別后文本糾錯(cuò)只需要處理前兩種, 其中'形似字錯(cuò)誤'主要針對五筆或者筆畫手寫輸入等。

Solution

規(guī)則的解決思路

  • 中文糾錯(cuò)分為兩步走,第一步是錯(cuò)誤檢測,第二步是錯(cuò)誤糾正;
  • 錯(cuò)誤檢測部分先通過結(jié)巴中文分詞器切詞,由于句子中含有錯(cuò)別字,所以切詞結(jié)果往往會(huì)有切分錯(cuò)誤的情況,這樣從字粒度和詞粒度兩方面檢測錯(cuò)誤, 整合這兩種粒度的疑似錯(cuò)誤結(jié)果,形成疑似錯(cuò)誤位置候選集;
  • 錯(cuò)誤糾正部分,是遍歷所有的疑似錯(cuò)誤位置,并使用音似、形似詞典替換錯(cuò)誤位置的詞,然后通過語言模型計(jì)算句子困惑度,對所有候選集結(jié)果比較并排序,得到最優(yōu)糾正詞。
  • 深度模型的解決思路

  • 端到端的深度模型可以避免人工提取特征,減少人工工作量,RNN序列模型對文本任務(wù)擬合能力強(qiáng),rnn_attention在英文文本糾錯(cuò)比賽中取得第一名成績,證明應(yīng)用效果不錯(cuò);
  • CRF會(huì)計(jì)算全局最優(yōu)輸出節(jié)點(diǎn)的條件概率,對句子中特定錯(cuò)誤類型的檢測,會(huì)根據(jù)整句話判定該錯(cuò)誤,阿里參賽2016中文語法糾錯(cuò)任務(wù)并取得第一名,證明應(yīng)用效果不錯(cuò);
  • seq2seq模型是使用encoder-decoder結(jié)構(gòu)解決序列轉(zhuǎn)換問題,目前在序列轉(zhuǎn)換任務(wù)中(如機(jī)器翻譯、對話生成、文本摘要、圖像描述)使用最廣泛、效果最好的模型之一。
  • Feature

    模型

    • kenlm:kenlm統(tǒng)計(jì)語言模型工具
    • rnn_attention模型:參考Stanford University的nlc模型,該模型是參加2014英文文本糾錯(cuò)比賽并取得第一名的方法
    • rnn_crf模型:參考阿里巴巴2016參賽中文語法糾錯(cuò)比賽CGED2018并取得第一名的方法(整理中)
    • seq2seq_attention模型:在seq2seq模型加上attention機(jī)制,對于長文本效果更好,模型更容易收斂,但容易過擬合
    • transformer模型:全attention的結(jié)構(gòu)代替了lstm用于解決sequence to sequence問題,語義特征提取效果更好
    • bert模型:中文fine-tuned模型,使用MASK特征糾正錯(cuò)字
    • conv_seq2seq模型:基于Facebook出品的fairseq,北京語言大學(xué)團(tuán)隊(duì)改進(jìn)ConvS2S模型用于中文糾錯(cuò),在NLPCC-2018的中文語法糾錯(cuò)比賽中,是唯一使用單模型并取得第三名的成績
    • electra模型:斯坦福和谷歌聯(lián)合提出的一種更具效率的預(yù)訓(xùn)練模型,學(xué)習(xí)文本上下文表示優(yōu)于同等計(jì)算資源的BERT和XLNet

    錯(cuò)誤檢測

    • 字粒度:語言模型困惑度(ppl)檢測某字的似然概率值低于句子文本平均值,則判定該字是疑似錯(cuò)別字的概率大。
    • 詞粒度:切詞后不在詞典中的詞是疑似錯(cuò)詞的概率大。

    錯(cuò)誤糾正

    • 通過錯(cuò)誤檢測定位所有疑似錯(cuò)誤后,取所有疑似錯(cuò)字的音似、形似候選詞,
    • 使用候選詞替換,基于語言模型得到類似翻譯模型的候選排序結(jié)果,得到最優(yōu)糾正詞。

    思考

  • 現(xiàn)在的處理手段,在詞粒度的錯(cuò)誤召回還不錯(cuò),但錯(cuò)誤糾正的準(zhǔn)確率還有待提高,更多優(yōu)質(zhì)的糾錯(cuò)集及糾錯(cuò)詞庫會(huì)有提升,我更希望算法上有更大的突破。
  • 另外,現(xiàn)在的文本錯(cuò)誤不再局限于字詞粒度上的拼寫錯(cuò)誤,需要提高中文語法錯(cuò)誤檢測(CGED, Chinese Grammar Error Diagnosis)及糾正能力,列在TODO中,后續(xù)調(diào)研。
  • Install

    • 全自動(dòng)安裝:pip install pycorrector
    • 半自動(dòng)安裝:
  • git clone https://github.com/shibing624/pycorrector.git
  • cd pycorrector
  • python setup.py install
  • 通過以上兩種方法的任何一種完成安裝都可以。如果不想安裝,可以下載github源碼包,安裝下面依賴再使用。

    安裝依賴

    • kenlm安裝
    pip install https://github.com/kpu/kenlm/archive/master.zip
    • 其他庫包安裝
    pip install -r requirements.txt

    Usage

    • 文本糾錯(cuò)
  • import pycorrector
  • corrected_sent, detail = pycorrector.correct('少先隊(duì)員因該為老人讓坐')
  • print(corrected_sent, detail)
  • output:

    少先隊(duì)員應(yīng)該為老人讓座 [[('因該', '應(yīng)該', 4, 6)], [('坐', '座', 10, 11)]]

    規(guī)則方法默認(rèn)會(huì)從路徑~/.pycorrector/datasets/zh_giga.no_cna_cmn.prune01244.klm加載kenlm語言模型文件,如果檢測沒有該文件,則程序會(huì)自動(dòng)聯(lián)網(wǎng)下載。當(dāng)然也可以手動(dòng)下載模型文件(2.8G)并放置于該位置。

    • 錯(cuò)誤檢測
  • import pycorrector
  • idx_errors = pycorrector.detect('少先隊(duì)員因該為老人讓坐')
  • print(idx_errors)
  • output:

    [['因該', 4, 6, 'word'], ['坐', 10, 11, 'char']]

    返回類型是list,?[error_word, begin_pos, end_pos, error_type],pos索引位置以0開始。

    • 關(guān)閉字粒度糾錯(cuò)
  • import pycorrector
  • error_sentence_1 = '我的喉嚨發(fā)炎了要買點(diǎn)阿莫細(xì)林吃'
  • correct_sent = pycorrector.correct(error_sentence_1)
  • print(correct_sent)
  • output:

    '我的喉嚨發(fā)炎了要買點(diǎn)阿莫西林吉', [['細(xì)林', '西林', 12, 14], ['吃', '吉', 14, 15]]

    上例中吃發(fā)生誤糾,如下代碼關(guān)閉字粒度糾錯(cuò):

  • import pycorrector
  • error_sentence_1 = '我的喉嚨發(fā)炎了要買點(diǎn)阿莫細(xì)林吃'
  • pycorrector.enable_char_error(enable=False)
  • correct_sent = pycorrector.correct(error_sentence_1)
  • print(correct_sent)
  • output:

    '我的喉嚨發(fā)炎了要買點(diǎn)阿莫西林吃', [['細(xì)林', '西林', 12, 14]]

    默認(rèn)字粒度、詞粒度的糾錯(cuò)都打開,一般情況下單字錯(cuò)誤發(fā)生較少,而且字粒度糾錯(cuò)準(zhǔn)確率較低。關(guān)閉字粒度糾錯(cuò),這樣可以提高糾錯(cuò)準(zhǔn)確率,提高糾錯(cuò)速度。

    默認(rèn)enable_char_error方法的enable參數(shù)為True,即打開錯(cuò)字糾正,這種方式可以召回字粒度錯(cuò)誤,但是整體準(zhǔn)確率會(huì)低;

    如果追求準(zhǔn)確率而不追求召回率的話,建議將enable設(shè)為False,僅使用錯(cuò)詞糾正。

    • 加載自定義混淆集

    通過加載自定義混淆集,支持用戶糾正已知的錯(cuò)誤,包括兩方面功能:1)錯(cuò)誤補(bǔ)召回;2)誤殺加白。

  • import pycorrector
  • pycorrector.set_log_level('INFO')
  • error_sentences = [
  • '買iPhone差,要多少錢',
  • '共同實(shí)際控制人蕭華、霍榮銓、張旗康',
  • ]
  • for line in error_sentences:
  • print(pycorrector.correct(line))
  • print('*' * 53)
  • pycorrector.set_custom_confusion_dict(path='./my_custom_confusion.txt')
  • for line in error_sentences:
  • print(pycorrector.correct(line))
  • output:

  • ('買iPhone差,要多少錢', []) # "iPhone差"漏召,應(yīng)該是"iphoneX"
  • ('共同實(shí)際控制人蕭華、霍榮銓、張啟康', [['張旗康', '張啟康', 14, 17]]) # "張啟康"誤殺,應(yīng)該不用糾
  • *****************************************************
  • ('買iPhoneX,要多少錢', [['iPhone差', 'iPhoneX', 1, 8]])
  • ('共同實(shí)際控制人蕭華、霍榮銓、張旗康', [])
  • 具體demo見example/use_custom_confusion.py,其中./my_custom_confusion.txt的內(nèi)容格式如下,以空格間隔:

  • iPhone差 iPhoneX 100
  • 張旗康 張旗康
  • set_custom_confusion_dict方法的path參數(shù)為用戶自定義混淆集文件路徑。

    • 加載自定義語言模型

    默認(rèn)提供下載并使用的kenlm語言模型zh_giga.no_cna_cmn.prune01244.klm文件是2.8G,內(nèi)存較小的電腦使用pycorrector程序可能會(huì)吃力些。

    支持用戶加載自己訓(xùn)練的kenlm語言模型,或使用2014版人民日報(bào)數(shù)據(jù)訓(xùn)練的模型,模型小(20M),準(zhǔn)確率低些。

  • from pycorrector import Corrector
  • pwd_path = os.path.abspath(os.path.dirname(__file__))
  • lm_path = os.path.join(pwd_path, './people_chars_lm.klm')
  • model = Corrector(language_model_path=lm_path)
  • corrected_sent, detail = model.correct('少先隊(duì)員因該為老人讓坐')
  • print(corrected_sent, detail)
  • output:

    少先隊(duì)員應(yīng)該為老人讓座 [[('因該', '應(yīng)該', 4, 6)], [('坐', '座', 10, 11)]]

    具體demo見example/load_custom_language_model.py,其中./people_chars_lm.klm是自定義語言模型文件。

    • 英文拼寫糾錯(cuò)

    支持英文單詞的拼寫錯(cuò)誤糾正。

  • import pycorrector
  • sent_lst = ['what', 'hapenning', 'how', 'to', 'speling', 'it', 'you', 'can', 'gorrect', 'it']
  • for i in sent_lst:
  • print(i, '=>', pycorrector.en_correct(i))
  • output:

  • what => what
  • hapenning => happening
  • how => how
  • to => to
  • speling => spelling
  • it => it
  • you => you
  • can => can
  • gorrect => correct
  • it => it
    • 中文簡繁互換

    支持中文繁體到簡體的轉(zhuǎn)換,和簡體到繁體的轉(zhuǎn)換。

  • import pycorrector
  • traditional_sentence = '憂郁的臺灣烏龜'
  • simplified_sentence = pycorrector.traditional2simplified(traditional_sentence)
  • print(traditional_sentence, '=>', simplified_sentence)
  • simplified_sentence = '憂郁的臺灣烏龜'
  • traditional_sentence = pycorrector.simplified2traditional(simplified_sentence)
  • print(simplified_sentence, '=>', traditional_sentence)
  • output:

  • 憂郁的臺灣烏龜 => 憂郁的臺灣烏龜
  • 憂郁的臺灣烏龜 => 憂郁的臺灣烏龜
  • Command Line Usage

    • 命令行模式

    支持批量文本糾錯(cuò)。

  • python -m pycorrector -h
  • usage: __main__.py [-h] -o OUTPUT [-n] [-d] input
  • @description:
  • positional arguments:
  • input the input file path, file encode need utf-8.
  • optional arguments:
  • -h, --help show this help message and exit
  • -o OUTPUT, --output OUTPUT
  • the output file path.
  • -n, --no_char disable char detect mode.
  • -d, --detail print detail info
  • case:

    python -m pycorrector input.txt -o out.txt -n -d

    輸入文件:input.txt;輸出文件:out.txt;關(guān)閉字粒度糾錯(cuò);打印詳細(xì)糾錯(cuò)信息;糾錯(cuò)結(jié)果以\t間隔

    Evaluate

    提供評估腳本pycorrector/utils/eval.py,該腳本有兩個(gè)功能:

    • 構(gòu)建評估樣本集:自動(dòng)生成評估集pycorrector/data/eval_corpus.json, 包括字粒度錯(cuò)誤100條、詞粒度錯(cuò)誤100條、語法錯(cuò)誤100條,正確句子200條。用戶可以修改條數(shù)生成其他評估樣本分布。
    • 計(jì)算糾錯(cuò)準(zhǔn)召率:采用保守計(jì)算方式,簡單把糾錯(cuò)之后與正確句子完成匹配的視為正確,否則為錯(cuò)。

    執(zhí)行該腳本后得到,規(guī)則方法糾錯(cuò)效果評估如下:

    • 準(zhǔn)確率:320/500=64%
    • 召回率:152/300=50.67%

    看來還有比較大的提升空間,誤殺和漏召回的都有。

    深度模型使用說明

    安裝依賴

    pip install -r requirements-dev.txt

    介紹

    本項(xiàng)目的初衷之一是比對、共享各種文本糾錯(cuò)方法,拋磚引玉的作用,如果對大家在文本糾錯(cuò)任務(wù)上有一點(diǎn)小小的啟發(fā)就是我莫大的榮幸了。

    主要使用了多種深度模型應(yīng)用于文本糾錯(cuò)任務(wù),分別是前面模型小節(jié)介紹的conv_seq2seq、seq2seq_attention、?transformer、bert、electra,各模型方法內(nèi)置于pycorrector文件夾下,有README.md詳細(xì)指導(dǎo),各模型可獨(dú)立運(yùn)行,相互之間無依賴。

    使用方法

    各模型均可獨(dú)立的預(yù)處理數(shù)據(jù)、訓(xùn)練、預(yù)測,下面以其中seq2seq_attention為例:

    seq2seq_attention 模型使用示例:

    配置

    通過修改config.py。

    數(shù)據(jù)預(yù)處理

  • cd seq2seq_attention
  • # 數(shù)據(jù)預(yù)處理
  • python preprocess.py
  • 自動(dòng)新建文件夾output,在output下生成train.txt和test.txt文件,以TAB("\t")間隔錯(cuò)誤文本和糾正文本,文本以空格切分詞,文件內(nèi)容示例:

  • 希 望 少 吸 煙 。 希 望 煙 民 們 少 吸 煙 。
  • 以 前 , 包 括 中 國 , 我 國 也 是 。 以 前 , 不 僅 中 國 , 我 國 也 是 。
  • 我 現(xiàn) 在 好 得 多 了 。 我 現(xiàn) 在 好 多 了 。
  • 訓(xùn)練

    python train.py

    訓(xùn)練過程截圖:?

    預(yù)測

    python infer.py

    預(yù)測輸出效果樣例:

  • input: 少先隊(duì)員因該給老人讓坐 output: 少先隊(duì)員因該給老人讓座
  • input: 少先隊(duì)員應(yīng)該給老人讓坐 output: 少先隊(duì)員應(yīng)該給老人讓座
  • input: 沒有解決這個(gè)問題, output: 沒有解決這個(gè)問題,,
  • input: 由我起開始做。 output: 由我起開始做
  • input: 由我起開始做 output: 由我開始做
  • PS:

  • 如果訓(xùn)練數(shù)據(jù)太少(不足萬條),深度模型擬合不足,會(huì)出現(xiàn)預(yù)測結(jié)果全為<unk>的情況,解決方法:增大訓(xùn)練樣本集,使用下方提供的糾錯(cuò)熟語料(nlpcc2018+hsk,130萬對句子)測試。
  • 深度模型訓(xùn)練耗時(shí)長,有GPU盡量用GPU,加速訓(xùn)練,節(jié)省時(shí)間。
  • 自定義語言模型

    語言模型對于糾錯(cuò)步驟至關(guān)重要,當(dāng)前默認(rèn)使用的是從千兆中文文本訓(xùn)練的中文語言模型zh_giga.no_cna_cmn.prune01244.klm(2.8G)。

    大家可以用中文維基(繁體轉(zhuǎn)簡體,pycorrector.utils.text_utils下有此功能)等語料數(shù)據(jù)訓(xùn)練通用的語言模型,或者也可以用專業(yè)領(lǐng)域語料訓(xùn)練更專用的語言模型。更適用的語言模型,對于糾錯(cuò)效果會(huì)有比較好的提升。

  • kenlm語言模型訓(xùn)練工具的使用,請見博客:http://blog.csdn.net/mingzai624/article/details/79560063
  • 附上訓(xùn)練語料<人民日報(bào)2014版熟語料>,包括: 1)標(biāo)準(zhǔn)人工切詞及詞性數(shù)據(jù)people2014.tar.gz, 2)未切詞文本數(shù)據(jù)people2014_words.txt, 3)kenlm訓(xùn)練字粒度語言模型文件及其二進(jìn)制文件people2014corpus_chars.arps/klm, 4)kenlm詞粒度語言模型文件及其二進(jìn)制文件people2014corpus_words.arps/klm。
  • 人民日報(bào)2014版熟語料,網(wǎng)盤鏈接:https://pan.baidu.com/s/1971a5XLQsIpL0zL0zxuK2A?密碼:uc11。尊重版權(quán),傳播請注明出處。

    中文糾錯(cuò)數(shù)據(jù)集

  • NLPCC 2018 GEC官方數(shù)據(jù)集NLPCC2018-GEC, 訓(xùn)練集trainingdata[解壓后114.5MB],該數(shù)據(jù)格式是原始文本,未做切詞處理。
  • 漢語水平考試(HSK)和lang8原始平行語料HSK+Lang8[190MB],該數(shù)據(jù)集已經(jīng)切詞,可用作數(shù)據(jù)擴(kuò)增
  • 以上語料,再加上CGED16、CGED17、CGED18的數(shù)據(jù),經(jīng)過以字切分,繁體轉(zhuǎn)簡體,打亂數(shù)據(jù)順序的預(yù)處理后,生成用于糾錯(cuò)的熟語料(nlpcc2018+hsk),網(wǎng)盤鏈接:https://pan.baidu.com/s/1BkDru60nQXaDVLRSr7ktfA密碼:m6fg [130萬對句子,215MB]
  • 貢獻(xiàn)及優(yōu)化點(diǎn)

    • [x] 優(yōu)化形似字字典,提高形似字糾錯(cuò)準(zhǔn)確率
    • [x] 整理中文糾錯(cuò)訓(xùn)練數(shù)據(jù),使用seq2seq做深度中文糾錯(cuò)模型
    • [x] 添加中文語法錯(cuò)誤檢測及糾正能力
    • [x] 規(guī)則方法添加用戶自定義糾錯(cuò)集,并將其糾錯(cuò)優(yōu)先度調(diào)為最高
    • [x] seq2seq_attention 添加dropout,減少過擬合
    • [x] 在seq2seq模型框架上,新增Pointer-generator network、Beam search、Unknown words replacement、Coverage mechanism等特性
    • [x] 更新bert的fine-tuned使用wiki,適配transformers 2.10.0庫
    • [x] 升級代碼,兼容TensorFlow 2.0庫
    • [x] 升級bert糾錯(cuò)邏輯,提升基于mask的糾錯(cuò)效果
    • [x] 新增基于electra模型的糾錯(cuò)邏輯,參數(shù)更小,預(yù)測更快

    討論群

    微信交流群,感興趣的同學(xué)可以加入溝通NLP文本糾錯(cuò)相關(guān)技術(shù),issues上回復(fù)不及時(shí)也可以在群里面提問。

    PS: 由于微信群滿100人了,掃碼加不了。掃我微信二維碼,或者搜索我微信號:xuming624, 備注:個(gè)人名稱-NLP糾錯(cuò)?進(jìn)群。

    ?

    ?

    引用

    如果你在研究中使用了pycorrector,請按如下格式引用:

  • @software{pycorrector,
  • author = {Xu Ming},
  • title = {{pycorrector: Text Correction Tool}},
  • year = {2020},
  • url = {https://github.com/shibing624/pycorrector},
  • }
  • License

    pycorrector 的授權(quán)協(xié)議為?Apache License 2.0,可免費(fèi)用做商業(yè)用途。請?jiān)诋a(chǎn)品說明中附加pycorrector的鏈接和授權(quán)協(xié)議。pycorrector受版權(quán)法保護(hù),侵權(quán)必究。

    References

    • 基于文法模型的中文糾錯(cuò)系統(tǒng)
    • Norvig’s spelling corrector
    • Chinese Spelling Error Detection and Correction Based on Language Model, Pronunciation, and Shape[Yu, 2013]
    • Chinese Spelling Checker Based on Statistical Machine Translation[Chiu, 2013]
    • Chinese Word Spelling Correction Based on Rule Induction[yeh, 2014]
    • Neural Language Correction with Character-Based Attention[Ziang Xie, 2016]
    • Chinese Spelling Check System Based on Tri-gram Model[Qiang Huang, 2014]
    • Neural Abstractive Text Summarization with Sequence-to-Sequence Models[Tian Shi, 2018]
    • 基于深度學(xué)習(xí)的中文文本自動(dòng)校對研究與實(shí)現(xiàn)[楊宗霖, 2019]
    • A Sequence to Sequence Learning for Chinese Grammatical Error Correction[Hongkai Ren, 2018]
    • ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators

    pycorrector

    Chinese text error correction tool.

    pycorrector?Use the language model to detect errors, pinyin feature and shape feature to correct chinese text error, it can be used for Chinese Pinyin and stroke input method.

    Features

    language model

    • Kenlm
    • RNNLM

    Usage

    install

    • pip install pycorrector / pip install pycorrector
    • Or download?https://github.com/shibing624/pycorrector, Unzip and run: python setup.py install

    correct

    input:

  • import pycorrector
  • corrected_sent, detail = pycorrector.correct('少先隊(duì)員因該為老人讓坐')
  • print(corrected_sent, detail)
  • output:

    少先隊(duì)員應(yīng)該為老人讓座 [[('因該', '應(yīng)該', 4, 6)], [('坐', '座', 10, 11)]]

    Future work

  • P(c), the language model. We could create a better language model by collecting more data, and perhaps by using a little English morphology (such as adding "ility" or "able" to the end of a word).

  • P(w|c), the error model. So far, the error model has been trivial: the smaller the edit distance, the smaller the error. Clearly we could use a better model of the cost of edits. get a corpus of spelling errors, and count how likely it is to make each insertion, deletion, or alteration, given the surrounding characters.

  • It turns out that in many cases it is difficult to make a decision based only on a single word. This is most obvious when there is a word that appears in the dictionary, but the test set says it should be corrected to another word anyway: correction('where') => 'where' (123); expected 'were' (452) We can't possibly know that correction('where') should be 'were' in at least one case, but should remain 'where' in other cases. But if the query had been correction('They where going') then it seems likely that "where" should be corrected to "were".

  • Finally, we could improve the implementation by making it much faster, without changing the results. We could re-implement in a compiled language rather than an interpreted one. We could cache the results of computations so that we don't have to repeat them multiple times. One word of advice: before attempting any speed optimizations, profile carefully to see where the time is actually going.

  • Further Reading

    • Roger Mitton has a survey article on spell checking.

    References

    • Norvig’s spelling corrector
    • Norvig’s spelling corrector(java version)
    pycorrector 糾錯(cuò)工具安裝 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

    總結(jié)

    以上是生活随笔為你收集整理的文本纠错pycorrector的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。