notebook: example for loading the dataset with Flair
Browse files- FlairDatasetExample.ipynb +196 -0
FlairDatasetExample.ipynb
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "784eec04-5ea5-4664-b5dc-76aca7af15d6",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import flair\n",
|
11 |
+
"\n",
|
12 |
+
"from flair.datasets.sequence_labeling import ColumnCorpus\n",
|
13 |
+
"from flair.file_utils import cached_path\n",
|
14 |
+
"\n",
|
15 |
+
"from pathlib import Path\n",
|
16 |
+
"from typing import Optional, Union"
|
17 |
+
]
|
18 |
+
},
|
19 |
+
{
|
20 |
+
"cell_type": "code",
|
21 |
+
"execution_count": 4,
|
22 |
+
"id": "8a44d348-22a7-4b40-a982-e227ebf5e942",
|
23 |
+
"metadata": {},
|
24 |
+
"outputs": [],
|
25 |
+
"source": [
|
26 |
+
"class NER_CO_FUNER(ColumnCorpus):\n",
|
27 |
+
" def __init__(\n",
|
28 |
+
" self,\n",
|
29 |
+
" base_path: Optional[Union[str, Path]] = None,\n",
|
30 |
+
" in_memory: bool = True,\n",
|
31 |
+
" **corpusargs,\n",
|
32 |
+
" ) -> None:\n",
|
33 |
+
" base_path = flair.cache_root / \"datasets\" if not base_path else Path(base_path)\n",
|
34 |
+
" dataset_name = self.__class__.__name__.lower()\n",
|
35 |
+
" data_folder = base_path / dataset_name\n",
|
36 |
+
" data_path = flair.cache_root / \"datasets\" / dataset_name\n",
|
37 |
+
"\n",
|
38 |
+
" columns = {0: \"text\", 2: \"ner\"}\n",
|
39 |
+
"\n",
|
40 |
+
" hf_download_path = \"https://huggingface.co/datasets/stefan-it/co-funer/resolve/main\"\n",
|
41 |
+
"\n",
|
42 |
+
" for split in [\"train\", \"dev\", \"test\"]:\n",
|
43 |
+
" cached_path(f\"{hf_download_path}/{split}.tsv\", data_path)\n",
|
44 |
+
" \n",
|
45 |
+
" super().__init__(\n",
|
46 |
+
" data_folder,\n",
|
47 |
+
" columns,\n",
|
48 |
+
" in_memory=in_memory,\n",
|
49 |
+
" comment_symbol=None,\n",
|
50 |
+
" **corpusargs,\n",
|
51 |
+
" )"
|
52 |
+
]
|
53 |
+
},
|
54 |
+
{
|
55 |
+
"cell_type": "code",
|
56 |
+
"execution_count": 5,
|
57 |
+
"id": "efea04d2-957b-4b8c-a5b3-bb53f8b5264a",
|
58 |
+
"metadata": {},
|
59 |
+
"outputs": [
|
60 |
+
{
|
61 |
+
"name": "stdout",
|
62 |
+
"output_type": "stream",
|
63 |
+
"text": [
|
64 |
+
"2024-03-25 13:46:15,348 https://huggingface.co/datasets/stefan-it/co-funer/resolve/main/train.tsv not found in cache, downloading to /tmp/tmpezcb9ala\n"
|
65 |
+
]
|
66 |
+
},
|
67 |
+
{
|
68 |
+
"name": "stderr",
|
69 |
+
"output_type": "stream",
|
70 |
+
"text": [
|
71 |
+
"100%|βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 669k/669k [00:00<00:00, 3.97MB/s]"
|
72 |
+
]
|
73 |
+
},
|
74 |
+
{
|
75 |
+
"name": "stdout",
|
76 |
+
"output_type": "stream",
|
77 |
+
"text": [
|
78 |
+
"2024-03-25 13:46:15,771 copying /tmp/tmpezcb9ala to cache at /home/stefan/.flair/datasets/ner_co_funer/train.tsv\n",
|
79 |
+
"2024-03-25 13:46:15,775 removing temp file /tmp/tmpezcb9ala\n"
|
80 |
+
]
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"name": "stderr",
|
84 |
+
"output_type": "stream",
|
85 |
+
"text": [
|
86 |
+
"\n"
|
87 |
+
]
|
88 |
+
},
|
89 |
+
{
|
90 |
+
"name": "stdout",
|
91 |
+
"output_type": "stream",
|
92 |
+
"text": [
|
93 |
+
"2024-03-25 13:46:16,020 https://huggingface.co/datasets/stefan-it/co-funer/resolve/main/dev.tsv not found in cache, downloading to /tmp/tmptresi_zq\n"
|
94 |
+
]
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"name": "stderr",
|
98 |
+
"output_type": "stream",
|
99 |
+
"text": [
|
100 |
+
"100%|βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 74.9k/74.9k [00:00<00:00, 2.40MB/s]"
|
101 |
+
]
|
102 |
+
},
|
103 |
+
{
|
104 |
+
"name": "stdout",
|
105 |
+
"output_type": "stream",
|
106 |
+
"text": [
|
107 |
+
"2024-03-25 13:46:16,299 copying /tmp/tmptresi_zq to cache at /home/stefan/.flair/datasets/ner_co_funer/dev.tsv\n",
|
108 |
+
"2024-03-25 13:46:16,300 removing temp file /tmp/tmptresi_zq\n"
|
109 |
+
]
|
110 |
+
},
|
111 |
+
{
|
112 |
+
"name": "stderr",
|
113 |
+
"output_type": "stream",
|
114 |
+
"text": [
|
115 |
+
"\n"
|
116 |
+
]
|
117 |
+
},
|
118 |
+
{
|
119 |
+
"name": "stdout",
|
120 |
+
"output_type": "stream",
|
121 |
+
"text": [
|
122 |
+
"2024-03-25 13:46:16,516 https://huggingface.co/datasets/stefan-it/co-funer/resolve/main/test.tsv not found in cache, downloading to /tmp/tmpma214nwe\n"
|
123 |
+
]
|
124 |
+
},
|
125 |
+
{
|
126 |
+
"name": "stderr",
|
127 |
+
"output_type": "stream",
|
128 |
+
"text": [
|
129 |
+
"100%|ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββοΏ½οΏ½ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ| 75.0k/75.0k [00:00<00:00, 2.13MB/s]"
|
130 |
+
]
|
131 |
+
},
|
132 |
+
{
|
133 |
+
"name": "stdout",
|
134 |
+
"output_type": "stream",
|
135 |
+
"text": [
|
136 |
+
"2024-03-25 13:46:16,810 copying /tmp/tmpma214nwe to cache at /home/stefan/.flair/datasets/ner_co_funer/test.tsv\n",
|
137 |
+
"2024-03-25 13:46:16,811 removing temp file /tmp/tmpma214nwe\n",
|
138 |
+
"2024-03-25 13:46:16,818 Reading data from /home/stefan/.flair/datasets/ner_co_funer\n",
|
139 |
+
"2024-03-25 13:46:16,819 Train: /home/stefan/.flair/datasets/ner_co_funer/train.tsv\n",
|
140 |
+
"2024-03-25 13:46:16,819 Dev: /home/stefan/.flair/datasets/ner_co_funer/dev.tsv\n",
|
141 |
+
"2024-03-25 13:46:16,820 Test: /home/stefan/.flair/datasets/ner_co_funer/test.tsv\n"
|
142 |
+
]
|
143 |
+
},
|
144 |
+
{
|
145 |
+
"name": "stderr",
|
146 |
+
"output_type": "stream",
|
147 |
+
"text": [
|
148 |
+
"\n"
|
149 |
+
]
|
150 |
+
}
|
151 |
+
],
|
152 |
+
"source": [
|
153 |
+
"corpus = NER_CO_FUNER()"
|
154 |
+
]
|
155 |
+
},
|
156 |
+
{
|
157 |
+
"cell_type": "code",
|
158 |
+
"execution_count": 6,
|
159 |
+
"id": "a83b693a-de00-4bd9-bb00-91cb6b99fdc2",
|
160 |
+
"metadata": {},
|
161 |
+
"outputs": [
|
162 |
+
{
|
163 |
+
"name": "stdout",
|
164 |
+
"output_type": "stream",
|
165 |
+
"text": [
|
166 |
+
"Corpus: 758 train + 94 dev + 96 test sentences\n"
|
167 |
+
]
|
168 |
+
}
|
169 |
+
],
|
170 |
+
"source": [
|
171 |
+
"print(str(corpus))"
|
172 |
+
]
|
173 |
+
}
|
174 |
+
],
|
175 |
+
"metadata": {
|
176 |
+
"kernelspec": {
|
177 |
+
"display_name": "Python 3 (ipykernel)",
|
178 |
+
"language": "python",
|
179 |
+
"name": "python3"
|
180 |
+
},
|
181 |
+
"language_info": {
|
182 |
+
"codemirror_mode": {
|
183 |
+
"name": "ipython",
|
184 |
+
"version": 3
|
185 |
+
},
|
186 |
+
"file_extension": ".py",
|
187 |
+
"mimetype": "text/x-python",
|
188 |
+
"name": "python",
|
189 |
+
"nbconvert_exporter": "python",
|
190 |
+
"pygments_lexer": "ipython3",
|
191 |
+
"version": "3.11.6"
|
192 |
+
}
|
193 |
+
},
|
194 |
+
"nbformat": 4,
|
195 |
+
"nbformat_minor": 5
|
196 |
+
}
|