|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""Sogou News""" |
|
|
|
|
|
import csv |
|
import ctypes |
|
|
|
import datasets |
|
|
|
|
|
csv.field_size_limit(int(ctypes.c_ulong(-1).value // 2)) |
|
|
|
|
|
_CITATION = """\ |
|
@misc{zhang2015characterlevel, |
|
title={Character-level Convolutional Networks for Text Classification}, |
|
author={Xiang Zhang and Junbo Zhao and Yann LeCun}, |
|
year={2015}, |
|
eprint={1509.01626}, |
|
archivePrefix={arXiv}, |
|
primaryClass={cs.LG} |
|
} |
|
""" |
|
|
|
_DESCRIPTION = """\ |
|
The Sogou News dataset is a mixture of 2,909,551 news articles from the SogouCA and SogouCS news corpora, in 5 categories. |
|
The number of training samples selected for each class is 90,000 and testing 12,000. Note that the Chinese characters have been converted to Pinyin. |
|
classification labels of the news are determined by their domain names in the URL. For example, the news with |
|
URL http://sports.sohu.com is categorized as a sport class. |
|
""" |
|
|
|
_DATA_URL = "https://s3.amazonaws.com/fast-ai-nlp/sogou_news_csv.tgz" |
|
|
|
|
|
class Sogou_News(datasets.GeneratorBasedBuilder): |
|
"""Sogou News dataset""" |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"title": datasets.Value("string"), |
|
"content": datasets.Value("string"), |
|
"label": datasets.features.ClassLabel( |
|
names=["sports", "finance", "entertainment", "automobile", "technology"] |
|
), |
|
} |
|
), |
|
|
|
|
|
supervised_keys=None, |
|
homepage="", |
|
citation=_CITATION, |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
archive = dl_manager.download(_DATA_URL) |
|
|
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={"filepath": "sogou_news_csv/test.csv", "files": dl_manager.iter_archive(archive)}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={"filepath": "sogou_news_csv/train.csv", "files": dl_manager.iter_archive(archive)}, |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath, files): |
|
"""This function returns the examples in the raw (text) form.""" |
|
for path, f in files: |
|
if path == filepath: |
|
lines = (line.decode("utf-8") for line in f) |
|
data = csv.reader(lines) |
|
for id_, row in enumerate(data): |
|
yield id_, {"title": row[1], "content": row[2], "label": int(row[0]) - 1} |
|
break |
|
|