forked from k2-fsa/icefall
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
stage=1 | ||
|
||
text=data/local/lm/librispeech-lm-norm.txt.gz | ||
text_dir=data/lm/text | ||
all_train_text=$text_dir/librispeech.txt | ||
# there are 40,398,052 pieces in all_train_text, which will take 50 MINUTES to be tokenized, with a single process. | ||
# use $train_pieces data to validate pipeline | ||
# train_pieces=300000 # 15 times of dev.txt | ||
# uncomment follwoing line to use all_train_text | ||
train_pieces= | ||
dev_text=$text_dir/dev.txt | ||
if [ $stage -le 0 ]; then | ||
# reference: | ||
# https://github.com/kaldi-asr/kaldi/blob/pybind11/egs/librispeech/s5/local/rnnlm/tuning/run_tdnn_lstm_1a.sh#L75 | ||
# use the same data seperation method to kaldi whose result can be used as a baseline | ||
if [ ! -f $text ]; then | ||
wget http://www.openslr.org/resources/11/librispeech-lm-norm.txt.gz -P data/local/lm | ||
fi | ||
echo -n >$text_dir/dev.txt | ||
# hold out one in every 2000 lines as dev data. | ||
gunzip -c $text | cut -d ' ' -f2- | awk -v text_dir=$text_dir '{if(NR%2000 == 0) { print >text_dir"/dev.txt"; } else {print;}}' >$all_train_text | ||
fi | ||
|
||
if [ $stage -eq 1 ]; then | ||
# for text_file in dev.txt librispeech.txt; do | ||
# python ./vq_pruned_transducer_stateless2/tokenize_text.py \ | ||
# --tokenizer-path ./data/lang_bpe_500/bpe.model \ | ||
# --text-file ./data/lm/text/$text_file | ||
# done | ||
lmplz -o 4 --text data/lm/text/librispeech.txt --arpa train.arpa -S 10% | ||
# lmplz -o 4 --text data/lm/text/librispeech.txt --arpa discount_train.arpa -S 10% \ | ||
# --discount_fallback | ||
# lmplz -o 4 --text data/lm/text/librispeech.txt.tokens --arpa token_train.arpa -S 10% \ | ||
# --discount_fallback 0.5 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from pathlib import Path | ||
from icefall.lexicon import read_lexicon | ||
import sentencepiece as spm | ||
import kenlm | ||
|
||
def extract_start_tokens(lang_dir: Path = Path("./data/lang_bpe_500/"): | ||
tokens = read_lexicon(lang_dir / "/tokens.txt") | ||
|
||
# Get the leading underscore of '▁THE 4'. | ||
# Actually its not a underscore, its just looks similar to it. | ||
word_start_char = tokens[4][0][0] | ||
|
||
word_start_token = [] | ||
non_start_token = [] | ||
|
||
aux=['<sos/eos>', '<unk>'] | ||
for t in tokens: | ||
leading_char = t[0][0] | ||
if leading_char == word_start_char or t[0] in aux: | ||
word_start_token.append(t) | ||
else: | ||
non_start_token.append(t) | ||
|
||
write_lexicon(lang_dir / "word_start_tokens.txt", word_start_token) | ||
write_lexicon(lang_dir / "non_start_tokens.txt", non_start_token) | ||
|
||
def lexicon_to_dict(lexicon): | ||
token2idx = {} | ||
idx2token = {} | ||
for token, idx in lexicon: | ||
assert len(idx) == 1 | ||
idx = idx[0] | ||
token2idx[token] = int(idx) | ||
idx2token[int(idx)] = token | ||
return token2idx, idx2token | ||
|
||
|
||
class LMRescorer: | ||
def __init__(self, lang_dir, blank_id, lm, weight): | ||
self.lm=lm | ||
self.start_token2idx, self.start_idx2token = lexicon_to_dict(read_lexicon(lang_dir/"word_start_tokens.txt")) | ||
self.nonstart_token2idx, self.nonstart_idx2token = lexicon_to_dict(read_lexicon(lang_dir/"non_start_tokens.txt")) | ||
self.token2idx, self.idx2token = lexicon_to_dict(read_lexicon(lang_dir/"tokens.txt")) | ||
self.sp = spm.SentencePieceProcessor() | ||
self.sp.load(str(lang_dir/"bpe.model")) | ||
self.blank_id = blank_id | ||
self.weight = weight | ||
|
||
def rescore(self, hyp): | ||
if self.weight > 0 and hyp.ys[-1] in self.start_idx2token: | ||
word = self.previous_word(hyp) | ||
output_state= kenlm.State() | ||
lm_score = self.lm.BaseScore(hyp.state, word, output_state) | ||
hyp.state = output_state | ||
hyp.log_prob += self.weight * lm_score | ||
return hyp | ||
|
||
def previous_word(self, hyp): | ||
last_start_idx = hyp.last_start_idx | ||
tokens_seq = hyp.ys[last_start_idx: -1] | ||
tokens_seq = [t for t in tokens_seq if t!=self.blank_id] | ||
word = self.sp.decode(tokens_seq) | ||
hyp.last_start_idx = len(hyp.ys) - 1 | ||
return word | ||
|
||
|
||
|