File size: 9,146 Bytes
217de99 a849f84 f35912b 9061724 f35912b a849f84 f35912b a849f84 f35912b a849f84 f35912b a849f84 040b1a0 f35912b 217de99 a849f84 f35912b a849f84 217de99 a849f84 217de99 a679392 a849f84 be7e99c a849f84 be7e99c a849f84 be7e99c a679392 f35912b a849f84 be7e99c a849f84 be7e99c a849f84 be7e99c a679392 be7e99c a849f84 be7e99c a679392 a849f84 a679392 a849f84 f35912b a849f84 a679392 217de99 a849f84 217de99 be7e99c a849f84 c2827c5 a849f84 f35912b be7e99c a849f84 be7e99c a849f84 be7e99c a849f84 be7e99c f35912b a849f84 806a21e f35912b a849f84 a679392 a849f84 806a21e f35912b a679392 a849f84 f35912b a849f84 a679392 217de99 a849f84 a679392 3484aca a679392 3484aca a679392 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
import json
import tarfile
from datasets import DatasetInfo, DatasetBuilder, DownloadManager, BuilderConfig, SplitGenerator, Split, Version
import datasets
import os
import requests
import re
from tqdm import tqdm
SUPPORTED_YEARS = ["1774"]
# Add years from 1798 to 1964 to the supported years
SUPPORTED_YEARS = SUPPORTED_YEARS + [str(year) for year in range(1798, 1964)]
def make_year_file_splits():
"""
Collects a list of available files for each year.
Returns:
dict: A dictionary mapping each year to its corresponding file URL.
list: A list of years.
"""
base_url = "https://huggingface.co/datasets/dell-research-harvard/AmericanStories/resolve/main/"
# Make a list of years from 1774 to 1960
year_list = [str(year) for year in range(1774, 1960)]
data_files = [f"faro_{year}.tar.gz" for year in year_list]
url_list = [base_url + file for file in data_files]
splits = {year: file for year, file in zip(year_list, url_list)}
years = year_list
return splits, years
_CITATION = """\
Coming Soon
"""
_DESCRIPTION = """\
American Stories offers high-quality structured data from historical newspapers suitable for pre-training large language models to enhance the understanding of historical English and world knowledge. It can also be integrated into external databases of retrieval-augmented language models, enabling broader access to historical information, including interpretations of political events and intricate details about people's ancestors. Additionally, the structured article texts facilitate the application of transformer-based methods for popular tasks like detecting reproduced content, significantly improving accuracy compared to traditional OCR methods. American Stories serves as a substantial and valuable dataset for advancing multimodal layout analysis models and other multimodal applications.
"""
_FILE_DICT, _YEARS = make_year_file_splits()
class CustomBuilderConfig(datasets.BuilderConfig):
"""BuilderConfig for AmericanStories dataset with different configurations."""
def __init__(self, year_list=None,features=["article_id", "newspaper_name", "edition", "date", "page", "headline", "byline", "article"], **kwargs):
"""
BuilderConfig for AmericanStories dataset.
Args:
year_list (list): A list of years to include in the dataset.
**kwargs: Additional keyword arguments forwarded to the superclass.
"""
super(CustomBuilderConfig, self).__init__(**kwargs)
self.year_list = year_list
self.features = features
class AmericanStories(datasets.GeneratorBasedBuilder):
"""Dataset builder class for AmericanStories dataset."""
VERSION = datasets.Version("0.1.0")
BUILDER_CONFIGS = [
CustomBuilderConfig(
name="all_years",
version=VERSION,
description="All years in the dataset"
),
CustomBuilderConfig(
name="subset_years",
version=VERSION,
description="Subset of years in the dataset",
year_list=["1774", "1804"]
),
CustomBuilderConfig(
name="all_years_content_regions",
version=VERSION,
description="All years in the dataset",
),
CustomBuilderConfig(
name="subset_years_content_regions",
version=VERSION,
description="Subset of years in the dataset",
year_list=["1774", "1804"],
)
]
DEFAULT_CONFIG_NAME = "subset_years"
def _info(self):
"""
Specifies the DatasetInfo object for the AmericanStories dataset.
Returns:
datasets.DatasetInfo: The DatasetInfo object.
"""
if not self.config.name.endswith("content_regions"):
features = datasets.Features(
{
"article_id": datasets.Value("string"),
"newspaper_name": datasets.Value("string"),
"edition": datasets.Value("string"),
"date": datasets.Value("string"),
"page": datasets.Value("string"),
"headline": datasets.Value("string"),
"byline": datasets.Value("string"),
"article": datasets.Value("string"),
}
)
else:
features = datasets.Features(
{
"raw_data_string": datasets.Value("string"),
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""
Downloads and extracts the data, and defines the dataset splits.
Args:
dl_manager (datasets.DownloadManager): The DownloadManager instance.
Returns:
list: A list of SplitGenerator objects.
"""
if self.config.name == "subset_years":
print("Only taking a subset of years. Change name to 'all_years' to use all years in the dataset.")
if not self.config.year_list:
raise ValueError("Please provide a valid year_list")
elif not set(self.config.year_list).issubset(set(SUPPORTED_YEARS)):
raise ValueError(f"Only {SUPPORTED_YEARS} are supported. Please provide a valid year_list")
urls = _FILE_DICT
year_list = _YEARS
# Subset _FILE_DICT and year_list to only include years in config.year_list
if self.config.year_list:
urls = {year: urls[year] for year in self.config.year_list}
year_list = self.config.year_list
data_dir = dl_manager.download_and_extract(urls)
# Return a list of splits, where each split corresponds to a year
return [
datasets.SplitGenerator(
name=year,
gen_kwargs={
"year_dir": os.path.join(data_dir[year], "mnt", "122a7683-fa4b-45dd-9f13-b18cc4f4a187", "ca_rule_based_fa_clean", "faro_" + year),
"split": year,
"associated": True if not self.config.name.endswith("content_regions") else False,
},
) for year in year_list
]
def _generate_examples(self, year_dir,split, associated):
"""
Generates examples for the specified year and split.
Args:
year_dir (str): The directory path for the year.
associated (bool): Whether or not the output should be contents associated into an "article" or raw contents.
Yields:
tuple: The key-value pair containing the example ID and the example data.
"""
print("Associated: " + str(associated))
if associated:
for filepath in os.listdir(year_dir):
with open(os.path.join(year_dir, filepath), encoding="utf-8") as f:
try :
data = json.load(f)
except:
print("Error loading file: " + filepath)
continue
if "lccn" in data.keys():
scan_id = filepath.split('.')[0]
scan_date = filepath.split("_")[0]
scan_page = filepath.split("_")[1]
scan_edition = filepath.split("_")[-2][8:]
newspaper_name = data["lccn"]["title"]
full_articles_in_data = data["full articles"]
for article in full_articles_in_data:
article_id = str(article["full_article_id"]) + "_" + scan_id
yield article_id, {
"article_id": article_id,
"newspaper_name": newspaper_name,
"edition": scan_edition,
"date": scan_date,
"page": scan_page,
"headline": article["headline"],
"byline": article["byline"],
"article": article["article"],
}
else:
print("Returning a json as a string, feel free to parse it yourself!")
for filepath in os.listdir(year_dir):
with open(os.path.join(year_dir, filepath), encoding="utf-8") as f:
try :
data = json.load(f)
except:
# print("Error loading file: " + filepath)
continue
###Convert json to strng
data=json.dumps(data)
print((data))
print(type(data))
scan_id=filepath.split('.')[0]
##Yield the scan id and the raw data string
yield scan_id, {
"raw_data_string": str(data)
}
|