# coding=utf-8 # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Class for loading datafrom rtGender""" from __future__ import absolute_import, division, print_function import csv from enum import Enum import os import datasets _CITATION = """\ @inproceedings{voigt-etal-2018-rtgender, title = "{R}t{G}ender: A Corpus for Studying Differential Responses to Gender", author = "Voigt, Rob and Jurgens, David and Prabhakaran, Vinodkumar and Jurafsky, Dan and Tsvetkov, Yulia", booktitle = "Proceedings of the Eleventh International Conference on Language Resources and Evaluation ({LREC} 2018)", month = may, year = "2018", address = "Miyazaki, Japan", publisher = "European Language Resources Association (ELRA)", url = "https://www.aclweb.org/anthology/L18-1445", } """ _DESCRIPTION = """\ RtGender is a corpus for studying responses to gender online, including posts and responses from Facebook, TED, Fitocracy, and Reddit where the gender of the source poster/speaker is known. """ _HOMEPAGE = "https://nlp.stanford.edu/robvoigt/rtgender/#contact" _LICENSE = "Research Only" _URL = "https://nlp.stanford.edu/robvoigt/rtgender/rtgender.tar.gz" class Config(Enum): ANNOTATIONS = "annotations" POSTS = "posts" RESPONSES = "responses" FB_POLI = "fb_politicians" FB_PUB = "fb_public" TED = "ted" FITOCRACY = "fitocracy" REDDIT = "reddit" class rtGender(datasets.GeneratorBasedBuilder): """TODO: Short description of my dataset.""" VERSION = datasets.Version("1.1.0") # This is an example of a dataset with multiple configurations. # If you don't want/need to define several sub-sets in your dataset, # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes. # If you need to make complex sub-parts in the datasets with configurable options # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig # BUILDER_CONFIG_CLASS = MyBuilderConfig # You will be able to load one or the other configurations in the following list with # data = datasets.load_dataset('my_dataset', 'first_domain') # data = datasets.load_dataset('my_dataset', 'second_domain') BUILDER_CONFIGS = [ datasets.BuilderConfig( name=Config.ANNOTATIONS.value, version=VERSION, description="Retrieves only the annotations.", ), datasets.BuilderConfig( name=Config.POSTS.value, version=VERSION, description="Retrieves all posts.", ), datasets.BuilderConfig( name=Config.RESPONSES.value, version=VERSION, description="Retrieves all responses.", ) ] DEFAULT_CONFIG_NAME = Config.ANNOTATIONS.value # It's not mandatory to have a default configuration. Just use one if it make sense. POSTS_FEATURES = { "source": datasets.Value("string"), "op_id": datasets.Value("string"), "op_gender": datasets.Value("string"), "post_id": datasets.Value("string"), "post_text": datasets.Value("string"), "post_type": datasets.Value("string"), # only for fb "subreddit": datasets.Value("string"), # only for reddit "op_gender_visible": datasets.Value("string"), # only for reddit } RESPONSES_FEATURES = { "source": datasets.Value("string"), "op_id": datasets.Value("string"), "op_gender": datasets.Value("string"), "post_id": datasets.Value("string"), "responder_id": datasets.Value("string"), "response_text": datasets.Value("string"), "op_name": datasets.Value("string"), # only for fb "op_category": datasets.Value("string"), # only for fb "responder_gender": datasets.Value("string"), # only for fitocracy and reddit "responder_gender_visible": datasets.Value("string"), # only for reddit "subreddit": datasets.Value("string"), } ANNOTATION_FEATURES = { "source": datasets.Value("string"), "op_gender": datasets.Value("string"), "post_text": datasets.Value("string"), "response_text": datasets.Value("string"), "sentiment": datasets.Value("string"), "relevance": datasets.Value("string"), } def _info(self): if ( self.config.name == Config.ANNOTATIONS.value ): # This is the name of the configuration selected in BUILDER_CONFIGS above features = datasets.Features(self.ANNOTATION_FEATURES) elif self.config.name == Config.POSTS.value: features = datasets.Features(self.POSTS_FEATURES) else: features = datasets.Features(self.RESPONSES_FEATURES) return datasets.DatasetInfo( # This is the description that will appear on the datasets page. description=_DESCRIPTION, # This defines the different columns of the dataset and their types features=features, # Here we define them above because they are different between the two configurations # If there's a common (input, target) tuple from the features, # specify them here. They'll be used if as_supervised=True in # builder.as_dataset. supervised_keys=None, # Homepage of the dataset for documentation homepage=_HOMEPAGE, # License for the dataset if available license=_LICENSE, # Citation for the dataset citation=_CITATION, ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLs # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files. # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive data_dir = dl_manager.download_and_extract(_URL) if self.config.name == Config.ANNOTATIONS.value: files = ["annotations.csv"] elif self.config.name == Config.POSTS.value: files = [ "facebook_congress_posts.csv", "facebook_wiki_posts.csv", "fitocracy_posts.csv", "reddit_posts.csv", ] else: files = [ "facebook_congress_responses.csv", "facebook_wiki_responses.csv", "fitocracy_responses.csv", "reddit_responses.csv", "ted_responses.csv", ] return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, # These kwargs will be passed to _generate_examples gen_kwargs={ "filepaths": list(map(lambda x: os.path.join(data_dir, x), files)), "split": "train", }, ), ] def _generate_examples( self, filepaths, split, # method parameters are unpacked from `gen_kwargs` as given in `_split_generators` ): """ Yields examples as (key, example) tuples. """ # This method handles input defined in _split_generators to yield (key, example) tuples from the dataset. # The `key` is here for legacy reason (tfds) and is not important in itself. files = [] readers = {} for fp in filepaths: f = open(fp, encoding="utf-8") reader = csv.reader(f) next(reader) readers[fp.replace(".csv", "")] = reader files.append(f) id_ = 0 for reader_name, reader in readers.items(): for row in reader: if self.config.name == Config.ANNOTATIONS.value: yield id_, { "source": row[0], "op_gender": row[1], "post_text": row[2], "response_text": row[3], "sentiment": row[4], "relevance": row[5], } elif self.config.name == Config.POSTS.value: r = { "source": reader_name, "op_id": row[0], "op_gender": row[1], "post_id": row[2], "post_text": row[3], "post_type": None, "subreddit": None, "op_gender_visible": None, } if "facebook" in reader_name: r["post_type"] = row[4] elif "reddit" in reader_name: r["subreddit"] = row[4] r["op_gender_visible"] = row[5] yield id_, r else: r = { "source": reader_name, "op_id": row[0], "op_gender": row[1], "post_id": row[2], "responder_id": row[3], "response_text": row[4], "op_name": None, "op_category": None, "responder_gender": None, "responder_gender_visible": None, "subreddit": None } if "facebook" in reader_name: r["op_name"] = row[5] r["op_category"] = row[6] elif "fitocracy" in reader_name: r["responder_gender"] = row[5] elif "reddit" in reader_name: r["subreddit"] = row[5] r["responder_gender"] = row[6] r["responder_gender_visible"] = row[7] yield id_, r id_ += 1 for fd in files: fd.close()