Datasets:

Modalities:
Text
Formats:
parquet
Languages:
Danish
Size:
< 1K
Libraries:
Datasets
pandas
License:
grundtvigs-works / src /push_to_hub.py
Kenneth Enevoldsen
Added source data and processing code
f1e5b38 unverified
"""Push processed data to the Hugging Face Hub."""
from pathlib import Path
import yaml
from datasets import Dataset, DatasetDict
def get_frontmatter(md_content: str) -> dict:
"""Extract frontmatter from Markdown content."""
if not md_content.startswith("---"):
return {}
end_index = md_content.find("---", 3)
if end_index == -1:
return {}
frontmatter_str = md_content[3:end_index].strip()
try:
return yaml.safe_load(frontmatter_str)
except yaml.YAMLError:
return {}
def process_file(xml_file: Path) -> dict:
md_file = xml_file.with_suffix(".md")
txt_file = xml_file.with_suffix(".txt")
with xml_file.open("r") as file:
xml_content = file.read()
with md_file.open("r") as file:
md_content = file.read()
with txt_file.open("r") as file:
txt_content = file.read()
# extract front
frontmatter = get_frontmatter(md_content)
title = frontmatter.get("title", "")
if isinstance(title, list):
if len(title) == 1:
title = title[0]
else:
raise ValueError(f"Title is a list with more than one item: {title}")
return {
"id": xml_file.stem,
"title": title,
"author": frontmatter.get("author", "Nicolai Frederik Severin Grundtvig"),
"date": str(frontmatter["date"]) if "date" in frontmatter else "",
"publisher": frontmatter.get("publisher", "Faculty of Arts, Aarhus University"),
"xml": xml_content,
"md": md_content,
"txt": txt_content,
}
def main():
source_data = Path(__file__).parent.parent / "source-data"
xml_files = list(source_data.glob("*.xml"))
processed_data = [process_file(xml_file) for xml_file in xml_files]
for p in processed_data:
assert isinstance(p, dict), f"Processed data item is not a dict: {p}"
for key, value in p.items():
assert isinstance(value, str), (
f"Value for key '{key}' is not a string: {value}"
)
ds = Dataset.from_list(processed_data)
ds_dict = DatasetDict({"train": ds})
# push to hub
ds_dict.push_to_hub("chcaa/grundtvigs-works")
if __name__ == "__main__":
main()