stefan-it commited on
Commit
7059d85
1 Parent(s): 27ee354

notebook: add example of how to construct Flair dataset

Browse files
Files changed (1) hide show
  1. FlairDatasetExample.ipynb +202 -0
FlairDatasetExample.ipynb ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "id": "8f719117-0591-4f80-a071-16b8b1a0a829",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stderr",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "2024-05-10 12:21:50.698143: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n",
14
+ "2024-05-10 12:21:50.701405: I external/local_tsl/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.\n",
15
+ "2024-05-10 12:21:50.745330: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
16
+ "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
17
+ "2024-05-10 12:21:51.413922: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n"
18
+ ]
19
+ }
20
+ ],
21
+ "source": [
22
+ "import flair\n",
23
+ "\n",
24
+ "from flair.datasets.sequence_labeling import ColumnCorpus\n",
25
+ "from flair.file_utils import cached_path\n",
26
+ "\n",
27
+ "from pathlib import Path\n",
28
+ "from typing import Optional, Union"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": 2,
34
+ "id": "0ddcffce-b914-4250-848a-e8cb5b9cc049",
35
+ "metadata": {},
36
+ "outputs": [],
37
+ "source": [
38
+ "class NER_HISTNERO(ColumnCorpus):\n",
39
+ " def __init__(\n",
40
+ " self,\n",
41
+ " base_path: Optional[Union[str, Path]] = None,\n",
42
+ " in_memory: bool = True,\n",
43
+ " **corpusargs,\n",
44
+ " ) -> None:\n",
45
+ " base_path = flair.cache_root / \"datasets\" if not base_path else Path(base_path)\n",
46
+ " dataset_name = self.__class__.__name__.lower()\n",
47
+ " data_folder = base_path / dataset_name\n",
48
+ " data_path = flair.cache_root / \"datasets\" / dataset_name\n",
49
+ "\n",
50
+ " column_format = {0: \"text\", 1: \"ner\"}\n",
51
+ "\n",
52
+ " hf_download_path = \"https://huggingface.co/datasets/stefan-it/histnero/resolve/main\"\n",
53
+ "\n",
54
+ " for split in [\"train\", \"dev\", \"test\"]:\n",
55
+ " cached_path(f\"{hf_download_path}/{split}.tsv\", data_path)\n",
56
+ " \n",
57
+ " super().__init__(\n",
58
+ " data_folder,\n",
59
+ " column_format = {0: \"text\", 1: \"ner\"},\n",
60
+ " column_delimiter=\"\\t\",\n",
61
+ " document_separator_token=\"-DOCSTART-\",\n",
62
+ " in_memory=in_memory,\n",
63
+ " comment_symbol=\"# \",\n",
64
+ " **corpusargs,\n",
65
+ " )"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "code",
70
+ "execution_count": 3,
71
+ "id": "caf404fb-b4f1-4688-81ef-abbd950beb0a",
72
+ "metadata": {},
73
+ "outputs": [
74
+ {
75
+ "name": "stdout",
76
+ "output_type": "stream",
77
+ "text": [
78
+ "2024-05-10 12:21:56,117 Reading data from /home/stefan/.flair/datasets/ner_histnero\n",
79
+ "2024-05-10 12:21:56,118 Train: /home/stefan/.flair/datasets/ner_histnero/train.tsv\n",
80
+ "2024-05-10 12:21:56,119 Dev: /home/stefan/.flair/datasets/ner_histnero/dev.tsv\n",
81
+ "2024-05-10 12:21:56,120 Test: /home/stefan/.flair/datasets/ner_histnero/test.tsv\n"
82
+ ]
83
+ }
84
+ ],
85
+ "source": [
86
+ "corpus = NER_HISTNERO()"
87
+ ]
88
+ },
89
+ {
90
+ "cell_type": "code",
91
+ "execution_count": 4,
92
+ "id": "c38074b8-36c6-47f8-9a9c-b9685514e4bf",
93
+ "metadata": {},
94
+ "outputs": [
95
+ {
96
+ "name": "stdout",
97
+ "output_type": "stream",
98
+ "text": [
99
+ "Corpus: 8149 train + 1114 dev + 1117 test sentences\n"
100
+ ]
101
+ }
102
+ ],
103
+ "source": [
104
+ "print(str(corpus))"
105
+ ]
106
+ },
107
+ {
108
+ "cell_type": "markdown",
109
+ "id": "7f1bfac4-d09f-477a-b1d8-0543d2e4368c",
110
+ "metadata": {},
111
+ "source": [
112
+ "# Tests\n",
113
+ "\n",
114
+ "We now check the number of parsed sentences and compare with the reference values from the HistNERo paper."
115
+ ]
116
+ },
117
+ {
118
+ "cell_type": "code",
119
+ "execution_count": 5,
120
+ "id": "f1dc9c90-c14c-44c6-9716-5e341c73d370",
121
+ "metadata": {},
122
+ "outputs": [],
123
+ "source": [
124
+ "gold_training_split_sentences = {\n",
125
+ " \"train\": 8_020,\n",
126
+ " \"dev\": 1_003,\n",
127
+ " \"test\": 1_003,\n",
128
+ "}\n",
129
+ "\n",
130
+ "flair_corpus_mapping = {\n",
131
+ " \"train\": corpus.train,\n",
132
+ " \"dev\": corpus.dev,\n",
133
+ " \"test\": corpus.test,\n",
134
+ "}"
135
+ ]
136
+ },
137
+ {
138
+ "cell_type": "code",
139
+ "execution_count": 8,
140
+ "id": "957921b4-53f6-4c49-a916-339ff0296eee",
141
+ "metadata": {},
142
+ "outputs": [
143
+ {
144
+ "name": "stdout",
145
+ "output_type": "stream",
146
+ "text": [
147
+ "✔️ Number of parsed sentences for train split\n",
148
+ "✔️ Number of parsed sentences for dev split\n",
149
+ "✔️ Number of parsed sentences for test split\n",
150
+ "✔️ Number of parsed sentences for complete dataset\n"
151
+ ]
152
+ }
153
+ ],
154
+ "source": [
155
+ "actual_total_sentences = 0\n",
156
+ "gold_total_sentences = sum(gold_training_split_sentences.values())\n",
157
+ "\n",
158
+ "for dataset_split in [\"train\", \"dev\", \"test\"]:\n",
159
+ " gold_sentences = gold_training_split_sentences[dataset_split]\n",
160
+ "\n",
161
+ " actual_sentences = 0\n",
162
+ " \n",
163
+ " for sentence in flair_corpus_mapping[dataset_split]:\n",
164
+ " # We do not count document marker as sentences!\n",
165
+ " if sentence[0].text.startswith(\"-DOCSTART-\"):\n",
166
+ " continue\n",
167
+ " actual_sentences += 1\n",
168
+ "\n",
169
+ " actual_total_sentences += actual_sentences\n",
170
+ "\n",
171
+ " assert gold_sentences == actual_sentences, f\"Mismatch of parsed sentences for {dataset_split} split!\"\n",
172
+ "\n",
173
+ " print(f\"✔️ Number of parsed sentences for {dataset_split} split\")\n",
174
+ "\n",
175
+ "assert actual_total_sentences == gold_total_sentences, f\"Mismatch in total parsed sentences!\"\n",
176
+ "\n",
177
+ "print(f\"✔️ Number of parsed sentences for complete dataset\")"
178
+ ]
179
+ }
180
+ ],
181
+ "metadata": {
182
+ "kernelspec": {
183
+ "display_name": "Python 3 (ipykernel)",
184
+ "language": "python",
185
+ "name": "python3"
186
+ },
187
+ "language_info": {
188
+ "codemirror_mode": {
189
+ "name": "ipython",
190
+ "version": 3
191
+ },
192
+ "file_extension": ".py",
193
+ "mimetype": "text/x-python",
194
+ "name": "python",
195
+ "nbconvert_exporter": "python",
196
+ "pygments_lexer": "ipython3",
197
+ "version": "3.11.6"
198
+ }
199
+ },
200
+ "nbformat": 4,
201
+ "nbformat_minor": 5
202
+ }