dlxj commited on
Commit ·
8b090a7
1
Parent(s): eecbab9
同时使用多个 tar 数据集生成词表
Browse files- text_tokenizer.py +16 -10
text_tokenizer.py
CHANGED
|
@@ -9,14 +9,15 @@ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "scripts
|
|
| 9 |
# 为了避免冲突,我们直接调用其内部使用的函数,或者使用 os.system 来调用脚本
|
| 10 |
# 考虑到动态计算 VOCAB_SIZE,我们首先读取 manifest,收集所有的字符来计算 VOCAB_SIZE
|
| 11 |
|
| 12 |
-
def calculate_vocab_size(
|
| 13 |
unique_chars = set()
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
# 词汇表大小 = 独立字符数 + 预留特殊 token (如 <s>, </s>, <pad>, <unk> 等) 的数量
|
| 22 |
# 通常预留 10-20 个位置给特殊 token 和未见过的字符,这里我们加 20
|
|
@@ -25,11 +26,16 @@ def calculate_vocab_size(manifest_path):
|
|
| 25 |
return vocab_size
|
| 26 |
|
| 27 |
def main():
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
data_root = "data/common_voice_11_0/ja/tokenizers"
|
| 30 |
|
| 31 |
print("Calculating dynamic VOCAB_SIZE...")
|
| 32 |
-
vocab_size = calculate_vocab_size(
|
| 33 |
print(f"Calculated VOCAB_SIZE: {vocab_size}")
|
| 34 |
|
| 35 |
# 调用 process_asr_text_tokenizer
|
|
@@ -42,7 +48,7 @@ def main():
|
|
| 42 |
command = [
|
| 43 |
"python",
|
| 44 |
"scripts/tokenizers/process_asr_text_tokenizer.py",
|
| 45 |
-
f"--manifest={
|
| 46 |
f"--vocab_size={vocab_size}",
|
| 47 |
f"--data_root={data_root}",
|
| 48 |
"--tokenizer=spe",
|
|
|
|
| 9 |
# 为了避免冲突,我们直接调用其内部使用的函数,或者使用 os.system 来调用脚本
|
| 10 |
# 考虑到动态计算 VOCAB_SIZE,我们首先读取 manifest,收集所有的字符来计算 VOCAB_SIZE
|
| 11 |
|
| 12 |
+
def calculate_vocab_size(manifest_paths):
|
| 13 |
unique_chars = set()
|
| 14 |
+
for manifest_path in manifest_paths:
|
| 15 |
+
with open(manifest_path, 'r', encoding='utf-8') as f:
|
| 16 |
+
for line in f:
|
| 17 |
+
item = json.loads(line)
|
| 18 |
+
text = item.get('text', '')
|
| 19 |
+
for char in text:
|
| 20 |
+
unique_chars.add(char)
|
| 21 |
|
| 22 |
# 词汇表大小 = 独立字符数 + 预留特殊 token (如 <s>, </s>, <pad>, <unk> 等) 的数量
|
| 23 |
# 通常预留 10-20 个位置给特殊 token 和未见过的字符,这里我们加 20
|
|
|
|
| 26 |
return vocab_size
|
| 27 |
|
| 28 |
def main():
|
| 29 |
+
manifest_paths = [
|
| 30 |
+
"data/common_voice_11_0/ja/train/train_common_voice_11_0_manifest.json",
|
| 31 |
+
"data/common_voice_11_0/ja/test_tarred_1bk/tarred_audio_manifest.json",
|
| 32 |
+
"data/common_voice_11_0/ja/validation/validation_common_voice_11_0_manifest.json"
|
| 33 |
+
]
|
| 34 |
+
manifest_paths_str = ",".join(manifest_paths)
|
| 35 |
data_root = "data/common_voice_11_0/ja/tokenizers"
|
| 36 |
|
| 37 |
print("Calculating dynamic VOCAB_SIZE...")
|
| 38 |
+
vocab_size = calculate_vocab_size(manifest_paths)
|
| 39 |
print(f"Calculated VOCAB_SIZE: {vocab_size}")
|
| 40 |
|
| 41 |
# 调用 process_asr_text_tokenizer
|
|
|
|
| 48 |
command = [
|
| 49 |
"python",
|
| 50 |
"scripts/tokenizers/process_asr_text_tokenizer.py",
|
| 51 |
+
f"--manifest={manifest_paths_str}",
|
| 52 |
f"--vocab_size={vocab_size}",
|
| 53 |
f"--data_root={data_root}",
|
| 54 |
"--tokenizer=spe",
|