speechbrain.k2_integration.prepare_lang 模块
该模块包含用于为k2训练准备词典和语言模型的函数。它基于k2/icefall中的脚本prepare_lang.sh(Fangjun Kuang的工作)。原始脚本遵循Apache 2.0许可证。此脚本已修改以与SpeechBrain一起使用。
- Modified by:
皮埃尔·冠军 2023
赵泽宇 2023
乔治奥斯·卡拉卡西迪斯 2023
摘要
函数:
它在标记的末尾添加伪标记消歧符号#1、#2等,以确保所有发音都不同,并且没有一个发音是另一个发音的前缀。 |
|
向FST的状态添加自环以通过它传播消歧符号。 |
|
生成ID映射,即将符号映射到唯一ID。 |
|
从词汇表中获取标记。 |
|
从词典中获取单词。 |
|
将词典转换为FST(k2格式),每个单词的开头和结尾可以选择性地添加静音。 |
|
将词典转换为FST(以k2格式)。 |
|
此函数以词汇文件“$lang_dir/lexicon.txt”作为输入,该文件由单词和标记(即音素)组成,并执行以下操作: |
|
将符号到ID的映射写入文件。 |
参考
- speechbrain.k2_integration.prepare_lang.write_mapping(filename: str | Path, sym2id: Dict[str, int]) None[source]
将符号到ID的映射写入文件。
- NOTE: No need to implement
read_mappingas it can be done through k2.SymbolTable.from_file().
- NOTE: No need to implement
- speechbrain.k2_integration.prepare_lang.get_tokens(lexicon: List[Tuple[str, List[str]]], sil_token='SIL', manually_add_sil_to_tokens=False) List[str][source]
从词典中获取标记。
- speechbrain.k2_integration.prepare_lang.get_words(lexicon: List[Tuple[str, List[str]]]) List[str][source]
从词典中获取单词。
- Parameters:
词汇表 (Lexicon) – 它是
read_lexicon()的返回值。- Returns:
返回一个唯一单词的列表。
- Return type:
sorted_ans
- speechbrain.k2_integration.prepare_lang.add_disambig_symbols(lexicon: List[Tuple[str, List[str]]]) Tuple[List[Tuple[str, List[str]]], int][source]
它在标记的末尾添加伪标记消歧符号#1、#2等,以确保所有发音都不同,并且没有一个发音是另一个发音的前缀。
另请参阅 kaldi 中的 add_lex_disambig.pl。
- Parameters:
词汇表 (Lexicon) – 它由
read_lexicon()返回。- Returns:
ans – 带有消歧符号的输出词典
max_disambig – 出现在词典中的最大消歧符号的ID
- speechbrain.k2_integration.prepare_lang.generate_id_map(symbols: List[str]) Dict[str, int][source]
生成ID映射,即将符号映射到唯一ID。
- Parameters:
symbols (List[str]) – 一组唯一的符号列表。
- Return type:
一个包含符号和ID之间映射的字典。
- speechbrain.k2_integration.prepare_lang.add_self_loops(arcs: List[List[Any]], disambig_token: int, disambig_word: int) List[List[Any]][source]
向FST的状态添加自环以传播消歧符号。它们被添加到每个状态上,这些状态至少有一条出弧具有非ε输出符号。
另请参见Kaldi中的fstaddselfloops.pl。一个区别是 Kaldi使用OpenFst风格的FST,并且它有多个最终状态。 此函数使用k2风格的FST,并且不需要向最终状态添加自环。
自循环的输入标签是
disambig_token,而输出标签是disambig_word。
- speechbrain.k2_integration.prepare_lang.lexicon_to_fst(lexicon: List[Tuple[str, List[str]]], token2id: Dict[str, int], word2id: Dict[str, int], sil_token: str = 'SIL', sil_prob: float = 0.5, need_self_loops: bool = False) k2.Fsa[source]
将词典转换为FST(以k2格式),每个单词的开头和结尾可以选择性地添加静音。
- speechbrain.k2_integration.prepare_lang.lexicon_to_fst_no_sil(lexicon: List[Tuple[str, List[str]]], token2id: Dict[str, int], word2id: Dict[str, int], need_self_loops: bool = False) k2.Fsa[source]
将词典转换为FST(以k2格式)。
- Parameters:
- Returns:
fsa – 表示给定词典的FSA。
- Return type:
k2.Fsa
- speechbrain.k2_integration.prepare_lang.prepare_lang(lang_dir, sil_token='SIL', sil_prob=0.5, cache=True)[source]
此函数将词汇文件“$lang_dir/lexicon.txt”作为输入,该文件包含单词和标记(即音素),并执行以下操作:
向词典中添加消歧符号并生成lexicon_disambig.txt
生成tokens.txt,即映射令牌到唯一整数的令牌表。
生成words.txt,将单词映射到唯一整数的单词表。
生成 L.pt,以 k2 格式。可以通过以下方式加载
d = torch.load(“L.pt”) lexicon = k2.Fsa.from_dict(d)
生成 L_disambig.pt,格式为 k2。
- Parameters:
- Return type:
无
Example
>>> from speechbrain.k2_integration.prepare_lang import prepare_lang
>>> # Create a small lexicon containing only two words and write it to a file. >>> lang_tmpdir = getfixture('tmpdir') >>> lexicon_sample = '''hello h e l l o\nworld w o r l d''' >>> lexicon_file = lang_tmpdir.join("lexicon.txt") >>> lexicon_file.write(lexicon_sample)
>>> prepare_lang(lang_tmpdir) >>> for expected_file in ["tokens.txt", "words.txt", "L.pt", "L_disambig.pt", "Linv.pt" ]: ... assert os.path.exists(os.path.join(lang_tmpdir, expected_file))