File size: 6,687 Bytes
5472d34 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import srsly\n",
"import glob\n",
"from collections import Counter"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"files = glob.glob(\"./gold-training-data/*.jsonl\")\n",
"len(files)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"all_data = []\n",
"for filename in files:\n",
" data = list(srsly.read_jsonl(filename))\n",
" for item in data:\n",
" if len(item[\"spans\"]) > 0:\n",
" all_data.append(item)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7868"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(all_data)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'text': 'I was born in a small town called , and I was born May 5, 1928.',\n",
" 'spans': [{'start': 22,\n",
" 'end': 26,\n",
" 'token_start': 6,\n",
" 'token_end': 6,\n",
" 'label': 'POPULATED_PLACE'}],\n",
" '_input_hash': 1949719959,\n",
" '_task_hash': 335893137,\n",
" 'tokens': [{'text': 'I', 'start': 0, 'end': 1, 'id': 0, 'ws': True},\n",
" {'text': 'was', 'start': 2, 'end': 5, 'id': 1, 'ws': True},\n",
" {'text': 'born', 'start': 6, 'end': 10, 'id': 2, 'ws': True},\n",
" {'text': 'in', 'start': 11, 'end': 13, 'id': 3, 'ws': True},\n",
" {'text': 'a', 'start': 14, 'end': 15, 'id': 4, 'ws': True},\n",
" {'text': 'small', 'start': 16, 'end': 21, 'id': 5, 'ws': True},\n",
" {'text': 'town', 'start': 22, 'end': 26, 'id': 6, 'ws': True},\n",
" {'text': 'called', 'start': 27, 'end': 33, 'id': 7, 'ws': True},\n",
" {'text': ',', 'start': 34, 'end': 35, 'id': 8, 'ws': True},\n",
" {'text': 'and', 'start': 36, 'end': 39, 'id': 9, 'ws': True},\n",
" {'text': 'I', 'start': 40, 'end': 41, 'id': 10, 'ws': True},\n",
" {'text': 'was', 'start': 42, 'end': 45, 'id': 11, 'ws': True},\n",
" {'text': 'born', 'start': 46, 'end': 50, 'id': 12, 'ws': True},\n",
" {'text': 'May', 'start': 51, 'end': 54, 'id': 13, 'ws': True},\n",
" {'text': '5', 'start': 55, 'end': 56, 'id': 14, 'ws': False},\n",
" {'text': ',', 'start': 56, 'end': 57, 'id': 15, 'ws': True},\n",
" {'text': '1928', 'start': 58, 'end': 62, 'id': 16, 'ws': False},\n",
" {'text': '.', 'start': 62, 'end': 63, 'id': 17, 'ws': False}],\n",
" '_view_id': 'spans_manual',\n",
" 'answer': 'accept',\n",
" '_timestamp': 1669136567}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"all_data[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"POPULATED_PLACE: 15222\n",
"BUILDING: 11513\n",
"COUNTRY: 5969\n",
"SPATIAL_OBJ: 5680\n",
"DLF: 6191\n",
"INT_SPACE: 3262\n",
"ENV_FEATURES: 1555\n",
"REGION: 1408\n",
"NPIP: 2573\n"
]
}
],
"source": [
"def merge_and_deduplicate_spans(all_data):\n",
" # Mapping of labels to be merged\n",
" label_mapping = {\n",
" 'INTERIOR_SPACE': 'INT_SPACE',\n",
" 'RIVER': 'ENV_FEATURES',\n",
" 'FOREST': 'ENV_FEATURES',\n",
" 'GHETTO': 'POPULATED_PLACE'\n",
" }\n",
"\n",
" # Process each annotation in the dataset\n",
" for annotation in all_data:\n",
" new_spans = [] # List to hold updated and unique spans\n",
"\n",
" # Process each span\n",
" for span in annotation['spans']:\n",
" # Skip spans with the label \"CONTINENT\"\n",
" if span[\"label\"] == \"CONTINENT\":\n",
" continue\n",
"\n",
" # Update label if it's in the mapping\n",
" if span['label'] in label_mapping:\n",
" span['label'] = label_mapping[span['label']]\n",
"\n",
" # Check for duplicates\n",
" if span not in new_spans:\n",
" new_spans.append(span)\n",
"\n",
" # Replace old spans with new_spans\n",
" annotation['spans'] = new_spans\n",
" return all_data\n",
"\n",
"\n",
"all_data = merge_and_deduplicate_spans(all_data)\n",
"\n",
"srsly.write_jsonl(\"assets/annotated_data_spans.jsonl\", all_data)\n",
"\n",
"\n",
"# Create a Counter object for counting labels\n",
"label_counter = Counter()\n",
"\n",
"# Iterate over each annotation in the dataset\n",
"for annotation in all_data:\n",
" # Extract labels from each 'span' in the 'spans' list and add to the counter\n",
" labels = [span['label'] for span in annotation['spans']]\n",
" label_counter.update(labels)\n",
"\n",
"# Print out the counts\n",
"for label, count in label_counter.items():\n",
" print(f\"{label}: {count}\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7868"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(all_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "holocaust",
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|