leonadase commited on
Commit
9c55458
·
1 Parent(s): 5a68a39

Upload fdner.py

Browse files
Files changed (1) hide show
  1. fdner.py +150 -0
fdner.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Fault_Detection_Ner Task: Language-Independent Named Entity Recognition",
30
+ author = "Tian Jie",
31
+ year = "2022"
32
+ }
33
+ """
34
+
35
+ _DESCRIPTION = """\
36
+ 用于故障诊断领域相关知识的命名实体识别语料
37
+ """
38
+
39
+ _URL = "https://huggingface.co/datasets/leonadase/NER_fault_detection/tree/main/ner.zip"
40
+ _TRAINING_FILE = "train.txt"
41
+ _DEV_FILE = "valid.txt"
42
+ _TEST_FILE = "test.txt"
43
+
44
+
45
+ class fdNerConfig(datasets.BuilderConfig):
46
+ """BuilderConfig for fdNer"""
47
+
48
+ def __init__(self, **kwargs):
49
+ """BuilderConfig for fdNer.
50
+ Args:
51
+ **kwargs: keyword arguments forwarded to super.
52
+ """
53
+ super(fdNerConfig, self).__init__(**kwargs)
54
+
55
+
56
+ class fdNer(datasets.GeneratorBasedBuilder):
57
+ """fdNer dataset."""
58
+
59
+ BUILDER_CONFIGS = [
60
+ fdNerConfig(name="fdNer", version=datasets.Version("1.0.0"), description="fdNer dataset"),
61
+ ]
62
+
63
+ def _info(self):
64
+ return datasets.DatasetInfo(
65
+ description=_DESCRIPTION,
66
+ features=datasets.Features(
67
+ {
68
+ "id": datasets.Value("string"),
69
+ "tokens": datasets.Sequence(datasets.Value("string")),
70
+ "ner_tags": datasets.Sequence(
71
+ datasets.features.ClassLabel(
72
+ names=[
73
+ "O",
74
+ "B-EN",
75
+ "I-EN",
76
+ "B-STRUC",
77
+ "I-STRUC",
78
+ "B-CHA",
79
+ "I-CHA",
80
+ "B-KIND",
81
+ "I-KIND",
82
+ "B-ADV",
83
+ "I-ADV",
84
+ "B-DISA",
85
+ "I-DISA",
86
+ "B-METH",
87
+ "I-METH",
88
+ "B-NUM",
89
+ "I-NUM",
90
+ "B-PRO",
91
+ "I-PRO",
92
+ "B-THE",
93
+ "I-THE",
94
+ "B-DEF",
95
+ "I-DEF",
96
+ "B-FUC",
97
+ "I-FUC",
98
+ ]
99
+ )
100
+ ),
101
+ }
102
+ ),
103
+ supervised_keys=None,
104
+ # homepage="https://www.aclweb.org/anthology/W03-0419/",
105
+ citation=_CITATION,
106
+ )
107
+
108
+ def _split_generators(self, dl_manager):
109
+ """Returns SplitGenerators."""
110
+ downloaded_file = dl_manager.download_and_extract(_URL)
111
+ data_files = {
112
+ "train": os.path.join(downloaded_file, _TRAINING_FILE),
113
+ "dev": os.path.join(downloaded_file, _DEV_FILE),
114
+ "test": os.path.join(downloaded_file, _TEST_FILE),
115
+ }
116
+
117
+ return [
118
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_files["train"]}),
119
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": data_files["dev"]}),
120
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": data_files["test"]}),
121
+ ]
122
+
123
+ def _generate_examples(self, filepath):
124
+ logger.info("⏳ Generating examples from = %s", filepath)
125
+ with open(filepath, encoding="utf-8") as f:
126
+ guid = 0
127
+ tokens = []
128
+ ner_tags = []
129
+ for line in f:
130
+ if line.startswith("-DOCSTART-") or line == "" or line == "\n":
131
+ if tokens:
132
+ yield guid, {
133
+ "id": str(guid),
134
+ "tokens": tokens,
135
+ "ner_tags": ner_tags,
136
+ }
137
+ guid += 1
138
+ tokens = []
139
+ ner_tags = []
140
+ else:
141
+ # conll2003 tokens are space separated
142
+ splits = line.split(" ")
143
+ tokens.append(splits[0])
144
+ ner_tags.append(splits[1].rstrip())
145
+ # last example
146
+ yield guid, {
147
+ "id": str(guid),
148
+ "tokens": tokens,
149
+ "ner_tags": ner_tags,
150
+ }