notebook: add demos for CoNLL-like exporter and Flair dataset loader
Browse files- Export-To-CoNLL.ipynb +76 -0
- FlairDatasetTest.ipynb +152 -0
Export-To-CoNLL.ipynb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "7b378ac1-1035-4f5d-9c16-68c14178389b",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"from pathlib import Path\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"from ftfy import fix_encoding"
|
| 13 |
+
]
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"cell_type": "code",
|
| 17 |
+
"execution_count": 2,
|
| 18 |
+
"id": "1a8017c7-f7a1-4b17-804b-c4d404511969",
|
| 19 |
+
"metadata": {},
|
| 20 |
+
"outputs": [],
|
| 21 |
+
"source": [
|
| 22 |
+
"# Download and unzip archive from: https://www.dfki.uni-kl.de/cybermapping/data/CO-Fun-1.0-anonymized.zip\n",
|
| 23 |
+
"base_path = Path(\"./prepared-data-and-code/NER/CRF/\")\n",
|
| 24 |
+
"\n",
|
| 25 |
+
"splits = {\n",
|
| 26 |
+
" \"train\": base_path / \"train_set.txt\",\n",
|
| 27 |
+
" \"dev\": base_path / \"dev_set.txt\",\n",
|
| 28 |
+
" \"test\": base_path / \"test_set.txt\",\n",
|
| 29 |
+
"}"
|
| 30 |
+
]
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"cell_type": "code",
|
| 34 |
+
"execution_count": 3,
|
| 35 |
+
"id": "ab1deaef-adf3-4c4e-8da2-0bcc833615b3",
|
| 36 |
+
"metadata": {},
|
| 37 |
+
"outputs": [],
|
| 38 |
+
"source": [
|
| 39 |
+
"for split_name, split_file in splits.items():\n",
|
| 40 |
+
" with open(split_file, \"rt\") as f_p:\n",
|
| 41 |
+
" buffer = f_p.readlines()\n",
|
| 42 |
+
" buffer = \"[\" + \"\".join(buffer) + \"]\"\n",
|
| 43 |
+
" data = eval(buffer)\n",
|
| 44 |
+
"\n",
|
| 45 |
+
" with open(f\"{split_name}.tsv\", \"wt\") as f_out:\n",
|
| 46 |
+
" for sentence in data:\n",
|
| 47 |
+
" for line in sentence:\n",
|
| 48 |
+
" token, pos, ner = line\n",
|
| 49 |
+
" token = fix_encoding(token)\n",
|
| 50 |
+
" f_out.write(f\"{token}\\t{pos}\\t{ner}\\n\")\n",
|
| 51 |
+
" f_out.write(\"\\n\")"
|
| 52 |
+
]
|
| 53 |
+
}
|
| 54 |
+
],
|
| 55 |
+
"metadata": {
|
| 56 |
+
"kernelspec": {
|
| 57 |
+
"display_name": "Python 3 (ipykernel)",
|
| 58 |
+
"language": "python",
|
| 59 |
+
"name": "python3"
|
| 60 |
+
},
|
| 61 |
+
"language_info": {
|
| 62 |
+
"codemirror_mode": {
|
| 63 |
+
"name": "ipython",
|
| 64 |
+
"version": 3
|
| 65 |
+
},
|
| 66 |
+
"file_extension": ".py",
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "87270bc7-2da0-490e-be8a-8cb06f38507b",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"from flair.datasets.sequence_labeling import ColumnCorpus\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"from pathlib import Path"
|
| 13 |
+
]
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"cell_type": "code",
|
| 17 |
+
"execution_count": 2,
|
| 18 |
+
"id": "08ebe948-4179-4a75-8c6b-0e2fcad293d7",
|
| 19 |
+
"metadata": {},
|
| 20 |
+
"outputs": [],
|
| 21 |
+
"source": [
|
| 22 |
+
"columns = {0: \"text\", 2: \"ner\"}"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"cell_type": "code",
|
| 27 |
+
"execution_count": 3,
|
| 28 |
+
"id": "af10c49b-002a-41f4-afde-172ac07251c0",
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"outputs": [
|
| 31 |
+
{
|
| 32 |
+
"name": "stdout",
|
| 33 |
+
"output_type": "stream",
|
| 34 |
+
"text": [
|
| 35 |
+
"2024-03-25 12:57:38,583 Reading data from .\n",
|
| 36 |
+
"2024-03-25 12:57:38,585 Train: train.tsv\n",
|
| 37 |
+
"2024-03-25 12:57:38,586 Dev: dev.tsv\n",
|
| 38 |
+
"2024-03-25 12:57:38,586 Test: test.tsv\n"
|
| 39 |
+
]
|
| 40 |
+
}
|
| 41 |
+
],
|
| 42 |
+
"source": [
|
| 43 |
+
"corpus = ColumnCorpus(\".\",\n",
|
| 44 |
+
" column_format=columns,\n",
|
| 45 |
+
" train_file=\"train.tsv\",\n",
|
| 46 |
+
" dev_file=\"dev.tsv\",\n",
|
| 47 |
+
" test_file=\"test.tsv\",\n",
|
| 48 |
+
" comment_symbol=None)"
|
| 49 |
+
]
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"cell_type": "code",
|
| 53 |
+
"execution_count": 4,
|
| 54 |
+
"id": "efd08709-9308-4244-a730-0d51b738520c",
|
| 55 |
+
"metadata": {},
|
| 56 |
+
"outputs": [
|
| 57 |
+
{
|
| 58 |
+
"data": {
|
| 59 |
+
"text/plain": [
|
| 60 |
+
"'Corpus: 758 train + 94 dev + 96 test sentences'"
|
| 61 |
+
]
|
| 62 |
+
},
|
| 63 |
+
"execution_count": 4,
|
| 64 |
+
"metadata": {},
|
| 65 |
+
"output_type": "execute_result"
|
| 66 |
+
}
|
| 67 |
+
],
|
| 68 |
+
"source": [
|
| 69 |
+
"str(corpus)"
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"cell_type": "code",
|
| 74 |
+
"execution_count": 5,
|
| 75 |
+
"id": "6d857c95-ae9f-4aab-803a-0a7e03ff632d",
|
| 76 |
+
"metadata": {},
|
| 77 |
+
"outputs": [],
|
| 78 |
+
"source": [
|
| 79 |
+
"base_path = Path(\"./prepared-data-and-code/\")\n",
|
| 80 |
+
"\n",
|
| 81 |
+
"indices_for_split = {\n",
|
| 82 |
+
" \"train\": base_path / \"train_indices.txt\",\n",
|
| 83 |
+
" \"dev\": base_path / \"valid_indices.txt\",\n",
|
| 84 |
+
" \"test\": base_path / \"test_indices.txt\",\n",
|
| 85 |
+
"}"
|
| 86 |
+
]
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"cell_type": "code",
|
| 90 |
+
"execution_count": 6,
|
| 91 |
+
"id": "2529666f-793f-4d8c-a7aa-9b7d83975310",
|
| 92 |
+
"metadata": {},
|
| 93 |
+
"outputs": [],
|
| 94 |
+
"source": [
|
| 95 |
+
"def get_number_of_total_indices(filename: str) -> int:\n",
|
| 96 |
+
" indices_counter = 0\n",
|
| 97 |
+
" with open(filename, \"rt\") as f_p:\n",
|
| 98 |
+
" for line in f_p:\n",
|
| 99 |
+
" line = line.strip()\n",
|
| 100 |
+
" if not line:\n",
|
| 101 |
+
" continue\n",
|
| 102 |
+
" indices_counter += 1\n",
|
| 103 |
+
" return indices_counter"
|
| 104 |
+
]
|
| 105 |
+
},
|
| 106 |
+
{
|
| 107 |
+
"cell_type": "code",
|
| 108 |
+
"execution_count": 7,
|
| 109 |
+
"id": "49a69247-ef66-445b-b9e8-adcaac282e14",
|
| 110 |
+
"metadata": {},
|
| 111 |
+
"outputs": [],
|
| 112 |
+
"source": [
|
| 113 |
+
"train_indices = get_number_of_total_indices(indices_for_split[\"train\"])\n",
|
| 114 |
+
"dev_indices = get_number_of_total_indices(indices_for_split[\"dev\"])\n",
|
| 115 |
+
"test_indices = get_number_of_total_indices(indices_for_split[\"test\"])"
|
| 116 |
+
]
|
| 117 |
+
},
|
| 118 |
+
{
|
| 119 |
+
"cell_type": "code",
|
| 120 |
+
"execution_count": 8,
|
| 121 |
+
"id": "fb22489b-07d2-4403-a30c-6d92f00d252e",
|
| 122 |
+
"metadata": {},
|
| 123 |
+
"outputs": [],
|
| 124 |
+
"source": [
|
| 125 |
+
"assert len(corpus.train) == train_indices\n",
|
| 126 |
+
"assert len(corpus.dev) == dev_indices\n",
|
| 127 |
+
"assert len(corpus.test) == test_indices"
|
| 128 |
+
]
|
| 129 |
+
}
|
| 130 |
+
],
|
| 131 |
+
"metadata": {
|
| 132 |
+
"kernelspec": {
|
| 133 |
+
"display_name": "Python 3 (ipykernel)",
|
| 134 |
+
"language": "python",
|
| 135 |
+
"name": "python3"
|
| 136 |
+
},
|
| 137 |
+
"language_info": {
|
| 138 |
+
"codemirror_mode": {
|
| 139 |
+
"name": "ipython",
|
| 140 |
+
"version": 3
|
| 141 |
+
},
|
| 142 |
+
"file_extension": ".py",
|
| 143 |
+
"mimetype": "text/x-python",
|
| 144 |
+
"name": "python",
|
| 145 |
+
"nbconvert_exporter": "python",
|
| 146 |
+
"pygments_lexer": "ipython3",
|
| 147 |
+
"version": "3.11.6"
|
| 148 |
+
}
|
| 149 |
+
},
|
| 150 |
+
"nbformat": 4,
|
| 151 |
+
"nbformat_minor": 5
|
| 152 |
+
}
|