notebook: add example for extracting and build CoNLL-like dataset
Browse files- Export-To-CoNLL.ipynb +115 -0
Export-To-CoNLL.ipynb
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "3ab2e823-50c9-40d4-9401-3ed7869da6e2",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"from datasets import load_dataset"
|
11 |
+
]
|
12 |
+
},
|
13 |
+
{
|
14 |
+
"cell_type": "code",
|
15 |
+
"execution_count": 2,
|
16 |
+
"id": "241ea0f7-02bf-4a3e-845c-e262b1d32031",
|
17 |
+
"metadata": {},
|
18 |
+
"outputs": [],
|
19 |
+
"source": [
|
20 |
+
"# Use specific revision for reproducibility!\n",
|
21 |
+
"# See https://huggingface.co/datasets/avramandrei/histnero\n",
|
22 |
+
"revision = \"433ca166efac28c952813c0e78bf301643cf5af3\"\n",
|
23 |
+
"\n",
|
24 |
+
"ds = load_dataset(\"avramandrei/histnero\", revision=revision)"
|
25 |
+
]
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"cell_type": "code",
|
29 |
+
"execution_count": 4,
|
30 |
+
"id": "66878e9e-83e8-4010-b81c-cefbc2ef0da7",
|
31 |
+
"metadata": {},
|
32 |
+
"outputs": [],
|
33 |
+
"source": [
|
34 |
+
"# We are grouping together documents together first!\n",
|
35 |
+
"def perform_document_grouping(dataset_split):\n",
|
36 |
+
" # Document identifier -> Training example\n",
|
37 |
+
" document_mapping = {}\n",
|
38 |
+
"\n",
|
39 |
+
" for document in dataset_split:\n",
|
40 |
+
" doc_id = document[\"doc_id\"]\n",
|
41 |
+
" if doc_id in document_mapping:\n",
|
42 |
+
" document_mapping[doc_id].append(document)\n",
|
43 |
+
" else:\n",
|
44 |
+
" document_mapping[doc_id] = [document]\n",
|
45 |
+
" return document_mapping\n",
|
46 |
+
"\n",
|
47 |
+
"def export_to_conll(grouped_dataset_split, export_filename):\n",
|
48 |
+
" dataset_labels = ds[\"train\"].features[\"ner_tags\"].feature.names\n",
|
49 |
+
" dataset_label_id_to_string = {idx: label_string for idx, label_string in enumerate(dataset_labels)}\n",
|
50 |
+
"\n",
|
51 |
+
" with open(export_filename, \"wt\") as f_out:\n",
|
52 |
+
" for document_name, training_examples in grouped_dataset_split.items():\n",
|
53 |
+
" f_out.write(\"-DOCSTART-\\tO\\n\\n\")\n",
|
54 |
+
"\n",
|
55 |
+
" for training_example in training_examples:\n",
|
56 |
+
" tokens = training_example[\"tokens\"]\n",
|
57 |
+
" ner_label_ids = training_example[\"ner_tags\"]\n",
|
58 |
+
" ner_label_iobs = [dataset_label_id_to_string[ner_label_id] for ner_label_id in ner_label_ids]\n",
|
59 |
+
"\n",
|
60 |
+
" assert len(tokens) == len(ner_label_iobs)\n",
|
61 |
+
"\n",
|
62 |
+
" # Write some metadata first\n",
|
63 |
+
" metadata = [\n",
|
64 |
+
" {\"id\": training_example[\"id\"]},\n",
|
65 |
+
" {\"doc_id\": training_example[\"doc_id\"]},\n",
|
66 |
+
" {\"region\": training_example[\"region\"]},\n",
|
67 |
+
" ]\n",
|
68 |
+
"\n",
|
69 |
+
" for metadata_entry in metadata:\n",
|
70 |
+
" for metadata_name, metadata_value in metadata_entry.items():\n",
|
71 |
+
" f_out.write(f\"# histnero:{metadata_name} = {metadata_value}\\n\")\n",
|
72 |
+
" \n",
|
73 |
+
" for token, ner_label_iob in zip(tokens, ner_label_iobs):\n",
|
74 |
+
" f_out.write(f\"{token}\\t{ner_label_iob}\\n\")\n",
|
75 |
+
"\n",
|
76 |
+
" f_out.write(\"\\n\")"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "code",
|
81 |
+
"execution_count": 7,
|
82 |
+
"id": "afb1dc77-1cde-43d5-9d9a-e7b458c08bb5",
|
83 |
+
"metadata": {},
|
84 |
+
"outputs": [],
|
85 |
+
"source": [
|
86 |
+
"for dataset_split in [\"train\", \"valid\", \"test\"]:\n",
|
87 |
+
" grouped_dataset = perform_document_grouping(ds[dataset_split])\n",
|
88 |
+
"\n",
|
89 |
+
" split_filename = \"dev\" if dataset_split == \"valid\" else dataset_split\n",
|
90 |
+
" export_to_conll(grouped_dataset, f\"{split_filename}.tsv\")"
|
91 |
+
]
|
92 |
+
}
|
93 |
+
],
|
94 |
+
"metadata": {
|
95 |
+
"kernelspec": {
|
96 |
+
"display_name": "Python 3 (ipykernel)",
|
97 |
+
"language": "python",
|
98 |
+
"name": "python3"
|
99 |
+
},
|
100 |
+
"language_info": {
|
101 |
+
"codemirror_mode": {
|
102 |
+
"name": "ipython",
|
103 |
+
"version": 3
|
104 |
+
},
|
105 |
+
"file_extension": ".py",
|
106 |
+
"mimetype": "text/x-python",
|
107 |
+
"name": "python",
|
108 |
+
"nbconvert_exporter": "python",
|
109 |
+
"pygments_lexer": "ipython3",
|
110 |
+
"version": "3.11.6"
|
111 |
+
}
|
112 |
+
},
|
113 |
+
"nbformat": 4,
|
114 |
+
"nbformat_minor": 5
|
115 |
+
}
|