parent
db022fac6e
commit
bedbfac5a2
@ -0,0 +1 @@
|
|||||||
|
exp/
|
@ -0,0 +1,2 @@
|
|||||||
|
text_correct.txt: https://github.com/shibing624/pycorrector/raw/master/tests/test_file.txt
|
||||||
|
custom_confusion.txt: https://github.com/shibing624/pycorrector/raw/master/tests/custom_confusion.txt
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,37 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
stage=0
|
||||||
|
stop_stage=100
|
||||||
|
|
||||||
|
order=5
|
||||||
|
mem=80%
|
||||||
|
prune=0
|
||||||
|
a=22
|
||||||
|
q=8
|
||||||
|
b=8
|
||||||
|
|
||||||
|
source ${MAIN_ROOT}/utils/parse_options.sh || exit 1;
|
||||||
|
|
||||||
|
if [ $# != 3 ]; then
|
||||||
|
echo "$0 token_type exp/text exp/text.arpa"
|
||||||
|
echo $@
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# char or word
|
||||||
|
type=$1
|
||||||
|
text=$2
|
||||||
|
arpa=$3
|
||||||
|
|
||||||
|
if [ $stage -le 0 ] && [ $stop_stage -ge 0 ];then
|
||||||
|
# text tn & wordseg preprocess
|
||||||
|
echo "process text."
|
||||||
|
python3 local/zh_preprocess.py ${type} ${text} ${text}.${type}.tn
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $stage -le 1 ] && [ $stop_stage -ge 1 ];then
|
||||||
|
# train ngram lm
|
||||||
|
echo "build lm."
|
||||||
|
bash local/ngram_train.sh --order ${order} --mem ${mem} --prune "${prune}" ${text}.${type}.tn ${arpa}
|
||||||
|
fi
|
@ -0,0 +1,21 @@
|
|||||||
|
#! /usr/bin/env bash
|
||||||
|
|
||||||
|
. ${MAIN_ROOT}/utils/utility.sh
|
||||||
|
|
||||||
|
DIR=data/lm
|
||||||
|
mkdir -p ${DIR}
|
||||||
|
|
||||||
|
URL='https://deepspeech.bj.bcebos.com/zh_lm/zh_giga.no_cna_cmn.prune01244.klm'
|
||||||
|
MD5="29e02312deb2e59b3c8686c7966d4fe3"
|
||||||
|
TARGET=${DIR}/zh_giga.no_cna_cmn.prune01244.klm
|
||||||
|
|
||||||
|
|
||||||
|
echo "Download language model ..."
|
||||||
|
download $URL $MD5 $TARGET
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
echo "Fail to download the language model!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
exit 0
|
@ -0,0 +1,29 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
order=5
|
||||||
|
mem=80%
|
||||||
|
prune=0
|
||||||
|
a=22
|
||||||
|
q=8
|
||||||
|
b=8
|
||||||
|
|
||||||
|
source ${MAIN_ROOT}/utils/parse_options.sh || exit 1;
|
||||||
|
|
||||||
|
if [ $# != 2 ]; then
|
||||||
|
echo "$0 exp/text exp/text.arpa"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
text=${1}
|
||||||
|
arpa=${2}
|
||||||
|
lmbin=${2}.klm.bin
|
||||||
|
|
||||||
|
# https://kheafield.com/code/kenlm/estimation/
|
||||||
|
echo "build arpa lm."
|
||||||
|
lmplz -o ${order} -S ${mem} --prune ${prune} < ${text} >${arpa} || { echo "train kenlm error!"; exit -1; }
|
||||||
|
|
||||||
|
# https://kheafield.com/code/kenlm/
|
||||||
|
echo "build binary lm."
|
||||||
|
build_binary -a ${a} -q ${q} -b ${b} trie ${arpa} ${lmbin} || { echo "build kenlm binary error!"; exit -1; }
|
@ -0,0 +1,55 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from typing import List, Text
|
||||||
|
import sys
|
||||||
|
import jieba
|
||||||
|
import string
|
||||||
|
import re
|
||||||
|
from zhon import hanzi
|
||||||
|
|
||||||
|
def char_token(s: Text) -> List[Text]:
|
||||||
|
return list(s)
|
||||||
|
|
||||||
|
def word_token(s: Text) -> List[Text]:
|
||||||
|
return jieba.lcut(s)
|
||||||
|
|
||||||
|
def tn(s: Text) -> Text:
|
||||||
|
s = s.strip()
|
||||||
|
s = s.replace('*', '')
|
||||||
|
# rm english punctuations
|
||||||
|
s = re.sub(f'[re.escape(string.punctuation)]' , "", s)
|
||||||
|
# rm chinese punctuations
|
||||||
|
s = re.sub(f'[{hanzi.punctuation}]', "", s)
|
||||||
|
# text normalization
|
||||||
|
|
||||||
|
# rm english
|
||||||
|
s = ''.join(re.findall(hanzi.sent, s))
|
||||||
|
return s
|
||||||
|
|
||||||
|
def main(infile, outfile, tokenizer=None):
|
||||||
|
with open(infile, 'rt') as fin, open(outfile, 'wt') as fout:
|
||||||
|
lines = fin.readlines()
|
||||||
|
for l in lines:
|
||||||
|
l = tn(l)
|
||||||
|
if tokenizer:
|
||||||
|
l = ' '.join(tokenizer(l))
|
||||||
|
fout.write(l)
|
||||||
|
fout.write('\n')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
if len(sys.argv) != 4:
|
||||||
|
print(f"sys.arv[0] [char|word] text text_out ")
|
||||||
|
exit(-1)
|
||||||
|
|
||||||
|
token_type = sys.argv[1]
|
||||||
|
text = sys.argv[2]
|
||||||
|
text_out = sys.argv[3]
|
||||||
|
|
||||||
|
if token_type == 'char':
|
||||||
|
tokenizer = char_token
|
||||||
|
elif token_type == 'word':
|
||||||
|
tokenizer = word_token
|
||||||
|
else:
|
||||||
|
tokenizer = None
|
||||||
|
|
||||||
|
main(text, text_out, tokenizer)
|
@ -0,0 +1,13 @@
|
|||||||
|
export MAIN_ROOT=${PWD}/../../
|
||||||
|
|
||||||
|
export PATH=${MAIN_ROOT}:${PWD}/tools:${PATH}
|
||||||
|
export LC_ALL=C
|
||||||
|
|
||||||
|
# Use UTF-8 in Python to avoid UnicodeDecodeError when LC_ALL=C
|
||||||
|
export PYTHONIOENCODING=UTF-8
|
||||||
|
export PYTHONPATH=${MAIN_ROOT}:${PYTHONPATH}
|
||||||
|
|
||||||
|
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib/
|
||||||
|
|
||||||
|
MODEL=deepspeech2
|
||||||
|
export BIN_DIR=${MAIN_ROOT}/deepspeech/exps/${MODEL}/bin
|
@ -0,0 +1 @@
|
|||||||
|
jieba>=0.39
|
@ -0,0 +1,57 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
source path.sh
|
||||||
|
|
||||||
|
stage=0
|
||||||
|
stop_stage=100
|
||||||
|
|
||||||
|
source ${MAIN_ROOT}/utils/parse_options.sh || exit -1
|
||||||
|
|
||||||
|
python3 -c 'import kenlm;' || { echo "kenlm package not install!"; exit -1; }
|
||||||
|
|
||||||
|
if [ $stage -le 0 ] && [ $stop_stage -ge 0 ];then
|
||||||
|
# case 1, test kenlm
|
||||||
|
# download language model
|
||||||
|
bash local/download_lm_zh.sh
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# test kenlm `score` and `full_score`
|
||||||
|
python local/kenlm_score_test.py data/lm/zh_giga.no_cna_cmn.prune01244.klm
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p exp
|
||||||
|
cp data/text_correct.txt exp/text
|
||||||
|
|
||||||
|
if [ $stage -le 1 ] && [ $stop_stage -ge 1 ];then
|
||||||
|
# case 2, chinese chararctor ngram lm build
|
||||||
|
# output: xxx.arpa xxx.kenlm.bin
|
||||||
|
input=exp/text
|
||||||
|
token_type=char
|
||||||
|
lang=zh
|
||||||
|
order=5
|
||||||
|
prune="0 1 2 4 4"
|
||||||
|
a=22
|
||||||
|
q=8
|
||||||
|
b=8
|
||||||
|
output=${input}_${lang}_${token_type}_o${order}_p${prune// /_}_a${a}_q${q}_b${b}.arpa
|
||||||
|
echo "build ${token_type} lm."
|
||||||
|
bash local/build_zh_lm.sh --order ${order} --prune "${prune}" --a ${a} --q ${a} --b ${b} ${token_type} ${input} ${output}
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $stage -le 2 ] && [ $stop_stage -ge 2 ];then
|
||||||
|
# case 2, chinese chararctor ngram lm build
|
||||||
|
# output: xxx.arpa xxx.kenlm.bin
|
||||||
|
input=exp/text
|
||||||
|
token_type=word
|
||||||
|
lang=zh
|
||||||
|
order=3
|
||||||
|
prune="0 0 0"
|
||||||
|
a=22
|
||||||
|
q=8
|
||||||
|
b=8
|
||||||
|
output=${input}_${lang}_${token_type}_o${order}_p${prune// /_}_a${a}_q${q}_b${b}.arpa
|
||||||
|
echo "build ${token_type} lm."
|
||||||
|
bash local/build_zh_lm.sh --order ${order} --prune "${prune}" --a ${a} --q ${a} --b ${b} ${token_type} ${input} ${output}
|
||||||
|
fi
|
Loading…
Reference in new issue