Commit
·
1ad7203
1
Parent(s):
acb598f
TEDHRLR done. Not working
Browse files- rosetta_balcanica.py +63 -75
rosetta_balcanica.py
CHANGED
@@ -17,13 +17,14 @@
|
|
17 |
#!/usr/bin/env python
|
18 |
|
19 |
import datasets, logging
|
|
|
20 |
|
21 |
logging.basicConfig(format='[%(name)s] %(levelname)s -> %(message)s')
|
22 |
logger = logging.getLogger(__name__)
|
23 |
logger.setLevel(logging.DEBUG)
|
24 |
|
25 |
-
_SUPPORTED_WB_LANGS = ['ma', 'sh']
|
26 |
-
_VALID_LANGUAGE_PAIRS = [(
|
27 |
|
28 |
# TODO: Add BibTeX citation
|
29 |
# Find for instance the citation on arxiv or on the dataset repo/website
|
@@ -38,104 +39,91 @@ _CITATION="""\
|
|
38 |
_DESCRIPTION="""
|
39 |
Rosetta-Balcanica is a set of evaluation datasets for low resource western Balkan languages manually sourced from articles from OSCE website.
|
40 |
"""
|
|
|
41 |
_HOMEPAGE='https://github.com/ebegoli/rosetta-balcanica'
|
42 |
-
|
43 |
_VERSION=datasets.Version('1.0.0')
|
44 |
|
45 |
class RosettaBalcanicaConfig(datasets.BuilderConfig):
|
46 |
-
"""BuilderConfig for Rosetta Balcanica
|
47 |
"""
|
48 |
|
49 |
-
def __init__(self,
|
50 |
-
|
|
|
|
|
51 |
super(RosettaBalcanicaConfig, self).__init__(
|
52 |
-
name=
|
53 |
-
description=
|
54 |
version=_VERSION,
|
55 |
**kwargs
|
56 |
)
|
57 |
|
58 |
-
|
59 |
-
assert wb_lang in supported_wb, (f"Supported West Balkan languages are {supported_wb}, got {wb_lang}")
|
60 |
-
self.wb_lang = wb_lang
|
61 |
|
62 |
class RoesettaBalcancia(datasets.GeneratorBasedBuilder):
|
63 |
logger.debug("i'm in builder")
|
64 |
BUILDER_CONFIGS = [
|
65 |
RosettaBalcanicaConfig(
|
66 |
-
|
|
|
67 |
)
|
68 |
-
for
|
69 |
]
|
70 |
|
71 |
def _info(self):
|
72 |
-
source,target = 'en', self.config.wb_lang
|
73 |
-
features = datasets.Features(
|
74 |
-
{
|
75 |
-
'id': datasets.Value('string'),
|
76 |
-
'translation': datasets.features.Translation(languages=(source, target))
|
77 |
-
}
|
78 |
-
)
|
79 |
-
|
80 |
return datasets.DatasetInfo(
|
81 |
description=_DESCRIPTION,
|
82 |
-
features=
|
83 |
-
|
|
|
84 |
homepage=_HOMEPAGE,
|
|
|
85 |
citation=_CITATION,
|
86 |
)
|
87 |
|
88 |
def _split_generators(self, dl_manager):
|
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 |
-
'id': str(sent_counter),
|
134 |
-
'translation': {
|
135 |
-
'en': en_sent,
|
136 |
-
f'{wb_lang}': wb_sent
|
137 |
-
}
|
138 |
-
}
|
139 |
-
)
|
140 |
-
yield result
|
141 |
|
|
|
17 |
#!/usr/bin/env python
|
18 |
|
19 |
import datasets, logging
|
20 |
+
from itertools import permutations
|
21 |
|
22 |
logging.basicConfig(format='[%(name)s] %(levelname)s -> %(message)s')
|
23 |
logger = logging.getLogger(__name__)
|
24 |
logger.setLevel(logging.DEBUG)
|
25 |
|
26 |
+
_SUPPORTED_WB_LANGS = ['en', 'ma', 'sh']
|
27 |
+
_VALID_LANGUAGE_PAIRS = [lang_pair for lang_pair in permutations(_SUPPORTED_WB_LANGS, 2) if lang_pair[0] == 'en' or lang_pair[1] == 'en']
|
28 |
|
29 |
# TODO: Add BibTeX citation
|
30 |
# Find for instance the citation on arxiv or on the dataset repo/website
|
|
|
39 |
_DESCRIPTION="""
|
40 |
Rosetta-Balcanica is a set of evaluation datasets for low resource western Balkan languages manually sourced from articles from OSCE website.
|
41 |
"""
|
42 |
+
|
43 |
_HOMEPAGE='https://github.com/ebegoli/rosetta-balcanica'
|
44 |
+
_DATA_URL='https://github.com/ebegoli/rosetta-balcanica/raw/main/rosetta_balcanica.tar.gz'
|
45 |
_VERSION=datasets.Version('1.0.0')
|
46 |
|
47 |
class RosettaBalcanicaConfig(datasets.BuilderConfig):
|
48 |
+
"""BuilderConfig for Rosetta Balcanica for low resource West Balcan languages
|
49 |
"""
|
50 |
|
51 |
+
def __init__(self, lang_pair=(None, None), **kwargs):
|
52 |
+
assert lang_pair in _VALID_LANGUAGE_PAIRS, (f"Language pair {lang_pair} not supported (yet)")
|
53 |
+
name = f'{lang_pair[0]} to {lang_pair[1]}'
|
54 |
+
desc = f'Translation dataset from {lang_pair[0]} to {lang_pair[1]}'
|
55 |
super(RosettaBalcanicaConfig, self).__init__(
|
56 |
+
name=name,
|
57 |
+
description=desc,
|
58 |
version=_VERSION,
|
59 |
**kwargs
|
60 |
)
|
61 |
|
62 |
+
self.lang_pair = lang_pair
|
|
|
|
|
63 |
|
64 |
class RoesettaBalcancia(datasets.GeneratorBasedBuilder):
|
65 |
logger.debug("i'm in builder")
|
66 |
BUILDER_CONFIGS = [
|
67 |
RosettaBalcanicaConfig(
|
68 |
+
lang_pair=lang_pair,
|
69 |
+
versino=_VERSION,
|
70 |
)
|
71 |
+
for lang_pair in _VALID_LANGUAGE_PAIRS
|
72 |
]
|
73 |
|
74 |
def _info(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
return datasets.DatasetInfo(
|
76 |
description=_DESCRIPTION,
|
77 |
+
features=datasets.Features(
|
78 |
+
{'translation': datasets.features.Translation(languages=self.config.lang_pair)}
|
79 |
+
),
|
80 |
homepage=_HOMEPAGE,
|
81 |
+
supervised_keys=self.config.lang_pair,
|
82 |
citation=_CITATION,
|
83 |
)
|
84 |
|
85 |
def _split_generators(self, dl_manager):
|
86 |
+
archive = dl_manager.download(_DATA_URL)
|
87 |
+
source,target = self.config.lang_pair
|
88 |
+
non_en = source if target == 'en' else target
|
89 |
+
data_dir = f'en-{non_en}'
|
90 |
+
|
91 |
+
return [
|
92 |
+
datasets.SplitGenerator(
|
93 |
+
name=datasets.Split.TRAIN,
|
94 |
+
gen_kwargs={
|
95 |
+
'source_file': f'{data_dir}/train_{source}.txt',
|
96 |
+
'target_file': f'{data_dir}/train_{target}.txt',
|
97 |
+
'files': dl_manager.iter_archive(archive)
|
98 |
+
}
|
99 |
+
),
|
100 |
+
datasets.SplitGenerator(
|
101 |
+
name=datasets.Split.TEST,
|
102 |
+
gen_kwargs={
|
103 |
+
'source_file': f'{data_dir}/test_{source}.txt',
|
104 |
+
'target_file': f'{data_dir}/test_{target}.txt',
|
105 |
+
'files': dl_manager.iter_archive(archive)
|
106 |
+
}
|
107 |
+
),
|
108 |
+
]
|
109 |
+
|
110 |
+
def _generate_examples(self, source_file, target_file, files):
|
111 |
+
source_sents, target_sents = None, None
|
112 |
+
for path, f in files:
|
113 |
+
if path == source_file:
|
114 |
+
source_sents = f.read().decode('utf-8').split('\n')
|
115 |
+
elif path == target_file:
|
116 |
+
target_sents = f.read().decode('utf-8').split('\n')
|
117 |
+
if source_sents is not None and target_sents is not None:
|
118 |
+
break
|
119 |
+
|
120 |
+
assert len(target_sents) == len(source_sents), (f"Sizes do not match: {len(source_sents) vs len(target_sents)} for {source_file} vs {target_file}")
|
121 |
+
|
122 |
+
source,target = self.config.lang_pair
|
123 |
+
for idx, (l1, l2) in enumerate(zip(source_sents, target_sents)):
|
124 |
+
result = {
|
125 |
+
'translation': {source: l1, target: l2}
|
126 |
+
}
|
127 |
+
if all(result.values()):
|
128 |
+
yield idx, result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|