| |
| |
| import os |
| from functools import partial |
| from pathlib import Path |
|
|
| import datasets |
| import pandas as pd |
| from datasets import DatasetDict, load_dataset |
|
|
|
|
| VERSION = datasets.Version("0.0.1") |
|
|
| AVAILABLE_DATASETS = { |
| 'geonames': 'https://download.geonames.org/export/dump/allCountries.zip', |
| 'alternateNames': 'https://download.geonames.org/export/dump/alternateNames.zip', |
| } |
| FIELDS = [ |
| "geonameid", |
| "name", |
| "asciiname", |
| "alternatenames", |
| |
| "latitude", |
| "longitude", |
| "feature_class", |
| "feature_code", |
| "country_code", |
| "cc2", |
| |
| "admin1_code", |
| |
| "admin2_code", |
| |
| "admin3_code", |
| "admin4_code", |
| "population", |
| "elevation", |
| "dem", |
| |
| "timezone", |
| "modification_date", |
| ] |
|
|
|
|
| class GeonamesDataset(datasets.GeneratorBasedBuilder): |
| """GeonamesDataset dataset.""" |
|
|
| @staticmethod |
| def load(data_name_config: str = "geonames") -> DatasetDict: |
| ds = load_dataset(__file__, data_name_config) |
| return ds |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| description="", |
| features=datasets.Features( |
| { |
| "geonameid": datasets.Value("string"), |
| "name": datasets.Value("string"), |
| "asciiname": datasets.Value("string"), |
| "alternatenames": datasets.Value("string"), |
| "latitude": datasets.Value("string"), |
| "longitude": datasets.Value("string"), |
| "feature_class": datasets.Value("string"), |
| "feature_code": datasets.Value("string"), |
| "country_code": datasets.Value("string"), |
| "cc2": datasets.Value("string"), |
| "admin1_code": datasets.Value("string"), |
| "admin2_code": datasets.Value("string"), |
| "admin3_code": datasets.Value("string"), |
| "admin4_code": datasets.Value("string"), |
| "population": datasets.Value("string"), |
| "elevation": datasets.Value("string"), |
| "dem": datasets.Value("string"), |
| "timezone": datasets.Value("string"), |
| "modification_date": datasets.Value("string"), |
| } |
| ), |
| supervised_keys=None, |
| homepage="https://download.geonames.org/export/zip/", |
| citation="", |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| root_paths = { |
| "geonames": Path(dl_manager.download_and_extract(AVAILABLE_DATASETS["geonames"]))/'allCountries.txt', |
| "alternateNames": Path(dl_manager.download_and_extract(AVAILABLE_DATASETS["alternateNames"]))/'alternateNames.txt', |
| } |
| return [ |
| datasets.SplitGenerator( |
| name=datasets.Split.TRAIN, |
| gen_kwargs={ |
| "root_path": root_paths, "split": "train" |
| }, |
| ), |
| ] |
|
|
| def _generate_examples(self, root_path, split): |
| data_file = str(root_path["geonames"]) |
| data = pd.read_csv( |
| data_file, sep="\t", header=None, |
| encoding='utf-8', usecols=list(range(len(FIELDS))), names=FIELDS |
| ) |
| |
| |
| |
| |
| |
| |
| for idx, sample in data.iterrows(): |
| yield idx, dict(sample) |
|
|
|
|