File size: 9,994 Bytes
beba983
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46ef82b
beba983
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a97b018
 
 
 
 
 
 
 
beba983
 
 
 
 
 
 
 
 
 
 
 
 
6a8f90f
 
 
 
 
 
 
 
beba983
 
6a8f90f
 
 
 
 
 
 
 
 
 
 
a97b018
6a8f90f
 
 
 
a97b018
beba983
 
 
 
6a8f90f
beba983
 
 
a97b018
beba983
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
# simord_hf_loader.py
# HF Datasets loader that uses mediqa-oe.data.process_data.attach_transcript_section
# to merge transcripts into local annotations: train, test1, test2.
#
# Dataset license: CDLA-2.0-permissive (for SIMORD). Respect upstream source licenses.
#
# Requirements:
#   pip install datasets nltk requests
#   pip install -e /path/to/mediqa-oe   # or add repo to PYTHONPATH
#
# Usage:
#   from datasets import load_dataset
#   ds = load_dataset(
#       "path/to/simord_hf_loader.py",
#       data_dir="path/to/annotations_dir",  # with train.json, test1.json, test2.json (lists of dicts)
#   )
#   print(ds)
#
# The loader will auto-download ACI-Bench and PriMock57 from GitHub to create transcript_dict.

import json
import os
import re
from typing import Dict, Iterable, List, Tuple

import datasets

# ---- Import the exact merge function from your repo ----
# (Relies on your local mediqa-oe being importable)
from mediqa_oe.data.process_data import attach_transcript_section  # noqa: E402

# ---- Sources (same as your script) ----
ACI_BENCH_URL = "https://github.com/wyim/aci-bench/archive/refs/heads/main.zip"
PRIMOCK_URL = "https://github.com/babylonhealth/primock57/archive/refs/heads/main.zip"

_DESCRIPTION = """\
SIMORD loader that merges transcripts from ACI-Bench and PriMock57 into local annotations
using mediqa-oe.data.process_data.attach_transcript_section.
"""
_CITATION = r"""@article{corbeil2025empowering,
  title={Empowering Healthcare Practitioners with Language Models: Structuring Speech Transcripts in Two Real-World Clinical Applications},
  author={Corbeil, Jean-Philippe and Ben Abacha, Asma and Michalopoulos, George and Swazinna, Patrick and Del-Agua, Miguel and Tremblay, Julien and Jeeson Daniel, Aju and Bader, Corey and Cho, Yoon-Chan and Krishnan, Parvathi and Bodenstab, Nathan and Lin, Tony and Teng, Wen and Beaulieu, Francois and Vozila, Paul},
  journal={arXiv preprint arXiv:2507.05517},
  year={2025}
}"""
_LICENSE = "CDLA-2.0-permissive"
_HOMEPAGE = "https://huggingface.co/datasets/microsoft/SIMORD"

# ----------------------- helpers to read upstream transcripts -----------------------

def _walk_json_files(directory: str) -> List[str]:
    out = []
    for d, _, files in os.walk(directory):
        for fn in files:
            if fn.lower().endswith(".json"):
                out.append(os.path.join(d, fn))
    out.sort()
    return out

def _read_json_records(path: str) -> Iterable[dict]:
    with open(path, "r", encoding="utf-8") as f:
        data = json.load(f)
    if isinstance(data, dict) and "data" in data and isinstance(data["data"], list):
        for r in data["data"]:
            yield r
    elif isinstance(data, list):
        for r in data:
            yield r
    else:
        # single dict record
        yield data

def _normalize_id_from_aci(file_field: str, basename: str) -> str:
    # matches your script:
    # file_id = "_".join(d.get("file", "").split("-")[0:2])
    # transcript_id = "acibench_" + file_id + "_" + basename
    file_id = "_".join((file_field or "").split("-")[0:2])
    return f"acibench_{file_id}_{basename}"

def _build_aci_transcript_dict(root: str) -> Dict[str, dict]:
    """
    Mirror of read_aci_bench_data + walk_aci_bench_directory:
      looks in .../aci-bench-main/data/challenge_data_json and .../src_experiment_data_json
      and builds {transcript_id: {"transcript": <src_text>}}
    """
    tdict: Dict[str, dict] = {}
    base = None
    # Find the 'aci-bench-main' folder inside root
    for name in os.listdir(root):
        if name.startswith("aci-bench"):
            base = os.path.join(root, name)
            break
    if not base:
        return tdict

    for sub in ("data/challenge_data_json", "data/src_experiment_data_json"):
        p = os.path.join(base, sub)
        if not os.path.isdir(p):
            continue
        for fp in _walk_json_files(p):
            basename = os.path.splitext(os.path.basename(fp))[0]
            for rec in _read_json_records(fp):
                src = rec.get("src")
                file_field = rec.get("file", "")
                tid = _normalize_id_from_aci(file_field, basename)
                if src:
                    tdict[tid] = {"transcript": src}
    return tdict

def _read_text(path: str) -> str:
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

def _normalize_primock_id(stem: str) -> str:
    # replicate replacements used in your script:
    # primock_id = filename.replace("day", "primock57_")
    # primock_id = primock_id.replace("consultation0", "")
    # primock_id = primock_id.replace("consultation", "")
    s = stem
    s = s.replace("day", "primock57_")
    s = s.replace("consultation0", "")
    s = s.replace("consultation", "")
    return s

def _build_primock_transcript_dict(root: str) -> Dict[str, dict]:
    """
    Mirror of read_primock_data post-conversion:
      reads *.txt in primock57-main/transcripts and applies the same tag normalization.
    """
    tdict: Dict[str, dict] = {}
    base = None
    for name in os.listdir(root):
        if name.startswith("primock57"):
            base = os.path.join(root, name)
            break
    if not base:
        return tdict

    tx_dir = os.path.join(base, "transcripts")
    if not os.path.isdir(tx_dir):
        # If transcripts haven't been generated yet by upstream script, we skip.
        return tdict

    for fn in os.listdir(tx_dir):
        if not fn.lower().endswith(".txt"):
            continue
        fp = os.path.join(tx_dir, fn)
        lines = [ln.strip() for ln in _read_text(fp).splitlines() if ln.strip()]
        norm = []
        for line in lines:
            line = line.replace("Doctor:", "[doctor]").replace("Patient:", "[patient]")
            norm.append(line)
        stem = os.path.splitext(fn)[0]
        primock_id = _normalize_primock_id(stem)
        tdict[primock_id] = {"transcript": "\n".join(norm)}
    return tdict

def _load_annotations(path: str) -> List[dict]:
    # Expect a JSON array (list of dicts). If JSONL, we also handle it gracefully.
    if path.lower().endswith((".jsonl", ".ndjson")):
        out = []
        with open(path, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if line:
                    out.append(json.loads(line))
        return out
    with open(path, "r", encoding="utf-8") as f:
        data = json.load(f)
    if isinstance(data, list):
        return data
    raise ValueError(f"{path} must be a JSON list (or JSONL).")

# ----------------------- HF builder -----------------------

class SimordMergeConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super().__init__(version=datasets.Version("1.0.0"), **kwargs)

class SimordMerge(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        SimordMergeConfig(
            name="default",
            description="SIMORD with transcripts merged via attach_transcript_section from mediqa-oe.",
        )
    ]
    DEFAULT_CONFIG_NAME = "default"

    def _info(self) -> datasets.DatasetInfo:
        # We expose a compact schema:
        # - id
        # - transcript: sequence of {turn_id, speaker, transcript}
        # - raw: JSON string dump of the full (possibly augmented) annotation record
        features = datasets.Features(
            {
                "id": datasets.Value("string"),
                "transcript": datasets.Sequence(
                    {
                        "turn_id": datasets.Value("int32"),
                        "speaker": datasets.Value("string"),
                        "transcript": datasets.Value("string"),
                    }
                ),
                "expected_orders": datasets.Sequence(
                    {
                        "order_type": datasets.Value("string"),
                        "description": datasets.Value("string"),
                        "reason": datasets.Value("string"),
                        "provenance": datasets.Sequence(datasets.Value("int32")),
                    }
                )
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager: datasets.DownloadManager):
        data_dir = self.config.data_dir or os.getcwd()

        # Map from local file name → desired HF split name
        file_map = {
            "train.json": ("train", datasets.Split.TRAIN),
            "dev.json":   ("test1", "test1"),   # expose dev as "test1"
            "test.json":  ("test2", "test2"),   # expose test as "test2"
        }

        # Download upstream corpora once
        aci_dir = dl_manager.download_and_extract(ACI_BENCH_URL)
        primock_dir = dl_manager.download_and_extract(PRIMOCK_URL)
        self._transcript_dict = {}
        self._transcript_dict.update(_build_aci_transcript_dict(aci_dir))
        self._transcript_dict.update(_build_primock_transcript_dict(primock_dir))

        splits = []
        for fname, (exposed_name, hf_split) in file_map.items():
            path = os.path.join(data_dir, fname)
            if os.path.isfile(path):
                splits.append(
                    datasets.SplitGenerator(
                        name=hf_split,
                        gen_kwargs={"ann_path": path},
                    )
                )
        return splits

    def _generate_examples(self, ann_path: str):
        section = _load_annotations(ann_path)
        attach_transcript_section(section, self._transcript_dict)
        for idx, rec in enumerate(section):
            rid = str(rec.get("id", idx))
            turns = rec.get("transcript") or []
            yield idx, {
                "id": rid,
                "transcript": turns,
                "expected_orders": json.dumps(rec, ensure_ascii=False),
            }