Python のスペルチェッカー
-
Python の
autocorrectライブラリを使用したスペルチェッカー -
Python の
pyspellcheckerライブラリを使用したスペルチェッカー -
Python の
textblobライブラリを使用したスペルチェッカー
このチュートリアルでは、Python でスペルチェッカーを作成するために使用できる方法について説明します。
Python の autocorrect ライブラリを使用したスペルチェッカー
autocorrect は、Python でスペルチェッカーを開発するために使用できる外部ライブラリです。これは外部ライブラリであるため、コードで使用する前にダウンロードしてインストールする必要があります。autocorrect モジュールをインストールするコマンドを以下に示します。
pip install autocorrect
autocorrect ライブラリ内の Speller クラスを使用して、コンストラクターで言語を指定できます。次のサンプルコードは、autocorrect モジュールを使用してスペルチェッカーを作成する方法を示しています。
from autocorrect import Speller
spell = Speller(lang="en")
misspelled = ["scisors", "chemp", "celender", "berthday"]
for word in misspelled:
print("original word: " + word)
print("corrected word: " + spell(word))
出力:
original word: scisors
corrected word: scissors
original word: chemp
corrected word: champ
original word: celender
corrected word: calendar
original word: berthday
corrected word: birthday
上記のコードでは、autocorrect ライブラリ内の Speller クラスを使用してスペルチェッカーを開発しました。Speller クラスのインスタンス spell を作成し、コンストラクター内で英語を指定しました。通常の関数の場合と同じように、スペルミスのある単語をオブジェクト内に渡し、修正された単語を返しました。
Python の pyspellchecker ライブラリを使用したスペルチェッカー
pyspellchecker は、autocorrect ライブラリの代わりに使用して Python でスペルチェッカーを開発できる別の外部ライブラリです。
これは外部ライブラリでもあるため、コードで使用するには、ダウンロードしてインストールする必要があります。pyspellchecker ライブラリをインストールするコマンドを以下に示します。
pip install pyspellchecker
pyspellchecker ライブラリ内の SpellChecker クラスを使用して、正しい単語を予測できます。SpellChecker クラス内の correction() 関数は、スペルミスのある単語を入力引数として受け取り、修正された単語を文字列として返します。
次のプログラムは、pyspellchecker ライブラリを使用してスペルチェッカーを作成する方法を示しています。
from spellchecker import SpellChecker
spell = SpellChecker()
misspelled = ["scisors", "chemp", "celender", "berthday"]
for word in misspelled:
print("original word: " + word)
print("corrected word: " + spell.correction(word))
出力:
original word: scisors
corrected word: scissors
original word: chemp
corrected word: cheap
original word: celender
corrected word: calender
original word: berthday
corrected word: birthday
上記のコードの spellchecker モジュール内の SpellChecker クラスを使用してスペルチェッカーを開発しました。SpellChecker クラスのインスタンス spell を作成しました。デフォルトの言語は英語です。spell オブジェクトの correction() 関数内にスペルミスのある単語を渡し、修正された単語を返しました。
Python の textblob ライブラリを使用したスペルチェッカー
Python スペルチェッカーを開発するために、textblob ライブラリを使用することもできます。textblob は、テキストデータの処理に使用されます。これは外部ライブラリであり、次のコマンドでインストールする必要があります。
pip install textblob
textblob ライブラリ内の correct() 関数は、誤った単語の修正を返します。次のサンプルプログラムは、Python の textblob ライブラリを使用してスペルチェッカープログラムを作成する方法を示しています。
from textblob import TextBlob
misspelled = ["scisors", "chemp", "celender", "berthday"]
for word in misspelled:
print("original word: " + word)
spell = TextBlob(word)
print("corrected word: " + str(spell.correct()))
出力:
original word: scisors
corrected word: scissors
original word: chemp
corrected word: cheap
original word: celender
corrected word: slender
original word: berthday
corrected word: birthday
上記のコードでは、textblob ライブラリ内の TextBlob クラスを使用してスペルチェッカーを開発しました。TextBlob クラスのインスタンス spell を作成し、コンストラクター内に単語を渡しました。デフォルトの言語は英語です。次に、correct() 関数を使用して、その特定の単語に適したスペルを表示しました。
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn