File size: 5,485 Bytes
266d93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
048280e
266d93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datasets import BuilderConfig, Version, GeneratorBasedBuilder, DatasetInfo, Features, Value, \
    Sequence, ClassLabel, DownloadManager, SplitGenerator, Split
import datasets
import os
import textwrap
import csv
from ast import literal_eval


_DESCRIPTION = """
The recognition and classification of proper nouns and names in plain text is of key importance in Natural Language 
Processing (NLP) as it has a beneficial effect on the performance of various types of applications, including 
Information Extraction, Machine Translation, Syntactic Parsing/Chunking, etc."""
_CITATION = """"""
_FEATURES = Features(
    {
        "id": Value("int32"),
        "tokens": Sequence(Value("string")),
        "ner": Sequence(
            ClassLabel(
                names=[
                    "O",
                    "B-PER",
                    "I-PER",
                    "B-ORG",
                    "I-ORG",
                    "B-LOC",
                    "I-LOC",
                    "B-MISC",
                    "I-MISC",
                ]
            )
        ),
        "document_id": Value("int32"),
        "sentence_id": Value("int32")
    }
)


class SzegedNERConfig(BuilderConfig):
    """BuilderConfig for SzegedNER."""

    def __init__(
        self,
        features,
        label_column,
        data_dir,
        citation,
        url,
        process_label=lambda x: x,
        **kwargs,
    ):
        super(SzegedNERConfig, self).__init__(version=Version("1.0.0", ""), **kwargs)
        self.features = features
        self.label_column = label_column
        self.data_dir = data_dir
        self.citation = citation
        self.url = url
        self.process_label = process_label


class SzegedNER(GeneratorBasedBuilder):
    """SzegedNER datasets."""

    BUILDER_CONFIGS = [
        SzegedNERConfig(
            name="business",
            description=textwrap.dedent(
                """\
            The Named Entity Corpus for Hungarian is a subcorpus of the Szeged Treebank, which contains full syntactic 
            annotations done manually by linguist experts. A significant part of these texts has been annotated with 
            Named Entity class labels in line with the annotation standards used on the CoNLL-2003 shared task."""
            ),
            features=_FEATURES,
            label_column="ner_tags",
            data_dir="data/business/",
            citation=textwrap.dedent(_CITATION),
            url="https://rgai.inf.u-szeged.hu/node/130"
        ),
        SzegedNERConfig(
            name="criminal",
            description=textwrap.dedent(
                """\
            The Hungarian National Corpus and its Heti Világgazdaság (HVG) subcorpus provided the basis for corpus text 
            selection: articles related to the topic of financially liable offences were selected and annotated for the 
            categories person, organization, location and miscellaneous. There are two annotated versions of the corpus. 
            When preparing the tag-for-meaning annotation, our linguists took into consideration the context in which 
            the Named Entity under investigation occurred, thus, it was not the primary sense of the Named Entity that 
            determined the tag (e.g. Manchester=LOC) but its contextual reference (e.g. Manchester won the Premier 
            League=ORG). As for tag-for-tag annotation, these cases were not differentiated: tags were always given on 
            the basis of the primary sense."""
            ),
            features=_FEATURES,
            label_column="ner_tags",
            data_dir="data/criminal/",
            citation=textwrap.dedent(_CITATION),
            url="https://rgai.inf.u-szeged.hu/node/130"
        )
    ]

    def _info(self):
        return DatasetInfo(
            description=self.config.description,
            features=self.config.features,
            homepage=self.config.url,
            citation=self.config.citation,
        )

    def _split_generators(self, dl_manager: DownloadManager):
        url = f"{self.base_path}{self.config.data_dir}"

        path = dl_manager.download({key: f"{url}{key}.csv" for key in ["train", "validation", "test"]})
        return [
            SplitGenerator(
                name=Split.TRAIN,
                gen_kwargs={"split_key": "train", "data_file": path['train']},
            ),
            SplitGenerator(
                name=Split.VALIDATION,
                gen_kwargs={"split_key": "validation", "data_file": path['validation']},
            ),
            SplitGenerator(
                name=Split.TEST,
                gen_kwargs={"split_key": "test", "data_file": path['test']},
            )
        ]

    def _generate_examples(self, data_file, split_key, **kwargs):
        with open(data_file, encoding="utf8") as f:
            reader = csv.DictReader(f, delimiter=",", quoting=csv.QUOTE_MINIMAL)
            for n, row in enumerate(reader):
                labels = literal_eval(row['ner'])
                tokens = literal_eval(row['tokens'])
                if len(labels) != len(tokens):
                    raise ValueError("Number of tokens and labels does not match")
                yield n, {
                    "id": int(row['id']),
                    "tokens": tokens,
                    "ner": labels,
                    "document_id": int(row['document_id']),
                    "sentence_id": int(row['sentence_id'])
                }