v2ex / v2ex.py
shjwudp's picture
update
c5e68a0
import json
import gzip
import datasets
from datasets import Value, Sequence
logger = datasets.logging.get_logger(__name__)
_DESCRIPTION = """\
V2EX is a dataset curated by https://www.v2ex.com/ open data.
"""
_HOMEPAGE = "https://huggingface.co/datasets/Dialogue-Model-Research-Group/v2ex"
_DATA_URLS = {
"topic": "v2ex_topic.jsonl.gz",
"replies": "v2ex_replies.jsonl.gz",
"conversation": "v2ex_conversation.jsonl.gz",
"context-response": "v2ex_context_response.jsonl.gz",
}
class V2EXConfig(datasets.BuilderConfig):
"""BuilderConfig for shu."""
def __init__(self, *args, subsets, **kwargs) -> None:
"""BuilderConfig for shu.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(V2EXConfig, self).__init__(
*args,
name="+".join(subsets),
**kwargs
)
self.subsets = subsets
class V2EX(datasets.GeneratorBasedBuilder):
"""V2EX: Chinese book dataset."""
VERSION = datasets.Version("0.1.0")
BUILDER_CONFIG_CLASS = V2EXConfig
BUILDER_CONFIGS = [V2EXConfig(subsets=[subset]) for subset in _DATA_URLS]
DEFAULT_CONFIG_NAME = "topic"
def _info(self):
if self.config.name == "topic":
features = datasets.Features({
"id": Value("int64"),
"title": Value("string"),
"content": Value("string"),
"content_rendered": Value("string"),
"syntax": Value("int64"),
"url": Value("string"),
"replies": Value("int64"),
"last_reply_by": Value("string"),
"created": Value("int64"),
"last_modified": Value("int64"),
"last_touched": Value("int64"),
"member": {
"id": Value("int64"),
"username": Value("string"),
"bio": Value("string"),
"website": Value("string"),
"github": Value("string"),
"url": Value("string"),
"avatar": Value("string"),
"created": Value("int64"),
},
"node": {
"id": Value("int64"),
"url": Value("string"),
"name": Value("string"),
"title": Value("string"),
"header": Value("string"),
"footer": Value("string"),
"avatar": Value("string"),
"topics": Value("int64"),
"created": Value("int64"),
"last_modified": Value("int64"),
},
"supplements": Sequence({
"id": Value("int64"),
"content": Value("string"),
"content_rendered": Value("string"),
"syntax": Value("int64"),
"created": Value("int64"),
}),
})
elif self.config.name == "replies":
features = datasets.Features({
"id": Value(dtype="int64", id=None),
"content": Value(dtype="string", id=None),
"content_rendered": Value(dtype="string", id=None),
"created": Value(dtype="int64", id=None),
"member": {
"id": Value(dtype="int64", id=None),
"username": Value(dtype="string", id=None),
"bio": Value(dtype="string", id=None),
"website": Value(dtype="string", id=None),
"github": Value(dtype="string", id=None),
"url": Value(dtype="string", id=None),
"avatar": Value(dtype="string", id=None),
"created": Value(dtype="int64", id=None)
},
"topic_id": Value(dtype="int64", id=None),
})
elif self.config.name == "conversation":
features = datasets.Features({
"conversation": Sequence({
"username": Value("string"),
"post": Value("string"),
}),
"url": Value(dtype="string"),
})
elif self.config.name == "context-response":
features = datasets.Features({
"context": Value("string"),
"response": Value("string"),
})
else:
raise NotImplementedError(
"This dataset is not implemented."
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
)
def _split_generators(self, dl_manager):
data_urls = {subset: _DATA_URLS[subset] for subset in self.config.subsets}
archive = dl_manager.download(data_urls)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
gen_kwargs={
"files": {
subset: archive[subset] for subset in self.config.subsets
},
},
),
]
def _generate_examples(self, files):
key = 0
for subset in files:
filepath = files[subset]
for line in gzip.open(filepath, "rt", encoding="utf-8"):
j = json.loads(line)
yield key, j
key += 1