import sys
import re


# キャラクターの割り当て（必要に応じて変更可能）
character_map = {
    "あなたの名前": "ずんだもん（ノーマル）",
    "神谷先生": "四国めたん（ノーマル）"
}

# 入力ファイルと出力ファイル
input_file = "発表原稿_textbook.md"
output_file = "voicevox_script.txt"

argv = sys.argv
nargs = len(argv)
if nargs > 1: input_file = argv[1]
if nargs > 2: output_file = argv[2]

print()
print(f"{input_file=}")
print(f"{output_file=}")


# 出力用リスト
output_lines = []

# 正規表現でセリフ抽出
pattern = r"^\*\*(.+?)\*\*:\s*(.+)"

with open(input_file, "r", encoding="utf-8") as f:
    for line in f:
        line = line.strip()
        if not line.startswith("**"): continue

        match = re.match(pattern, line)
        if match:
            speaker, text = match.groups()
            if ':' in speaker: continue
            speaker = speaker.lstrip('[').rstrip(']')
            print("speaker:", speaker)
            character = character_map.get(speaker, "ずんだもん（ノーマル）")  # 未定義ならデフォルト
            output_lines.append(f"{character},{text}")

# ファイルに書き出し
with open(output_file, "w", encoding="utf-8") as f:
    f.write("\n".join(output_lines))

print("VOICEVOX用ファイルを生成しました:", output_file)
