File size: 11,860 Bytes
85d56c8
 
 
f869b0b
85d56c8
f869b0b
 
85d56c8
f869b0b
 
 
 
85d56c8
 
f869b0b
85d56c8
 
 
 
 
 
f869b0b
85d56c8
f869b0b
85d56c8
 
 
f869b0b
85d56c8
 
f869b0b
85d56c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f869b0b
85d56c8
f869b0b
85d56c8
 
 
 
f869b0b
85d56c8
 
 
 
 
 
 
 
 
 
 
 
 
f869b0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85d56c8
f869b0b
 
85d56c8
f869b0b
85d56c8
 
 
f869b0b
85d56c8
 
 
 
f869b0b
 
85d56c8
 
 
 
f869b0b
85d56c8
 
 
 
 
 
 
 
 
 
f869b0b
85d56c8
 
 
 
 
 
 
 
 
 
 
f869b0b
85d56c8
 
 
 
 
 
 
 
f869b0b
85d56c8
 
 
 
 
 
f869b0b
85d56c8
 
 
f869b0b
85d56c8
f869b0b
85d56c8
 
 
 
f869b0b
85d56c8
f869b0b
85d56c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f869b0b
 
85d56c8
 
f869b0b
85d56c8
f869b0b
85d56c8
 
 
 
f869b0b
85d56c8
 
 
 
 
 
f869b0b
 
85d56c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f869b0b
85d56c8
f869b0b
85d56c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f869b0b
85d56c8
 
f869b0b
85d56c8
 
 
 
 
 
f869b0b
 
 
 
 
 
 
 
 
85d56c8
 
 
 
 
f869b0b
85d56c8
 
f869b0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85d56c8
 
f869b0b
 
85d56c8
 
cda796a
 
 
 
 
85d56c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f869b0b
 
 
 
 
85d56c8
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
cha_json.py — 將單一 CLAN .cha 轉成 JSON(強化 %mor/%wor/%gra 對齊)
用法:
    # CLI
    python3 cha_json.py --input /path/to/input.cha --output /path/to/output.json

程式化呼叫(供 pipeline 使用):
    from cha_json import cha_to_json_file, cha_to_dict
    out_path, data = cha_to_json_file("/path/in.cha", "/path/out.json")
    data2 = cha_to_dict("/path/in.cha")
"""

from __future__ import annotations
import re
import json
import sys
import argparse
from pathlib import Path
from collections import defaultdict
from typing import List, Dict, Any, Tuple, Optional

# 可接受的跨行停止條件(用於 %mor/%wor/%gra 合併)
TAG_PREFIXES = ("*PAR", "*INV", "%mor:", "%gra:", "%wor:", "@")
WORD_RE      = re.compile(r"[A-Za-z0-9]+")

# 病人角色:PAR / PAR0 / PAR1 / ...
ID_PAR_RE = re.compile(r"\|PAR\d*\|")

# 對話行:*INV: 或 *PAR0: / *PAR1: / ...
UTTER_RE = re.compile(r"^\*(INV|PAR\d+):")

# ────────── 同義集合(對齊時容忍形態變化) ──────────
SYN_SETS = [
    {"be", "am", "is", "are", "was", "were", "been", "being"},
    {"have", "has", "had"},
    {"do", "does", "did", "done", "doing"},
    {"go", "goes", "going", "went", "gone"},
    {"run", "runs", "running", "ran"},
    {"see", "sees", "seeing", "saw", "seen"},
    {"get", "gets", "getting", "got", "gotten"},
    {"drop", "drops", "dropping", "dropped"},
    {"swim", "swims", "swimming", "swam", "swum"},
]
def same_syn(a: str, b: str) -> bool:
    if not a or not b:
        return False
    for s in SYN_SETS:
        if a in s and b in s:
            return True
    return False

def canonical(txt: str) -> str:
    """token/word → 比對用字串:去掉 & ~ - | 之後的非字母數字、轉小寫"""
    head = re.split(r"[~\-\&|]", txt, 1)[0]
    m = WORD_RE.search(head)
    return m.group(0).lower() if m else ""

def merge_multiline(block_lines: List[str]) -> str:
    """
    合併跨行的 %mor/%wor/%gra。
    規則:以 '%' 開頭者作為起始,往下串,遇到新標籤或 @ 開頭就停。
    """
    merged, buf = [], None
    for raw in block_lines:
        ln = raw.rstrip("\n").replace("\x15", "")  # 去掉 CLAN 控制字
        if ln.lstrip().startswith("%") and ":" in ln:
            if buf:
                merged.append(buf)
            buf = ln
        else:
            if buf and ln.strip():
                buf += " " + ln.strip()
            else:
                merged.append(ln)
    if buf:
        merged.append(buf)
    return "\n".join(merged)

def cha_to_json(lines: List[str]) -> Dict[str, Any]:
    """
    將 .cha 檔行列表轉 JSON 結構。
    回傳格式:
    {
      "sentences": [...],
      "pos_mapping": {...},
      "grammar_mapping": {...},
      "aphasia_types": {...},
      "text_all": "..."        # 方便下游模型使用的 PAR 合併文字
    }
    """
    # 對應表(pos / gra 從 1 起算;aphasia 類型 0 起)
    pos_map: Dict[str, int]     = defaultdict(lambda: len(pos_map) + 1)
    gra_map: Dict[str, int]     = defaultdict(lambda: len(gra_map) + 1)
    aphasia_map: Dict[str, int] = defaultdict(lambda: len(aphasia_map))

    data: List[Dict[str, Any]] = []
    sent: Optional[Dict[str, Any]] = None

    i = 0
    while i < len(lines):
        line = lines[i].rstrip("\n")

        # 啟段
        if line.startswith("@Begin"):
            sent = {
                "sentence_id": f"S{len(data)+1}",
                "sentence_pid": None,
                "aphasia_type": None,   # 若最後仍沒有,就標 UNKNOWN
                "dialogues": []         # [ { "INV": [...], "PAR": [...] }, ... ]
            }
            i += 1
            continue

        # 結束
        if line.startswith("@End"):
            if sent and sent["dialogues"]:
                if not sent.get("aphasia_type"):
                    sent["aphasia_type"] = "UNKNOWN"
                    aphasia_map["UNKNOWN"]
                data.append(sent)
            sent = None
            i += 1
            continue

        # 句子屬性
        if sent and line.startswith("@PID:"):
            parts = line.split("\t")
            if len(parts) > 1:
                sent["sentence_pid"] = parts[1].strip()
            i += 1
            continue

        if sent and line.startswith("@ID:"):
            # 是否為病人那位 PAR*
            if ID_PAR_RE.search(line):
                aph = "UNKNOWN"
                # 如果 @ID 有標註失語類型,可在此使用 regex 抓出來並替換 aph
                # m = re.search(r"WAB:([A-Za-z]+)", line)
                # if m: aph = m.group(1)
                aph = aph.upper()
                aphasia_map[aph]            # 建立 map(自動編號)
                sent["aphasia_type"] = aph
            i += 1
            continue

        # 對話行:*INV: 或 *PARx:
        if sent and UTTER_RE.match(line):
            role_tag = UTTER_RE.match(line).group(1)
            role = "INV" if role_tag == "INV" else "PAR"

            if not sent["dialogues"]:
                sent["dialogues"].append({"INV": [], "PAR": []})
            # 新輪對話:若來的是 INV 且上一輪已有 PAR,視為下一輪
            if role == "INV" and sent["dialogues"][-1]["PAR"]:
                sent["dialogues"].append({"INV": [], "PAR": []})

            # 新增一個空 turn(之後 %mor/%wor/%gra 會補)
            sent["dialogues"][-1][role].append(
                {"tokens": [], "word_pos_ids": [], "word_grammar_ids": [], "word_durations": [], "utterance_text": ""}
            )
            i += 1
            continue

        # %mor
        if sent and line.startswith("%mor:"):
            blk = [line]; i += 1
            while i < len(lines) and not lines[i].lstrip().startswith(TAG_PREFIXES):
                blk.append(lines[i]); i += 1

            units = merge_multiline(blk).replace("%mor:", "").strip().split()
            toks, pos_ids = [], []
            for u in units:
                if "|" in u:
                    pos, rest = u.split("|", 1)
                    word = rest.split("|", 1)[0]
                    toks.append(word)
                    pos_ids.append(pos_map[pos])

            dlg = sent["dialogues"][-1]
            tgt = dlg["PAR"][-1] if dlg["PAR"] else dlg["INV"][-1]
            tgt["tokens"], tgt["word_pos_ids"] = toks, pos_ids
            # 也保存 plain text 供下游模型使用
            tgt["utterance_text"] = " ".join(toks).strip()
            continue

        # %wor
        if sent and line.startswith("%wor:"):
            blk = [line]; i += 1
            while i < len(lines) and not lines[i].lstrip().startswith(TAG_PREFIXES):
                blk.append(lines[i]); i += 1

            merged = merge_multiline(blk).replace("%wor:", "").strip()
            # 抓 <word> <start>_<end>
            raw_pairs = re.findall(r"(\S+)\s+(\d+)_(\d+)", merged)
            wor = [(w, int(s), int(e)) for (w, s, e) in raw_pairs]

            dlg = sent["dialogues"][-1]
            tgt = dlg["PAR"][-1] if dlg["PAR"] else dlg["INV"][-1]

            # 與 %mor tokens 對齊,duration = end - start
            aligned: List[Tuple[str, int]] = []
            j = 0
            for tok in tgt.get("tokens", []):
                c_tok = canonical(tok)
                match = None
                for k in range(j, len(wor)):
                    c_w = canonical(wor[k][0])
                    if (
                        c_tok == c_w
                        or c_w.startswith(c_tok)
                        or c_tok.startswith(c_w)
                        or same_syn(c_tok, c_w)
                    ):
                        match = wor[k]
                        j = k + 1
                        break
                dur = (match[2] - match[1]) if match else 0
                aligned.append([tok, dur])
            tgt["word_durations"] = aligned
            continue

        # %gra
        if sent and line.startswith("%gra:"):
            blk = [line]; i += 1
            while i < len(lines) and not lines[i].lstrip().startswith(TAG_PREFIXES):
                blk.append(lines[i]); i += 1

            units = merge_multiline(blk).replace("%gra:", "").strip().split()
            triples = []
            for u in units:
                # 例:1|2|DET
                parts = u.split("|")
                if len(parts) == 3:
                    a, b, r = parts
                    if a.isdigit() and b.isdigit():
                        triples.append([int(a), int(b), gra_map[r]])

            dlg = sent["dialogues"][-1]
            tgt = dlg["PAR"][-1] if dlg["PAR"] else dlg["INV"][-1]
            tgt["word_grammar_ids"] = triples
            continue

        # 其他行
        i += 1

    # 收尾(檔案若意外沒 @End)
    if sent and sent["dialogues"]:
        if not sent.get("aphasia_type"):
            sent["aphasia_type"] = "UNKNOWN"
            aphasia_map["UNKNOWN"]
        data.append(sent)

    # 建立 text_all:把所有 PAR utterance_text 串起來
    par_texts: List[str] = []
    for s in data:
        for turn in s.get("dialogues", []):
            for par_ut in turn.get("PAR", []):
                if par_ut.get("utterance_text"):
                    par_texts.append(par_ut["utterance_text"])
    text_all = "\n".join(par_texts).strip()

    return {
        "sentences": data,
        "pos_mapping": dict(pos_map),
        "grammar_mapping": dict(gra_map),
        "aphasia_types": dict(aphasia_map),
        "text_all": text_all
    }

# ────────── 封裝:檔案 → dict / 檔案 → 檔案 ──────────
def cha_to_dict(cha_path: str) -> Dict[str, Any]:
    """讀取 .cha 檔並回傳 dict(不寫檔)。"""
    p = Path(cha_path)
    if not p.exists():
        raise FileNotFoundError(f"找不到檔案: {cha_path}")
    with p.open("r", encoding="utf-8") as fh:
        lines = fh.readlines()
    return cha_to_json(lines)

def cha_to_json_file(cha_path: str, output_json: Optional[str] = None) -> Tuple[str, Dict[str, Any]]:
    """
    將 .cha 轉成 JSON 並寫檔。
    回傳:(output_json_path, data_dict)
    """
    data = cha_to_dict(cha_path)
    out_path = Path(output_json) if output_json else Path(cha_path).with_suffix(".json")
    out_path.parent.mkdir(parents=True, exist_ok=True)
    with out_path.open("w", encoding="utf-8") as fh:
        json.dump(data, fh, ensure_ascii=False, indent=4)
    return str(out_path), data

# ────────── CLI ──────────
def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument("--input", "-i", type=str, required=True, help="輸入 .cha 檔")
    p.add_argument("--output", "-o", type=str, required=True, help="輸出 .json 檔")
    return p.parse_args()

def cha_to_json_path(cha_path: str, output_json: str | None = None) -> str:
    """Backward-compatible alias for old code."""
    out, _ = cha_to_json_file(cha_path, output_json=output_json)
    return out

def main():
    args = parse_args()
    in_path  = Path(args.input)
    out_path = Path(args.output)

    if not in_path.exists():
        sys.exit(f"❌ 找不到檔案: {in_path}")

    with in_path.open("r", encoding="utf-8") as fh:
        lines = fh.readlines()

    dataset = cha_to_json(lines)

    out_path.parent.mkdir(parents=True, exist_ok=True)
    with out_path.open("w", encoding="utf-8") as fh:
        json.dump(dataset, fh, ensure_ascii=False, indent=4)

    print(
        f"✅ 轉換完成 → {out_path}(句數 {len(dataset['sentences'])},"
        f"pos={len(dataset['pos_mapping'])},gra={len(dataset['grammar_mapping'])},"
        f"類型鍵={list(dataset['aphasia_types'].keys())})"
    )

if __name__ == "__main__":
    main()