Datasets:

Languages:
Japanese
Size:
n<1K
Tags:
news
License:
ner-wikinews-dataset / ner-wikinews-dataset.py
Kosuke-Yamada
modify file
e39dd70
raw
history blame
3.22 kB
import json
import datasets
_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(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
datasets.BuilderConfig(
name="new-wikinews-dataset",
version=datasets.Version("1.1.0"),
description=_DESCRIPTION,
),
]
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"curid": datasets.Value("string"),
"text": datasets.Value("string"),
"entities": [
{
"name": datasets.Value(dtype="string"),
"span": datasets.Sequence(
datasets.Value(dtype="int64"), length=2
),
"type": datasets.Value(dtype="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):
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 [
datasets.SplitGenerator(
name=datasets.Split.TEST,
gen_kwargs={"data": data},
),
]
def _generate_examples(self, data):
for key, data in enumerate(data):
yield key, {
"curid": data["curid"],
"text": data["text"],
"entities": data["entities"],
}