#!/usr/bin/python3 # -*- coding: utf-8 -*- from glob import glob import os from pathlib import Path import datasets import pandas as pd import requests _METADATA_URL = "metadata.csv" _CITATION = """\ @dataset{h_novel, author = {Xing Tian}, title = {h_novel}, month = aug, year = 2023, publisher = {Xing Tian}, version = {1.0}, } """ _DESCRIPTION = """\ This dataset contains some SQ novel. It is supposed to be used for text generation tasks. """ class HNovel(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("1.0.0") BUILDER_CONFIGS = [ datasets.BuilderConfig(name="ltxsba", version=VERSION, description="ltxsba"), datasets.BuilderConfig(name="ltxsba_1gb", version=VERSION, description="ltxsba_1gb"), datasets.BuilderConfig(name="ltxsba_5gb", version=VERSION, description="ltxsba_5gb"), datasets.BuilderConfig(name="ltxsba_100m", version=VERSION, description="ltxsba_100m"), datasets.BuilderConfig(name="ltxsba_500m", version=VERSION, description="ltxsba_500m"), datasets.BuilderConfig(name="yazhou", version=VERSION, description="yazhou"), datasets.BuilderConfig(name="yazhou_5m", version=VERSION, description="yazhou_5m"), datasets.BuilderConfig(name="yazhou_10m", version=VERSION, description="yazhou_10m"), datasets.BuilderConfig(name="yazhou_20m", version=VERSION, description="yazhou_20m"), datasets.BuilderConfig(name="yazhou_50m", version=VERSION, description="yazhou_50m"), datasets.BuilderConfig(name="yazhou_70m", version=VERSION, description="yazhou_70m"), datasets.BuilderConfig(name="all", version=VERSION, description="all"), ] def _info(self): features = datasets.Features( { "source": datasets.Value("string"), "idx": datasets.Value("string"), "filename": datasets.Value("string"), "novel_name": datasets.Value("string"), "row_idx": datasets.Value("string"), "text": datasets.Value("string"), } ) return datasets.DatasetInfo( description=_DESCRIPTION, features=features, supervised_keys=None, homepage="", license="", citation=_CITATION, ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" dl_path = dl_manager.download(_METADATA_URL) archive_path = dl_path return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={"archive_path": archive_path, "dl_manager": dl_manager}, ), ] def _generate_examples(self, archive_path, dl_manager): """Yields examples.""" sample_idx = 0 df = pd.read_csv(archive_path) for i, row in df.iterrows(): source = row["source"] filename = row["filename"] if self.config.name != "all" and source != self.config.name: continue try: filename = dl_manager.download(filename) filename = Path(filename) name = filename.stem splits = name.split("_") idx = splits[-1] novel_name = "_".join(splits[:-1]) row_idx = 1 with open(filename.as_posix(), "r", encoding="utf-8") as f: for txt_row in f: txt_row = str(txt_row).strip() if len(txt_row) == 0: continue yield sample_idx, { "source": source, "idx": idx, "filename": "/".join(filename.parts[-3:]), "novel_name": novel_name, "row_idx": row_idx, "text": txt_row, } row_idx += 1 sample_idx += 1 except Exception: continue if __name__ == '__main__': pass