Datasets:

ArXiv:
License:
holylovenia commited on
Commit
10c4263
·
verified ·
1 Parent(s): 7e412ca

Upload phomt.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. phomt.py +139 -0
phomt.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+ from typing import Dict, List, Tuple
4
+
5
+ import datasets
6
+
7
+ from seacrowd.utils import schemas
8
+ from seacrowd.utils.configs import SEACrowdConfig
9
+ from seacrowd.utils.constants import Licenses, Tasks
10
+
11
+ _CITATION = """\
12
+ @inproceedings{PhoMT,
13
+ title = {{PhoMT: A High-Quality and Large-Scale Benchmark Dataset for Vietnamese-English Machine Translation}},
14
+ author = {Long Doan and Linh The Nguyen and Nguyen Luong Tran and Thai Hoang and Dat Quoc Nguyen},
15
+ booktitle = {Proceedings of the 2021 Conference on Empirical Methods in Natural Language Processing},
16
+ year = {2021},
17
+ pages = {4495--4503}
18
+ }
19
+ """
20
+
21
+ _DATASETNAME = "phomt"
22
+
23
+
24
+ _DESCRIPTION = """\
25
+ PhoMT is a high-quality and large-scale Vietnamese-English parallel dataset of 3.02M sentence pairs, which is 2.9M
26
+ pairs larger than the benchmark Vietnamese-English machine translation corpus IWSLT15. This is the first large-scale
27
+ Vietnamese-English machine translation study.
28
+ """
29
+
30
+ _LANGUAGES = ["vie", "eng"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
31
+ _LOCAL = True
32
+
33
+ _HOMEPAGE = "https://github.com/VinAIResearch/PhoMT"
34
+
35
+ _LICENSE = Licenses.MIT.value
36
+
37
+ _SUPPORTED_TASKS = [Tasks.MACHINE_TRANSLATION]
38
+
39
+ _SOURCE_VERSION = "1.0.0"
40
+
41
+ _SEACROWD_VERSION = "2024.06.20"
42
+
43
+ MAP_LANG = {"eng": "en", "vie": "vi"}
44
+
45
+
46
+ def seacrowd_config_constructor(src_lang, tgt_lang, schema, version):
47
+ if src_lang == "" or tgt_lang == "":
48
+ raise ValueError(f"Invalid src_lang {src_lang} or tgt_lang {tgt_lang}")
49
+
50
+ if schema not in ["source", "seacrowd_t2t"]:
51
+ raise ValueError(f"Invalid schema: {schema}")
52
+
53
+ return SEACrowdConfig(
54
+ name="phomt_{src}_{tgt}_{schema}".format(src=src_lang, tgt=tgt_lang, schema=schema),
55
+ version=datasets.Version(version),
56
+ description="phomt schema for {schema} from {src} to {tgt}".format(schema=schema, src=src_lang, tgt=tgt_lang),
57
+ schema=schema,
58
+ subset_id="phomt_{src}_{tgt}".format(src=src_lang, tgt=tgt_lang),
59
+ )
60
+
61
+
62
+ class PhoMT(datasets.GeneratorBasedBuilder):
63
+ """
64
+ PhoMT is a high-quality and large-scale Vietnamese-English parallel dataset of 3.02M sentence pairs, which is
65
+ 2.9M pairs larger than the benchmark Vietnamese-English machine translation corpus IWSLT15.
66
+ """
67
+
68
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
69
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
70
+
71
+ BUILDER_CONFIGS = [
72
+ seacrowd_config_constructor("eng", "vie", "source", _SOURCE_VERSION),
73
+ seacrowd_config_constructor("eng", "vie", "seacrowd_t2t", _SEACROWD_VERSION),
74
+ ]
75
+
76
+ DEFAULT_CONFIG_NAME = "phomt_eng_vie_source"
77
+
78
+ def _info(self) -> datasets.DatasetInfo:
79
+ if self.config.schema in ("source", "seacrowd_t2t"):
80
+ features = schemas.text2text_features
81
+ else:
82
+ raise ValueError(f"Invalid config schema: {self.config.schema}")
83
+
84
+ return datasets.DatasetInfo(
85
+ description=_DESCRIPTION,
86
+ features=features,
87
+ homepage=_HOMEPAGE,
88
+ license=_LICENSE,
89
+ citation=_CITATION,
90
+ )
91
+
92
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
93
+ """Returns SplitGenerators."""
94
+ if self.config.data_dir is None:
95
+ raise ValueError("This is a local dataset. Please pass the data_dir kwarg to load_dataset.")
96
+ else:
97
+ data_dir = self.config.data_dir
98
+
99
+ return [
100
+ datasets.SplitGenerator(
101
+ name=datasets.Split.TRAIN,
102
+ gen_kwargs={"filepath": os.path.join(data_dir, "detokenization", "train", "train.{lang}")},
103
+ ),
104
+ datasets.SplitGenerator(
105
+ name=datasets.Split.VALIDATION,
106
+ gen_kwargs={"filepath": os.path.join(data_dir, "detokenization", "dev", "dev.{lang}")},
107
+ ),
108
+ datasets.SplitGenerator(
109
+ name=datasets.Split.TEST,
110
+ gen_kwargs={"filepath": os.path.join(data_dir, "detokenization", "test", "test.{lang}")},
111
+ ),
112
+ ]
113
+
114
+ def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
115
+ config_names_split = self.config.name.split("_")
116
+ src_lang = config_names_split[1]
117
+ tgt_lang = config_names_split[2]
118
+
119
+ src_path = filepath.format(lang=MAP_LANG[src_lang])
120
+ tgt_path = filepath.format(lang=MAP_LANG[tgt_lang])
121
+
122
+ with open(src_path, "r", encoding="utf8") as f:
123
+ src_lines = f.readlines()
124
+ with open(tgt_path, "r", encoding="utf8") as f:
125
+ tgt_lines = f.readlines()
126
+
127
+ if self.config.schema in ("source", "seacrowd_t2t"):
128
+ for idx, (src_line, tgt_line) in enumerate(zip(src_lines, tgt_lines)):
129
+ ex = {
130
+ "id": str(idx),
131
+ "text_1": src_line.strip(),
132
+ "text_2": tgt_line.strip(),
133
+ "text_1_name": src_lang,
134
+ "text_2_name": tgt_lang,
135
+ }
136
+ yield idx, ex
137
+
138
+ else:
139
+ raise NotImplementedError(f"Schema '{self.config.schema}' is not defined.")