{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# `VICE Deutschland (vice.com/de)` — consolidation notebook\n", "\n", "This notebook is the second half of the pipeline behind\n", "[`SinclairSchneider/vice_de`](https://huggingface.co/datasets/SinclairSchneider/vice_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,\n", "3. drops duplicate articles by headline (`drop_duplicates(subset=['title'])`),\n", "4. writes `vice_de.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/zu-sein-ist-kein-political-correctness-wahn-es-ist-ein-ausdruck-von-menschlichkeit-345%252F%26app_id%3D966242223397117%26source_surface%3Dexternal_reshare%26display%26hashtag&locale=de_DE&display=page.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", "keywords = []\n", "category = []\n", "datePublished = []\n", "dateModified = []\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", " keywords.append(d['additional_attributes']['keywords'])\n", " category.append(d['additional_attributes']['category'])\n", " datePublished.append(d['additional_attributes']['datePublished'])\n", " dateModified.append(d['additional_attributes']['dateModified'])\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", " 'keywords': keywords,\n", " 'category': category,\n", " 'datePublished': datePublished,\n", " 'dateModified': dateModified,\n", " 'url': url,\n", " }\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df" ] }, { "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[[\"We’re sorry, the page you’re looking for could not be found\" not in x for x in df['content']]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = df.reset_index(drop=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.to_json(\"vice_de.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/vice_de\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds.push_to_hub(\"NLP-UniBW/vice_de\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }