|
"""Universal Text Classification Dataset (UTCD)""" |
|
|
|
|
|
import os |
|
import json |
|
from os.path import join as os_join |
|
from typing import List |
|
|
|
import datasets |
|
|
|
from stefutil import * |
|
|
|
|
|
_DESCRIPTION = """ |
|
UTCD is a compilation of 18 classification datasets spanning 3 categories of Sentiment, |
|
Intent/Dialogue and Topic classification. UTCD focuses on the task of zero-shot text classification where the |
|
candidate labels are descriptive of the text being classified. UTCD consists of ~ 6M/800K train/test examples. |
|
""" |
|
|
|
|
|
|
|
_URL = "https://github.com/ChrisIsKing/zero-shot-text-classification/tree/master" |
|
_URL_ZIP = "https://huggingface.co/datasets/claritylab/UTCD/raw/main/datasets.zip" |
|
|
|
_VERSION = datasets.Version('0.0.1') |
|
|
|
|
|
class UtcdConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for SuperGLUE.""" |
|
|
|
def __init__(self, domain: str, normalize_aspect: bool = False, **kwargs): |
|
"""BuilderConfig for UTCD. |
|
Args: |
|
domain: `string`, dataset domain, one of [`in`, `out`]. |
|
normalize_aspect: `bool`, if True, an aspect-normalized version of the dataset is returned. |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
|
|
|
|
super(UtcdConfig, self).__init__(version=_VERSION, **kwargs) |
|
ca.check_mismatch('Dataset Domain', domain, ['in', 'out']) |
|
self.domain = domain |
|
self.normalize_aspect = normalize_aspect |
|
|
|
def to_dir_name(self): |
|
""" |
|
:return: directory name for the dataset files for this config stored on hub |
|
""" |
|
domain_str = 'in-domain' if self.domain == 'in' else 'out-of-domain' |
|
prefix = 'aspect-normalized-' if self.normalize_aspect else '' |
|
return f'{prefix}{domain_str}' |
|
|
|
|
|
config = StefConfig('config.json') |
|
|
|
|
|
|
|
class Utcd(datasets.GeneratorBasedBuilder): |
|
"""UTCD: Universal Text Classification Dataset. Version 0.0.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VERSION = _VERSION |
|
|
|
BUILDER_CONFIGS = [ |
|
UtcdConfig( |
|
name='in-domain', |
|
description='All in-domain datasets.', |
|
domain='in', |
|
normalize_aspect=False |
|
), |
|
UtcdConfig( |
|
name='aspect-normalized-in-domain', |
|
description='Aspect-normalized version of all in-domain datasets.', |
|
domain='in', |
|
normalize_aspect=True |
|
), |
|
UtcdConfig( |
|
name='out-of-domain', |
|
description='All out-of-domain datasets.', |
|
domain='out', |
|
normalize_aspect=False |
|
), |
|
UtcdConfig( |
|
name='aspect-normalized-out-of-domain', |
|
description='Aspect-normalized version of all out-of-domain datasets.', |
|
domain='out', |
|
normalize_aspect=True |
|
) |
|
] |
|
|
|
def _get_dataset_names(self): |
|
return [dnm for dnm, d_dset in config().items() if d_dset['domain'] == self.config.domain] |
|
|
|
def _info(self): |
|
dnms = self._get_dataset_names() |
|
labels = [config(f'{dnm}.splits.{split}.labels') for dnm in dnms for split in ['train', 'test']] |
|
mic(dnms, labels) |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
text=datasets.Value(dtype='string'), labels=labels, dataset_name=datasets.ClassLabel(names=dnms) |
|
), |
|
homepage=_URL |
|
|
|
) |
|
|
|
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: |
|
|
|
splits = ['train', 'eval', 'test'] if self.config.normalize_aspect else ['train', 'test'] |
|
dnms = self._get_dataset_names() |
|
dir_nm = self.config.to_dir_name() |
|
split2paths = {s: [os_join(dir_nm, dnm, s) for dnm in dnms] for s in splits} |
|
mic(split2paths) |
|
|
|
downloaded_files = dl_manager.download_and_extract('datasets.zip') |
|
mic(downloaded_files) |
|
raise NotImplementedError |
|
|
|
return [ |
|
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": downloaded_files["train"]}), |
|
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": downloaded_files["dev"]}), |
|
] |
|
|