Hướng dẫn nlp spelling correction python - trăn sửa chính tả nlp

Trong xử lý ngôn ngữ tự nhiên, điều quan trọng là các lỗi chính tả nên ít nhất có thể để bất cứ điều gì chúng ta thực hiện nên chính xác cao. Có những thư viện thực hiện nhiệm vụ tẻ nhạt này, thay vì bạn thực hiện tất cả các kiểm tra và sửa chữa.

Chúng tôi sẽ sử dụng khoảng cách Levenshtein, khoảng cách Hamming, Needman-Scunsch để kiểm tra độ chính xác của đầu ra.levenshtein distance, Hamming distance, Needleman-Wunsch to check accuracy of output.

Sử dụng ở đâu và tại sao

- Trong khi có cuộc trò chuyện với loại lỗi/lỗi chính tả xảy ra và do đó, sự hiểu biết ngữ cảnh trở nên khó khăn, đây là nơi điều chỉnh chính tả có thể đến

-Xử lý hậu kỳ OCR-Cho đến bây giờ không có OCR nào cho kết quả chính xác 100%, luôn có một số lỗi chính tả xảy ra.

- Tìm kiếm mờ & Kết hợp chuỗi gần đúng là một trường khác trong đó có thể sử dụng kiểm tra/chỉnh sửa chính tả. Và có nhiều ứng dụng hơn.

Thư viện chúng tôi sẽ sử dụng:

  1. Jamspell pip install jamspell là một thư viện đánh vần hiện đại. Đó là trọng lượng nhẹ, nhanh và chính xác. Nó xem xét từ xung quanh để thực hiện các sửa chữa tốt hơn. Nó có các tính năng sau: Nó xem xét các từ xung quanh [ngữ cảnh] để điều chỉnh tốt hơn 5k từ mỗi thứ hai → nó được viết bằng C ++ và có sẵn cho nhiều ngôn ngữ có liên kết Swig pip install jamspell is a modern spellchecking library. It is light-weight, fast and accurate. It consider word surroundings to make better corrections. It has following features:
    It considers words surroundings [context] for better correction
    Nearly 5K words per second
    Multi-language →it’s written in C++ and available for many languages with swig bindings
  2. Symspellpy pip install symspellpy Thuật toán hiệu chỉnh chính tả đối xứng làm giảm sự phức tạp của việc chỉnh sửa tạo ứng cử viên và tra cứu từ điển cho khoảng cách Damerau-Levenshtein nhất định. Đó là sáu bậc nhanh hơn [so với phương pháp tiêu chuẩn với Xóa + Chuyển + Thay thế + Chèn] và Ngôn ngữ độc lập. Một từ 5 chữ cái trung bình có khoảng 3 triệu lỗi chính tả có thể trong khoảng cách chỉnh sửa tối đa là 3, nhưng Symspell chỉ cần tạo ra 25 xóa để bao gồm tất cả, cả ở mức trước khi tính toán và thời gian tra cứu.pip install symspellpyThe Symmetric Delete spelling correction algorithm reduces the complexity of edit candidate generation and dictionary lookup for a given Damerau-Levenshtein distance. It is six orders of magnitude faster [than the standard approach with deletes + transposes + replaces + inserts] and language independent. An average 5 letter word has about 3 million possible spelling errors within a maximum edit distance of 3, but SymSpell needs to generate only 25 deletes to cover them all, both at pre-calculation and at lookup time.
  3. TextBLOB pip install textblob TextBlob, sửa lỗi chính tả dựa trên Peter Norvig, Cách viết một người chỉnh sửa chính tả như được thực hiện trong thư viện mẫu.pip install textblob textblob’s spelling correction is based on Peter Norvig’s “How to Write a Spelling Corrector” as implemented in the pattern library.

Hãy để sử dụng một số mẫu para và gây ra một số lỗi chính tả

Lỗi gây ra đoạn văn:

para_1:

para_1 = “wherre is the love hehad dated forImuch of the past who couqdn’tread in sixthgrade and ins pired him”

para_2:

para_2 = """As far as I am abl to judg, after long attnding to the sbject, the condiions of lfe apear to act in two ways—directly on the whle organsaton or on certin parts alne and indirectly by afcting the reproducte sstem. Wit respct to te dirct action, we mst bea in mid tht in every cse, as Profesor Weismann hs latly insistd, and as I have inidently shwn in my wrk on "Variatin undr Domesticcation," thcere arae two factrs: namly, the natre of the orgnism and the natture of the condiions. The frmer sems to be much th mre importannt; foor nealy siimilar variations sometimes aris under, as far as we cn juddge, disimilar conditios; annd, on te oter hannd, disssimilar variatioons arise undder conditions which aappear to be nnearly uniiform. The efffects on tthe offspring arre ieither definnite or in definite. They maay be considdered as definnite whhen allc or neearly all thhe ofefspring off inadividuals exnposed tco ceertain conditionas duriing seveal ggenerations aree moodified in te saame maner."""

para_3:

para_3 = """Cinderella came frm a grea family. She is the only daughter of an affluent and widowrr duke who has rewed to provide her witha stepmom and two stepsistrs. Cinderella’s mother died due to illness when she was stilll a younng girl, leawing her with a doll, faworite dress, and a pair of glasss slipppers."""
  • Jamspell
!wget //github.com/bakwc/JamSpell-models/raw/master/en.tar.gz
!tar -xvf en.tar.gz
import jamspell
jsp = jamspell.TSpellCorrector[]
assert jsp.LoadLangModel['en.bin']
jsp.FixFragment[para_1]
jsp.FixFragment[para_2]
jsp.FixFragment[para_3]

Kiểm tra đầu ra của cả ba para, đầu ra chính xác và điểm tương tự dưới đây.output of all three para, Correct output and similarity metrics below.

Lưu ý: Bạn có thể phải đối mặt với vấn đề trong việc cài đặt và chạy Jamspell, vì vậy tôi đã tạo một container Docker phơi bày gói Jamspell dưới dạng API, mà bạn có thể chạy trên máy cục bộ của mình. Bạn có thể tìm thấy các hướng dẫn ở đây về cách chạy hình ảnh Docker và đưa ra yêu cầu.: You might face problem in installing and running jamspell, so i have made a docker container that exposes jamspell package as API, that you can run on your local machine. You can find instructions here on how to run docker image and make a request.

  • Symspellpy

Ở đây tôi đã tải freq_dictionay_symspellpy.txt được sử dụng như một kho từ. Dữ liệu ở dạng 2 cột được phân tách bằng không gian, cột 1 là Word, cột thứ 2 là tần số của từ đó.

freq_dictionay_symspellpy.txt

Bạn có thể sử dụng kho văn bản của riêng bạn. Corpus được sử dụng trong mã có thể được tìm thấy ở đây

from symspellpy import SymSpell
symsp = SymSpell[]
symsp.load_dictionary['freq_dictionay_symspellpy.txt',\
term_index=0, \
count_index=1, \
separator=' ']

Bây giờ khi chúng tôi đã tải cho các từ chính xác của chúng tôi trong

para_2 = """As far as I am abl to judg, after long attnding to the sbject, the condiions of lfe apear to act in two ways—directly on the whle organsaton or on certin parts alne and indirectly by afcting the reproducte sstem. Wit respct to te dirct action, we mst bea in mid tht in every cse, as Profesor Weismann hs latly insistd, and as I have inidently shwn in my wrk on "Variatin undr Domesticcation," thcere arae two factrs: namly, the natre of the orgnism and the natture of the condiions. The frmer sems to be much th mre importannt; foor nealy siimilar variations sometimes aris under, as far as we cn juddge, disimilar conditios; annd, on te oter hannd, disssimilar variatioons arise undder conditions which aappear to be nnearly uniiform. The efffects on tthe offspring arre ieither definnite or in definite. They maay be considdered as definnite whhen allc or neearly all thhe ofefspring off inadividuals exnposed tco ceertain conditionas duriing seveal ggenerations aree moodified in te saame maner."""
0, hãy thử sửa chính tả các từ chính tả.

terms = symsp.lookup_compound[para_1,
max_edit_distance=2]
print[terms[0].term]
terms = symsp.lookup_compound[para_2,
max_edit_distance=2]
print[terms[0].term]
terms = symsp.lookup_compound[para_3,
max_edit_distance=2]
print[terms[0].term]
#max_edit_distance is the number of characters that can be #mismatched , you can say number of wrong characters it can tolerate

Đầu ra

Kiểm tra đầu ra của cả ba para, đầu ra chính xác và điểm tương tự dưới đây.output of all three para, Correct output and similarity metrics below.

  • Lưu ý: Bạn có thể phải đối mặt với vấn đề trong việc cài đặt và chạy Jamspell, vì vậy tôi đã tạo một container Docker phơi bày gói Jamspell dưới dạng API, mà bạn có thể chạy trên máy cục bộ của mình. Bạn có thể tìm thấy các hướng dẫn ở đây về cách chạy hình ảnh Docker và đưa ra yêu cầu.
from textblob import TextBlobprint[str[TextBlob[para_1].correct[]]]
print[str[TextBlob[para_2].correct[]]]
print[str[TextBlob[para_3].correct[]]]

Kiểm tra đầu ra của cả ba para, đầu ra chính xác và điểm tương tự dưới đây.output of all three para, Correct output and similarity metrics below.

Lưu ý: Bạn có thể phải đối mặt với vấn đề trong việc cài đặt và chạy Jamspell, vì vậy tôi đã tạo một container Docker phơi bày gói Jamspell dưới dạng API, mà bạn có thể chạy trên máy cục bộ của mình. Bạn có thể tìm thấy các hướng dẫn ở đây về cách chạy hình ảnh Docker và đưa ra yêu cầu.: yellow are the high score [Higher the better] among symspell, jamspell, textblob against para1, para2, para3 and measuring it with 3 different similarity measures with help of Damerau–Levenshtein, Hamming distance, Needleman-Wunsch. But it is just a rough estimation, you can check string output below.

Symspellpy

Chủ Đề