|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""The CC-News dataset is based on Common Crawl News Dataset by Sebastian Nagel""" |
|
|
|
import json |
|
import os |
|
import tarfile |
|
from fnmatch import fnmatch |
|
|
|
import datasets |
|
|
|
def custom_iter_archive(path_or_buf, _filter=lambda x: True): |
|
def _iter_archive(f): |
|
stream = tarfile.open(fileobj=f, mode="r|*") |
|
for i, tarinfo in enumerate(stream): |
|
if not _filter(i): |
|
continue |
|
file_path = tarinfo.name |
|
if not tarinfo.isreg(): |
|
continue |
|
if file_path is None: |
|
continue |
|
if os.path.basename(file_path).startswith(".") or os.path.basename(file_path).startswith("__"): |
|
|
|
continue |
|
file_obj = stream.extractfile(tarinfo) |
|
yield file_path, file_obj |
|
stream.members = [] |
|
del stream |
|
|
|
if hasattr(path_or_buf, "read"): |
|
yield from _iter_archive(path_or_buf) |
|
else: |
|
with open(path_or_buf, "rb") as f: |
|
yield from _iter_archive(f) |
|
|
|
logger = datasets.logging.get_logger(__name__) |
|
|
|
|
|
_DESCRIPTION = """\ |
|
CC-News containing news articles from news sites all over the world \ |
|
The data is available on AWS S3 in the Common Crawl bucket at /crawl-data/CC-NEWS/. \ |
|
This version of the dataset has 708241 articles. It represents a small portion of English \ |
|
language subset of the CC-News dataset created using news-please(Hamborg et al.,2017) to \ |
|
collect and extract English language portion of CC-News. |
|
""" |
|
|
|
_CITATION = """\ |
|
@InProceedings{Hamborg2017, |
|
author = {Hamborg, Felix and Meuschke, Norman and Breitinger, Corinna and Gipp, Bela}, |
|
title = {news-please: A Generic News Crawler and Extractor}, |
|
year = {2017}, |
|
booktitle = {Proceedings of the 15th International Symposium of Information Science}, |
|
location = {Berlin}, |
|
doi = {10.5281/zenodo.4120316}, |
|
pages = {218--223}, |
|
month = {March} |
|
} |
|
""" |
|
_PROJECT_URL = "https://commoncrawl.org/2016/10/news-dataset-available/" |
|
_DOWNLOAD_URL = "https://storage.googleapis.com/huggingface-nlp/datasets/cc_news/cc_news.tar.gz" |
|
|
|
|
|
class CCNewsConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for CCNews.""" |
|
|
|
def __init__(self, **kwargs): |
|
"""BuilderConfig for CCNews. |
|
Args: |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(CCNewsConfig, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs) |
|
|
|
|
|
class CCNews(datasets.GeneratorBasedBuilder): |
|
"""CC-News dataset.""" |
|
|
|
BUILDER_CONFIGS = [ |
|
CCNewsConfig( |
|
name="plain_text", |
|
description="Plain text", |
|
) |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"title": datasets.Value("string"), |
|
"text": datasets.Value("string"), |
|
"domain": datasets.Value("string"), |
|
"date": datasets.Value("string"), |
|
"description": datasets.Value("string"), |
|
"url": datasets.Value("string"), |
|
"image_url": datasets.Value("string"), |
|
} |
|
), |
|
supervised_keys=None, |
|
homepage=_PROJECT_URL, |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
archive = dl_manager.download(_DOWNLOAD_URL) |
|
|
|
train_filter = lambda x : (x%10) < 8 |
|
val_filter = lambda x: (x%10) == 8 |
|
test_filter = lambda x: (x%10) == 9 |
|
|
|
return [ |
|
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"files": custom_iter_archive(archive, train_filter)}), |
|
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"files": custom_iter_archive(archive, val_filter)}), |
|
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"files": custom_iter_archive(archive, test_filter)}), |
|
] |
|
|
|
def _generate_examples(self, files): |
|
id_ = 0 |
|
for article_file_path, f in files: |
|
if fnmatch(os.path.basename(article_file_path), "*.json"): |
|
article = json.load(f) |
|
yield id_, { |
|
"title": article["title"].strip() if article["title"] is not None else "", |
|
"text": article["maintext"].strip() if article["maintext"] is not None else "", |
|
"domain": article["source_domain"].strip() if article["source_domain"] is not None else "", |
|
"date": article["date_publish"].strip() if article["date_publish"] is not None else "", |
|
"description": article["description"].strip() if article["description"] is not None else "", |
|
"url": article["url"].strip() if article["url"] is not None else "", |
|
"image_url": article["image_url"].strip() if article["image_url"] is not None else "", |
|
} |
|
id_ += 1 |
|
|
|
|