{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `EUobserver (euobserver.com)` — consolidation notebook\n", "\n", "This notebook is the second half of the pipeline behind\n", "[`SinclairSchneider/euobserver_com`](https://huggingface.co/datasets/SinclairSchneider/euobserver_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 headline (`drop_duplicates(subset=['title'])`),\n", "4. writes `euobserver_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/*_ar02116e5d.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", "author = []\n", "description = []\n", "keywords = []\n", "category = []\n", "datePublished = []\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", " author.append(d['additional_attributes']['author'])\n", " description.append(d['additional_attributes']['description'])\n", " keywords.append(d['additional_attributes']['keywords'])\n", " category.append(d['additional_attributes']['category'])\n", " datePublished.append(d['additional_attributes']['datePublished'])\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", " 'author':author,\n", " 'description':description,\n", " 'keywords':keywords,\n", " 'category':category,\n", " 'datePublished':datePublished,\n", " 'url':url,\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = df.drop_duplicates(subset=['title'])" ] }, { "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(\"euobserver_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/euobserver_com\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.push_to_hub(\"NLP-UniBW/euobserver_com\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }