File size: 5,173 Bytes
02997e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 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