tweet_topic_multilingual / tweet_topic_multilingual.py
asahi417's picture
init
4c69e22
raw
history blame
3.07 kB
""" TweetTopicMultilingual Dataset """
import json
import datasets
logger = datasets.logging.get_logger(__name__)
_DESCRIPTION = """[TweetTopicMultilingual](TBA)"""
_VERSION = "0.0.0"
_CITATION = """TBA"""
_HOME_PAGE = "https://cardiffnlp.github.io"
_NAME = "tweet_topic_multilingual"
_ROOT_URL = f'https://huggingface.co/datasets/cardiffnlp/{_NAME}/raw/main/dataset'
_LANGUAGES = ["en", "es", "ja", "gr"]
_URL = {}
# plain split
for lan in _LANGUAGES:
_URL[lan] = {split: f"{_ROOT_URL}/{lan}/{lan}_{split}.jsonl" for split in ["train", "test", "validation"]}
_URL["en_2022"] = {split: f"{_ROOT_URL}/en_2022/en_{split}.jsonl" for split in ["train", "validation"]}
# cross validation
for lan in _LANGUAGES:
_URL.update({
f"{lan}/cross_validation_{n}": {
split: f"{_ROOT_URL}/{lan}/cross_validation_{n}/{lan}_{split}.jsonl"
for split in ["train", "test", "validation"]
} for n in range(5)
})
class Config(datasets.BuilderConfig):
"""BuilderConfig"""
def __init__(self, **kwargs):
"""BuilderConfig.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(Config, self).__init__(**kwargs)
class TweetTopicMultilingual(datasets.GeneratorBasedBuilder):
"""Dataset."""
BUILDER_CONFIGS = [
Config(name=f"{_NAME}/{i}", version=datasets.Version(_VERSION), description=_DESCRIPTION) for i in _URL.keys()
]
def _split_generators(self, dl_manager):
downloaded_file = dl_manager.download_and_extract(_URL[self.config.name])
splits = _URL[self.config.name]
return [datasets.SplitGenerator(name=i, gen_kwargs={"filepaths": downloaded_file[i]}) for i in splits]
def _generate_examples(self, filepath):
_key = 0
logger.info("generating examples from = %s", filepath)
with open(filepath, encoding="utf-8") as f:
_list = f.read().split('\n')
for i in _list:
data = json.loads(i)
yield _key, data
_key += 1
def _info(self):
names = [
"arts_&_culture", "business_&_entrepreneurs", "celebrity_&_pop_culture", "diaries_&_daily_life", "family",
"fashion_&_style", "film_tv_&_video", "fitness_&_health", "food_&_dining", "gaming",
"learning_&_educational", "music", "news_&_social_concern", "other_hobbies", "relationships",
"science_&_technology", "sports", "travel_&_adventure", "youth_&_student_life"
]
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"id": datasets.Value("string"),
"text": datasets.Value("string"),
"label": datasets.Sequence(datasets.features.ClassLabel(names=names)),
"label_name": datasets.Sequence(datasets.Value("string"))
}
),
supervised_keys=None,
homepage=_HOME_PAGE,
citation=_CITATION,
)