Datasets:

ArXiv:
License:
File size: 4,089 Bytes
821e463
 
 
 
 
 
 
 
 
 
 
 
4cba128
 
 
 
 
 
 
821e463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e1af0f
 
 
c7ca5de
0e1af0f
 
 
 
c7ca5de
0e1af0f
 
c7ca5de
0e1af0f
 
 
c7ca5de
4cba128
 
0e1af0f
 
 
 
c7ca5de
0e1af0f
 
 
 
4cba128
 
0e1af0f
 
 
821e463
 
 
 
 
4cba128
 
 
 
 
 
 
821e463
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7ca5de
 
 
 
821e463
 
c7ca5de
821e463
 
 
 
 
 
 
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from glob import glob
import json
import os
from pathlib import Path

import datasets


_URLS = {
    "amazon_reviews_multi": "data/amazon_reviews_multi.jsonl",
    "mike0307": "data/mike0307.jsonl",
    "nbnn": "data/nbnn.jsonl",
    "nordic_langid": "data/nordic_langid.jsonl",
    "scandi_langid": "data/scandi_langid.jsonl",
    "stsb_multi_mt": "data/stsb_multi_mt.jsonl",
    "xnli": "data/xnli.jsonl",

}


_CITATION = """\
@dataset{language_identification,
  author       = {Xing Tian},
  title        = {language_identification},
  month        = aug,
  year         = 2024,
  publisher    = {Xing Tian},
  version      = {1.0},
}
"""


LANGUAGE_MAP = {
    "ar": "arabic",
    "bg": "bulgarian",
    "da": "danish",
    "de": "german",
    "el": "modern greek",
    "en": "english",
    "es": "spanish",
    "fo": "faroese",
    "fr": "french",
    "hi": "hindi",
    "is": "icelandic",
    "it": "italian",
    "ja": "japanese",
    "nl": "dutch",
    "no": "norwegian",
    "no-b": "norwegian (bokmål)",
    "no-n": "norwegian (nynorsk)",
    "pl": "polish",
    "pt": "portuguese",
    "ru": "russian",
    "sw": "swahili",
    "sv": "swedish",
    "th": "thai",
    "tr": "turkish",
    "ur": "urdu",
    "vi": "vietnamese",
    "zh-cn": "simplified chinese",
    "zh-tw": "traditional chinese",
}


class LanguageIdentification(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="amazon_reviews_multi", version=VERSION, description="amazon_reviews_multi"),
        datasets.BuilderConfig(name="mike0307", version=VERSION, description="mike0307"),
        datasets.BuilderConfig(name="nbnn", version=VERSION, description="nbnn"),
        datasets.BuilderConfig(name="nordic_langid", version=VERSION, description="nordic_langid"),
        datasets.BuilderConfig(name="scandi_langid", version=VERSION, description="scandi_langid"),
        datasets.BuilderConfig(name="stsb_multi_mt", version=VERSION, description="stsb_multi_mt"),
        datasets.BuilderConfig(name="xnli", version=VERSION, description="xnli"),

    ]

    def _info(self):
        features = datasets.Features(
            {
                "text": datasets.Value("string"),
                "language": datasets.Value("string"),
                "data_source": datasets.Value("string"),
            }
        )

        return datasets.DatasetInfo(
            features=features,
            supervised_keys=None,
            homepage="",
            license="",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        url = _URLS[self.config.name]
        dl_path = dl_manager.download(url)
        archive_path = dl_path

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"archive_path": archive_path, "split": "train"},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"archive_path": archive_path, "split": "validation"},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"archive_path": archive_path, "split": "test"},
            ),
        ]

    def _generate_examples(self, archive_path, split):
        archive_path = Path(archive_path)

        idx = 0
        with open(archive_path, "r", encoding="utf-8") as f:
            for row in f:
                sample = json.loads(row)

                if sample["split"] != split:
                    continue

                language = sample["language"]
                if language not in LANGUAGE_MAP.keys():
                    raise AssertionError(language)

                yield idx, {
                    "text": sample["text"],
                    "language": language,
                    "data_source": sample["data_source"],
                }
                idx += 1


if __name__ == '__main__':
    pass