このプロジェクトでは、ペルシア語のウィキダンプ データセットをウィキペディアからダウンロードし、前処理して、最後にスペル チェッカーと kenlm 言語モデルをトレーニングします。
次の bash スクリプトを使用して、ペルシア語の wiki ダンプをダウンロードします。データセットのサイズは約 1G なので、しばらくお待ちください。
注: このリポジトリはペルシア語用であるため、イランに住んでいる場合は、VPN をオンにしてください。
language=fa
bash download_wiki_dump.sh $language
.bz2
形式を抽出して.txt
に変換します。 wikiextractor
使用すると、ダンプがクリーンアップされ、 .txt
ファイルに変換されます。こちらも時間がかかるかも知れません!
n_processors=16
bash extract_and_clean_wiki_dump.sh ${language}wiki-latest-pages-articles.xml.bz2 $n_processors
注: PDB エラーが発生した場合は、 expand_templates=True
変数をexpand_templates=False
に変更します。これは、wikiextractor/wikiextractor/extract.py の 948 行目あたりにあるclean_text
関数への入力引数です。
出力テキストは前処理および正規化して、「[doc]」などの不要なテキストを削除し、 hazm
およびnltk
ライブラリを使用してテキストを正規化する必要があります。
要件をインストールします。
pip install -r requirements.txt
メイン処理。少し時間がかかるかもしれません!
python preprocess_wiki_dump.py fawiki-latest-pages-articles.txt
python cleaner.py
このスクリプトを使用すると、コーパスの単語がカウントされます。その前に、追加の正規化とクリーニングも単語に適用されます。
sudo apt-get install pv
bash get_counts.sh
Symspell には、語彙とその出現を含むテキスト ファイルが必要です。 Get the word-count of the corpus
セクションで作成したfa_wiki.counts
は、頻度の高い上位 80,000 単語のみを含み、頻度が 50 未満の単語が含まれないようにトリミングする必要があります。
python get_spellchecker_top_vocabs.py --top-vocabs 80000 --ignore-less 25 --output wiki_fa_80k.txt
Symspell はシンプルなスペル チェッカーです。まず、次のコマンドを使用して pypi からインストールします。
pip install symspellpy
これを使用するには、 Get top frequent vocabs for SymSpell
作成した語彙辞書を使用してインスタンスを作成するだけです。
# import symspell
from symspellpy import SymSpell , Verbosity
# instantiate it
sym_spell = SymSpell ( max_dictionary_edit_distance = 2 , prefix_length = 7 )
dictionary_path = "wiki_fa_80k.txt"
sym_spell . load_dictionary ( dictionary_path , term_index = 0 , count_index = 1 )
# input sample:
input_term = "اهوار" # misspelling of "اهواز" It's a city name!
# lookup the dictionary
suggestions = sym_spell . lookup ( input_term , Verbosity . ALL , max_edit_distance = 2 )
# display suggestion term, term frequency, and edit distance
for suggestion in suggestions [: 5 ]:
print ( suggestion )
出力は次のとおりです。ご覧のとおり、 اهواز
が正しく選択されています。
اهواز, 1, 4692
ادوار, 1, 1350
الوار, 1, 651
انوار, 1, 305
اهورا, 1, 225
次のコードを使用すると、上位の最も頻繁に使用される 80K サンプルがkenlm_vocabs.txt
に書き込まれます。高速化するために、出現回数が 25 未満の語彙は破棄されます。
python get_kenlm_top_vocabs.py --top-vocabs 80000 --ignore-less 25 --output wiki_fa_kenlm_vocabs.txt
まず、次のコマンドを使用して KenLM 要件をインストールします。
sudo apt-get update
sudo apt-get install cmake build-essential libssl-dev libeigen3-dev libboost-all-dev zlib1g-dev libbz2-dev liblzma-dev -y
次に、C++ モジュールをclone
て作成します。
git clone https://github.com/kpu/kenlm.git
cd kenlm
mkdir -p build
cd build
cmake ..
make -j 4
すべてがうまくいけば、 ./kenlm/build/bin
/bin ディレクトリにlmplz
とbuild_binary
見つかります。最終的には、次の bash スクリプトを使用してkenlm
言語モデルをトレーニングします。
bash train_kenlm.sh -o 4 -l fa
注: バイナリ モジュールは、バイナリ化されていないモジュールよりもはるかに高速であるため、バイナリ モジュールも作成されます。
KenLm をインストールします。
pip install https://github.com/kpu/kenlm/archive/master.zip
使用方法:
import kenlm
model = kenlm.Model('fa_wiki.binary')
print("score: ", model.score('کشور ایران شهر تهران', bos=True, eos=True))
print("score: ", model.score('کشور تهران شهر ایران', bos=True, eos=True))
# score: -11.683658599853516
# score: -15.572178840637207
その他の例については、次のリンクを参照してください: https://github.com/kpu/kenlm/blob/master/python/example.py