File size: 24,096 Bytes
64e8aea |
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
{
"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
}
|