Commit
·
a679392
1
Parent(s):
a9c8423
added raw text
Browse files- AmericanStories.py +86 -42
AmericanStories.py
CHANGED
|
@@ -46,7 +46,7 @@ _FILE_DICT, _YEARS = make_year_file_splits()
|
|
| 46 |
class CustomBuilderConfig(datasets.BuilderConfig):
|
| 47 |
"""BuilderConfig for AmericanStories dataset with different configurations."""
|
| 48 |
|
| 49 |
-
def __init__(self, year_list=None, **kwargs):
|
| 50 |
"""
|
| 51 |
BuilderConfig for AmericanStories dataset.
|
| 52 |
|
|
@@ -56,6 +56,7 @@ class CustomBuilderConfig(datasets.BuilderConfig):
|
|
| 56 |
"""
|
| 57 |
super(CustomBuilderConfig, self).__init__(**kwargs)
|
| 58 |
self.year_list = year_list
|
|
|
|
| 59 |
|
| 60 |
|
| 61 |
class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
@@ -67,13 +68,26 @@ class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
| 67 |
CustomBuilderConfig(
|
| 68 |
name="all_years",
|
| 69 |
version=VERSION,
|
| 70 |
-
description="All years in the dataset"
|
| 71 |
),
|
| 72 |
CustomBuilderConfig(
|
| 73 |
name="subset_years",
|
| 74 |
version=VERSION,
|
| 75 |
description="Subset of years in the dataset",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
year_list=["1774", "1804"],
|
|
|
|
| 77 |
)
|
| 78 |
]
|
| 79 |
DEFAULT_CONFIG_NAME = "subset_years"
|
|
@@ -85,18 +99,27 @@ class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
| 85 |
Returns:
|
| 86 |
datasets.DatasetInfo: The DatasetInfo object.
|
| 87 |
"""
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
return datasets.DatasetInfo(
|
| 102 |
description=_DESCRIPTION,
|
|
@@ -138,44 +161,65 @@ class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
| 138 |
gen_kwargs={
|
| 139 |
"year_dir": os.path.join(data_dir[year], "mnt", "122a7683-fa4b-45dd-9f13-b18cc4f4a187", "ca_rule_based_fa_clean", "faro_" + year),
|
| 140 |
"split": year,
|
|
|
|
| 141 |
},
|
| 142 |
) for year in year_list
|
| 143 |
]
|
| 144 |
|
| 145 |
-
def _generate_examples(self, year_dir,
|
| 146 |
"""
|
| 147 |
Generates examples for the specified year and split.
|
| 148 |
|
| 149 |
Args:
|
| 150 |
year_dir (str): The directory path for the year.
|
| 151 |
-
|
| 152 |
|
| 153 |
Yields:
|
| 154 |
tuple: The key-value pair containing the example ID and the example data.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
class CustomBuilderConfig(datasets.BuilderConfig):
|
| 47 |
"""BuilderConfig for AmericanStories dataset with different configurations."""
|
| 48 |
|
| 49 |
+
def __init__(self, year_list=None,features=["article_id", "newspaper_name", "edition", "date", "page", "headline", "byline", "article"], **kwargs):
|
| 50 |
"""
|
| 51 |
BuilderConfig for AmericanStories dataset.
|
| 52 |
|
|
|
|
| 56 |
"""
|
| 57 |
super(CustomBuilderConfig, self).__init__(**kwargs)
|
| 58 |
self.year_list = year_list
|
| 59 |
+
self.features = features
|
| 60 |
|
| 61 |
|
| 62 |
class AmericanStories(datasets.GeneratorBasedBuilder):
|
|
|
|
| 68 |
CustomBuilderConfig(
|
| 69 |
name="all_years",
|
| 70 |
version=VERSION,
|
| 71 |
+
description="All years in the dataset"
|
| 72 |
),
|
| 73 |
CustomBuilderConfig(
|
| 74 |
name="subset_years",
|
| 75 |
version=VERSION,
|
| 76 |
description="Subset of years in the dataset",
|
| 77 |
+
year_list=["1774", "1804"]
|
| 78 |
+
|
| 79 |
+
),
|
| 80 |
+
CustomBuilderConfig(
|
| 81 |
+
name="all_years_content_regions",
|
| 82 |
+
version=VERSION,
|
| 83 |
+
description="All years in the dataset",
|
| 84 |
+
),
|
| 85 |
+
CustomBuilderConfig(
|
| 86 |
+
name="subset_years_content_regions",
|
| 87 |
+
version=VERSION,
|
| 88 |
+
description="Subset of years in the dataset",
|
| 89 |
year_list=["1774", "1804"],
|
| 90 |
+
|
| 91 |
)
|
| 92 |
]
|
| 93 |
DEFAULT_CONFIG_NAME = "subset_years"
|
|
|
|
| 99 |
Returns:
|
| 100 |
datasets.DatasetInfo: The DatasetInfo object.
|
| 101 |
"""
|
| 102 |
+
if not self.config.name.endswith("content_regions"):
|
| 103 |
+
features = datasets.Features(
|
| 104 |
+
{
|
| 105 |
+
"article_id": datasets.Value("string"),
|
| 106 |
+
"newspaper_name": datasets.Value("string"),
|
| 107 |
+
"edition": datasets.Value("string"),
|
| 108 |
+
"date": datasets.Value("string"),
|
| 109 |
+
"page": datasets.Value("string"),
|
| 110 |
+
"headline": datasets.Value("string"),
|
| 111 |
+
"byline": datasets.Value("string"),
|
| 112 |
+
"article": datasets.Value("string"),
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
}
|
| 116 |
+
)
|
| 117 |
+
else:
|
| 118 |
+
features = datasets.Features(
|
| 119 |
+
{
|
| 120 |
+
"raw_data_string": datasets.Value("string"),
|
| 121 |
+
}
|
| 122 |
+
)
|
| 123 |
|
| 124 |
return datasets.DatasetInfo(
|
| 125 |
description=_DESCRIPTION,
|
|
|
|
| 161 |
gen_kwargs={
|
| 162 |
"year_dir": os.path.join(data_dir[year], "mnt", "122a7683-fa4b-45dd-9f13-b18cc4f4a187", "ca_rule_based_fa_clean", "faro_" + year),
|
| 163 |
"split": year,
|
| 164 |
+
"associated": True if not self.config.name.endswith("content_regions") else False,
|
| 165 |
},
|
| 166 |
) for year in year_list
|
| 167 |
]
|
| 168 |
|
| 169 |
+
def _generate_examples(self, year_dir,split, associated):
|
| 170 |
"""
|
| 171 |
Generates examples for the specified year and split.
|
| 172 |
|
| 173 |
Args:
|
| 174 |
year_dir (str): The directory path for the year.
|
| 175 |
+
associated (bool): Whether or not the output should be contents associated into an "article" or raw contents.
|
| 176 |
|
| 177 |
Yields:
|
| 178 |
tuple: The key-value pair containing the example ID and the example data.
|
| 179 |
"""
|
| 180 |
+
print("Associated: " + str(associated))
|
| 181 |
+
if associated:
|
| 182 |
+
for filepath in os.listdir(year_dir):
|
| 183 |
+
with open(os.path.join(year_dir, filepath), encoding="utf-8") as f:
|
| 184 |
+
try :
|
| 185 |
+
data = json.load(f)
|
| 186 |
+
except:
|
| 187 |
+
print("Error loading file: " + filepath)
|
| 188 |
+
continue
|
| 189 |
+
if "lccn" in data.keys():
|
| 190 |
+
scan_id = filepath.split('.')[0]
|
| 191 |
+
scan_date = filepath.split("_")[0]
|
| 192 |
+
scan_page = filepath.split("_")[1]
|
| 193 |
+
scan_edition = filepath.split("_")[-2][8:]
|
| 194 |
+
newspaper_name = data["lccn"]["title"]
|
| 195 |
+
full_articles_in_data = data["full articles"]
|
| 196 |
+
for article in full_articles_in_data:
|
| 197 |
+
article_id = str(article["full_article_id"]) + "_" + scan_id
|
| 198 |
+
yield article_id, {
|
| 199 |
+
"article_id": article_id,
|
| 200 |
+
"newspaper_name": newspaper_name,
|
| 201 |
+
"edition": scan_edition,
|
| 202 |
+
"date": scan_date,
|
| 203 |
+
"page": scan_page,
|
| 204 |
+
"headline": article["headline"],
|
| 205 |
+
"byline": article["byline"],
|
| 206 |
+
"article": article["article"],
|
| 207 |
+
}
|
| 208 |
+
else:
|
| 209 |
+
for filepath in os.listdir(year_dir):
|
| 210 |
+
with open(os.path.join(year_dir, filepath), encoding="utf-8") as f:
|
| 211 |
+
try :
|
| 212 |
+
data = json.load(f)
|
| 213 |
+
except:
|
| 214 |
+
print("Error loading file: " + filepath)
|
| 215 |
+
continue
|
| 216 |
+
###Convert json to strng
|
| 217 |
+
data=json.dumps(data)
|
| 218 |
+
print((data))
|
| 219 |
+
print(type(data))
|
| 220 |
+
scan_id=filepath.split('.')[0]
|
| 221 |
+
##Yield the scan id and the raw data string
|
| 222 |
+
yield scan_id, {
|
| 223 |
+
"raw_data_string": str(data)
|
| 224 |
+
}
|
| 225 |
+
|