File size: 2,429 Bytes
1bb0419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""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)