from __future__ import annotations import random from pathlib import Path from typing import Generator from datasets import ( BuilderConfig, DatasetInfo, DownloadManager, Features, GeneratorBasedBuilder, Split, SplitGenerator, Value, Version, ) from datasets.data_files import DataFilesDict _CITATION = "" _DESCRIPTION = "This is a dataset of livedoor news articles." _HOMEPAGE = "https://www.rondhuit.com/download.html#news%20corpus" _LICENSE = "This work is license under CC BY-ND 2.1 JP" _URL = "https://www.rondhuit.com/download/ldcc-20140209.tar.gz" class LivedoorNewsCorpusConfig(BuilderConfig): def __init__( self, name: str = "default", version: Version | str | None = Version("0.0.0"), data_dir: str | None = None, data_files: DataFilesDict | None = None, description: str | None = None, shuffle: bool = True, seed: int = 42, train_ratio: float = 0.8, validation_ratio: float = 0.1, ) -> None: super().__init__( name=name, version=version, data_dir=data_dir, data_files=data_files, description=description, ) self.shuffle = shuffle self.seed = seed self.train_ratio = train_ratio self.validation_ratio = validation_ratio class LivedoorNewsCorpus(GeneratorBasedBuilder): BUILDER_CONFIG_CLASS = LivedoorNewsCorpusConfig BUILDER_CONFIGS = [ LivedoorNewsCorpusConfig( name="livedoor-news-corpus", version=Version("1.1.0"), description=_DESCRIPTION, ), ] def _info(self) -> DatasetInfo: return DatasetInfo( description=_DESCRIPTION, citation=_CITATION, homepage=_HOMEPAGE, license=_LICENSE, features=Features( { "url": Value("string"), "date": Value("string"), "title": Value("string"), "content": Value("string"), "category": Value("string"), } ), ) def _split_generators( self, dl_manager: DownloadManager ) -> list[SplitGenerator]: dataset_dir = Path(dl_manager.download_and_extract(_URL)) data = [] for file_name in sorted(dataset_dir.glob("*/*/*")): if "LICENSE.txt" in str(file_name): continue with open(file_name, "r") as f: d = [line.strip() for line in f] data.append( { "url": d[0], "date": d[1], "title": d[2], "content": " ".join(d[3:]), "category": file_name.parent.name, } ) if self.config.shuffle == True: random.seed(self.config.seed) random.shuffle(data) num_data = len(data) num_train_data = int(num_data * self.config.train_ratio) num_validation_data = int(num_data * self.config.validation_ratio) 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, d