nordic_langid / nordic_langid.py
leondz's picture
basic reader
02997e8
# coding=utf-8
# Copyright 2020 HuggingFace Datasets Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""NordicDSL: A language identification datasets for Nordic languages"""
import csv
import os
import datasets
logger = datasets.logging.get_logger(__name__)
_CITATION = """\
@inproceedings{haas-derczynski-2021-discriminating,
title = "Discriminating Between Similar Nordic Languages",
author = "Haas, Ren{\'e} and
Derczynski, Leon",
booktitle = "Proceedings of the Eighth Workshop on NLP for Similar Languages, Varieties and Dialects",
month = apr,
year = "2021",
address = "Kiyv, Ukraine",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2021.vardial-1.8",
pages = "67--75",
}
"""
_DESCRIPTION = """\
Automatic language identification is a challenging problem. Discriminating
between closely related languages is especially difficult. This paper presents
a machine learning approach for automatic language identification for the
Nordic languages, which often suffer miscategorisation by existing
state-of-the-art tools. Concretely we will focus on discrimination between six
Nordic languages: Danish, Swedish, Norwegian (Nynorsk), Norwegian (Bokmål),
Faroese and Icelandic.
This is the data for the tasks. Two variants are provided: 10K and 50K, with
holding 10,000 and 50,000 examples for each language respectively.
"""
_URLS = {
"10K": "nordic_dsl_10000",
"50K": "nordic_dsl_50000",
}
class NordicLangIdConfig(datasets.BuilderConfig):
"""BuilderConfig for NordicLangId"""
def __init__(self, **kwargs):
"""BuilderConfig NordicLangId.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(NordicLangIdConfig, self).__init__(**kwargs)
class NordicLangId(datasets.GeneratorBasedBuilder):
"""NordicLangId dataset."""
VERSION = datasets.Version("1.0.0")
BUILDER_CONFIGS = [
NordicLangIdConfig(
name="10k",
description="Data for distinguishing between similar Nordic languages: 10k examples per class",
version=VERSION,
),
NordicLangIdConfig(
name="50k",
description="Data for distinguishing between similar Nordic languages: 50k examples per class",
version=VERSION,
),
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"id": datasets.Value("string"),
"sentence": datasets.Value("string"),
"language": datasets.features.ClassLabel(
names=[
"dk",
"sv",
"nb",
"nn",
"fo",
"is",
]
),
}
),
supervised_keys=None,
homepage="https://aclanthology.org/2021.vardial-1.8/",
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
if self.config.name == "10k":
downloaded_train = dl_manager.download(_URLS["10K"] + 'train.csv')
downloaded_test = dl_manager.download(_URLS["10K"] + 'test.csv')
elif self.config.name == "50k":
downloaded_train = dl_manager.download(_URLS["50K"] + 'train.csv')
downloaded_test = dl_manager.download(_URLS["50K"] + 'test.csv')
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_train}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": downloaded_test}),
]
def _generate_examples(self, filepath):
logger.info("⏳ Generating examples from = %s", filepath)
with open(filepath, encoding="utf-8") as f:
guid = 0
for line in f:
line = line.strip()
if not line:
continue
if self.config.name == "10k":
line = line.replace('dataset10000, ', '')
if self.config.name == "50k":
line = line.replace('dataset50000, ', '')
instance = {
"id": str(guid),
"language": line[-2:],
"sentence": line[:-3],
}
yield guid, instance
guid += 1