File: test_iwslt2016.py

package info (click to toggle)
pytorch-text 0.14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 11,560 kB
  • sloc: python: 14,197; cpp: 2,404; sh: 214; makefile: 20
file content (207 lines) | stat: -rw-r--r-- 7,160 bytes parent folder | download
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
import itertools
import os
import random
import shutil
import string
import tarfile
import tempfile
from collections import defaultdict
from unittest.mock import patch

from parameterized import parameterized
from torchtext.data.datasets_utils import _generate_iwslt_files_for_lang_and_split
from torchtext.datasets.iwslt2016 import (
    DATASET_NAME,
    IWSLT2016,
    SUPPORTED_DATASETS,
    SET_NOT_EXISTS,
)

from ..common.case_utils import zip_equal
from ..common.torchtext_test_case import TorchtextTestCase

SUPPORTED_LANGPAIRS = [(k, e) for k, v in SUPPORTED_DATASETS["language_pair"].items() for e in v]
SUPPORTED_DEVTEST_SPLITS = SUPPORTED_DATASETS["valid_test"]
DEV_TEST_SPLITS = [(dev, test) for dev, test in itertools.product(SUPPORTED_DEVTEST_SPLITS, repeat=2) if dev != test]


def _generate_uncleaned_train():
    """Generate tags files"""
    file_contents = []
    examples = []
    xml_tags = [
        "<url",
        "<keywords",
        "<talkid",
        "<description",
        "<reviewer",
        "<translator",
        "<title",
        "<speaker",
        "<doc",
    ]
    for i in range(100):
        rand_string = " ".join(random.choice(string.ascii_letters) for i in range(10))
        # With a 10% change, add one of the XML tags which is cleaned
        # to ensure cleaning happens appropriately
        if random.random() < 0.1:
            open_tag = random.choice(xml_tags) + ">"
            # Open tag already contains the closing >
            close_tag = "</" + open_tag[1:]
            file_contents.append(open_tag + rand_string + close_tag)
        else:
            examples.append(rand_string + "\n")
            file_contents.append(rand_string)
    return examples, "\n".join(file_contents)


def _generate_uncleaned_valid():
    file_contents = ["<root>"]
    examples = []

    for doc_id in range(5):
        file_contents.append(f'<doc docid="{doc_id}" genre="lectures">')
        for seg_id in range(100):
            rand_string = " ".join(random.choice(string.ascii_letters) for i in range(10))
            examples.append(rand_string)
            file_contents.append(f"<seg>{rand_string} </seg>" + "\n")
        file_contents.append("</doc>")
    file_contents.append("</root>")
    return examples, " ".join(file_contents)


def _generate_uncleaned_test():
    return _generate_uncleaned_valid()


def _generate_uncleaned_contents(split):
    random.seed(1)
    return {
        "train": _generate_uncleaned_train(),
        "valid": _generate_uncleaned_valid(),
        "test": _generate_uncleaned_test(),
    }[split]


def _get_mock_dataset(root_dir, split, src, tgt, valid_set, test_set):
    """
    root_dir: directory to the mocked dataset
    """

    base_dir = os.path.join(root_dir, DATASET_NAME)
    temp_dataset_dir = os.path.join(base_dir, "temp_dataset_dir")
    outer_temp_dataset_dir = os.path.join(temp_dataset_dir, f"texts/{src}/{tgt}/")
    inner_temp_dataset_dir = os.path.join(outer_temp_dataset_dir, f"{src}-{tgt}")

    os.makedirs(outer_temp_dataset_dir, exist_ok=True)
    os.makedirs(inner_temp_dataset_dir, exist_ok=True)

    mocked_data = defaultdict(lambda: defaultdict(list))

    cleaned_file_names, uncleaned_file_names = _generate_iwslt_files_for_lang_and_split(
        16, src, tgt, valid_set, test_set
    )
    uncleaned_src_file = uncleaned_file_names[src][split]
    uncleaned_tgt_file = uncleaned_file_names[tgt][split]

    cleaned_src_file = cleaned_file_names[src][split]
    cleaned_tgt_file = cleaned_file_names[tgt][split]

    for (unclean_file_name, clean_file_name) in [
        (uncleaned_src_file, cleaned_src_file),
        (uncleaned_tgt_file, cleaned_tgt_file),
    ]:
        # Get file extension (i.e., the language) without the . prefix (.en -> en)
        lang = os.path.splitext(unclean_file_name)[1][1:]

        out_file = os.path.join(inner_temp_dataset_dir, unclean_file_name)
        with open(out_file, "w") as f:
            mocked_data_for_split, file_contents = _generate_uncleaned_contents(split)
            mocked_data[split][lang] = mocked_data_for_split
            f.write(file_contents)

    inner_compressed_dataset_path = os.path.join(outer_temp_dataset_dir, f"{src}-{tgt}.tgz")

    # create tar file from dataset folder
    with tarfile.open(inner_compressed_dataset_path, "w:gz") as tar:
        tar.add(inner_temp_dataset_dir, arcname=f"{src}-{tgt}")

    # this is necessary so that the outer tarball only includes the inner tarball
    shutil.rmtree(inner_temp_dataset_dir)

    outer_temp_dataset_path = os.path.join(base_dir, "2016-01.tgz")

    with tarfile.open(outer_temp_dataset_path, "w:gz") as tar:
        tar.add(temp_dataset_dir, arcname="2016-01")

    return list(zip(mocked_data[split][src], mocked_data[split][tgt]))


class TestIWSLT2016(TorchtextTestCase):
    root_dir = None
    patcher = None

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.patcher = patch("torchdata.datapipes.iter.util.cacheholder._hash_check", return_value=True)
        cls.patcher.start()

    @classmethod
    def tearDownClass(cls):
        cls.patcher.stop()
        super().tearDownClass()

    @parameterized.expand(
        [
            (split, src, tgt, dev_set, test_set)
            for split in ("train", "valid", "test")
            for dev_set, test_set in DEV_TEST_SPLITS
            for src, tgt in SUPPORTED_LANGPAIRS
            if (dev_set not in SET_NOT_EXISTS[(src, tgt)] and test_set not in SET_NOT_EXISTS[(src, tgt)])
        ]
    )
    def test_iwslt2016(self, split, src, tgt, dev_set, test_set):

        with tempfile.TemporaryDirectory() as root_dir:
            expected_samples = _get_mock_dataset(os.path.join(root_dir, "datasets"), split, src, tgt, dev_set, test_set)

            dataset = IWSLT2016(
                root=root_dir,
                split=split,
                language_pair=(src, tgt),
                valid_set=dev_set,
                test_set=test_set,
            )

            samples = list(dataset)

            for sample, expected_sample in zip_equal(samples, expected_samples):
                self.assertEqual(sample, expected_sample)

    @parameterized.expand(["train", "valid", "test"])
    def test_iwslt2016_split_argument(self, split):
        with tempfile.TemporaryDirectory() as root_dir:
            language_pair = ("de", "en")
            valid_set = "tst2013"
            test_set = "tst2014"
            _ = _get_mock_dataset(
                os.path.join(root_dir, "datasets"), split, language_pair[0], language_pair[1], valid_set, test_set
            )
            dataset1 = IWSLT2016(
                root=root_dir,
                split=split,
                language_pair=language_pair,
                valid_set=valid_set,
                test_set=test_set,
            )
            (dataset2,) = IWSLT2016(
                root=root_dir,
                split=(split,),
                language_pair=language_pair,
                valid_set=valid_set,
                test_set=test_set,
            )

            for d1, d2 in zip_equal(dataset1, dataset2):
                self.assertEqual(d1, d2)