{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "1a6ca1fd-66c6-4da6-af7f-35879fb663ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: tldextract in /home/stefan/.venvs/flair/lib/python3.12/site-packages (5.1.2)\n", "Requirement already satisfied: idna in /home/stefan/.venvs/flair/lib/python3.12/site-packages (from tldextract) (3.7)\n", "Requirement already satisfied: requests>=2.1.0 in /home/stefan/.venvs/flair/lib/python3.12/site-packages (from tldextract) (2.32.2)\n", "Requirement already satisfied: requests-file>=1.4 in /home/stefan/.venvs/flair/lib/python3.12/site-packages (from tldextract) (2.1.0)\n", "Requirement already satisfied: filelock>=3.0.8 in /home/stefan/.venvs/flair/lib/python3.12/site-packages (from tldextract) (3.13.1)\n", "Requirement already satisfied: charset-normalizer<4,>=2 in /home/stefan/.venvs/flair/lib/python3.12/site-packages (from requests>=2.1.0->tldextract) (3.3.2)\n", "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/stefan/.venvs/flair/lib/python3.12/site-packages (from requests>=2.1.0->tldextract) (1.26.18)\n", "Requirement already satisfied: certifi>=2017.4.17 in /home/stefan/.venvs/flair/lib/python3.12/site-packages (from requests>=2.1.0->tldextract) (2024.2.2)\n" ] } ], "source": [ "!pip3 install tldextract" ] }, { "cell_type": "code", "execution_count": 2, "id": "4906d36b-6a06-4987-b032-4de6a11bfc65", "metadata": {}, "outputs": [], "source": [ "import random\n", "import tldextract\n", "\n", "from collections import Counter\n", "from tabulate import tabulate" ] }, { "cell_type": "code", "execution_count": 3, "id": "0107c076-c40a-41ae-bf51-92ccddaf2e61", "metadata": {}, "outputs": [], "source": [ "dataset_splits = {\n", " \"train\": \"./original/NER-de-train.tsv\",\n", " \"dev\": \"./original/NER-de-dev.tsv\",\n", " \"test\": \"./original/NER-de-test.tsv\",\n", "}" ] }, { "cell_type": "markdown", "id": "b11a81f2-0e71-4bbc-9538-b1f3295c4b38", "metadata": {}, "source": [ "# Dataset Stats" ] }, { "cell_type": "code", "execution_count": 4, "id": "530c2634-19ee-4814-842e-b834024fa8c9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "GermEval 2014 Dataset Stats for train split:\n", "| TLD | Number of examples (Percentage) |\n", "|----------------------|-----------------------------------|\n", "| wikipedia.org | 12007 (50.03%) |\n", "| welt.de | 662 (2.76%) |\n", "| spiegel.de | 512 (2.13%) |\n", "| tagesspiegel.de | 424 (1.77%) |\n", "| handelsblatt.com | 369 (1.54%) |\n", "| fr-aktuell.de | 344 (1.43%) |\n", "| sueddeutsche.de | 308 (1.28%) |\n", "| abendblatt.de | 283 (1.18%) |\n", "| berlinonline.de | 255 (1.06%) |\n", "| szon.de | 249 (1.04%) |\n", "| n-tv.de | 195 (0.81%) |\n", "| yahoo.com | 192 (0.8%) |\n", "| feedsportal.com | 173 (0.72%) |\n", "| ngz-online.de | 173 (0.72%) |\n", "| faz.net | 156 (0.65%) |\n", "| nzz.ch | 146 (0.61%) |\n", "| morgenweb.de | 134 (0.56%) |\n", "| rp-online.de | 132 (0.55%) |\n", "| gea.de | 131 (0.55%) |\n", "| sat1.de | 126 (0.53%) |\n", "| tagesschau.de | 124 (0.52%) |\n", "| pnp.de | 101 (0.42%) |\n", "| orf.at | 98 (0.41%) |\n", "| n24.de | 98 (0.41%) |\n", "| finanznachrichten.de | 91 (0.38%) |\n", "\n", "GermEval 2014 Dataset Stats for dev split:\n", "| TLD | Number of examples (Percentage) |\n", "|----------------------|-----------------------------------|\n", "| wikipedia.org | 1119 (50.86%) |\n", "| welt.de | 46 (2.09%) |\n", "| spiegel.de | 43 (1.95%) |\n", "| fr-aktuell.de | 38 (1.73%) |\n", "| tagesspiegel.de | 37 (1.68%) |\n", "| handelsblatt.com | 35 (1.59%) |\n", "| sueddeutsche.de | 28 (1.27%) |\n", "| szon.de | 25 (1.14%) |\n", "| feedsportal.com | 24 (1.09%) |\n", "| berlinonline.de | 22 (1.0%) |\n", "| rp-online.de | 21 (0.95%) |\n", "| abendblatt.de | 20 (0.91%) |\n", "| ngz-online.de | 19 (0.86%) |\n", "| n-tv.de | 18 (0.82%) |\n", "| yahoo.com | 15 (0.68%) |\n", "| sat1.de | 15 (0.68%) |\n", "| orf.at | 13 (0.59%) |\n", "| finanznachrichten.de | 13 (0.59%) |\n", "| tagesschau.de | 13 (0.59%) |\n", "| nzz.ch | 12 (0.55%) |\n", "| faz.net | 12 (0.55%) |\n", "| morgenweb.de | 12 (0.55%) |\n", "| 20min.ch | 11 (0.5%) |\n", "| pnp.de | 11 (0.5%) |\n", "| focus.de | 10 (0.45%) |\n", "\n", "GermEval 2014 Dataset Stats for test split:\n", "| TLD | Number of examples (Percentage) |\n", "|----------------------|-----------------------------------|\n", "| wikipedia.org | 2547 (49.94%) |\n", "| welt.de | 139 (2.73%) |\n", "| spiegel.de | 88 (1.73%) |\n", "| tagesspiegel.de | 86 (1.69%) |\n", "| handelsblatt.com | 84 (1.65%) |\n", "| sueddeutsche.de | 78 (1.53%) |\n", "| abendblatt.de | 72 (1.41%) |\n", "| fr-aktuell.de | 62 (1.22%) |\n", "| berlinonline.de | 59 (1.16%) |\n", "| szon.de | 57 (1.12%) |\n", "| feedsportal.com | 52 (1.02%) |\n", "| n-tv.de | 47 (0.92%) |\n", "| sat1.de | 42 (0.82%) |\n", "| nzz.ch | 39 (0.76%) |\n", "| yahoo.com | 38 (0.75%) |\n", "| ngz-online.de | 37 (0.73%) |\n", "| faz.net | 37 (0.73%) |\n", "| morgenweb.de | 36 (0.71%) |\n", "| taz.de | 28 (0.55%) |\n", "| finanznachrichten.de | 25 (0.49%) |\n", "| tagesschau.de | 24 (0.47%) |\n", "| gea.de | 24 (0.47%) |\n", "| bernerzeitung.ch | 23 (0.45%) |\n", "| ftd.de | 22 (0.43%) |\n", "| orf.at | 21 (0.41%) |\n", "\n" ] } ], "source": [ "def print_stats(dataset_split, dataset_path, limit=25):\n", " hostname_counter = Counter()\n", "\n", " with open(dataset_path, \"rt\") as f_p:\n", " for line in f_p:\n", " if not line.startswith(\"#\"):\n", " continue\n", " \n", " current_url = line.split(\"\\t\")[1].split(\" \")[0]\n", " \n", " ext = tldextract.extract(current_url)\n", " \n", " hostname = ext.registered_domain\n", " \n", " hostname_counter[hostname] += 1\n", "\n", " # Print nice table\n", " headers = [\"TLD\", \"Number of examples (Percentage)\"]\n", "\n", " table = []\n", "\n", " total_examples = sum(hostname_counter.values())\n", " \n", " for tld_name, examples in hostname_counter.most_common(limit):\n", " current_percentage = round(examples / total_examples * 100, 2)\n", " table.append([tld_name, f\"{examples} ({current_percentage}%)\"])\n", "\n", " print(tabulate(table, headers=headers, tablefmt=\"github\"))\n", "\n", "for dataset_split in dataset_splits.keys():\n", " print(f\"GermEval 2014 Dataset Stats for {dataset_split} split:\")\n", " print_stats(dataset_split, dataset_splits[dataset_split])\n", " print(\"\")" ] }, { "cell_type": "markdown", "id": "d97e3e86-ae0e-4a54-af15-de195608a071", "metadata": {}, "source": [ "# Generate New Dataset Split without Wikipedia" ] }, { "cell_type": "code", "execution_count": 5, "id": "4a081af0-f2fe-4e1a-ae56-da10b2bd7be1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing out train split...\n", "Writing out dev split...\n", "Writing out test split...\n" ] } ], "source": [ "def filter_out_wikipedia(dataset_split):\n", " with open(dataset_splits[dataset_split], \"rt\") as f_p:\n", " all_sentences = []\n", " current_sentence = []\n", " for line in f_p:\n", " line = line.strip()\n", " if not line:\n", " # We found new sentence, yeah!\n", "\n", " if len(current_sentence) == 0:\n", " continue\n", " \n", " all_sentences.append(current_sentence)\n", " current_sentence = []\n", " continue\n", "\n", " current_sentence.append(line)\n", "\n", " if len(current_sentence) > 0:\n", " all_sentences.append(current_sentence)\n", " \n", " with open(f\"NER-de-without-wikipedia-{dataset_split}.tsv\", \"wt\") as f_out:\n", " for sentence in all_sentences:\n", "\n", " header = sentence[0]\n", " assert header.startswith(\"#\")\n", "\n", " current_url = header.split(\"\\t\")[1].split(\" \")[0]\n", " ext = tldextract.extract(current_url)\n", " hostname = ext.registered_domain\n", "\n", " if hostname == \"wikipedia.org\":\n", " continue\n", "\n", " f_out.write(\"\\n\".join(sentence) + \"\\n\\n\")\n", "\n", "for dataset_split in dataset_splits.keys():\n", " print(f\"Writing out {dataset_split} split...\")\n", " filter_out_wikipedia(dataset_split)" ] }, { "cell_type": "markdown", "id": "84321c99-7085-467d-8d9c-7966bddd21de", "metadata": {}, "source": [ "# Load with Flair" ] }, { "cell_type": "code", "execution_count": 6, "id": "be671ee3-e562-43a5-8734-144b3a129993", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2024-05-29 17:08:26.593572: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-05-29 17:08:26.597226: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n", "2024-05-29 17:08:26.644331: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", "2024-05-29 17:08:27.578466: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" ] } ], "source": [ "import flair\n", "\n", "from flair.datasets import NER_GERMAN_GERMEVAL\n", "from flair.datasets.sequence_labeling import ColumnCorpus\n", "from flair.file_utils import cached_path\n", "\n", "from pathlib import Path\n", "from typing import Optional, Union\n", "\n", "\n", "class NER_GERMEVAL_2014_NO_WIKIPEDIA(ColumnCorpus):\n", " def __init__(\n", " self,\n", " base_path: Optional[Union[str, Path]] = None,\n", " in_memory: bool = True,\n", " **corpusargs,\n", " ) -> None:\n", " base_path = flair.cache_root / \"datasets\" if not base_path else Path(base_path)\n", " dataset_name = self.__class__.__name__.lower()\n", " data_folder = base_path / dataset_name\n", " data_path = flair.cache_root / \"datasets\" / dataset_name\n", "\n", " column_format = {1: \"text\", 2: \"ner\"}\n", "\n", " #hf_download_path = \"https://huggingface.co./datasets/stefan-it/germeval14_no_wikipedia/resolve/main\"\n", "\n", " #for split in [\"train\", \"dev\", \"test\"]:\n", " # cached_path(f\"{hf_download_path}/NER-de-without-wikipedia-{split}.tsv\", data_path)\n", " \n", " super().__init__(\n", " \"./\", #data_folder,\n", " column_format = {0: \"text\", 1: \"ner\"},\n", " column_delimiter=\"\\t\",\n", " document_separator_token=\"-DOCSTART-\",\n", " in_memory=in_memory,\n", " comment_symbol=\"# \",\n", " **corpusargs,\n", " )" ] }, { "cell_type": "markdown", "id": "7c9d0275-dc4e-4ca0-9a49-b582a42d6bca", "metadata": {}, "source": [ "# New Corpus Stats" ] }, { "cell_type": "code", "execution_count": 7, "id": "8b1a34ab-42c5-4e72-a63e-823f7f2b3cbe", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2024-05-29 17:08:30,120 Reading data from .\n", "2024-05-29 17:08:30,121 Train: NER-de-without-wikipedia-train.tsv\n", "2024-05-29 17:08:30,123 Dev: NER-de-without-wikipedia-dev.tsv\n", "2024-05-29 17:08:30,124 Test: NER-de-without-wikipedia-test.tsv\n", "2024-05-29 17:08:34,319 Reading data from /home/stefan/.flair/datasets/ner_german_germeval\n", "2024-05-29 17:08:34,320 Train: /home/stefan/.flair/datasets/ner_german_germeval/train.tsv\n", "2024-05-29 17:08:34,321 Dev: /home/stefan/.flair/datasets/ner_german_germeval/dev.tsv\n", "2024-05-29 17:08:34,321 Test: /home/stefan/.flair/datasets/ner_german_germeval/test.tsv\n" ] } ], "source": [ "corpus = NER_GERMEVAL_2014_NO_WIKIPEDIA()\n", "original_corpus = NER_GERMAN_GERMEVAL()" ] }, { "cell_type": "code", "execution_count": 8, "id": "86047123-c882-433b-8d19-92626e878afd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original GermEval 2014 stats: Corpus: 24000 train + 2200 dev + 5100 test sentences\n", "Filtered-out GermEval 2014 stats: Corpus: 11993 train + 1081 dev + 2553 test sentences\n" ] } ], "source": [ "print(\"Original GermEval 2014 stats:\", str(original_corpus))\n", "print(\"Filtered-out GermEval 2014 stats:\", str(corpus))" ] }, { "cell_type": "code", "execution_count": 9, "id": "9e6ffb09-b1c0-4559-b954-df8373492876", "metadata": {}, "outputs": [], "source": [ "new_dataset_splits = {\n", " \"train\": \"./NER-de-without-wikipedia-train.tsv\",\n", " \"dev\": \"./NER-de-without-wikipedia-dev.tsv\",\n", " \"test\": \"./NER-de-without-wikipedia-test.tsv\",\n", "}" ] }, { "cell_type": "code", "execution_count": 11, "id": "9ea60c74-70e2-473c-96c7-b6cdfc601297", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "New GermEval 2014 Dataset Stats for train split:\n", "| TLD | Number of examples (Percentage) |\n", "|----------------------|-----------------------------------|\n", "| welt.de | 662 (5.52%) |\n", "| spiegel.de | 512 (4.27%) |\n", "| tagesspiegel.de | 424 (3.54%) |\n", "| handelsblatt.com | 369 (3.08%) |\n", "| fr-aktuell.de | 344 (2.87%) |\n", "| sueddeutsche.de | 308 (2.57%) |\n", "| abendblatt.de | 283 (2.36%) |\n", "| berlinonline.de | 255 (2.13%) |\n", "| szon.de | 249 (2.08%) |\n", "| n-tv.de | 195 (1.63%) |\n", "| yahoo.com | 192 (1.6%) |\n", "| feedsportal.com | 173 (1.44%) |\n", "| ngz-online.de | 173 (1.44%) |\n", "| faz.net | 156 (1.3%) |\n", "| nzz.ch | 146 (1.22%) |\n", "| morgenweb.de | 134 (1.12%) |\n", "| rp-online.de | 132 (1.1%) |\n", "| gea.de | 131 (1.09%) |\n", "| sat1.de | 126 (1.05%) |\n", "| tagesschau.de | 124 (1.03%) |\n", "| pnp.de | 101 (0.84%) |\n", "| orf.at | 98 (0.82%) |\n", "| n24.de | 98 (0.82%) |\n", "| finanznachrichten.de | 91 (0.76%) |\n", "| taz.de | 91 (0.76%) |\n", "\n", "New GermEval 2014 Dataset Stats for dev split:\n", "| TLD | Number of examples (Percentage) |\n", "|----------------------|-----------------------------------|\n", "| welt.de | 46 (4.26%) |\n", "| spiegel.de | 43 (3.98%) |\n", "| fr-aktuell.de | 38 (3.52%) |\n", "| tagesspiegel.de | 37 (3.42%) |\n", "| handelsblatt.com | 35 (3.24%) |\n", "| sueddeutsche.de | 28 (2.59%) |\n", "| szon.de | 25 (2.31%) |\n", "| feedsportal.com | 24 (2.22%) |\n", "| berlinonline.de | 22 (2.04%) |\n", "| rp-online.de | 21 (1.94%) |\n", "| abendblatt.de | 20 (1.85%) |\n", "| ngz-online.de | 19 (1.76%) |\n", "| n-tv.de | 18 (1.67%) |\n", "| yahoo.com | 15 (1.39%) |\n", "| sat1.de | 15 (1.39%) |\n", "| orf.at | 13 (1.2%) |\n", "| finanznachrichten.de | 13 (1.2%) |\n", "| tagesschau.de | 13 (1.2%) |\n", "| nzz.ch | 12 (1.11%) |\n", "| faz.net | 12 (1.11%) |\n", "| morgenweb.de | 12 (1.11%) |\n", "| 20min.ch | 11 (1.02%) |\n", "| pnp.de | 11 (1.02%) |\n", "| focus.de | 10 (0.93%) |\n", "| ftd.de | 9 (0.83%) |\n", "\n", "New GermEval 2014 Dataset Stats for test split:\n", "| TLD | Number of examples (Percentage) |\n", "|----------------------|-----------------------------------|\n", "| welt.de | 139 (5.44%) |\n", "| spiegel.de | 88 (3.45%) |\n", "| tagesspiegel.de | 86 (3.37%) |\n", "| handelsblatt.com | 84 (3.29%) |\n", "| sueddeutsche.de | 78 (3.06%) |\n", "| abendblatt.de | 72 (2.82%) |\n", "| fr-aktuell.de | 62 (2.43%) |\n", "| berlinonline.de | 59 (2.31%) |\n", "| szon.de | 57 (2.23%) |\n", "| feedsportal.com | 52 (2.04%) |\n", "| n-tv.de | 47 (1.84%) |\n", "| sat1.de | 42 (1.65%) |\n", "| nzz.ch | 39 (1.53%) |\n", "| yahoo.com | 38 (1.49%) |\n", "| ngz-online.de | 37 (1.45%) |\n", "| faz.net | 37 (1.45%) |\n", "| morgenweb.de | 36 (1.41%) |\n", "| taz.de | 28 (1.1%) |\n", "| finanznachrichten.de | 25 (0.98%) |\n", "| tagesschau.de | 24 (0.94%) |\n", "| gea.de | 24 (0.94%) |\n", "| bernerzeitung.ch | 23 (0.9%) |\n", "| ftd.de | 22 (0.86%) |\n", "| orf.at | 21 (0.82%) |\n", "| rp-online.de | 21 (0.82%) |\n", "\n" ] } ], "source": [ "for dataset_split in new_dataset_splits.keys():\n", " print(f\"New GermEval 2014 Dataset Stats for {dataset_split} split:\")\n", " print_stats(dataset_split, new_dataset_splits[dataset_split])\n", " print(\"\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 5 }