在這個專案中,我從維基百科下載波斯語 wiki-dump 資料集,對其進行預處理,最後訓練拼字檢查器和 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
應修剪為僅包含 80k 個最常見的單詞,並阻止那些頻率低於 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
然後clone
並製作 C++ 模組:
git clone https://github.com/kpu/kenlm.git
cd kenlm
mkdir -p build
cd build
cmake ..
make -j 4
如果一切順利,你可以在./kenlm/build/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