holylovenia
commited on
Commit
•
c2da684
1
Parent(s):
adde2ab
Upload unimorph.py with huggingface_hub
Browse files- unimorph.py +447 -0
unimorph.py
ADDED
@@ -0,0 +1,447 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import Any, Dict, List, Tuple
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
from datasets.download.download_manager import DownloadManager
|
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 |
+
@misc{batsuren2022unimorph,
|
13 |
+
title={UniMorph 4.0: Universal Morphology},
|
14 |
+
author={
|
15 |
+
Khuyagbaatar Batsuren and Omer Goldman and Salam Khalifa and Nizar
|
16 |
+
Habash and Witold Kieraś and Gábor Bella and Brian Leonard and Garrett
|
17 |
+
Nicolai and Kyle Gorman and Yustinus Ghanggo Ate and Maria Ryskina and
|
18 |
+
Sabrina J. Mielke and Elena Budianskaya and Charbel El-Khaissi and Tiago
|
19 |
+
Pimentel and Michael Gasser and William Lane and Mohit Raj and Matt
|
20 |
+
Coler and Jaime Rafael Montoya Samame and Delio Siticonatzi Camaiteri
|
21 |
+
and Benoît Sagot and Esaú Zumaeta Rojas and Didier López Francis and
|
22 |
+
Arturo Oncevay and Juan López Bautista and Gema Celeste Silva Villegas
|
23 |
+
and Lucas Torroba Hennigen and Adam Ek and David Guriel and Peter Dirix
|
24 |
+
and Jean-Philippe Bernardy and Andrey Scherbakov and Aziyana Bayyr-ool
|
25 |
+
and Antonios Anastasopoulos and Roberto Zariquiey and Karina Sheifer and
|
26 |
+
Sofya Ganieva and Hilaria Cruz and Ritván Karahóǧa and Stella
|
27 |
+
Markantonatou and George Pavlidis and Matvey Plugaryov and Elena
|
28 |
+
Klyachko and Ali Salehi and Candy Angulo and Jatayu Baxi and Andrew
|
29 |
+
Krizhanovsky and Natalia Krizhanovskaya and Elizabeth Salesky and Clara
|
30 |
+
Vania and Sardana Ivanova and Jennifer White and Rowan Hall Maudslay and
|
31 |
+
Josef Valvoda and Ran Zmigrod and Paula Czarnowska and Irene Nikkarinen
|
32 |
+
and Aelita Salchak and Brijesh Bhatt and Christopher Straughn and Zoey
|
33 |
+
Liu and Jonathan North Washington and Yuval Pinter and Duygu Ataman and
|
34 |
+
Marcin Wolinski and Totok Suhardijanto and Anna Yablonskaya and Niklas
|
35 |
+
Stoehr and Hossep Dolatian and Zahroh Nuriah and Shyam Ratan and Francis
|
36 |
+
M. Tyers and Edoardo M. Ponti and Grant Aiton and Aryaman Arora and
|
37 |
+
Richard J. Hatcher and Ritesh Kumar and Jeremiah Young and Daria
|
38 |
+
Rodionova and Anastasia Yemelina and Taras Andrushko and Igor Marchenko
|
39 |
+
and Polina Mashkovtseva and Alexandra Serova and Emily Prud'hommeaux and
|
40 |
+
Maria Nepomniashchaya and Fausto Giunchiglia and Eleanor Chodroff and
|
41 |
+
Mans Hulden and Miikka Silfverberg and Arya D. McCarthy and David
|
42 |
+
Yarowsky and Ryan Cotterell and Reut Tsarfaty and Ekaterina Vylomova},
|
43 |
+
year={2022},
|
44 |
+
eprint={2205.03608},
|
45 |
+
archivePrefix={arXiv},
|
46 |
+
primaryClass={cs.CL}
|
47 |
+
}
|
48 |
+
"""
|
49 |
+
|
50 |
+
_LOCAL = False
|
51 |
+
_LANGUAGES = ["ind", "kod", "ceb", "hil", "tgl"]
|
52 |
+
_DATASETNAME = "unimorph"
|
53 |
+
_DESCRIPTION = """\
|
54 |
+
The Universal Morphology (UniMorph) project is a collaborative effort providing
|
55 |
+
broad-coverage instantiated normalized morphological inflection tables for
|
56 |
+
undreds of diverse world languages. The project comprises two major thrusts: a
|
57 |
+
language-independent feature schema for rich morphological annotation, and a
|
58 |
+
type-level resource of annotated data in diverse languages realizing that
|
59 |
+
schema. 5 Austronesian languages spoken in Southeast Asia, consisting 2
|
60 |
+
Malayo-Polynesian languages and 3 Greater Central Philippine languages, become
|
61 |
+
the part of UniMorph 4.0 release.
|
62 |
+
"""
|
63 |
+
|
64 |
+
_HOMEPAGE = "https://unimorph.github.io"
|
65 |
+
_LICENSE = Licenses.CC_BY_SA_3_0.value
|
66 |
+
_URL = "https://raw.githubusercontent.com/unimorph/"
|
67 |
+
|
68 |
+
_SUPPORTED_TASKS = [Tasks.MORPHOLOGICAL_INFLECTION]
|
69 |
+
_SOURCE_VERSION = "4.0.0"
|
70 |
+
_SEACROWD_VERSION = "2024.06.20"
|
71 |
+
|
72 |
+
|
73 |
+
class UnimorphDataset(datasets.GeneratorBasedBuilder):
|
74 |
+
"""Unimorh 4.0 dataset by Batsuren et al., (2022)"""
|
75 |
+
|
76 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
77 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
78 |
+
|
79 |
+
SEACROWD_SCHEMA_NAME = "pairs_multi"
|
80 |
+
|
81 |
+
dataset_names = sorted([f"{_DATASETNAME}_{lang}" for lang in _LANGUAGES])
|
82 |
+
BUILDER_CONFIGS = []
|
83 |
+
for name in dataset_names:
|
84 |
+
source_config = SEACrowdConfig(
|
85 |
+
name=f"{name}_source",
|
86 |
+
version=SOURCE_VERSION,
|
87 |
+
description=f"{_DATASETNAME} source schema",
|
88 |
+
schema="source",
|
89 |
+
subset_id=name,
|
90 |
+
)
|
91 |
+
BUILDER_CONFIGS.append(source_config)
|
92 |
+
seacrowd_config = SEACrowdConfig(
|
93 |
+
name=f"{name}_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
94 |
+
version=SEACROWD_VERSION,
|
95 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
96 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
97 |
+
subset_id=name,
|
98 |
+
)
|
99 |
+
BUILDER_CONFIGS.append(seacrowd_config)
|
100 |
+
|
101 |
+
# Add configuration that allows loading all datasets at once.
|
102 |
+
BUILDER_CONFIGS.extend(
|
103 |
+
[
|
104 |
+
# unimorph_source
|
105 |
+
SEACrowdConfig(
|
106 |
+
name=f"{_DATASETNAME}_source",
|
107 |
+
version=SOURCE_VERSION,
|
108 |
+
description=f"{_DATASETNAME} source schema (all)",
|
109 |
+
schema="source",
|
110 |
+
subset_id=_DATASETNAME,
|
111 |
+
),
|
112 |
+
# unimorph_seacrowd_pairs
|
113 |
+
SEACrowdConfig(
|
114 |
+
name=f"{_DATASETNAME}_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
115 |
+
version=SEACROWD_VERSION,
|
116 |
+
description=f"{_DATASETNAME} SEACrowd schema (all)",
|
117 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
118 |
+
subset_id=_DATASETNAME,
|
119 |
+
),
|
120 |
+
]
|
121 |
+
)
|
122 |
+
|
123 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
124 |
+
# https://huggingface.co/datasets/universal_morphologies/blob/main/universal_morphologies.py
|
125 |
+
CLASS_CATEGORIES = {
|
126 |
+
"Aktionsart": ["STAT", "DYN", "TEL", "ATEL", "PCT", "DUR", "ACH", "ACCMP", "SEMEL", "ACTY"],
|
127 |
+
"Animacy": ["ANIM", "INAN", "HUM", "NHUM"],
|
128 |
+
"Argument_Marking": [
|
129 |
+
"ARGNO1S",
|
130 |
+
"ARGNO2S",
|
131 |
+
"ARGNO3S",
|
132 |
+
"ARGNO1P",
|
133 |
+
"ARGNO2P",
|
134 |
+
"ARGNO3P",
|
135 |
+
"ARGAC1S",
|
136 |
+
"ARGAC2S",
|
137 |
+
"ARGAC3S",
|
138 |
+
"ARGAC1P",
|
139 |
+
"ARGAC2P",
|
140 |
+
"ARGAC3P",
|
141 |
+
"ARGAB1S",
|
142 |
+
"ARGAB2S",
|
143 |
+
"ARGAB3S",
|
144 |
+
"ARGAB1P",
|
145 |
+
"ARGAB2P",
|
146 |
+
"ARGAB3P",
|
147 |
+
"ARGER1S",
|
148 |
+
"ARGER2S",
|
149 |
+
"ARGER3S",
|
150 |
+
"ARGER1P",
|
151 |
+
"ARGER2P",
|
152 |
+
"ARGER3P",
|
153 |
+
"ARGDA1S",
|
154 |
+
"ARGDA2S",
|
155 |
+
"ARGDA3S",
|
156 |
+
"ARGDA1P",
|
157 |
+
"ARGDA2P",
|
158 |
+
"ARGDA3P",
|
159 |
+
"ARGBE1S",
|
160 |
+
"ARGBE2S",
|
161 |
+
"ARGBE3S",
|
162 |
+
"ARGBE1P",
|
163 |
+
"ARGBE2P",
|
164 |
+
"ARGBE3P",
|
165 |
+
],
|
166 |
+
"Aspect": ["IPFV", "PFV", "PRF", "PROG", "PROSP", "ITER", "HAB"],
|
167 |
+
"Case": [
|
168 |
+
"NOM",
|
169 |
+
"ACC",
|
170 |
+
"ERG",
|
171 |
+
"ABS",
|
172 |
+
"NOMS",
|
173 |
+
"DAT",
|
174 |
+
"BEN",
|
175 |
+
"PRP",
|
176 |
+
"GEN",
|
177 |
+
"REL",
|
178 |
+
"PRT",
|
179 |
+
"INS",
|
180 |
+
"COM",
|
181 |
+
"VOC",
|
182 |
+
"COMPV",
|
183 |
+
"EQTV",
|
184 |
+
"PRIV",
|
185 |
+
"PROPR",
|
186 |
+
"AVR",
|
187 |
+
"FRML",
|
188 |
+
"TRANS",
|
189 |
+
"BYWAY",
|
190 |
+
"INTER",
|
191 |
+
"AT",
|
192 |
+
"POST",
|
193 |
+
"IN",
|
194 |
+
"CIRC",
|
195 |
+
"ANTE",
|
196 |
+
"APUD",
|
197 |
+
"ON",
|
198 |
+
"ONHR",
|
199 |
+
"ONVR",
|
200 |
+
"SUB",
|
201 |
+
"REM",
|
202 |
+
"PROXM",
|
203 |
+
"ESS",
|
204 |
+
"ALL",
|
205 |
+
"ABL",
|
206 |
+
"APPRX",
|
207 |
+
"TERM",
|
208 |
+
],
|
209 |
+
"Comparison": ["CMPR", "SPRL", "AB", "RL", "EQT"],
|
210 |
+
"Definiteness": ["DEF", "INDF", "SPEC", "NSPEC"],
|
211 |
+
"Deixis": ["PROX", "MED", "REMT", "REF1", "REF2", "NOREF", "PHOR", "VIS", "NVIS", "ABV", "EVEN", "BEL"],
|
212 |
+
"Evidentiality": ["FH", "DRCT", "SEN", "VISU", "NVSEN", "AUD", "NFH", "QUOT", "RPRT", "HRSY", "INFER", "ASSUM"],
|
213 |
+
"Finiteness": ["FIN", "NFIN"],
|
214 |
+
"Gender": [
|
215 |
+
"MASC",
|
216 |
+
"FEM",
|
217 |
+
"NEUT",
|
218 |
+
"NAKH1",
|
219 |
+
"NAKH2",
|
220 |
+
"NAKH3",
|
221 |
+
"NAKH4",
|
222 |
+
"NAKH5",
|
223 |
+
"NAKH6",
|
224 |
+
"NAKH7",
|
225 |
+
"NAKH8",
|
226 |
+
"BANTU1",
|
227 |
+
"BANTU2",
|
228 |
+
"BANTU3",
|
229 |
+
"BANTU4",
|
230 |
+
"BANTU5",
|
231 |
+
"BANTU6",
|
232 |
+
"BANTU7",
|
233 |
+
"BANTU8",
|
234 |
+
"BANTU9",
|
235 |
+
"BANTU10",
|
236 |
+
"BANTU11",
|
237 |
+
"BANTU12",
|
238 |
+
"BANTU13",
|
239 |
+
"BANTU14",
|
240 |
+
"BANTU15",
|
241 |
+
"BANTU16",
|
242 |
+
"BANTU17",
|
243 |
+
"BANTU18",
|
244 |
+
"BANTU19",
|
245 |
+
"BANTU20",
|
246 |
+
"BANTU21",
|
247 |
+
"BANTU22",
|
248 |
+
"BANTU23",
|
249 |
+
],
|
250 |
+
"Information_Structure": ["TOP", "FOC"],
|
251 |
+
"Interrogativity": ["DECL", "INT"],
|
252 |
+
"Language_Specific": [
|
253 |
+
"LGSPEC1",
|
254 |
+
"LGSPEC2",
|
255 |
+
"LGSPEC3",
|
256 |
+
"LGSPEC4",
|
257 |
+
"LGSPEC5",
|
258 |
+
"LGSPEC6",
|
259 |
+
"LGSPEC7",
|
260 |
+
"LGSPEC8",
|
261 |
+
"LGSPEC9",
|
262 |
+
"LGSPEC10",
|
263 |
+
],
|
264 |
+
"Mood": [
|
265 |
+
"IND",
|
266 |
+
"SBJV",
|
267 |
+
"REAL",
|
268 |
+
"IRR",
|
269 |
+
"AUPRP",
|
270 |
+
"AUNPRP",
|
271 |
+
"IMP",
|
272 |
+
"COND",
|
273 |
+
"PURP",
|
274 |
+
"INTEN",
|
275 |
+
"POT",
|
276 |
+
"LKLY",
|
277 |
+
"ADM",
|
278 |
+
"OBLIG",
|
279 |
+
"DEB",
|
280 |
+
"PERM",
|
281 |
+
"DED",
|
282 |
+
"SIM",
|
283 |
+
"OPT",
|
284 |
+
],
|
285 |
+
"Number": ["SG", "PL", "GRPL", "DU", "TRI", "PAUC", "GRPAUC", "INVN"],
|
286 |
+
"Part_Of_Speech": [
|
287 |
+
"N",
|
288 |
+
"PROPN",
|
289 |
+
"ADJ",
|
290 |
+
"PRO",
|
291 |
+
"CLF",
|
292 |
+
"ART",
|
293 |
+
"DET",
|
294 |
+
"V",
|
295 |
+
"ADV",
|
296 |
+
"AUX",
|
297 |
+
"V.PTCP",
|
298 |
+
"V.MSDR",
|
299 |
+
"V.CVB",
|
300 |
+
"ADP",
|
301 |
+
"COMP",
|
302 |
+
"CONJ",
|
303 |
+
"NUM",
|
304 |
+
"PART",
|
305 |
+
"INTJ",
|
306 |
+
],
|
307 |
+
"Person": ["0", "1", "2", "3", "4", "INCL", "EXCL", "PRX", "OBV"],
|
308 |
+
"Polarity": ["POS", "NEG"],
|
309 |
+
"Politeness": [
|
310 |
+
"INFM",
|
311 |
+
"FORM",
|
312 |
+
"ELEV",
|
313 |
+
"HUMB",
|
314 |
+
"POL",
|
315 |
+
"AVOID",
|
316 |
+
"LOW",
|
317 |
+
"HIGH",
|
318 |
+
"STELEV",
|
319 |
+
"STSUPR",
|
320 |
+
"LIT",
|
321 |
+
"FOREG",
|
322 |
+
"COL",
|
323 |
+
],
|
324 |
+
"Possession": [
|
325 |
+
"ALN",
|
326 |
+
"NALN",
|
327 |
+
"PSS1S",
|
328 |
+
"PSS2S",
|
329 |
+
"PSS2SF",
|
330 |
+
"PSS2SM",
|
331 |
+
"PSS2SINFM",
|
332 |
+
"PSS2SFORM",
|
333 |
+
"PSS3S",
|
334 |
+
"PSS3SF",
|
335 |
+
"PSS3SM",
|
336 |
+
"PSS1D",
|
337 |
+
"PSS1DI",
|
338 |
+
"PSS1DE",
|
339 |
+
"PSS2D",
|
340 |
+
"PSS2DM",
|
341 |
+
"PSS2DF",
|
342 |
+
"PSS3D",
|
343 |
+
"PSS3DF",
|
344 |
+
"PSS3DM",
|
345 |
+
"PSS1P",
|
346 |
+
"PSS1PI",
|
347 |
+
"PSS1PE",
|
348 |
+
"PSS2P",
|
349 |
+
"PSS2PF",
|
350 |
+
"PSS2PM",
|
351 |
+
"PSS3PF",
|
352 |
+
"PSS3PM",
|
353 |
+
],
|
354 |
+
"Switch_Reference": ["SS", "SSADV", "DS", "DSADV", "OR", "SIMMA", "SEQMA", "LOG"],
|
355 |
+
"Tense": ["PRS", "PST", "FUT", "IMMED", "HOD", "1DAY", "RCT", "RMT"],
|
356 |
+
"Valency": ["IMPRS", "INTR", "TR", "DITR", "REFL", "RECP", "CAUS", "APPL"],
|
357 |
+
"Voice": ["ACT", "MID", "PASS", "ANTIP", "DIR", "INV", "AGFOC", "PFOC", "LFOC", "BFOC", "ACFOC", "IFOC", "CFOC"],
|
358 |
+
}
|
359 |
+
|
360 |
+
TAG_TO_CAT = dict([(tag, cat) for cat, tags in CLASS_CATEGORIES.items() for tag in tags])
|
361 |
+
CLASS_LABELS = [feat for _, category in CLASS_CATEGORIES.items() for feat in category]
|
362 |
+
|
363 |
+
def _info(self) -> datasets.DatasetInfo:
|
364 |
+
if self.config.schema == "source":
|
365 |
+
features = datasets.Features(
|
366 |
+
{
|
367 |
+
"lemma": datasets.Value("string"),
|
368 |
+
"forms": datasets.Sequence(
|
369 |
+
dict(
|
370 |
+
[("word", datasets.Value("string"))]
|
371 |
+
+ [(cat, datasets.Sequence(datasets.ClassLabel(names=tasks))) for cat, tasks in self.CLASS_CATEGORIES.items()]
|
372 |
+
+ [("Other", datasets.Sequence(datasets.Value("string")))] # for misspecified tags
|
373 |
+
)
|
374 |
+
),
|
375 |
+
}
|
376 |
+
)
|
377 |
+
|
378 |
+
if self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
379 |
+
all_features = [feat for _, category in self.CLASS_CATEGORIES.items() for feat in category]
|
380 |
+
features = schemas.pairs_multi_features(label_names=self.CLASS_LABELS)
|
381 |
+
|
382 |
+
return datasets.DatasetInfo(description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION)
|
383 |
+
|
384 |
+
def _split_generators(self, dl_manager: DownloadManager) -> List[datasets.SplitGenerator]:
|
385 |
+
"""Return SplitGenerators."""
|
386 |
+
source_data = []
|
387 |
+
|
388 |
+
lang = self.config.name.split("_")[1]
|
389 |
+
if lang in _LANGUAGES:
|
390 |
+
# Load data per language
|
391 |
+
source_data.append(dl_manager.download_and_extract(_URL + f"{lang}/main/{lang}"))
|
392 |
+
else:
|
393 |
+
# Load examples from all languages at once.
|
394 |
+
for lang in _LANGUAGES:
|
395 |
+
source_data.append(dl_manager.download_and_extract(_URL + f"{lang}/main/{lang}"))
|
396 |
+
|
397 |
+
return [
|
398 |
+
datasets.SplitGenerator(
|
399 |
+
name=datasets.Split.TRAIN,
|
400 |
+
gen_kwargs={
|
401 |
+
"filepaths": source_data,
|
402 |
+
},
|
403 |
+
)
|
404 |
+
]
|
405 |
+
|
406 |
+
def _generate_examples(self, filepaths: List[Path]) -> Tuple[int, Dict]:
|
407 |
+
"""Yield examples as (key, example) tuples"""
|
408 |
+
|
409 |
+
all_forms: Dict[str, List[Dict[str, Any]]] = {}
|
410 |
+
for source_file in filepaths:
|
411 |
+
with open(source_file, encoding="utf-8") as file:
|
412 |
+
for row in file:
|
413 |
+
if row.strip() == "" or row.strip().startswith("#"):
|
414 |
+
continue
|
415 |
+
lemma, word, tags = row.strip().split("\t")
|
416 |
+
all_forms[lemma] = all_forms.get(lemma, [])
|
417 |
+
tag_list = tags.replace("NDEF", "INDF").split(";")
|
418 |
+
form = dict([("word", word), ("Other", [])] + [(cat, []) for cat, tasks in self.CLASS_CATEGORIES.items()])
|
419 |
+
for tag_pre in tag_list:
|
420 |
+
tag = tag_pre.split("+")
|
421 |
+
if tag[0] in self.TAG_TO_CAT:
|
422 |
+
form[self.TAG_TO_CAT[tag[0]]] = tag
|
423 |
+
else:
|
424 |
+
form["Other"] += tag
|
425 |
+
all_forms[lemma] += [form]
|
426 |
+
|
427 |
+
if self.config.schema == "source":
|
428 |
+
for id_, (lemma, forms) in enumerate(all_forms.items()):
|
429 |
+
res = {"lemma": lemma, "forms": {}}
|
430 |
+
for k in ["word", "Other"] + list(self.CLASS_CATEGORIES.keys()):
|
431 |
+
res["forms"][k] = [form[k] for form in forms]
|
432 |
+
yield id_, res
|
433 |
+
|
434 |
+
if self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
435 |
+
idx = 0
|
436 |
+
for lemma, forms in all_forms.items():
|
437 |
+
for form in forms:
|
438 |
+
inflection = form.pop("word")
|
439 |
+
feats = [feat[0] for feat in list(form.values()) if feat and feat[0] in self.CLASS_LABELS]
|
440 |
+
example = {
|
441 |
+
"id": idx,
|
442 |
+
"text_1": lemma,
|
443 |
+
"text_2": inflection,
|
444 |
+
"label": feats,
|
445 |
+
}
|
446 |
+
idx += 1
|
447 |
+
yield idx, example
|