File size: 25,379 Bytes
7d5f092
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
"""ELAN Annotation Format (.eaf) Export

Generates standard ELAN XML files from voice profile analysis results.
Produces a multi-tier annotation corpus suitable for:
  - PhD linguistic research
  - Corpus-based phonological studies
  - Cross-speaker contrastive analysis
  - Longitudinal acquisition tracking

ELAN spec: https://archive.mpi.nl/tla/elan/documentation

Tiers generated:
  1. Transcription       β€” Word-level transcript with timestamps
  2. Phonemes            β€” Phone-level segmentation (from forced alignment or Wav2Vec)
  3. Prosody             β€” Intonation patterns, stress, rhythm annotations
  4. L1_Interference     β€” Detected L1 transfer patterns with severity
  5. Voice_Quality       β€” Phonation type, breathiness, creak annotations
  6. Cognitive_Load      β€” Filled pauses, hesitations, self-corrections
  7. Connected_Speech    β€” Assimilation, elision, linking events
  8. CIF_Score           β€” Overall Contrastive Interference Index per segment
  9. Emotion             β€” Emotional valence/arousal labels
  10. Speaker_Metadata   β€” Speaker ID, language, session info
"""

from __future__ import annotations

import logging
import time
import uuid
from pathlib import Path
from typing import Any
from xml.etree.ElementTree import Element, SubElement, tostring
from xml.dom import minidom

logger = logging.getLogger(__name__)

# ELAN namespace and schema
ELAN_SCHEMA = "http://www.mpi.nl/tools/elan/EAFv3.0.xsd"
ELAN_FORMAT = "3.0"


def _ts_id(counter: list[int]) -> str:
    """Generate a unique time slot ID."""
    counter[0] += 1
    return f"ts{counter[0]}"


def _ann_id(counter: list[int]) -> str:
    """Generate a unique annotation ID."""
    counter[0] += 1
    return f"a{counter[0]}"


def _ms_to_elan(ms: float) -> int:
    """Convert milliseconds (float) to ELAN time value (int ms)."""
    return int(round(ms))


def generate_eaf(
    profile: dict[str, Any],
    audio_path: Path | str,
    speaker_id: str = "anonymous",
    student_name: str = "Student",
    language: str = "en",
    l1_language: str = "bho",
    session_id: str | None = None,
) -> str:
    """Generate a complete ELAN .eaf XML document from analysis results.

    Args:
        profile: Full pipeline output dict (all 10 layers).
        audio_path: Path to the source audio file.
        speaker_id: Unique speaker identifier.
        student_name: Display name.
        language: Target language code.
        l1_language: L1 language code.
        session_id: Optional session identifier.

    Returns:
        EAF XML as a formatted string.
    """
    audio_path = Path(audio_path)
    session_id = session_id or f"session_{int(time.time())}"
    ts_counter = [0]
    ann_counter = [0]

    # ── Root element ─────────────────────────────────────────────────
    root = Element("ANNOTATION_DOCUMENT")
    root.set("AUTHOR", "Contrastive Voice Profiling Engine")
    root.set("DATE", time.strftime("%Y-%m-%dT%H:%M:%S+00:00"))
    root.set("FORMAT", ELAN_FORMAT)
    root.set("VERSION", ELAN_FORMAT)
    root.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
    root.set("xsi:noNamespaceSchemaLocation", ELAN_SCHEMA)

    # ── Header ───────────────────────────────────────────────────────
    header = SubElement(root, "HEADER")
    header.set("MEDIA_FILE", "")
    header.set("TIME_UNITS", "milliseconds")

    media = SubElement(header, "MEDIA_DESCRIPTOR")
    media.set("MEDIA_URL", f"file:///{audio_path.resolve()}")
    media.set("MIME_TYPE", "audio/x-wav")
    media.set("RELATIVE_MEDIA_URL", f"./{audio_path.name}")

    # Properties
    for key, val in [
        ("speaker_id", speaker_id),
        ("student_name", student_name),
        ("l1_language", l1_language),
        ("target_language", language),
        ("session_id", session_id),
        ("generator", "contrastive-voice-profiling-engine"),
    ]:
        prop = SubElement(header, "PROPERTY")
        prop.set("NAME", key)
        prop.text = str(val)

    # ── Collect all time points ──────────────────────────────────────
    time_slots: list[tuple[str, int]] = []

    def _add_ts(ms: float) -> str:
        ts_id = _ts_id(ts_counter)
        time_slots.append((ts_id, _ms_to_elan(ms)))
        return ts_id

    # ── Build tiers ──────────────────────────────────────────────────
    tiers_data: list[dict] = []

    # Tier 1: Transcription (word-level)
    _build_transcription_tier(profile, tiers_data, _add_ts, ann_counter)

    # Tier 2: Phonemes
    _build_phoneme_tier(profile, tiers_data, _add_ts, ann_counter)

    # Tier 3: Prosody
    _build_prosody_tier(profile, tiers_data, _add_ts, ann_counter)

    # Tier 4: L1 Interference
    _build_l1_tier(profile, tiers_data, _add_ts, ann_counter, l1_language)

    # Tier 5: Voice Quality
    _build_voice_quality_tier(profile, tiers_data, _add_ts, ann_counter)

    # Tier 6: Cognitive Load
    _build_cognitive_load_tier(profile, tiers_data, _add_ts, ann_counter)

    # Tier 7: Connected Speech
    _build_connected_speech_tier(profile, tiers_data, _add_ts, ann_counter)

    # Tier 8: CIF Score
    _build_cif_tier(profile, tiers_data, _add_ts, ann_counter)

    # Tier 9: Emotion
    _build_emotion_tier(profile, tiers_data, _add_ts, ann_counter)

    # Tier 10: Metadata (single span)
    _build_metadata_tier(
        profile, tiers_data, _add_ts, ann_counter,
        speaker_id, student_name, language, l1_language, session_id,
    )

    # ── Write TIME_ORDER ─────────────────────────────────────────────
    time_order = SubElement(root, "TIME_ORDER")
    # Sort by time value for valid EAF
    time_slots.sort(key=lambda x: x[1])
    for ts_id, ts_val in time_slots:
        ts_el = SubElement(time_order, "TIME_SLOT")
        ts_el.set("TIME_SLOT_ID", ts_id)
        ts_el.set("TIME_VALUE", str(ts_val))

    # ── Write TIERs ──────────────────────────────────────────────────
    for tier_info in tiers_data:
        tier_el = SubElement(root, "TIER")
        tier_el.set("LINGUISTIC_TYPE_REF", tier_info.get("type_ref", "default-lt"))
        tier_el.set("TIER_ID", tier_info["tier_id"])
        if "participant" in tier_info:
            tier_el.set("PARTICIPANT", tier_info["participant"])
        if "annotator" in tier_info:
            tier_el.set("ANNOTATOR", tier_info["annotator"])

        for ann in tier_info.get("annotations", []):
            ann_el = SubElement(tier_el, "ANNOTATION")
            align_ann = SubElement(ann_el, "ALIGNABLE_ANNOTATION")
            align_ann.set("ANNOTATION_ID", ann["id"])
            align_ann.set("TIME_SLOT_REF1", ann["ts1"])
            align_ann.set("TIME_SLOT_REF2", ann["ts2"])
            value_el = SubElement(align_ann, "ANNOTATION_VALUE")
            value_el.text = ann["value"]

    # ── Linguistic Types ─────────────────────────────────────────────
    ling_types = [
        "default-lt", "phoneme-lt", "prosody-lt", "interference-lt",
        "voice-quality-lt", "cognitive-lt", "connected-speech-lt",
        "cif-lt", "emotion-lt", "metadata-lt",
    ]
    for lt in ling_types:
        lt_el = SubElement(root, "LINGUISTIC_TYPE")
        lt_el.set("GRAPHIC_REFERENCES", "false")
        lt_el.set("LINGUISTIC_TYPE_ID", lt)
        lt_el.set("TIME_ALIGNABLE", "true")

    # ── Constraints ──────────────────────────────────────────────────
    for stereo, desc in [
        ("Time_Subdivision", "Time subdivision of parent annotation's time interval"),
        ("Symbolic_Subdivision", "Symbolic subdivision of parent annotation's time interval"),
        ("Symbolic_Association", "1-1 association with a parent annotation"),
        ("Included_In", "Time included in parent annotation's time interval"),
    ]:
        con = SubElement(root, "CONSTRAINT")
        con.set("DESCRIPTION", desc)
        con.set("STEREOTYPE", stereo)

    # ── Format and return ────────────────────────────────────────────
    raw_xml = tostring(root, encoding="unicode")
    parsed = minidom.parseString(raw_xml)
    return parsed.toprettyxml(indent="  ", encoding=None)


# ── Tier Builders ────────────────────────────────────────────────────────

def _build_transcription_tier(profile, tiers, add_ts, ann_ctr):
    """Tier 1: Word-level transcription with timestamps."""
    trans = profile.get("transcription", {})
    word_ts = trans.get("word_timestamps", [])

    annotations = []
    for w in word_ts:
        if not w.get("word"):
            continue
        start = w.get("start", 0) * 1000 if w.get("start", 0) < 100 else w.get("start", 0)
        end = w.get("end", 0) * 1000 if w.get("end", 0) < 100 else w.get("end", 0)
        ts1 = add_ts(start)
        ts2 = add_ts(end)
        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": w["word"],
        })

    # If no word timestamps, use segment-level
    if not annotations:
        for seg in trans.get("segments", []):
            ts1 = add_ts(seg["start"] * 1000)
            ts2 = add_ts(seg["end"] * 1000)
            annotations.append({
                "id": _ann_id(ann_ctr),
                "ts1": ts1, "ts2": ts2,
                "value": seg.get("text", "").strip(),
            })

    tiers.append({
        "tier_id": "Transcription",
        "type_ref": "default-lt",
        "participant": "Speaker",
        "annotator": "whisper",
        "annotations": annotations,
    })


def _build_phoneme_tier(profile, tiers, add_ts, ann_ctr):
    """Tier 2: Phone-level segmentation."""
    # Try forced alignment first, fall back to phoneme_analysis
    fa = profile.get("forced_alignment", {})
    phones = fa.get("phones", [])

    # Fall back to Wav2Vec phoneme spans
    if not phones:
        pa = profile.get("phoneme_analysis", {})
        phones = pa.get("phoneme_details", [])

    annotations = []
    for p in phones:
        phone = p.get("phone") or p.get("phoneme", "")
        start = p.get("start_ms", 0)
        end = p.get("end_ms", start + p.get("duration_ms", 50))
        if not phone:
            continue
        ts1 = add_ts(start)
        ts2 = add_ts(end)
        conf = p.get("confidence", 0)
        source = p.get("source", "wav2vec")
        label = f"{phone} [{source}:{conf:.2f}]" if conf else phone
        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": label,
        })

    tiers.append({
        "tier_id": "Phonemes",
        "type_ref": "phoneme-lt",
        "annotator": "forced_alignment",
        "annotations": annotations,
    })


def _build_prosody_tier(profile, tiers, add_ts, ann_ctr):
    """Tier 3: Prosodic annotations (intonation, stress, rhythm)."""
    prosody = profile.get("prosodic_profile", {})
    annotations = []

    # Intonation pattern annotations
    intonation = prosody.get("intonation", {})
    if intonation:
        pattern = intonation.get("pattern", "unknown")
        boundary_tones = intonation.get("boundary_tones", [])
        duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000

        ts1 = add_ts(0)
        ts2 = add_ts(duration_ms)
        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": f"Intonation: {pattern} | Tones: {', '.join(boundary_tones) if boundary_tones else 'N/A'}",
        })

    # Rhythm classification
    rhythm = prosody.get("rhythm", {})
    if rhythm:
        rhythm_class = rhythm.get("rhythm_class", "unknown")
        npvi = rhythm.get("nPVI_V", 0)
        duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000
        ts1 = add_ts(0)
        ts2 = add_ts(duration_ms)
        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": f"Rhythm: {rhythm_class} (nPVI={npvi:.1f})",
        })

    # Stressed words
    stress = prosody.get("stress_patterns", [])
    for s in stress:
        if s.get("stressed"):
            start = s.get("start", 0)
            end = s.get("end", start + 200)
            # Convert seconds to ms if needed
            if start < 100:
                start *= 1000
                end *= 1000
            ts1 = add_ts(start)
            ts2 = add_ts(end)
            annotations.append({
                "id": _ann_id(ann_ctr),
                "ts1": ts1, "ts2": ts2,
                "value": f"STRESS: {s.get('word', '')}",
            })

    tiers.append({
        "tier_id": "Prosody",
        "type_ref": "prosody-lt",
        "annotator": "prosodic_profiling",
        "annotations": annotations,
    })


def _build_l1_tier(profile, tiers, add_ts, ann_ctr, l1_language):
    """Tier 4: L1 interference patterns."""
    l1 = profile.get("l1_interference", {})
    annotations = []

    duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000

    # Overall interference score
    interference_score = l1.get("interference_score", 0)
    display_name = l1.get("l1_display_name", l1_language)
    ts1 = add_ts(0)
    ts2 = add_ts(duration_ms)
    annotations.append({
        "id": _ann_id(ann_ctr),
        "ts1": ts1, "ts2": ts2,
        "value": f"L1={display_name} | Interference={interference_score}/100",
    })

    # Individual detected patterns
    patterns = l1.get("detected_patterns", [])
    for pat in patterns:
        name = pat.get("pattern") or pat.get("name", "unknown")
        severity = pat.get("severity", "low")
        evidence = pat.get("evidence", "")
        remediation = pat.get("remediation", "")

        ts1 = add_ts(0)
        ts2 = add_ts(duration_ms)
        value = f"[{severity.upper()}] {name}"
        if evidence:
            value += f" | {evidence}"
        if remediation:
            value += f" | FIX: {remediation}"

        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": value,
        })

    tiers.append({
        "tier_id": "L1_Interference",
        "type_ref": "interference-lt",
        "annotator": "l1_targets",
        "annotations": annotations,
    })


def _build_voice_quality_tier(profile, tiers, add_ts, ann_ctr):
    """Tier 5: Voice quality annotations."""
    vq = profile.get("voice_quality", {})
    vs = profile.get("voicesauce", {})
    annotations = []

    duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000

    # Phonation type
    phonation = vs.get("phonation_type") or vq.get("breathiness", {}).get("classification", "modal")
    register = vq.get("register", {}).get("type", "unknown")

    ts1 = add_ts(0)
    ts2 = add_ts(duration_ms)
    annotations.append({
        "id": _ann_id(ann_ctr),
        "ts1": ts1, "ts2": ts2,
        "value": f"Phonation: {phonation} | Register: {register}",
    })

    # VoiceSauce measures
    if vs:
        h1h2 = vs.get("H1_H2", {})
        cpp = vs.get("CPP", {})
        shr = vs.get("SHR", {})
        ts1 = add_ts(0)
        ts2 = add_ts(duration_ms)
        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": (
                f"H1-H2={h1h2.get('mean', 0):.1f}dB | "
                f"CPP={cpp.get('mean', 0):.1f}dB | "
                f"SHR={shr.get('mean', 0):.3f} | "
                f"Breathiness={vs.get('breathiness_index', 0):.2f} | "
                f"Creak={vs.get('creak_index', 0):.2f}"
            ),
        })

    # Nasality
    nas = vq.get("nasality", {})
    if nas:
        ts1 = add_ts(0)
        ts2 = add_ts(duration_ms)
        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": f"Nasality index: {nas.get('nasality_index', 0):.2f}",
        })

    tiers.append({
        "tier_id": "Voice_Quality",
        "type_ref": "voice-quality-lt",
        "annotator": "voice_quality+voicesauce",
        "annotations": annotations,
    })


def _build_cognitive_load_tier(profile, tiers, add_ts, ann_ctr):
    """Tier 6: Cognitive load markers (filled pauses, hesitations)."""
    mb = profile.get("morpheme_boundary", {})
    cog = mb.get("cognitive_load", {})
    annotations = []

    # Filled pauses
    indicators = cog.get("indicators", [])
    for ind in indicators:
        if isinstance(ind, str):
            # Simple string indicator β€” create utterance-level annotation
            duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000
            ts1 = add_ts(0)
            ts2 = add_ts(duration_ms)
            annotations.append({
                "id": _ann_id(ann_ctr),
                "ts1": ts1, "ts2": ts2,
                "value": f"COGNITIVE: {ind}",
            })
        elif isinstance(ind, dict):
            start = ind.get("start_ms", 0)
            end = ind.get("end_ms", start + 200)
            ts1 = add_ts(start)
            ts2 = add_ts(end)
            annotations.append({
                "id": _ann_id(ann_ctr),
                "ts1": ts1, "ts2": ts2,
                "value": f"COGNITIVE: {ind.get('type', 'marker')} β€” {ind.get('description', '')}",
            })

    # Overall score
    score = cog.get("score", 0)
    if score > 0:
        duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000
        ts1 = add_ts(0)
        ts2 = add_ts(duration_ms)
        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": f"Cognitive Load Score: {score}/100",
        })

    tiers.append({
        "tier_id": "Cognitive_Load",
        "type_ref": "cognitive-lt",
        "annotator": "morpheme_boundary",
        "annotations": annotations,
    })


def _build_connected_speech_tier(profile, tiers, add_ts, ann_ctr):
    """Tier 7: Connected speech processes (assimilation, elision, linking)."""
    cs = profile.get("connected_speech", {})
    annotations = []

    for process_type in ["assimilations", "elisions", "linkings", "reductions"]:
        events = cs.get(process_type, [])
        for ev in events:
            if isinstance(ev, dict):
                start = ev.get("start_ms", ev.get("position_ms", 0))
                end = ev.get("end_ms", start + 150)
                label = ev.get("label") or ev.get("type", process_type.rstrip("s"))
                context = ev.get("context", "")

                ts1 = add_ts(start)
                ts2 = add_ts(end)
                value = f"[{process_type.upper().rstrip('S')}] {label}"
                if context:
                    value += f" | {context}"
                annotations.append({
                    "id": _ann_id(ann_ctr),
                    "ts1": ts1, "ts2": ts2,
                    "value": value,
                })

    # Fluency score
    fluency = cs.get("fluency_score", 0)
    if fluency > 0:
        duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000
        ts1 = add_ts(0)
        ts2 = add_ts(duration_ms)
        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": f"Fluency Score: {fluency}/100",
        })

    tiers.append({
        "tier_id": "Connected_Speech",
        "type_ref": "connected-speech-lt",
        "annotator": "connected_speech",
        "annotations": annotations,
    })


def _build_cif_tier(profile, tiers, add_ts, ann_ctr):
    """Tier 8: CIF (Contrastive Interference Field) scores."""
    cif = profile.get("cif_analysis", {})
    annotations = []

    duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000

    overall_cii = cif.get("overall_cii", 0)
    severity = cif.get("overall_severity", "unknown")

    ts1 = add_ts(0)
    ts2 = add_ts(duration_ms)
    annotations.append({
        "id": _ann_id(ann_ctr),
        "ts1": ts1, "ts2": ts2,
        "value": f"CII={overall_cii:.3f} [{severity}]",
    })

    # Per-dimension CII
    dimensions = cif.get("dimensions", {})
    for dim_name, dim_data in dimensions.items():
        if isinstance(dim_data, dict):
            dim_cii = dim_data.get("cii", 0)
            ts1 = add_ts(0)
            ts2 = add_ts(duration_ms)
            annotations.append({
                "id": _ann_id(ann_ctr),
                "ts1": ts1, "ts2": ts2,
                "value": f"CIF-{dim_name}: {dim_cii:.3f}",
            })

    tiers.append({
        "tier_id": "CIF_Score",
        "type_ref": "cif-lt",
        "annotator": "cif_model",
        "annotations": annotations,
    })


def _build_emotion_tier(profile, tiers, add_ts, ann_ctr):
    """Tier 9: Emotional analysis annotations."""
    annotations = []
    duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000

    # SpeechBrain emotion
    ai = profile.get("ai_classification", {})
    sb = ai.get("speechbrain", {})
    if sb:
        emotion = sb.get("emotion", {})
        if emotion:
            label = emotion.get("label", "unknown")
            conf = emotion.get("confidence", 0)
            ts1 = add_ts(0)
            ts2 = add_ts(duration_ms)
            annotations.append({
                "id": _ann_id(ann_ctr),
                "ts1": ts1, "ts2": ts2,
                "value": f"SpeechBrain: {label} ({conf:.2f})",
            })

    # auDeep emotion
    audeep = profile.get("audeep", {})
    if audeep:
        primary = audeep.get("primary_emotion", "neutral")
        conf = audeep.get("emotion_confidence", 0)
        v = audeep.get("valence", 0)
        a = audeep.get("arousal", 0)
        d = audeep.get("dominance", 0)
        ts1 = add_ts(0)
        ts2 = add_ts(duration_ms)
        annotations.append({
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": f"auDeep: {primary} ({conf:.2f}) | V={v:.2f} A={a:.2f} D={d:.2f}",
        })

    tiers.append({
        "tier_id": "Emotion",
        "type_ref": "emotion-lt",
        "annotator": "speechbrain+audeep",
        "annotations": annotations,
    })


def _build_metadata_tier(profile, tiers, add_ts, ann_ctr,
                         speaker_id, student_name, language, l1_language, session_id):
    """Tier 10: Session metadata."""
    duration_ms = profile.get("transcription", {}).get("duration_seconds", 5) * 1000
    processing_ms = profile.get("processing_time_ms", 0)

    ts1 = add_ts(0)
    ts2 = add_ts(duration_ms)

    meta_str = (
        f"Speaker: {speaker_id} ({student_name}) | "
        f"L1: {l1_language} β†’ L2: {language} | "
        f"Session: {session_id} | "
        f"Processing: {processing_ms:.0f}ms"
    )

    tiers.append({
        "tier_id": "Speaker_Metadata",
        "type_ref": "metadata-lt",
        "participant": speaker_id,
        "annotations": [{
            "id": _ann_id(ann_ctr),
            "ts1": ts1, "ts2": ts2,
            "value": meta_str,
        }],
    })


# ── Public API ───────────────────────────────────────────────────────────

def export_eaf(
    profile: dict[str, Any],
    audio_path: Path | str,
    output_path: Path | str | None = None,
    speaker_id: str = "anonymous",
    student_name: str = "Student",
    language: str = "en",
    l1_language: str = "bho",
    session_id: str | None = None,
) -> tuple[str, Path]:
    """Export analysis results to ELAN .eaf format.

    Args:
        profile: Full pipeline output dict.
        audio_path: Path to source audio.
        output_path: Where to save the .eaf file. If None, saves next to audio.
        speaker_id: Speaker identifier.
        student_name: Display name.
        language: Target language.
        l1_language: L1 language code.
        session_id: Session identifier.

    Returns:
        Tuple of (eaf_xml_string, output_path).
    """
    audio_path = Path(audio_path)
    if output_path is None:
        output_path = audio_path.with_suffix(".eaf")
    else:
        output_path = Path(output_path)

    eaf_xml = generate_eaf(
        profile=profile,
        audio_path=audio_path,
        speaker_id=speaker_id,
        student_name=student_name,
        language=language,
        l1_language=l1_language,
        session_id=session_id,
    )

    output_path.write_text(eaf_xml, encoding="utf-8")
    logger.info("ELAN export saved to %s", output_path)

    return eaf_xml, output_path