File size: 2,656 Bytes
901e06a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

from typing import Any, Dict, List, Union
from argparse import Namespace, ArgumentParser

SUPPORTED_MEDIUM = ["text", "speech"]
SUPPORTED_SOURCE_MEDIUM = ["youtube", "text", "speech"]
SUPPORTED_TARGET_MEDIUM = ["text", "speech"]
DATALOADER_DICT = {}


def register_dataloader(name):
    def register(cls):
        DATALOADER_DICT[name] = cls
        return cls

    return register


def register_dataloader_class(name, cls):
    DATALOADER_DICT[name] = cls


class GenericDataloader:
    """
    Load source and target data

    .. argparse::
        :ref: simuleval.options.add_data_args
        :passparser:
        :prog:

    """

    def __init__(
        self, source_list: List[str], target_list: Union[List[str], List[None]]
    ) -> None:
        self.source_list = source_list
        self.target_list = target_list
        assert len(self.source_list) == len(self.target_list)

    def __len__(self):
        return len(self.source_list)

    def get_source(self, index: int) -> Any:
        return self.preprocess_source(self.source_list[index])

    def get_target(self, index: int) -> Any:
        return self.preprocess_target(self.target_list[index])

    def __getitem__(self, index: int) -> Dict[str, Any]:
        return {"source": self.get_source(index), "target": self.get_target(index)}

    def preprocess_source(self, source: Any) -> Any:
        raise NotImplementedError

    def preprocess_target(self, target: Any) -> Any:
        raise NotImplementedError

    @classmethod
    def from_args(cls, args: Namespace):
        return cls(args.source, args.target)

    @staticmethod
    def add_args(parser: ArgumentParser):
        parser.add_argument(
            "--source",
            type=str,
            help="Source file.",
        )
        parser.add_argument(
            "--target",
            type=str,
            help="Target file.",
        )
        parser.add_argument(
            "--source-type",
            type=str,
            choices=SUPPORTED_SOURCE_MEDIUM,
            help="Source Data type to evaluate.",
        )
        parser.add_argument(
            "--target-type",
            type=str,
            choices=SUPPORTED_TARGET_MEDIUM,
            help="Data type to evaluate.",
        )
        parser.add_argument(
            "--source-segment-size",
            type=int,
            default=1,
            help="Source segment size, For text the unit is # token, for speech is ms",
        )