|
import json |
|
from typing import Generator |
|
|
|
from datasets import ( |
|
BuilderConfig, |
|
DatasetInfo, |
|
DownloadManager, |
|
Features, |
|
GeneratorBasedBuilder, |
|
Sequence, |
|
Split, |
|
SplitGenerator, |
|
Value, |
|
Version, |
|
) |
|
|
|
_CITATION = "" |
|
_DESCRIPTION = "This is a dataset of Wikinews articles manually labeled with the named entity label." |
|
_HOMEPAGE = "https://ja.wikinews.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8" |
|
_LICENSE = "This work is licensed under CC BY 2.5" |
|
_URL = "https://huggingface.co/datasets/llm-book/ner-wikinews-dataset/raw/main/annotated_wikinews.json" |
|
|
|
|
|
class NerWikinewsDataset(GeneratorBasedBuilder): |
|
BUILDER_CONFIGS = [ |
|
BuilderConfig( |
|
name="new-wikinews-dataset", |
|
version=Version("1.0.0"), |
|
description=_DESCRIPTION, |
|
), |
|
] |
|
|
|
def _info(self): |
|
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 _convert_data_format( |
|
self, annotated_data: list[dict[str, any]] |
|
) -> list[dict[str, any]]: |
|
outputs = [] |
|
for data in annotated_data: |
|
if data["annotations"] == []: |
|
continue |
|
entities = [] |
|
for annotations in data["annotations"]: |
|
for result in annotations["result"]: |
|
entities.append( |
|
{ |
|
"name": result["value"]["text"], |
|
"span": [ |
|
result["value"]["start"], |
|
result["value"]["end"], |
|
], |
|
"type": result["value"]["labels"][0], |
|
} |
|
) |
|
if entities != []: |
|
entities = sorted(entities, key=lambda x: x["span"][0]) |
|
outputs.append( |
|
{ |
|
"curid": data["id"], |
|
"text": data["data"]["text"], |
|
"entities": entities, |
|
} |
|
) |
|
return outputs |
|
|
|
def _split_generators( |
|
self, dl_manager: DownloadManager |
|
) -> list[SplitGenerator]: |
|
data_file = dl_manager.download_and_extract(_URL) |
|
with open(data_file, "r") as f: |
|
data = json.load(f) |
|
data = self._convert_data_format(data) |
|
return [ |
|
SplitGenerator( |
|
name=Split.TEST, |
|
gen_kwargs={"data": data}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, data: list[dict[str, str]]) -> Generator: |
|
for key, d in enumerate(data): |
|
yield key, { |
|
"curid": d["curid"], |
|
"text": d["text"], |
|
"entities": d["entities"], |
|
} |
|
|