Kosuke-Yamada
commited on
Commit
•
d07ecd5
1
Parent(s):
ca19cf5
add a code to acquire dataset
Browse files- README.md +21 -0
- ner-wikipedia-dataset.py +180 -0
README.md
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
annotations_creators:
|
3 |
+
- crowdsourced
|
4 |
+
language:
|
5 |
+
- ja
|
6 |
+
language_creators:
|
7 |
+
- crowdsourced
|
8 |
+
license: []
|
9 |
+
multilinguality:
|
10 |
+
- monolingual
|
11 |
+
pretty_name: ner-wikipedia-dataset
|
12 |
+
size_categories:
|
13 |
+
- 1K<n<10K
|
14 |
+
source_datasets:
|
15 |
+
- original
|
16 |
+
tags:
|
17 |
+
- NER
|
18 |
+
task_categories:
|
19 |
+
- token-classification
|
20 |
+
task_ids: []
|
21 |
+
---
|
ner-wikipedia-dataset.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
|
2 |
+
#
|
3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4 |
+
# you may not use this file except in compliance with the License.
|
5 |
+
# You may obtain a copy of the License at
|
6 |
+
#
|
7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8 |
+
#
|
9 |
+
# Unless required by applicable law or agreed to in writing, software
|
10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12 |
+
# See the License for the specific language governing permissions and
|
13 |
+
# limitations under the License.
|
14 |
+
# TODO: Address all TODOs and remove all explanatory comments
|
15 |
+
"""TODO: Add a description here."""
|
16 |
+
|
17 |
+
import csv
|
18 |
+
import json
|
19 |
+
import os
|
20 |
+
import random
|
21 |
+
|
22 |
+
import datasets
|
23 |
+
|
24 |
+
# TODO: Add BibTeX citation
|
25 |
+
# Find for instance the citation on arxiv or on the dataset repo/website
|
26 |
+
_CITATION = """\
|
27 |
+
@InProceedings{huggingface:dataset,
|
28 |
+
title = {A great new dataset},
|
29 |
+
author={huggingface, Inc.
|
30 |
+
},
|
31 |
+
year={2020}
|
32 |
+
}
|
33 |
+
"""
|
34 |
+
|
35 |
+
# TODO: Add description of the dataset here
|
36 |
+
# You can copy an official description
|
37 |
+
_DESCRIPTION = """\
|
38 |
+
This new dataset is designed to solve this great NLP task and is crafted with a lot of care.
|
39 |
+
"""
|
40 |
+
|
41 |
+
# TODO: Add a link to an official homepage for the dataset here
|
42 |
+
_HOMEPAGE = ""
|
43 |
+
|
44 |
+
# TODO: Add the licence for the dataset here if you can find it
|
45 |
+
_LICENSE = ""
|
46 |
+
|
47 |
+
# TODO: Add link to the official dataset URLs here
|
48 |
+
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
|
49 |
+
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
|
50 |
+
_URL = {
|
51 |
+
"all": "https://raw.githubusercontent.com/stockmarkteam/ner-wikipedia-dataset/main/ner.json",
|
52 |
+
}
|
53 |
+
|
54 |
+
|
55 |
+
class NerWikipediaDatasetConfig(datasets.BuilderConfig):
|
56 |
+
"""BuilderConfig for MS MARCO."""
|
57 |
+
|
58 |
+
def __init__(self, **kwargs):
|
59 |
+
"""BuilderConfig for MS MARCO
|
60 |
+
Args:
|
61 |
+
**kwargs: keyword arguments forwarded to super.
|
62 |
+
"""
|
63 |
+
super(NerWikipediaDatasetConfig, self).__init__(**kwargs)
|
64 |
+
|
65 |
+
|
66 |
+
# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
|
67 |
+
class NerWikipediaDataset(datasets.GeneratorBasedBuilder):
|
68 |
+
"""TODO: Short description of my dataset."""
|
69 |
+
|
70 |
+
VERSION = datasets.Version("1.1.0")
|
71 |
+
|
72 |
+
# This is an example of a dataset with multiple configurations.
|
73 |
+
# If you don't want/need to define several sub-sets in your dataset,
|
74 |
+
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
75 |
+
|
76 |
+
# If you need to make complex sub-parts in the datasets with configurable options
|
77 |
+
# You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
|
78 |
+
# BUILDER_CONFIG_CLASS = MyBuilderConfig
|
79 |
+
|
80 |
+
# You will be able to load one or the other configurations in the following list with
|
81 |
+
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
82 |
+
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
83 |
+
BUILDER_CONFIGS = [
|
84 |
+
datasets.BuilderConfig(
|
85 |
+
name="all",
|
86 |
+
version=VERSION,
|
87 |
+
description="This part of my dataset covers a first domain",
|
88 |
+
),
|
89 |
+
]
|
90 |
+
|
91 |
+
DEFAULT_CONFIG_NAME = "all" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
92 |
+
|
93 |
+
def _info(self):
|
94 |
+
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
95 |
+
return datasets.DatasetInfo(
|
96 |
+
# This is the description that will appear on the datasets page.
|
97 |
+
description=_DESCRIPTION,
|
98 |
+
# This defines the different columns of the dataset and their types
|
99 |
+
features=datasets.Features(
|
100 |
+
{
|
101 |
+
"curid": datasets.Value("int32"),
|
102 |
+
"text": datasets.Value("string"),
|
103 |
+
"entities": datasets.Sequence(
|
104 |
+
feature={
|
105 |
+
"name": datasets.Value(dtype="string"),
|
106 |
+
"span": datasets.Sequence(
|
107 |
+
feature=datasets.Value(dtype="int32"), length=2
|
108 |
+
),
|
109 |
+
"type": datasets.Value(dtype="string"),
|
110 |
+
},
|
111 |
+
)
|
112 |
+
# These are the features of your dataset like images, labels ...
|
113 |
+
}
|
114 |
+
), # Here we define them above because they are different between the two configurations
|
115 |
+
# If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
|
116 |
+
# specify them. They'll be used if as_supervised=True in builder.as_dataset.
|
117 |
+
# supervised_keys=("sentence", "label"),
|
118 |
+
# Homepage of the dataset for documentation
|
119 |
+
homepage=_HOMEPAGE,
|
120 |
+
# License for the dataset if available
|
121 |
+
license=_LICENSE,
|
122 |
+
# Citation for the dataset
|
123 |
+
citation=_CITATION,
|
124 |
+
)
|
125 |
+
|
126 |
+
def _split_generators(self, dl_manager):
|
127 |
+
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
128 |
+
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
129 |
+
|
130 |
+
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
131 |
+
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
132 |
+
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
133 |
+
data_dir = dl_manager.download_and_extract(_URL)
|
134 |
+
|
135 |
+
# ダウンロードしたファイルを読み込み、全てのデータを取得
|
136 |
+
with open(data_dir, "r", encoding="utf-8") as f:
|
137 |
+
data = json.load(f)
|
138 |
+
|
139 |
+
# データをランダムにシャッフルする
|
140 |
+
random.seed(42)
|
141 |
+
random.shuffle(data)
|
142 |
+
|
143 |
+
# 学習データ、開発データ、テストデータに分割する
|
144 |
+
train_ratio = 0.8
|
145 |
+
validation_ratio = 0.1
|
146 |
+
num_examples = len(data)
|
147 |
+
train_split = int(num_examples * train_ratio)
|
148 |
+
validation_split = int(num_examples * (train_ratio + validation_ratio))
|
149 |
+
train_data = data[:train_split]
|
150 |
+
validation_data = data[train_split:validation_split]
|
151 |
+
test_data = data[validation_split:]
|
152 |
+
|
153 |
+
return [
|
154 |
+
datasets.SplitGenerator(
|
155 |
+
name=datasets.Split.ALL, gen_kwargs={"data": data, "split": "all"}
|
156 |
+
),
|
157 |
+
datasets.SplitGenerator(
|
158 |
+
name=datasets.Split.TRAIN,
|
159 |
+
gen_kwargs={"data": train_data, "split": "train"},
|
160 |
+
),
|
161 |
+
datasets.SplitGenerator(
|
162 |
+
name=datasets.Split.VALIDATION,
|
163 |
+
gen_kwargs={"data": validation_data, "split": "validation"},
|
164 |
+
),
|
165 |
+
datasets.SplitGenerator(
|
166 |
+
name=datasets.Split.TEST,
|
167 |
+
gen_kwargs={"data": test_data, "split": "test"},
|
168 |
+
),
|
169 |
+
]
|
170 |
+
|
171 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
172 |
+
def _generate_examples(self, data, split):
|
173 |
+
# TODO: This method handles input defined in _split_generators to yield (key, example) tuples from the dataset.
|
174 |
+
# The `key` is for legacy reasons (tfds) and is not important in itself, but must be unique for each example.
|
175 |
+
for key, data in enumerate(data):
|
176 |
+
yield key, {
|
177 |
+
"curid": data["curid"],
|
178 |
+
"text": data["text"],
|
179 |
+
"entities": "" if split == "test" else data["entities"],
|
180 |
+
}
|