hacdc-wikipedia / hacdc-wikipedia.py
gabrielaltay's picture
Update hacdc-wikipedia.py
77a2397
raw
history blame
8.44 kB
import gzip
import json
import os
import datasets
from datasets import Features, Sequence, Value
logger = datasets.logging.get_logger(__name__)
_FILES = [
'kwnlp-enwiki-20220901-link-annotated-text1-p1p41242.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text2-p41243p151573.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text3-p151574p311329.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text4-p311330p558391.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text5-p558392p958045.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text6-p958046p1483661.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text7-p1483662p2134111.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text8-p2134112p2936260.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text9-p2936261p4045402.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text10-p4045403p5399366.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text11-p5399367p6899366.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text11-p6899367p7054859.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text12-p7054860p8554859.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text12-p8554860p9172788.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text13-p10672789p11659682.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text13-p9172789p10672788.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text14-p11659683p13159682.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text14-p13159683p14324602.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text15-p14324603p15824602.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text15-p15824603p17324602.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text15-p17324603p17460152.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text16-p17460153p18960152.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text16-p18960153p20460152.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text16-p20460153p20570392.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text17-p20570393p22070392.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text17-p22070393p23570392.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text17-p23570393p23716197.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text18-p23716198p25216197.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text18-p25216198p26716197.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text18-p26716198p27121850.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text19-p27121851p28621850.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text19-p28621851p30121850.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text19-p30121851p31308442.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text20-p31308443p32808442.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text20-p32808443p34308442.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text20-p34308443p35522432.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text21-p35522433p37022432.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text21-p37022433p38522432.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text21-p38522433p39996245.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text22-p39996246p41496245.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text22-p41496246p42996245.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text22-p42996246p44496245.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text22-p44496246p44788941.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text23-p44788942p46288941.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text23-p46288942p47788941.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text23-p47788942p49288941.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text23-p49288942p50564553.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text24-p50564554p52064553.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text24-p52064554p53564553.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text24-p53564554p55064553.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text24-p55064554p56564553.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text24-p56564554p57025655.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text25-p57025656p58525655.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text25-p58525656p60025655.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text25-p60025656p61525655.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text25-p61525656p62585850.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text26-p62585851p63975909.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text27-p63975910p65475909.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text27-p65475910p66975909.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text27-p66975910p68475909.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text27-p68475910p69975909.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text27-p69975910p71475909.jsonl.gz',
'kwnlp-enwiki-20220901-link-annotated-text27-p71475910p71655058.jsonl.gz',
]
_VERSION = datasets.Version("1.0.0")
_DESCRIPTION = "Wikipedia text with metadata"
FEATURES = {
"all": Features(
{
"page_id": Value("int64"),
"page_title": Value("string"),
"paragraphs": [
{
"plaintext": Value("string"),
"section_idx": Value("int64"),
"section_name": Value("string"),
"target_page_ids": Sequence(Value("int64")),
"anchor_spans": Sequence(Sequence(Value("int64"), length=2)),
}
],
"revision_id": Value("int64"),
"categories": Sequence(Value("string")),
"item_id": Value("int64"),
"in_link_count": Value("int64"),
"out_link_count": Value("int64"),
"tmpl_good_article": Value("bool"),
"tmpl_featured_article": Value("bool"),
"item_label": Value("string"),
"item_description": Value("string"),
"item_alias": Sequence(Value("string")),
}
),
"text": Features(
{
"paragraph": Value("string")
}
),
"text-intros": Features(
{
"paragraph": Value("string")
}
),
}
class HacdcWikimedia20220901(datasets.GeneratorBasedBuilder):
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="all", version=_VERSION, description=_DESCRIPTION),
datasets.BuilderConfig(name="text", version=_VERSION, description=_DESCRIPTION),
datasets.BuilderConfig(
name="text-intros", version=_VERSION, description=_DESCRIPTION
),
]
DEFAULT_CONFIG_NAME = "all"
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=FEATURES[self.config.name],
)
def _split_generators(self, dl_manager):
paths = [os.path.join("data", _FILES[0])]
downloaded_files = dl_manager.download_and_extract(paths)
print("PATHS", paths)
print("DLFILES", downloaded_files)
return [
datasets.SplitGenerator(name="every", gen_kwargs={"filepaths": downloaded_files}),
]
def _generate_examples(self, filepaths):
"""Generate examples.
Args:
filepaths: a list of file paths
Yields:
dictionaries representing article data and metadata
"""
_id = 0
for filepath in filepaths:
logger.info("generating examples from = %s", filepath)
print("FP", filepath)
with gzip.open(filepath, "r") as fp:
for line in fp:
data = json.loads(line.decode("utf-8"))
if self.config.name == "all":
yield _id, data
_id += 1
continue
for paragraph in data["paragraphs"]:
if self.config.name == "text":
yield _id, {"paragraph": paragraph["plaintext"]}
_id += 1
if self.config.name == "text-intros":
if paragraph["section_idx"] == 0:
yield _id, {"paragraph": paragraph["plaintext"]}
_id += 1