alex1qaz commited on
Commit
d5b4aff
·
1 Parent(s): 7c3e7fc

Upload conll2003.py

Browse files

extract zip file from local

Files changed (1) hide show
  1. conll2003.py +246 -0
conll2003.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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 = "https://data.deepai.org/conll2003.zip"
56
+ _LOCAL_PATH = "conll2003.zip"
57
+ _TRAINING_FILE = "train.txt"
58
+ _DEV_FILE = "valid.txt"
59
+ _TEST_FILE = "test.txt"
60
+
61
+
62
+ class Conll2003Config(datasets.BuilderConfig):
63
+ """BuilderConfig for Conll2003"""
64
+
65
+ def __init__(self, **kwargs):
66
+ """BuilderConfig forConll2003.
67
+
68
+ Args:
69
+ **kwargs: keyword arguments forwarded to super.
70
+ """
71
+ super(Conll2003Config, self).__init__(**kwargs)
72
+
73
+
74
+ class Conll2003(datasets.GeneratorBasedBuilder):
75
+ """Conll2003 dataset."""
76
+
77
+ BUILDER_CONFIGS = [
78
+ Conll2003Config(name="conll2003", version=datasets.Version("1.0.0"), description="Conll2003 dataset"),
79
+ ]
80
+
81
+ def _info(self):
82
+ return datasets.DatasetInfo(
83
+ description=_DESCRIPTION,
84
+ features=datasets.Features(
85
+ {
86
+ "id": datasets.Value("string"),
87
+ "tokens": datasets.Sequence(datasets.Value("string")),
88
+ "pos_tags": datasets.Sequence(
89
+ datasets.features.ClassLabel(
90
+ names=[
91
+ '"',
92
+ "''",
93
+ "#",
94
+ "$",
95
+ "(",
96
+ ")",
97
+ ",",
98
+ ".",
99
+ ":",
100
+ "``",
101
+ "CC",
102
+ "CD",
103
+ "DT",
104
+ "EX",
105
+ "FW",
106
+ "IN",
107
+ "JJ",
108
+ "JJR",
109
+ "JJS",
110
+ "LS",
111
+ "MD",
112
+ "NN",
113
+ "NNP",
114
+ "NNPS",
115
+ "NNS",
116
+ "NN|SYM",
117
+ "PDT",
118
+ "POS",
119
+ "PRP",
120
+ "PRP$",
121
+ "RB",
122
+ "RBR",
123
+ "RBS",
124
+ "RP",
125
+ "SYM",
126
+ "TO",
127
+ "UH",
128
+ "VB",
129
+ "VBD",
130
+ "VBG",
131
+ "VBN",
132
+ "VBP",
133
+ "VBZ",
134
+ "WDT",
135
+ "WP",
136
+ "WP$",
137
+ "WRB",
138
+ ]
139
+ )
140
+ ),
141
+ "chunk_tags": datasets.Sequence(
142
+ datasets.features.ClassLabel(
143
+ names=[
144
+ "O",
145
+ "B-ADJP",
146
+ "I-ADJP",
147
+ "B-ADVP",
148
+ "I-ADVP",
149
+ "B-CONJP",
150
+ "I-CONJP",
151
+ "B-INTJ",
152
+ "I-INTJ",
153
+ "B-LST",
154
+ "I-LST",
155
+ "B-NP",
156
+ "I-NP",
157
+ "B-PP",
158
+ "I-PP",
159
+ "B-PRT",
160
+ "I-PRT",
161
+ "B-SBAR",
162
+ "I-SBAR",
163
+ "B-UCP",
164
+ "I-UCP",
165
+ "B-VP",
166
+ "I-VP",
167
+ ]
168
+ )
169
+ ),
170
+ "ner_tags": datasets.Sequence(
171
+ datasets.features.ClassLabel(
172
+ names=[
173
+ "O",
174
+ "B-PER",
175
+ "I-PER",
176
+ "B-ORG",
177
+ "I-ORG",
178
+ "B-LOC",
179
+ "I-LOC",
180
+ "B-MISC",
181
+ "I-MISC",
182
+ ]
183
+ )
184
+ ),
185
+ }
186
+ ),
187
+ supervised_keys=None,
188
+ homepage="https://www.aclweb.org/anthology/W03-0419/",
189
+ citation=_CITATION,
190
+ )
191
+
192
+ def _split_generators(self, dl_manager):
193
+ """Returns SplitGenerators."""
194
+ # downloaded_file = dl_manager.download_and_extract(_URL)
195
+ downloaded_file = dl_manager.extract(_LOCAL_PATH)
196
+ data_files = {
197
+ "train": os.path.join(downloaded_file, _TRAINING_FILE),
198
+ "dev": os.path.join(downloaded_file, _DEV_FILE),
199
+ "test": os.path.join(downloaded_file, _TEST_FILE),
200
+ }
201
+
202
+ return [
203
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_files["train"]}),
204
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_files["dev"]}),
205
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_files["test"]}),
206
+ ]
207
+
208
+ def _generate_examples(self, filepath):
209
+ logger.info("⏳ Generating examples from = %s", filepath)
210
+ with open(filepath, encoding="utf-8") as f:
211
+ guid = 0
212
+ tokens = []
213
+ pos_tags = []
214
+ chunk_tags = []
215
+ ner_tags = []
216
+ for line in f:
217
+ if line.startswith("-DOCSTART-") or line == "" or line == "\n":
218
+ if tokens:
219
+ yield guid, {
220
+ "id": str(guid),
221
+ "tokens": tokens,
222
+ "pos_tags": pos_tags,
223
+ "chunk_tags": chunk_tags,
224
+ "ner_tags": ner_tags,
225
+ }
226
+ guid += 1
227
+ tokens = []
228
+ pos_tags = []
229
+ chunk_tags = []
230
+ ner_tags = []
231
+ else:
232
+ # conll2003 tokens are space separated
233
+ splits = line.split(" ")
234
+ tokens.append(splits[0])
235
+ pos_tags.append(splits[1])
236
+ chunk_tags.append(splits[2])
237
+ ner_tags.append(splits[3].rstrip())
238
+ # last example
239
+ if tokens:
240
+ yield guid, {
241
+ "id": str(guid),
242
+ "tokens": tokens,
243
+ "pos_tags": pos_tags,
244
+ "chunk_tags": chunk_tags,
245
+ "ner_tags": ner_tags,
246
+ }