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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文本纠错pycorrector

發布時間:2024/7/5 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文本纠错pycorrector 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

這一段時間再研究身份證和面單識別項目,總發現一些識別準確率問題,在想辦法提高識別率,突然看了一篇文字糾錯方面資料,可以發現與ocr結合。開始收集糾錯方面的框架,感覺網上評價pycorrector 這個項目不錯

pycorrector

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

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

Demo

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

Question

中文文本糾錯任務,常見錯誤類型包括:

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

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

Solution

規則的解決思路

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

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

    模型

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

    錯誤檢測

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

    錯誤糾正

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

    思考

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

    • 全自動安裝:pip install pycorrector
    • 半自動安裝:
  • 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

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

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

    規則方法默認會從路徑~/.pycorrector/datasets/zh_giga.no_cna_cmn.prune01244.klm加載kenlm語言模型文件,如果檢測沒有該文件,則程序會自動聯網下載。當然也可以手動下載模型文件(2.8G)并放置于該位置。

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

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

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

    • 關閉字粒度糾錯
  • import pycorrector
  • error_sentence_1 = '我的喉嚨發炎了要買點阿莫細林吃'
  • correct_sent = pycorrector.correct(error_sentence_1)
  • print(correct_sent)
  • output:

    '我的喉嚨發炎了要買點阿莫西林吉', [['細林', '西林', 12, 14], ['吃', '吉', 14, 15]]

    上例中吃發生誤糾,如下代碼關閉字粒度糾錯:

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

    '我的喉嚨發炎了要買點阿莫西林吃', [['細林', '西林', 12, 14]]

    默認字粒度、詞粒度的糾錯都打開,一般情況下單字錯誤發生較少,而且字粒度糾錯準確率較低。關閉字粒度糾錯,這樣可以提高糾錯準確率,提高糾錯速度。

    默認enable_char_error方法的enable參數為True,即打開錯字糾正,這種方式可以召回字粒度錯誤,但是整體準確率會低;

    如果追求準確率而不追求召回率的話,建議將enable設為False,僅使用錯詞糾正。

    • 加載自定義混淆集

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

  • import pycorrector
  • pycorrector.set_log_level('INFO')
  • error_sentences = [
  • '買iPhone差,要多少錢',
  • '共同實際控制人蕭華、霍榮銓、張旗康',
  • ]
  • 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差"漏召,應該是"iphoneX"
  • ('共同實際控制人蕭華、霍榮銓、張啟康', [['張旗康', '張啟康', 14, 17]]) # "張啟康"誤殺,應該不用糾
  • *****************************************************
  • ('買iPhoneX,要多少錢', [['iPhone差', 'iPhoneX', 1, 8]])
  • ('共同實際控制人蕭華、霍榮銓、張旗康', [])
  • 具體demo見example/use_custom_confusion.py,其中./my_custom_confusion.txt的內容格式如下,以空格間隔:

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

    • 加載自定義語言模型

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

    支持用戶加載自己訓練的kenlm語言模型,或使用2014版人民日報數據訓練的模型,模型小(20M),準確率低些。

  • 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('少先隊員因該為老人讓坐')
  • print(corrected_sent, detail)
  • output:

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

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

    • 英文拼寫糾錯

    支持英文單詞的拼寫錯誤糾正。

  • 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
    • 中文簡繁互換

    支持中文繁體到簡體的轉換,和簡體到繁體的轉換。

  • 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

    • 命令行模式

    支持批量文本糾錯。

  • 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;關閉字粒度糾錯;打印詳細糾錯信息;糾錯結果以\t間隔

    Evaluate

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

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

    執行該腳本后得到,規則方法糾錯效果評估如下:

    • 準確率:320/500=64%
    • 召回率:152/300=50.67%

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

    深度模型使用說明

    安裝依賴

    pip install -r requirements-dev.txt

    介紹

    本項目的初衷之一是比對、共享各種文本糾錯方法,拋磚引玉的作用,如果對大家在文本糾錯任務上有一點小小的啟發就是我莫大的榮幸了。

    主要使用了多種深度模型應用于文本糾錯任務,分別是前面模型小節介紹的conv_seq2seq、seq2seq_attention、?transformer、bert、electra,各模型方法內置于pycorrector文件夾下,有README.md詳細指導,各模型可獨立運行,相互之間無依賴。

    使用方法

    各模型均可獨立的預處理數據、訓練、預測,下面以其中seq2seq_attention為例:

    seq2seq_attention 模型使用示例:

    配置

    通過修改config.py。

    數據預處理

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

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

    python train.py

    訓練過程截圖:?

    預測

    python infer.py

    預測輸出效果樣例:

  • input: 少先隊員因該給老人讓坐 output: 少先隊員因該給老人讓座
  • input: 少先隊員應該給老人讓坐 output: 少先隊員應該給老人讓座
  • input: 沒有解決這個問題, output: 沒有解決這個問題,,
  • input: 由我起開始做。 output: 由我起開始做
  • input: 由我起開始做 output: 由我開始做
  • PS:

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

    語言模型對于糾錯步驟至關重要,當前默認使用的是從千兆中文文本訓練的中文語言模型zh_giga.no_cna_cmn.prune01244.klm(2.8G)。

    大家可以用中文維基(繁體轉簡體,pycorrector.utils.text_utils下有此功能)等語料數據訓練通用的語言模型,或者也可以用專業領域語料訓練更專用的語言模型。更適用的語言模型,對于糾錯效果會有比較好的提升。

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

    中文糾錯數據集

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

    • [x] 優化形似字字典,提高形似字糾錯準確率
    • [x] 整理中文糾錯訓練數據,使用seq2seq做深度中文糾錯模型
    • [x] 添加中文語法錯誤檢測及糾正能力
    • [x] 規則方法添加用戶自定義糾錯集,并將其糾錯優先度調為最高
    • [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糾錯邏輯,提升基于mask的糾錯效果
    • [x] 新增基于electra模型的糾錯邏輯,參數更小,預測更快

    討論群

    微信交流群,感興趣的同學可以加入溝通NLP文本糾錯相關技術,issues上回復不及時也可以在群里面提問。

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

    ?

    ?

    引用

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

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

    pycorrector 的授權協議為?Apache License 2.0,可免費用做商業用途。請在產品說明中附加pycorrector的鏈接和授權協議。pycorrector受版權法保護,侵權必究。

    References

    • 基于文法模型的中文糾錯系統
    • 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]
    • 基于深度學習的中文文本自動校對研究與實現[楊宗霖, 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('少先隊員因該為老人讓坐')
  • print(corrected_sent, detail)
  • output:

    少先隊員應該為老人讓座 [[('因該', '應該', 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 糾錯工具安裝 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

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

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。