Create conll2003.py
Browse files- conll2003.py +184 -0
conll2003.py
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2020 HuggingFace Datasets Authors.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
# Lint as: python3
|
17 |
+
"""Introduction to the CoNLL-2003 Shared Task: Language-Independent Named Entity Recognition"""
|
18 |
+
|
19 |
+
import os
|
20 |
+
|
21 |
+
import datasets
|
22 |
+
from datasets import load_dataset
|
23 |
+
|
24 |
+
logger = datasets.logging.get_logger(__name__)
|
25 |
+
|
26 |
+
|
27 |
+
_CITATION = """\
|
28 |
+
@inproceedings{tjong-kim-sang-de-meulder-2003-introduction,
|
29 |
+
title = "Introduction to the {C}o{NLL}-2003 Shared Task: Language-Independent Named Entity Recognition",
|
30 |
+
author = "Tjong Kim Sang, Erik F. and
|
31 |
+
De Meulder, Fien",
|
32 |
+
booktitle = "Proceedings of the Seventh Conference on Natural Language Learning at {HLT}-{NAACL} 2003",
|
33 |
+
year = "2003",
|
34 |
+
url = "https://www.aclweb.org/anthology/W03-0419",
|
35 |
+
pages = "142--147",
|
36 |
+
}
|
37 |
+
"""
|
38 |
+
|
39 |
+
_DESCRIPTION = """\
|
40 |
+
The shared task of CoNLL-2003 concerns language-independent named entity recognition. We will concentrate on
|
41 |
+
four types of named entities: persons, locations, organizations and names of miscellaneous entities that do
|
42 |
+
not belong to the previous three groups.
|
43 |
+
|
44 |
+
The CoNLL-2003 shared task data files contain four columns separated by a single space. Each word has been put on
|
45 |
+
a separate line and there is an empty line after each sentence. The first item on each line is a word, the second
|
46 |
+
a part-of-speech (POS) tag, the third a syntactic chunk tag and the fourth the named entity tag. The chunk tags
|
47 |
+
and the named entity tags have the format I-TYPE which means that the word is inside a phrase of type TYPE. Only
|
48 |
+
if two phrases of the same type immediately follow each other, the first word of the second phrase will have tag
|
49 |
+
B-TYPE to show that it starts a new phrase. A word with tag O is not part of a phrase. Note the dataset uses IOB2
|
50 |
+
tagging scheme, whereas the original dataset uses IOB1.
|
51 |
+
|
52 |
+
For more details see https://www.clips.uantwerpen.be/conll2003/ner/ and https://www.aclweb.org/anthology/W03-0419
|
53 |
+
"""
|
54 |
+
|
55 |
+
_URL = "http://github.com/lunesco/conll2003/raw/e3006b5e2c5c68c1c7ccc0f2583c7cab33a31c15/conll2003.zip"
|
56 |
+
_TRAINING_FILE = "train.txt"
|
57 |
+
_DEV_FILE = "valid.txt"
|
58 |
+
_TEST_FILE = "test.txt"
|
59 |
+
|
60 |
+
|
61 |
+
class Conll2003Config(datasets.BuilderConfig):
|
62 |
+
"""BuilderConfig for Conll2003"""
|
63 |
+
|
64 |
+
def __init__(self, **kwargs):
|
65 |
+
"""BuilderConfig forConll2003.
|
66 |
+
|
67 |
+
Args:
|
68 |
+
**kwargs: keyword arguments forwarded to super.
|
69 |
+
"""
|
70 |
+
super(Conll2003Config, self).__init__(**kwargs)
|
71 |
+
|
72 |
+
|
73 |
+
class Conll2003(datasets.GeneratorBasedBuilder):
|
74 |
+
"""Conll2003 dataset."""
|
75 |
+
|
76 |
+
BUILDER_CONFIGS = [
|
77 |
+
Conll2003Config(name="conll2003", version=datasets.Version("1.0.0"), description="Conll2003 dataset"),
|
78 |
+
]
|
79 |
+
|
80 |
+
def _info(self): # 49, 23, 42 dlugosc (vs 47, 23, 9)
|
81 |
+
return datasets.DatasetInfo(
|
82 |
+
description=_DESCRIPTION,
|
83 |
+
features=datasets.Features(
|
84 |
+
{
|
85 |
+
"id": datasets.Value("string"),
|
86 |
+
"tokens": datasets.Sequence(datasets.Value("string")),
|
87 |
+
"pos_tags": datasets.Sequence(
|
88 |
+
datasets.features.ClassLabel(
|
89 |
+
names=['VAFIN', 'PPOSAT', 'NN', 'APPR', 'ADV', 'VVINF', '$.', 'NE',
|
90 |
+
'CARD', 'TRUNC', 'XY', 'ADJA', 'ART', 'VVFIN', 'PPER', 'APPRART',
|
91 |
+
'$[', 'VVPP', 'KON', '$,', 'PTKVZ', 'ADJD', 'PIAT', 'PRELS',
|
92 |
+
'PTKNEG', 'VAINF', 'VMFIN', 'PTKZU', 'PROAV', 'PIDAT', 'PDS',
|
93 |
+
'PWAV', 'PWS', 'KOUS', 'PIS', 'PRF', 'FM', 'ITJ', 'PTKANT', 'PDAT',
|
94 |
+
'VVIZU', 'PWAT', 'APZR', 'KOKOM', 'VVIMP', 'PTKA', 'KOUI', 'APPO',
|
95 |
+
'VAPP']
|
96 |
+
)
|
97 |
+
),
|
98 |
+
"chunk_tags": datasets.Sequence(
|
99 |
+
datasets.features.ClassLabel(
|
100 |
+
names=['I-VA', 'I-PP', 'I-NN', 'I-AP', 'I-AD', 'I-VV', 'I-$.', 'I-NE',
|
101 |
+
'I-CA', 'I-TR', 'I-XY', 'I-AR', 'I-$[', 'I-KO', 'I-$,',
|
102 |
+
'I-PT', 'I-PI', 'I-PR', 'I-VM', 'I-PD', 'I-PW', 'I-FM', 'I-IT']
|
103 |
+
)
|
104 |
+
),
|
105 |
+
"ner_tags": datasets.Sequence(
|
106 |
+
datasets.features.ClassLabel(
|
107 |
+
names=['O', 'B-organization-company', 'B-location-route',
|
108 |
+
'B-trigger', 'B-location-stop', 'B-date', 'B-location-city',
|
109 |
+
'B-event-cause', 'I-event-cause', 'B-time', 'I-time', 'B-number',
|
110 |
+
'B-organization', 'I-organization', 'B-location-street',
|
111 |
+
'I-trigger', 'B-location', 'I-location', 'I-location-city',
|
112 |
+
'I-organization-company', 'B-duration', 'I-duration',
|
113 |
+
'I-location-street', 'I-location-stop', 'I-location-route',
|
114 |
+
'B-person', 'I-date', 'B-set', 'B-money', 'I-person', 'I-money',
|
115 |
+
'B-distance', 'I-distance', 'I-number', 'B-disaster-type',
|
116 |
+
'B-org-position', 'I-org-position', 'I-set', 'B-percent',
|
117 |
+
'I-percent', 'I-disaster-type']
|
118 |
+
)
|
119 |
+
),
|
120 |
+
}
|
121 |
+
),
|
122 |
+
supervised_keys=None,
|
123 |
+
homepage="https://www.aclweb.org/anthology/W03-0419/",
|
124 |
+
citation=_CITATION,
|
125 |
+
)
|
126 |
+
|
127 |
+
def _split_generators(self, dl_manager):
|
128 |
+
"""Returns SplitGenerators."""
|
129 |
+
downloaded_file = dl_manager.download_and_extract(_URL)
|
130 |
+
data_files = {
|
131 |
+
"train": os.path.join(downloaded_file, _TRAINING_FILE),
|
132 |
+
"dev": os.path.join(downloaded_file, _DEV_FILE),
|
133 |
+
"test": os.path.join(downloaded_file, _TEST_FILE),
|
134 |
+
}
|
135 |
+
|
136 |
+
return [
|
137 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_files["train"]}),
|
138 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_files["dev"]}),
|
139 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_files["test"]}),
|
140 |
+
]
|
141 |
+
|
142 |
+
def _generate_examples(self, filepath):
|
143 |
+
logger.info("⏳ Generating examples from = %s", filepath)
|
144 |
+
with open(filepath, encoding="utf-8") as f:
|
145 |
+
guid = 0
|
146 |
+
tokens = []
|
147 |
+
pos_tags = []
|
148 |
+
chunk_tags = []
|
149 |
+
ner_tags = []
|
150 |
+
for line in f:
|
151 |
+
if line.startswith("-DOCSTART-") or line == "" or line == "\n":
|
152 |
+
if tokens:
|
153 |
+
yield guid, {
|
154 |
+
"id": str(guid),
|
155 |
+
"tokens": tokens,
|
156 |
+
"pos_tags": pos_tags,
|
157 |
+
"chunk_tags": chunk_tags,
|
158 |
+
"ner_tags": ner_tags,
|
159 |
+
}
|
160 |
+
guid += 1
|
161 |
+
tokens = []
|
162 |
+
pos_tags = []
|
163 |
+
chunk_tags = []
|
164 |
+
ner_tags = []
|
165 |
+
else:
|
166 |
+
# conll2003 tokens are space separated
|
167 |
+
splits = line.split(" ")
|
168 |
+
tokens.append(splits[0])
|
169 |
+
pos_tags.append(splits[1])
|
170 |
+
chunk_tags.append(splits[2])
|
171 |
+
ner_tags.append(splits[3].rstrip())
|
172 |
+
# last example
|
173 |
+
if tokens:
|
174 |
+
yield guid, {
|
175 |
+
"id": str(guid),
|
176 |
+
"tokens": tokens,
|
177 |
+
"pos_tags": pos_tags,
|
178 |
+
"chunk_tags": chunk_tags,
|
179 |
+
"ner_tags": ner_tags,
|
180 |
+
}
|
181 |
+
|
182 |
+
|
183 |
+
if __name__ == '__main__':
|
184 |
+
datasets = load_dataset('conll2003')
|