"""Compile zh/ja parallel voice list label legend: n - normal h - h i - interjection o - oral """ from pathlib import Path import re import json import argparse import csv from typing import Iterable RE_PARANTHESIS = re.compile(r"(.+?)|【.+?】") RE_SINGLE_RM = re.compile(r"「|」|“|”|〜|—|♪") RE_QPREFIX = re.compile(r"^(…*?)") SET_INTJ = set("呜唔嗯哈啊呃呣呼咕咚唉嘿嘻噗啾噜") SET_INTJP = {*SET_INTJ, *"…―~。、,!?"} def gather_voice(fi: Iterable[str], d: dict[str, str], lang: str): for line in fi: if '"voice"' not in line: continue jl = json.loads(line) d[jl["voice"]] = clean_text(jl["text"], lang) def clean_text(s: str, lang: str) -> str: s = RE_PARANTHESIS.sub(r"", s) s = RE_SINGLE_RM.sub(r"", s) if lang == "zh": s = RE_QPREFIX.sub(r"嗯\1", s) else: s = RE_QPREFIX.sub(r"ん\1", s) return s def label_voice( voice_map_zh: dict[str, str], voice_map_ja: dict[str, str] ) -> Iterable[list[str]]: for k, vja in sorted(voice_map_ja.items()): if (vzh := voice_map_zh.get(k)) is None: raise ValueError(f"Voice entry {k} not found in zh") label = label_rule(vzh) yield [k, vja, vzh, label] def label_rule(s: str) -> str: """Simple heuristic to label voice entry; needs manual labeling for "h" and "o" and manual check """ if not (set(s) - SET_INTJP): return "i" i_stat = sum(c in SET_INTJ for c in s) if i_stat >= 3 or (i_stat / len(s) > 0.1): return "ni" return "n" if __name__ == "__main__": argp = argparse.ArgumentParser(description="Compile zh/ja parallel voice list") argp.add_argument("volume", type=str, help="Volume name (e.g. 2019-aiyoku)") args = argp.parse_args() scenario_zh: Path = Path("scenario") / args.volume scenario_ja: Path = Path("scenario_ja") / args.volume voice_map_zh = {} voice_map_ja = {} for scenario, voice_map, lang in ( (scenario_zh, voice_map_zh, "zh"), (scenario_ja, voice_map_ja, "ja"), ): for sc in scenario.glob("*.jsonl"): with sc.open("r") as fi: gather_voice(fi, voice_map, lang) with open(f"{args.volume}.csv", "w", newline="") as csvfile: cw = csv.writer(csvfile) for row in label_voice(voice_map_zh, voice_map_ja): cw.writerow(row)