File size: 1,821 Bytes
f4931e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import tempfile
import unittest
from pathlib import Path

from buddi_asr.data.manifest import read_manifest, write_manifest
from buddi_asr.data.split import SPLIT_ORDER, split_manifest
from tests.helpers import record


class SplitTests(unittest.TestCase):
    def test_speaker_disjoint_and_deterministic(self) -> None:
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            input_path = root / "source/manifest.jsonl"
            records = []
            index = 0
            for speaker_index in range(12):
                for text in ("مرحبا", "أبغى قصة"):
                    records.append(
                        record(index, speaker=f"speaker_{speaker_index}", text=text)
                    )
                    index += 1
            write_manifest(records, input_path)
            first = split_manifest(input_path, root / "split-a", seed=17)
            second = split_manifest(input_path, root / "split-b", seed=17)
            self.assertEqual(first["speaker_overlap"], 0)
            speakers = {}
            for name in SPLIT_ORDER:
                first_records = list(read_manifest(first["paths"][name]))
                second_records = list(read_manifest(second["paths"][name]))
                self.assertEqual(
                    [item.speaker_id for item in first_records],
                    [item.speaker_id for item in second_records],
                )
                speakers[name] = {item.speaker_id for item in first_records}
            self.assertFalse(speakers["train"] & speakers["validation"])
            self.assertFalse(speakers["train"] & speakers["test"])
            self.assertFalse(speakers["validation"] & speakers["test"])


if __name__ == "__main__":
    unittest.main()