Commit
·
be7e99c
1
Parent(s):
2fffd34
final testing
Browse files- AmericanStories.py +37 -29
AmericanStories.py
CHANGED
|
@@ -7,26 +7,6 @@ import requests
|
|
| 7 |
import re
|
| 8 |
|
| 9 |
|
| 10 |
-
###GEt list of files
|
| 11 |
-
DATASET_URL="https://huggingface.co/datasets/dell-research-harvard/AmericanStories/blob/main/"
|
| 12 |
-
def get_list_of_files(url):
|
| 13 |
-
page = requests.get(url).text
|
| 14 |
-
links=re.findall(r'href=[\'"]?([^\'" >]+)', page)
|
| 15 |
-
###Get only links containing faro_
|
| 16 |
-
links=[link for link in links if link.startswith('faro_')]
|
| 17 |
-
return links
|
| 18 |
-
|
| 19 |
-
###Arrange into splits by year - files follow the format faro_YYYY.tar.gz
|
| 20 |
-
def get_splits(links):
|
| 21 |
-
splits={}
|
| 22 |
-
years=[]
|
| 23 |
-
for link in links:
|
| 24 |
-
year=link.split('_')[1].split('.')[0]
|
| 25 |
-
if year not in splits:
|
| 26 |
-
splits[year]=[]
|
| 27 |
-
splits[year].append(link)
|
| 28 |
-
years.append(year)
|
| 29 |
-
return splits,years
|
| 30 |
|
| 31 |
####data dir
|
| 32 |
DATA_DIR="."
|
|
@@ -36,9 +16,9 @@ def make_year_file_splits(data_dir):
|
|
| 36 |
data_files=os.listdir(data_dir)
|
| 37 |
###Get only files containing faro_
|
| 38 |
data_files=[file for file in data_files if file.startswith('faro_')]
|
| 39 |
-
###Test case : starts with 17
|
| 40 |
-
data_files=[file for file in data_files if file.startswith('faro_17')]
|
| 41 |
|
|
|
|
|
|
|
| 42 |
###Arrange into splits by year - files follow the format faro_YYYY.tar.gz
|
| 43 |
splits={}
|
| 44 |
years=[]
|
|
@@ -64,13 +44,25 @@ American Stories offers high-quality structured data from historical newspapers
|
|
| 64 |
_FILE_DICT,_YEARS=make_year_file_splits(DATA_DIR)
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
class AmericanStories(datasets.GeneratorBasedBuilder):
|
| 70 |
"""TODO: Short description of my dataset."""
|
| 71 |
|
| 72 |
VERSION = datasets.Version("0.0.1")
|
| 73 |
|
|
|
|
|
|
|
| 74 |
# This is an example of a dataset with multiple configurations.
|
| 75 |
# If you don't want/need to define several sub-sets in your dataset,
|
| 76 |
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
|
@@ -82,7 +74,21 @@ class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
| 82 |
# You will be able to load one or the other configurations in the following list with
|
| 83 |
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
| 84 |
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
def _info(self):
|
| 88 |
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
|
@@ -112,19 +118,21 @@ class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
| 112 |
citation=_CITATION,
|
| 113 |
)
|
| 114 |
|
| 115 |
-
def _split_generators(self, dl_manager
|
| 116 |
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
| 117 |
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
| 118 |
|
| 119 |
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 120 |
# 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.
|
| 121 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 122 |
-
|
| 123 |
-
urls = _FILE_DICT
|
| 124 |
-
else:
|
| 125 |
-
_URL_DICT,year_list=get_splits(get_list_of_files(DATASET_URL))
|
| 126 |
-
urls = _URL_DICT
|
| 127 |
year_list=_YEARS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
data_dir = dl_manager.download_and_extract(urls)
|
| 129 |
|
| 130 |
###REturn a list of splits - but each split is for a year!
|
|
|
|
| 7 |
import re
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
####data dir
|
| 12 |
DATA_DIR="."
|
|
|
|
| 16 |
data_files=os.listdir(data_dir)
|
| 17 |
###Get only files containing faro_
|
| 18 |
data_files=[file for file in data_files if file.startswith('faro_')]
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
###Get only files for 17__ years
|
| 21 |
+
# data_files=[file for file in data_files if file.split('_')[1].startswith('17')]
|
| 22 |
###Arrange into splits by year - files follow the format faro_YYYY.tar.gz
|
| 23 |
splits={}
|
| 24 |
years=[]
|
|
|
|
| 44 |
_FILE_DICT,_YEARS=make_year_file_splits(DATA_DIR)
|
| 45 |
|
| 46 |
|
| 47 |
+
###Make a class of builderconfig that supports an year_list attribute
|
| 48 |
+
class MyBuilderConfig(datasets.BuilderConfig):
|
| 49 |
+
"""BuilderConfig for MyDataset for different configurations."""
|
| 50 |
|
| 51 |
+
def __init__(self, year_list=None, **kwargs):
|
| 52 |
+
"""BuilderConfig for MyDataset.
|
| 53 |
+
Args:
|
| 54 |
+
**kwargs: keyword arguments forwarded to super.
|
| 55 |
+
"""
|
| 56 |
+
super(MyBuilderConfig, self).__init__(**kwargs)
|
| 57 |
+
self.year_list = year_list
|
| 58 |
|
| 59 |
class AmericanStories(datasets.GeneratorBasedBuilder):
|
| 60 |
"""TODO: Short description of my dataset."""
|
| 61 |
|
| 62 |
VERSION = datasets.Version("0.0.1")
|
| 63 |
|
| 64 |
+
|
| 65 |
+
|
| 66 |
# This is an example of a dataset with multiple configurations.
|
| 67 |
# If you don't want/need to define several sub-sets in your dataset,
|
| 68 |
# just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
|
|
|
|
| 74 |
# You will be able to load one or the other configurations in the following list with
|
| 75 |
# data = datasets.load_dataset('my_dataset', 'first_domain')
|
| 76 |
# data = datasets.load_dataset('my_dataset', 'second_domain')
|
| 77 |
+
##Now use the custom builder config class
|
| 78 |
+
BUILDER_CONFIGS = [
|
| 79 |
+
MyBuilderConfig(
|
| 80 |
+
name="all_years",
|
| 81 |
+
version=VERSION,
|
| 82 |
+
description="All years in the dataset",
|
| 83 |
+
),
|
| 84 |
+
MyBuilderConfig(
|
| 85 |
+
name="subset_years",
|
| 86 |
+
version=VERSION,
|
| 87 |
+
description="Subset of years in the dataset",
|
| 88 |
+
year_list=["1774"],
|
| 89 |
+
)]
|
| 90 |
+
|
| 91 |
+
DEFAULT_CONFIG_NAME = "all_years" # It's not mandatory to have a default configuration. Just use one if it make sense.
|
| 92 |
|
| 93 |
def _info(self):
|
| 94 |
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
|
|
|
| 118 |
citation=_CITATION,
|
| 119 |
)
|
| 120 |
|
| 121 |
+
def _split_generators(self, dl_manager):
|
| 122 |
# TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
|
| 123 |
# If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
|
| 124 |
|
| 125 |
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 126 |
# 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.
|
| 127 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 128 |
+
urls = _FILE_DICT
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
year_list=_YEARS
|
| 130 |
+
|
| 131 |
+
##Subset _FILE_DICT and year_list to only years in the config.year_list
|
| 132 |
+
if self.config.year_list:
|
| 133 |
+
urls={year:urls[year] for year in self.config.year_list}
|
| 134 |
+
year_list=self.config.year_list
|
| 135 |
+
|
| 136 |
data_dir = dl_manager.download_and_extract(urls)
|
| 137 |
|
| 138 |
###REturn a list of splits - but each split is for a year!
|