Sanjib Narzary commited on
Commit
98345ba
·
1 Parent(s): 9aed422

initial commit

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