speechbrain.k2_integration.lexicon 模块
词汇表类和实用工具。提供读取/写入词汇表文件并将其转换为k2不规则张量的函数。Lexicon类提供了一种将单词列表转换为包含令牌ID的不规则张量的方法。它还存储了词汇表图,可供图编译器用于解码序列。
这段代码经过调整,因此很大程度上受到或取自 icefall 的 (https://github.com/k2-fsa/icefall) Lexicon 类及其实用函数的启发。
- Authors:
皮埃尔·冠军 2023
赵泽宇 2023
乔治奥斯·卡拉卡西迪斯 2023
摘要
类:
基于单元的词典。 |
函数:
读取extra_csv_files以生成一个$lang_dir/lexicon.txt用于k2训练。 |
|
从 |
|
将词汇表写入文件。 |
参考
- class speechbrain.k2_integration.lexicon.Lexicon(lang_dir: Path)[source]
基础类:
object基于单元的词典。它用于将单词列表映射到每个单词的标记序列(字符)。它还存储了词典图,该图可以被图编译器用来解码序列。
- Parameters:
lang_dir (str) –
语言目录的路径。预计包含以下文件:
tokens.txt
words.txt
L.pt
Example
>>> from speechbrain.k2_integration import k2 >>> from speechbrain.k2_integration.lexicon import Lexicon >>> from speechbrain.k2_integration.graph_compiler import CtcGraphCompiler >>> 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) >>> # Create a lang directory with the lexicon and L.pt, L_inv.pt, L_disambig.pt >>> prepare_lang(lang_tmpdir) >>> # Create a lexicon object >>> lexicon = Lexicon(lang_tmpdir) >>> # Make sure the lexicon was loaded correctly >>> assert isinstance(lexicon.token_table, k2.SymbolTable) >>> assert isinstance(lexicon.L, k2.Fsa)
- property L_disambig: k2.Fsa
返回词汇表FSA(带有消歧符号)。 HLG构建所需。
- remove_LG_disambig_symbols(LG: k2.Fsa) k2.Fsa[source]
移除LG图的歧义符号 用于HLG构建。
- Parameters:
LG (k2.Fsa) – 要修改的LG图
- Returns:
LG – 修改后的LG图
- Return type:
k2.Fsa
- texts_to_word_ids(texts: List[str], add_sil_token_as_separator=False, sil_token_id: int | None = None, log_unknown_warning=True) List[List[int]][source]
将文本列表转换为单词ID。
此方法执行输入文本中每个单词到其对应ID的映射。 结果是一个列表的列表,其中每个内部列表包含一个句子的单词ID。 如果
add_sil_token_as_separator标志为True,则在单词之间插入一个静音标记, 并且sil_token_id参数指定静音标记的ID。 如果词汇表中找不到某个单词,并且log_unknown_warning为True,则会记录警告。- Parameters:
- Returns:
word_ids – 一个列表的列表,其中每个内部列表代表一个句子的单词ID。 这些单词ID是基于词汇表映射获得的。
- Return type:
列表[列表[int]]
- texts_to_token_ids(texts: List[str], log_unknown_warning=True) List[List[List[int]]][source]
将文本句子列表转换为标记ID。
- Parameters:
- Returns:
token_ids – 一个包含输入中每个句子的token ID的列表。 列表的结构如下: [
- [ # 对于第一个句子
[token_id_1, token_id_2, …, token_id_n], [token_id_1, token_id_2, …, token_id_m], …
], [ # 对于第二个句子
[token_id_1, token_id_2, …, token_id_p], [token_id_1, token_id_2, …, token_id_q], …
] 每个最内层的列表代表句子中一个单词的token ID。
- Return type:
列表[列表[列表[int]]]
- texts_to_token_ids_with_multiple_pronunciation(texts: List[str], log_unknown_warning=True) List[List[List[List[int]]]][source]
将输入文本列表转换为具有多种发音变体的标记ID。
此方法将输入文本转换为标记ID,考虑多种发音变体。 生成的结构允许处理给定文本中单词的各种发音。
- Parameters:
- Returns:
token_ids – 一个包含每个话语的令牌ID的嵌套列表结构。结构如下: - 外层列表:代表不同的话语。 - 中层列表:代表每个话语的不同发音变体。 - 内层列表:代表每个发音变体的令牌ID序列。 - 最内层列表:代表序列中每个单词的令牌ID。
- Return type:
列表[列表[列表[列表[int]]]]
- speechbrain.k2_integration.lexicon.prepare_char_lexicon(lang_dir, vocab_files, extra_csv_files=[], column_text_key='wrd', add_word_boundary=True)[source]
读取extra_csv_files以生成用于k2训练的$lang_dir/lexicon.txt。 这通常包括输出文件夹中训练集和开发集的csv文件。在训练过程中,我们需要确保lexicon.txt包含训练集和开发集中的所有(或大部分)单词。
注意:这里假设csv文件的最后一列包含转录信息。
还要注意,在每个csv_file中,第一行是标题,其余行遵循以下格式:
ID, 持续时间, wav, 说话者ID, 文字(转录)
在这个函数中我们只需要转录。
写出 $lang_dir/lexicon.txt
请注意,lexicon.txt 是一个具有以下格式的文本文件: 单词1 音素1 音素2 音素3 … 单词2 音素1 音素2 音素3 …
在这段代码中,我们简单地使用单词中的字符作为音素。 你可以使用其他音素集,例如音位、BPEs,来训练一个更好的模型。
- Parameters:
Example
>>> from speechbrain.k2_integration.lexicon import prepare_char_lexicon >>> # Create some dummy csv files containing only the words `hello`, `world`. >>> # The first line is the header, and the remaining lines are in the following >>> # format: >>> # ID, duration, wav, spk_id, wrd (transcription) >>> csv_file = getfixture('tmpdir').join("train.csv") >>> # Data to be written to the CSV file. >>> import csv >>> data = [ ... ["ID", "duration", "wav", "spk_id", "wrd"], ... [1, 1, 1, 1, "hello world"], ... [2, 0.5, 1, 1, "hello"] ... ] >>> with open(csv_file, "w", newline="", encoding="utf-8") as f: ... writer = csv.writer(f) ... writer.writerows(data) >>> extra_csv_files = [csv_file] >>> lang_dir = getfixture('tmpdir') >>> vocab_files = [] >>> prepare_char_lexicon(lang_dir, vocab_files, extra_csv_files=extra_csv_files, add_word_boundary=False)