bwns / bwns.py
Dayyan's picture
Add latitude, longitude
a6f9aa7
import csv
import datasets
_DESCRIPTION = "BWNS: The Bahai World News Service dataset."
_HOMEPAGE = "https://news.bahai.org/"
_URLS = {
"train": "./train.csv",
}
class BwnsDataset(datasets.GeneratorBasedBuilder):
"""BWNS: The Bahai World News Service dataset."""
BUILDER_CONFIG_CLASS=datasets.BuilderConfig
VERSION = datasets.Version("1.1.0")
NAME = "BWNS"
BUILDER_CONFIGS = [
datasets.BuilderConfig(name="original", version=VERSION, description="Original"),
datasets.BuilderConfig(name="concat", version=VERSION, description="Title and content concatenated"),
]
DEFAULT_CONFIG_NAME = "original"
def _info(self):
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
if self.config.name == "original":
features = datasets.Features(
{
"id": datasets.Value("int32"),
"date": datasets.Value("timestamp[s]"),
"city": datasets.Value("string"),
"region": datasets.Value("string"),
"country": datasets.Value("string"),
"related_articles": datasets.Sequence(datasets.Value("int32")),
"title": datasets.Value("string"),
"content": datasets.Value("string"),
"latitude": datasets.Value("string"),
"longitude": datasets.Value("string"),
}
)
elif self.config.name == "concat":
features = datasets.Features(
{
"id": datasets.Value("int32"),
"date": datasets.Value("timestamp[s]"),
"city": datasets.Value("string"),
"region": datasets.Value("string"),
"country": datasets.Value("string"),
"related_articles": datasets.Sequence(datasets.Value("int32")),
"title": datasets.Value("string"),
"title_and_content": datasets.Value("string"),
"latitude": datasets.Value("string"),
"longitude": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
supervised_keys=None,
homepage=_HOMEPAGE,
)
def _split_generators(self, dl_manager):
data_dirs = dl_manager.download_and_extract(_URLS)
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN,
# These kwargs will be passed to _generate_examples
gen_kwargs={
"filepath": data_dirs['train'],
"split": "train",
},
),
]
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
def _generate_examples(self, filepath, split):
with open(filepath, encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
if self.config.name == 'original':
yield row['id'], {
"id": int(row['id']),
"date": row['date'],
"city": row['city'],
"region": row['region'],
"country": row['country'],
"latitude": row['latitude'],
"longitude": row['longitude'],
"related_articles": [int(x) for x in row['related_articles'][1:-1].split(' ') if x],
"title": row['title'],
"content": row['content'],
}
elif self.config.name == 'concat':
yield row['id'], {
"id": int(row['id']),
"date": row['date'],
"city": row['city'],
"region": row['region'],
"country": row['country'],
"latitude": row['latitude'],
"longitude": row['longitude'],
"related_articles": [int(x) for x in row['related_articles'][1:-1].split(' ') if x],
"title": row['title'],
"title_and_content": f"{row['title']} {row['content']}",
}