holylovenia
commited on
Upload openlid.py with huggingface_hub
Browse files- openlid.py +140 -0
openlid.py
ADDED
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import Dict, List, Tuple
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
|
6 |
+
from seacrowd.utils import schemas
|
7 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
8 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
9 |
+
|
10 |
+
_CITATION = """\
|
11 |
+
@inproceedings{burchell-etal-2023-open,
|
12 |
+
title = "An Open Dataset and Model for Language Identification",
|
13 |
+
author = "Burchell, Laurie and
|
14 |
+
Birch, Alexandra and
|
15 |
+
Bogoychev, Nikolay and
|
16 |
+
Heafield, Kenneth",
|
17 |
+
editor = "Rogers, Anna and
|
18 |
+
Boyd-Graber, Jordan and
|
19 |
+
Okazaki, Naoaki",
|
20 |
+
booktitle = "Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 2: Short Papers)",
|
21 |
+
month = jul,
|
22 |
+
year = "2023",
|
23 |
+
address = "Toronto, Canada",
|
24 |
+
publisher = "Association for Computational Linguistics",
|
25 |
+
url = "https://aclanthology.org/2023.acl-short.75",
|
26 |
+
doi = "10.18653/v1/2023.acl-short.75",
|
27 |
+
pages = "865--879",
|
28 |
+
abstract = "Language identification (LID) is a fundamental step in many natural language processing pipelines. However, current LID
|
29 |
+
systems are far from perfect, particularly on lower-resource languages. We present a LID model which achieves a macro-average F1
|
30 |
+
score of 0.93 and a false positive rate of 0.033{\%} across 201 languages, outperforming previous work. We achieve this by training
|
31 |
+
on a curated dataset of monolingual data, which we audit manually to ensure reliability. We make both the model and the dataset
|
32 |
+
available to the research community. Finally, we carry out detailed analysis into our model{'}s performance, both in comparison to
|
33 |
+
existing open models and by language class.",
|
34 |
+
}
|
35 |
+
"""
|
36 |
+
|
37 |
+
_LOCAL = False
|
38 |
+
_LANGUAGES = ["ace", "ban", "bjn", "bug", "ceb", "ilo", "ind", "jav", "kac", "khm", "lao", "min", "lus", "mya", "pag", "shn", "sun", "tgl", "tha", "vie", "war", "zsm"]
|
39 |
+
_DATASETNAME = "openlid"
|
40 |
+
|
41 |
+
_DESCRIPTION = """\
|
42 |
+
This is an open dataset for language identification covering 201 languages, which are curated and audited manually to
|
43 |
+
ensure high confidence in its data and language labels. 22 languages are native to Southeast Asia speakers.
|
44 |
+
"""
|
45 |
+
|
46 |
+
_HOMEPAGE = "https://github.com/laurieburchell/open-lid-dataset"
|
47 |
+
_LICENSE = Licenses.GPL_3_0.value
|
48 |
+
_URLS = {
|
49 |
+
_DATASETNAME: "https://data.statmt.org/lid/lid201-data.tsv.gz",
|
50 |
+
}
|
51 |
+
_SUPPORTED_TASKS = [Tasks.LANGUAGE_IDENTIFICATION]
|
52 |
+
_SOURCE_VERSION = "1.0.0"
|
53 |
+
_SEACROWD_VERSION = "2024.06.20"
|
54 |
+
|
55 |
+
# 201 languages. Each element contains a code for the language, and script (e.g. wol_Latn = Wolof in Latin script)
|
56 |
+
_TAGS = ['kbp_Latn', 'zul_Latn', 'zho_Hans', 'uig_Arab', 'smo_Latn', 'hrv_Latn', 'tgk_Cyrl', 'guj_Gujr', 'azj_Latn', 'mai_Deva', 'bul_Cyrl', 'hne_Deva', 'wol_Latn', 'ind_Latn', 'lit_Latn', 'epo_Latn', 'prs_Arab', 'kmr_Latn', 'fao_Latn', 'swh_Latn', 'slk_Latn', 'srp_Cyrl', 'bod_Tibt', 'eus_Latn', 'tir_Ethi', 'tam_Taml', 'kas_Deva', 'glg_Latn', 'crh_Latn', 'kon_Latn', 'ayr_Latn', 'por_Latn', 'ben_Beng', 'zho_Hant', 'bug_Latn', 'umb_Latn', 'tzm_Tfng', 'kan_Knda', 'tgl_Latn', 'luo_Latn', 'lij_Latn', 'hun_Latn', 'kin_Latn', 'hat_Latn', 'sag_Latn', 'khm_Khmr', 'heb_Hebr', 'hye_Armn', 'fuv_Latn', 'cjk_Latn', 'ckb_Arab', 'srd_Latn', 'cat_Latn', 'dan_Latn', 'lao_Laoo', 'fra_Latn', 'kam_Latn', 'aeb_Arab', 'ydd_Hebr', 'afr_Latn', 'khk_Cyrl', 'lug_Latn', 'lin_Latn', 'nya_Latn', 'tsn_Latn', 'dzo_Tibt', 'min_Latn', 'war_Latn', 'rus_Cyrl', 'nob_Latn', 'tpi_Latn', 'mlt_Latn', 'mni_Beng', 'ilo_Latn', 'amh_Ethi', 'taq_Latn', 'acq_Arab', 'gaz_Latn', 'ltg_Latn', 'kac_Latn', 'ibo_Latn', 'gle_Latn', 'mya_Mymr', 'grn_Latn', 'kik_Latn', 'jav_Latn', 'awa_Deva', 'ars_Arab', 'swe_Latn', 'uzn_Latn', 'mos_Latn', 'lus_Latn', 'mal_Mlym', 'ita_Latn', 'dik_Latn', 'ewe_Latn', 'sat_Olck', 'pan_Guru', 'est_Latn', 'kab_Latn', 'bam_Latn', 'pag_Latn', 'isl_Latn', 'eng_Latn', 'fon_Latn', 'kas_Arab', 'asm_Beng', 'lim_Latn', 'bjn_Arab', 'taq_Tfng', 'deu_Latn', 'pbt_Arab', 'pap_Latn', 'quy_Latn', 'kea_Latn', 'npi_Deva', 'xho_Latn', 'shn_Mymr', 'nso_Latn', 'urd_Arab', 'bos_Latn', 'ron_Latn', 'fur_Latn', 'gla_Latn', 'nus_Latn', 'ltz_Latn', 'arz_Arab', 'bem_Latn', 'fin_Latn', 'kir_Cyrl', 'tha_Thai', 'mag_Deva', 'azb_Arab', 'tel_Telu', 'ell_Grek', 'sot_Latn', 'spa_Latn', 'vie_Latn', 'yor_Latn', 'ceb_Latn', 'vec_Latn', 'sin_Sinh', 'pol_Latn', 'als_Latn', 'lmo_Latn', 'scn_Latn', 'ces_Latn', 'fij_Latn', 'run_Latn', 'som_Latn', 'mkd_Cyrl', 'mar_Deva', 'ast_Latn', 'san_Deva', 'ary_Arab', 'twi_Latn', 'acm_Arab', 'nno_Latn', 'zsm_Latn', 'mri_Latn', 'kor_Hang', 'sna_Latn', 'pes_Arab', 'ace_Latn', 'bak_Cyrl', 'kat_Geor', 'tur_Latn', 'jpn_Jpan', 'arb_Arab', 'ukr_Cyrl', 'yue_Hant', 'kaz_Cyrl', 'hau_Latn', 'nld_Latn', 'oci_Latn', 'apc_Arab', 'tum_Latn', 'ace_Arab', 'dyu_Latn', 'knc_Latn', 'knc_Arab', 'kmb_Latn', 'bel_Cyrl', 'slv_Latn', 'lvs_Latn', 'bho_Deva', 'tuk_Latn', 'snd_Arab', 'sun_Latn', 'lua_Latn', 'ajp_Arab', 'hin_Deva', 'tso_Latn', 'tat_Cyrl', 'cym_Latn', 'ory_Orya', 'ban_Latn', 'szl_Latn', 'plt_Latn', 'bjn_Latn', 'ssw_Latn']
|
57 |
+
|
58 |
+
|
59 |
+
class OpenLID(datasets.GeneratorBasedBuilder):
|
60 |
+
"""This is an open dataset for language identification covering 201 languages. 22 languages are native to Southeast Asia speakers."""
|
61 |
+
|
62 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
63 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
64 |
+
|
65 |
+
BUILDER_CONFIGS = [
|
66 |
+
SEACrowdConfig(
|
67 |
+
name="openlid_source",
|
68 |
+
version=SOURCE_VERSION,
|
69 |
+
description="OpenLID source schema",
|
70 |
+
schema="source",
|
71 |
+
subset_id="openlid",
|
72 |
+
),
|
73 |
+
SEACrowdConfig(
|
74 |
+
name="openlid_seacrowd_text",
|
75 |
+
version=SEACROWD_VERSION,
|
76 |
+
description="OpenLID Nusantara schema",
|
77 |
+
schema="seacrowd_text",
|
78 |
+
subset_id="openlid",
|
79 |
+
),
|
80 |
+
]
|
81 |
+
|
82 |
+
DEFAULT_CONFIG_NAME = "openlid_source"
|
83 |
+
|
84 |
+
def _info(self) -> datasets.DatasetInfo:
|
85 |
+
if self.config.schema == "source":
|
86 |
+
features = datasets.Features({"id": datasets.Value("string"), "text": datasets.Value("string"), "label": datasets.Value("string"), "source": datasets.Value("string")})
|
87 |
+
elif self.config.schema == "seacrowd_text":
|
88 |
+
features = schemas.text_features(_TAGS)
|
89 |
+
|
90 |
+
return datasets.DatasetInfo(
|
91 |
+
description=_DESCRIPTION,
|
92 |
+
features=features,
|
93 |
+
homepage=_HOMEPAGE,
|
94 |
+
license=_LICENSE,
|
95 |
+
citation=_CITATION,
|
96 |
+
)
|
97 |
+
|
98 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
99 |
+
"""Returns SplitGenerators."""
|
100 |
+
# Dataset does not have predetermined split, putting all as TRAIN
|
101 |
+
urls = _URLS[_DATASETNAME]
|
102 |
+
filepath = Path(dl_manager.download_and_extract(urls))
|
103 |
+
|
104 |
+
return [
|
105 |
+
datasets.SplitGenerator(
|
106 |
+
name=datasets.Split.TRAIN,
|
107 |
+
gen_kwargs={
|
108 |
+
"filepath": filepath,
|
109 |
+
},
|
110 |
+
),
|
111 |
+
]
|
112 |
+
|
113 |
+
def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
|
114 |
+
"""Yields examples as (key, example) tuples."""
|
115 |
+
# Dataset does not have id, using row index as id
|
116 |
+
with open(filepath) as f:
|
117 |
+
lines = f.readlines()
|
118 |
+
|
119 |
+
if self.config.schema == "source":
|
120 |
+
for _id, line in enumerate(lines):
|
121 |
+
line = line.split("\t")
|
122 |
+
ex = {
|
123 |
+
"id": str(_id),
|
124 |
+
"text": line[0],
|
125 |
+
"label": line[1],
|
126 |
+
"source": line[2].strip(),
|
127 |
+
}
|
128 |
+
yield _id, ex
|
129 |
+
|
130 |
+
elif self.config.schema == "seacrowd_text":
|
131 |
+
for _id, line in enumerate(lines):
|
132 |
+
line = line.split("\t")
|
133 |
+
ex = {
|
134 |
+
"id": str(_id),
|
135 |
+
"text": line[0],
|
136 |
+
"label": line[1],
|
137 |
+
}
|
138 |
+
yield _id, ex
|
139 |
+
else:
|
140 |
+
raise ValueError(f"Invalid config: {self.config.name}")
|