|
import json |
|
import random |
|
from typing import Generator |
|
|
|
from datasets import ( |
|
BuilderConfig, |
|
DatasetInfo, |
|
DownloadManager, |
|
Features, |
|
GeneratorBasedBuilder, |
|
Sequence, |
|
Split, |
|
SplitGenerator, |
|
Value, |
|
Version, |
|
) |
|
|
|
_CITATION = """ |
|
@inproceedings{omi-2021-wikipedia, |
|
title = "Wikipediaを用いた日本語の固有表現抽出のデータセットの構築", |
|
author = "近江 崇宏", |
|
booktitle = "言語処理学会第27回年次大会", |
|
year = "2021", |
|
url = "https://anlp.jp/proceedings/annual_meeting/2021/pdf_dir/P2-7.pdf", |
|
} |
|
""" |
|
_DESCRIPTION = "This is a dataset of Wikipedia articles with named entity labels created by Stockmark Inc." |
|
_HOMEPAGE = "https://github.com/stockmarkteam/ner-wikipedia-dataset" |
|
_LICENSE = "CC-BY-SA 3.0" |
|
_URL = "https://raw.githubusercontent.com/stockmarkteam/ner-wikipedia-dataset/main/ner.json" |
|
|
|
|
|
class NerWikipediaDataset(GeneratorBasedBuilder): |
|
BUILDER_CONFIGS = [ |
|
BuilderConfig( |
|
name="ner-wikipedia-dataset", |
|
version=Version("2.0.0"), |
|
description=_DESCRIPTION, |
|
), |
|
] |
|
|
|
def _info(self) -> DatasetInfo: |
|
return DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=Features( |
|
{ |
|
"curid": Value("string"), |
|
"text": Value("string"), |
|
"entities": [ |
|
{ |
|
"name": Value("string"), |
|
"span": Sequence(Value("int64"), length=2), |
|
"type": Value("string"), |
|
} |
|
], |
|
} |
|
), |
|
homepage=_HOMEPAGE, |
|
license=_LICENSE, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators( |
|
self, dl_manager: DownloadManager |
|
) -> list[SplitGenerator]: |
|
dataset_dir = str(dl_manager.download_and_extract(_URL)) |
|
with open(dataset_dir, "r", encoding="utf-8") as f: |
|
data = json.load(f) |
|
|
|
random.seed(42) |
|
random.shuffle(data) |
|
|
|
num_data = len(data) |
|
num_train_data = int(num_data * 0.8) |
|
num_validation_data = (num_data - num_train_data) // 2 |
|
train_data = data[:num_train_data] |
|
validation_data = data[ |
|
num_train_data : num_train_data + num_validation_data |
|
] |
|
test_data = data[num_train_data + num_validation_data :] |
|
return [ |
|
SplitGenerator( |
|
name=Split.TRAIN, |
|
gen_kwargs={"data": train_data}, |
|
), |
|
SplitGenerator( |
|
name=Split.VALIDATION, |
|
gen_kwargs={"data": validation_data}, |
|
), |
|
SplitGenerator( |
|
name=Split.TEST, |
|
gen_kwargs={"data": test_data}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, data: list[dict[str, str]]) -> Generator: |
|
for i, d in enumerate(data): |
|
yield i, { |
|
"curid": d["curid"], |
|
"text": d["text"], |
|
"entities": d["entities"], |
|
} |
|
|