{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `USA TODAY (usatoday.com)` — consolidation notebook\n", "\n", "This notebook is the second half of the pipeline behind\n", "[`SinclairSchneider/usa_today_com`](https://huggingface.co/datasets/SinclairSchneider/usa_today_com).\n", "\n", "`crawler.py` writes one JSON file per article into `pages/`. This notebook\n", "\n", "1. reads every `pages/*.json`,\n", "2. flattens the nested records into one `pandas` column per field,\n", "3. drops duplicate articles by body text (`drop_duplicates(subset=['content'])`),\n", "4. writes `usa_today_com.json` and pushes the result to the Hugging Face Hub.\n", "\n", "Outputs have been cleared before publication.\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import glob\n", "import json\n", "import pandas as pd\n", "from datasets import Dataset\n", "import huggingface_hub\n", "from dateutil.parser import parse\n", "from tqdm import tqdm" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open(\"pages/y_sports_college_ucla_baseball_2025_06_03_utsa-baseball-proved-toughness-ucla-super-regional_84004356007_?taid=683f2a449847150001c27a2b&utm_campaign=trueanthem&utm_medium=trueanthem&utm_source=twitter.json\", \"r\", encoding=\"utf-8\") as f:\n", " json_input = f.read()\n", " dict_input_example = json.loads(json_input)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict_input_example" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "json_files = glob.glob(\"pages/*.json\")\n", "len(json_files)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "title = []\n", "content = []\n", "authors = []\n", "description = []\n", "keywords = []\n", "category = []\n", "subcategory = []\n", "dateCreated = []\n", "datePublished = []\n", "dateModified = []\n", "primaryTag = []\n", "contentId = []\n", "url = []\n", "for file in tqdm(json_files):\n", " with open(file, \"r\") as f:\n", " d = json.loads(f.read())\n", " title.append(d['title'])\n", " content.append(d['content'])\n", " authors.append(d['additional_attributes']['authors'])\n", " description.append(d['additional_attributes']['description'])\n", " keywords.append(d['additional_attributes']['keywords'])\n", " category.append(d['additional_attributes']['category'])\n", " subcategory.append(d['additional_attributes']['subcategory'])\n", " dateCreated.append(d['additional_attributes']['dateCreated'])\n", " datePublished.append(d['additional_attributes']['datePublished'])\n", " dateModified.append(d['additional_attributes']['dateModified'])\n", " primaryTag.append(d['additional_attributes']['primaryTag'])\n", " contentId.append(d['additional_attributes']['contentId'])\n", " url.append(d['additional_attributes']['url'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(\n", " {\n", " 'title':title,\n", " 'content':content,\n", " 'authors':authors,\n", " 'description':description,\n", " 'keywords':keywords,\n", " 'category':category,\n", " 'subcategory':subcategory,\n", " 'dateCreated':dateCreated,\n", " 'datePublished':datePublished,\n", " 'dateModified':dateModified,\n", " 'primaryTag':primaryTag,\n", " 'contentId':contentId,\n", " 'url':url,\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df['keywords']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = df.drop_duplicates(subset=['content'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = df[df['datePublished']!=\"\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = df.sort_values(by=['datePublished'], ascending=False).reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.to_json(\"usa_today_com.json\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds = Dataset.from_pandas(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.push_to_hub(\"SinclairSchneider/usa_today_com\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.push_to_hub(\"NLP-UniBW/usa_today_com\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }