Datasets:
File size: 4,976 Bytes
5bf8f7f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | {
"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
} |