{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `FOCUS online (focus.de)` — consolidation notebook\n", "\n", "This notebook is the second half of the pipeline behind\n", "[`SinclairSchneider/focus_de`](https://huggingface.co/datasets/SinclairSchneider/focus_de).\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 and parses the publication date,\n", "3. writes `focus_de.json` and pushes the result to the Hugging Face Hub (there is no de-duplication).\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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with open(\"pages/__www.focus.de_kultur_kino_tv_focus-fernsehclub_tv-kolumne-rosins-fettkampf-lecker-schlank-mit-frank-koch-frank-rosin-muss-im-tv-halbnackt-auf-die-waage-sehr-peinlich-richtig-scheisse_id_10139924.html.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": [ "dict_input_example.get(\"additional_attributes\", {}).get(\"date_published_time_at\", \"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "json_files = glob.glob(\"pages/*.json\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "title = []\n", "content = []\n", "category = []\n", "author = []\n", "description = []\n", "date_published_time_at = []\n", "\n", "for json_file in json_files:\n", " with open(json_file, \"r\", encoding=\"utf-8\") as f:\n", " json_input = f.read()\n", " \n", " dict_input = json.loads(json_input)\n", " \n", " title.append(dict_input.get(\"title\", \"\"))\n", " content.append(dict_input.get(\"content\", \"\"))\n", " category.append(dict_input.get(\"additional_attributes\", {}).get(\"category\", \"\"))\n", " author.append(dict_input.get(\"additional_attributes\", {}).get(\"author\", \"\"))\n", " description.append(dict_input.get(\"additional_attributes\", {}).get(\"description\", \"\"))\n", " date_published_time_at.append(parse(dict_input.get(\"additional_attributes\", {}).get(\"date_published_time_at\", \"\")))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = {'title':title, 'content':content, 'category':category, 'author':author, 'description':description, 'date_published_time_at':date_published_time_at}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.to_json(\"focus_de.json\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.to_json(\"focus_de.csv\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset = Dataset.from_pandas(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset.features" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "huggingface_hub.login()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset.push_to_hub(\"SinclairSchneider/focus_de\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset.push_to_hub(\"NLP-UniBW/focus_de\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }