lynn-twinkl commited on
Commit ·
6c37d4b
1
Parent(s): b80badf
This code debugs labeled json data with trailing punctuation or whitespace
Browse files
notebooks/labeled-ner-data-debugging.ipynb
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 3,
|
| 6 |
+
"id": "dd46f692-4e46-40f2-bbb3-73240922a3e0",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import json\n",
|
| 11 |
+
"import random\n",
|
| 12 |
+
"import spacy\n",
|
| 13 |
+
"from spacy.training import offsets_to_biluo_tags"
|
| 14 |
+
]
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"cell_type": "code",
|
| 18 |
+
"execution_count": 2,
|
| 19 |
+
"id": "d2795420-7d1b-4bff-8662-fe0c7e41cdb0",
|
| 20 |
+
"metadata": {},
|
| 21 |
+
"outputs": [],
|
| 22 |
+
"source": [
|
| 23 |
+
"with open('data/ner-training/03-15-labeled.json', 'r') as file:\n",
|
| 24 |
+
" raw_data = json.load(file)"
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"cell_type": "code",
|
| 29 |
+
"execution_count": 4,
|
| 30 |
+
"id": "e8f1f763-f649-4e89-af08-209c72d17093",
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"outputs": [
|
| 33 |
+
{
|
| 34 |
+
"name": "stdout",
|
| 35 |
+
"output_type": "stream",
|
| 36 |
+
"text": [
|
| 37 |
+
"[Record 8] Mismatch in offset for label 'Usage', text='to develop a sensory garde'\n",
|
| 38 |
+
"[Record 12] Mismatch in offset for label 'Context', text='in a place of lower income, and a very tight budge'\n",
|
| 39 |
+
"[Record 14] Leading/trailing whitespace in span ' This is an extra activity to the curriculum, hence, budgets for seeds, etc are limited.'\n",
|
| 40 |
+
"[Record 20] Mismatch in offset for label 'Usage', text='to support all our children to access the gardening projects around nurser'\n",
|
| 41 |
+
"[Record 20] Leading/trailing whitespace in span ' We are a very outdoors nursery'\n",
|
| 42 |
+
"[Record 25] Mismatch in offset for label 'Context', text='We are a small village Preschool within the main school and We are following the Hygge approach in Preschoo'\n",
|
| 43 |
+
"[Record 34] Leading/trailing whitespace in span 'We have lots of opportunity as we have a pond and bench area but no money to bring it back to life! '\n",
|
| 44 |
+
"[Record 34] Mismatch in offset for label 'Context', text='We have lots of opportunity as we have a pond and bench area but no money to bring it back to life! '\n",
|
| 45 |
+
"[Record 34] Leading/trailing whitespace in span ' bench needs a repaint, the pond could do with some clearing and some logs around the pond would be great to make it a welcomed area.'\n",
|
| 46 |
+
"[Record 34] Mismatch in offset for label 'Usage', text=' bench needs a repaint, the pond could do with some clearing and some logs around the pond would be great to make it a welcomed area.'\n",
|
| 47 |
+
"[Record 40] Mismatch in offset for label 'Context', text='ur gardening club is brilliant, but we never have enough gloves or resources'\n",
|
| 48 |
+
"[Record 41] Leading/trailing whitespace in span ' invest in our outside areas to get children active'\n",
|
| 49 |
+
"[Record 41] Mismatch in offset for label 'Usage', text=' invest in our outside areas to get children active'\n",
|
| 50 |
+
"[Record 42] Leading/trailing whitespace in span ' help us transform more of the concrete box into a naturally diverse play environment.'\n",
|
| 51 |
+
"[Record 42] Mismatch in offset for label 'Usage', text=' help us transform more of the concrete box into a naturally diverse play environment.'\n",
|
| 52 |
+
"[Record 51] Mismatch in offset for label 'Context', text='t Netherbrook, we are committed to hands-on, outdoor learning'\n",
|
| 53 |
+
"[Record 52] Leading/trailing whitespace in span ' to resource this area with compost or seeds / plants'\n",
|
| 54 |
+
"[Record 52] Mismatch in offset for label 'Usage', text=' to resource this area with compost or seeds / plants'\n",
|
| 55 |
+
"[Record 55] Mismatch in offset for label 'Context', text='e have been utilising a large flower bed and the children have been working hard to get it ready to plant flowers and food that we can eat at the end of the summer term'\n",
|
| 56 |
+
"[Record 56] Leading/trailing whitespace in span 'We run a free after-school gardening club Providing opportunities for about twenty children each week '\n",
|
| 57 |
+
"[Record 56] Mismatch in offset for label 'Context', text='We run a free after-school gardening club Providing opportunities for about twenty children each week '\n",
|
| 58 |
+
"[Record 57] Leading/trailing whitespace in span ' to revamp our outdoor area'\n",
|
| 59 |
+
"[Record 57] Mismatch in offset for label 'Usage', text=' to revamp our outdoor area'\n",
|
| 60 |
+
"[Record 58] Leading/trailing whitespace in span ' Our school is in a deprived area so a lot of the children live in flats etc so do not have a garden'\n",
|
| 61 |
+
"[Record 58] Mismatch in offset for label 'Benefit', text='ives them the opportunity to support the school and learn all about gardening'\n",
|
| 62 |
+
"[Record 60] Leading/trailing whitespace in span ' have some planters in the reception outdoor area which need some love and attention!'\n",
|
| 63 |
+
"[Record 60] Mismatch in offset for label 'Context', text=' have some planters in the reception outdoor area which need some love and attention!'\n",
|
| 64 |
+
"[Record 64] Mismatch in offset for label 'Usage', text='to tidy up the village and local care homes with flower displays and planter'\n",
|
| 65 |
+
"[Record 70] Leading/trailing whitespace in span ' Our outdoor area is in desperate need of some love but unfortunately there just isn't the budget for it'\n",
|
| 66 |
+
"[Record 72] Leading/trailing whitespace in span 'We plan to build sensory gardens (such as a Zen garden and fairy garden) '\n",
|
| 67 |
+
"[Record 72] Mismatch in offset for label 'Context', text='We plan to build sensory gardens (such as a Zen garden and fairy garden) '\n",
|
| 68 |
+
"[Record 73] Leading/trailing whitespace in span ' These groups are focused upon our Catholic Social Teaching Principals, one of these being Stewardship'\n",
|
| 69 |
+
"[Record 73] Leading/trailing whitespace in span ' This pupil led group are starting a small project to help create a prayer and reflection space in the Early Years outdoor area'\n",
|
| 70 |
+
"[Record 92] Leading/trailing whitespace in span ' allowing the children to escape there for some peace and enjoy the colours, smells, and textures the garden will bring'\n",
|
| 71 |
+
"[Record 92] Mismatch in offset for label 'Benefit', text=' allowing the children to escape there for some peace and enjoy the colours, smells, and textures the garden will bring'\n",
|
| 72 |
+
"[Record 100] Leading/trailing whitespace in span ' We have dedicated areas across school for students engage with gardening that have fallen into disrepair due to lack of resources'\n",
|
| 73 |
+
"[Record 100] Mismatch in offset for label 'Context', text=' We have dedicated areas across school for students engage with gardening that have fallen into disrepair due to lack of resources'\n",
|
| 74 |
+
"[Record 101] Leading/trailing whitespace in span ' to set up a sensory room for pupils'\n",
|
| 75 |
+
"[Record 101] Mismatch in offset for label 'Usage', text=' to set up a sensory room for pupils'\n",
|
| 76 |
+
"[Record 111] Leading/trailing whitespace in span ' supplies and resources'\n",
|
| 77 |
+
"[Record 111] Mismatch in offset for label 'Usage', text=' supplies and resources'\n",
|
| 78 |
+
"[Record 112] Leading/trailing whitespace in span ' enhance our Learning Support resource room'\n",
|
| 79 |
+
"[Record 112] Mismatch in offset for label 'Benefit', text=' enhance our Learning Support resource room'\n",
|
| 80 |
+
"[Record 114] Leading/trailing whitespace in span ' to improve our school hall or to buy new swing seats for the secondary'\n",
|
| 81 |
+
"[Record 114] Mismatch in offset for label 'Usage', text=' to improve our school hall or to buy new swing seats for the secondary'\n",
|
| 82 |
+
"[Record 126] Mismatch in offset for label 'Usage', text='to develop a small stage area and secure props, costumes, and sound equipmen'\n",
|
| 83 |
+
"[Record 134] Mismatch in offset for label 'Usage', text='o convert an unused space into a community heritage hub featuring interactive exhibits and archival collections'\n",
|
| 84 |
+
"[Record 141] Leading/trailing whitespace in span ' our children have limited exposure to performing arts'\n",
|
| 85 |
+
"[Record 141] Mismatch in offset for label 'Context', text=' our children have limited exposure to performing arts'\n",
|
| 86 |
+
"[Record 148] Leading/trailing whitespace in span ' to purchase sensory equipment, including weighted blankets, fidget toys, and calming lighting'\n",
|
| 87 |
+
"[Record 148] Mismatch in offset for label 'Usage', text=' to purchase sensory equipment, including weighted blankets, fidget toys, and calming lighting'\n",
|
| 88 |
+
"[Record 151] Leading/trailing whitespace in span ' improve literacy and encourage a love of reading in reluctant readers'\n",
|
| 89 |
+
"[Record 151] Mismatch in offset for label 'Benefit', text=' improve literacy and encourage a love of reading in reluctant readers'\n",
|
| 90 |
+
"[Record 157] Leading/trailing whitespace in span ' improve focus, behaviour, and attendance'\n",
|
| 91 |
+
"[Record 157] Mismatch in offset for label 'Benefit', text=' improve focus, behaviour, and attendance'\n",
|
| 92 |
+
"[Record 160] Leading/trailing whitespace in span ' training materials and team-building activities'\n",
|
| 93 |
+
"[Record 160] Mismatch in offset for label 'Usage', text=' training materials and team-building activities'\n",
|
| 94 |
+
"[Record 163] Leading/trailing whitespace in span ' ensure equitable access to learning and promote independence.'\n",
|
| 95 |
+
"[Record 163] Mismatch in offset for label 'Benefit', text=' ensure equitable access to learning and promote independence.'\n",
|
| 96 |
+
"[Record 175] Leading/trailing whitespace in span ' to purchase soft floor mats, noise-canceling headphones, and interactive light panels'\n",
|
| 97 |
+
"[Record 175] Mismatch in offset for label 'Usage', text=' to purchase soft floor mats, noise-canceling headphones, and interactive light panels'\n",
|
| 98 |
+
"[Record 177] Leading/trailing whitespace in span ' I’ve watched our old kiln become almost unusable, and we’re constantly low on clay and paints'\n",
|
| 99 |
+
"[Record 177] Mismatch in offset for label 'Context', text=' I’ve watched our old kiln become almost unusable, and we’re constantly low on clay and paints'\n",
|
| 100 |
+
"[Record 178] Mismatch in offset for label 'Benefit', text='Building trust, resilience, and communication outside the classroom often translates into better collaboration back at schoo'\n"
|
| 101 |
+
]
|
| 102 |
+
},
|
| 103 |
+
{
|
| 104 |
+
"name": "stderr",
|
| 105 |
+
"output_type": "stream",
|
| 106 |
+
"text": [
|
| 107 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We have quite a high level of ALN children in our ...\" with entities \"[(101, 127, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 108 |
+
" warnings.warn(\n",
|
| 109 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We would love to enhance the environment and resou...\" with entities \"[(190, 240, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 110 |
+
" warnings.warn(\n",
|
| 111 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"To win the money or the gardening bundle would be ...\" with entities \"[(59, 133, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 112 |
+
" warnings.warn(\n",
|
| 113 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We are a small village Preschool within the main s...\" with entities \"[(0, 107, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 114 |
+
" warnings.warn(\n",
|
| 115 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We would love to add to our garden area in the bac...\" with entities \"[(80, 180, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 116 |
+
" warnings.warn(\n",
|
| 117 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We would love to add to our garden area in the bac...\" with entities \"[(183, 316, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 118 |
+
" warnings.warn(\n",
|
| 119 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Our gardening club is brilliant, but we never have...\" with entities \"[(1, 77, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 120 |
+
" warnings.warn(\n",
|
| 121 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We are an infant school in the middle of a city bu...\" with entities \"[(213, 264, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 122 |
+
" warnings.warn(\n",
|
| 123 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We are an urban school with a concrete playgroung....\" with entities \"[(238, 324, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 124 |
+
" warnings.warn(\n",
|
| 125 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"At Netherbrook, we are committed to hands-on, outd...\" with entities \"[(1, 62, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 126 |
+
" warnings.warn(\n",
|
| 127 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We have recently had our outdoor area redesigned a...\" with entities \"[(125, 178, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 128 |
+
" warnings.warn(\n",
|
| 129 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Our early years classes have been loving our mud k...\" with entities \"[(107, 275, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 130 |
+
" warnings.warn(\n",
|
| 131 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We run a free after-school gardening club Providin...\" with entities \"[(0, 102, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 132 |
+
" warnings.warn(\n",
|
| 133 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We are currently trying to revamp our outdoor area...\" with entities \"[(23, 50, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 134 |
+
" warnings.warn(\n",
|
| 135 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"The Eco-Council have been working hard to prepare ...\" with entities \"[(506, 583, 'Benefit')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 136 |
+
" warnings.warn(\n",
|
| 137 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We are an urban school without a green space (e.g....\" with entities \"[(68, 153, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 138 |
+
" warnings.warn(\n",
|
| 139 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We are a small, rural school of 60 children with l...\" with entities \"[(389, 465, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 140 |
+
" warnings.warn(\n",
|
| 141 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"I work in an SEN school, where we are introducing ...\" with entities \"[(90, 163, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 142 |
+
" warnings.warn(\n",
|
| 143 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We have a patch of grass that we are planning to t...\" with entities \"[(433, 552, 'Benefit')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 144 |
+
" warnings.warn(\n",
|
| 145 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"At our school, no child gets left behind. We offer...\" with entities \"[(266, 396, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 146 |
+
" warnings.warn(\n",
|
| 147 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We are moving to a new site in September and I wou...\" with entities \"[(57, 93, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 148 |
+
" warnings.warn(\n",
|
| 149 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Our children come from very difficult backgrounds ...\" with entities \"[(218, 241, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 150 |
+
" warnings.warn(\n",
|
| 151 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Budgets are tight, needs are great. We're a small ...\" with entities \"[(393, 436, 'Benefit')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 152 |
+
" warnings.warn(\n",
|
| 153 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Greetings from our SEN school Harford Manor in Nor...\" with entities \"[(1004, 1075, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 154 |
+
" warnings.warn(\n",
|
| 155 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Our school is proud to have recently formed a dram...\" with entities \"[(156, 232, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 156 |
+
" warnings.warn(\n",
|
| 157 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"At Heritage Intermediate, our curriculum lacks opp...\" with entities \"[(108, 219, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 158 |
+
" warnings.warn(\n",
|
| 159 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Coleridge Primary hopes to launch a drama club tha...\" with entities \"[(128, 182, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 160 |
+
" warnings.warn(\n",
|
| 161 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Our school has recently seen an increase in childr...\" with entities \"[(113, 207, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 162 |
+
" warnings.warn(\n",
|
| 163 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Our Year 5 cohort is behind in reading comprehensi...\" with entities \"[(162, 232, 'Benefit')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 164 |
+
" warnings.warn(\n",
|
| 165 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"Our pupils often arrive at school hungry. We would...\" with entities \"[(137, 178, 'Benefit')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 166 |
+
" warnings.warn(\n",
|
| 167 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We are launching a peer mentoring scheme for our Y...\" with entities \"[(94, 142, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 168 |
+
" warnings.warn(\n",
|
| 169 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"We have a number of students with visual impairmen...\" with entities \"[(155, 217, 'Benefit')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 170 |
+
" warnings.warn(\n",
|
| 171 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"During my time working with students who have sens...\" with entities \"[(265, 351, 'Usage')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 172 |
+
" warnings.warn(\n",
|
| 173 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"As the lead art teacher, I’ve watched our old kiln...\" with entities \"[(24, 118, 'Context')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 174 |
+
" warnings.warn(\n",
|
| 175 |
+
"/Users/lynn/venv/lib/python3.12/site-packages/spacy/training/iob_utils.py:149: UserWarning: [W030] Some entities could not be aligned in the text \"I help run a leadership program for high school ju...\" with entities \"[(373, 497, 'Benefit')]\". Use `spacy.training.offsets_to_biluo_tags(nlp.make_doc(text), entities)` to check the alignment. Misaligned entities ('-') will be ignored during training.\n",
|
| 176 |
+
" warnings.warn(\n"
|
| 177 |
+
]
|
| 178 |
+
}
|
| 179 |
+
],
|
| 180 |
+
"source": [
|
| 181 |
+
"debug_nlp = spacy.blank(\"en\")\n",
|
| 182 |
+
"\n",
|
| 183 |
+
"for i, record in enumerate(raw_data):\n",
|
| 184 |
+
" text = record[\"additional_info\"]\n",
|
| 185 |
+
" doc = debug_nlp.make_doc(text)\n",
|
| 186 |
+
" \n",
|
| 187 |
+
" for ann in record[\"label\"]:\n",
|
| 188 |
+
" label = ann[\"labels\"][0]\n",
|
| 189 |
+
" start, end = ann[\"start\"], ann[\"end\"]\n",
|
| 190 |
+
" span_text = text[start:end]\n",
|
| 191 |
+
"\n",
|
| 192 |
+
" # Quick check: leading or trailing whitespace?\n",
|
| 193 |
+
" if span_text != span_text.strip():\n",
|
| 194 |
+
" print(f\"[Record {i}] Leading/trailing whitespace in span '{span_text}'\")\n",
|
| 195 |
+
"\n",
|
| 196 |
+
" # Attempt to convert offset(s) -> BILUO\n",
|
| 197 |
+
" try:\n",
|
| 198 |
+
" biluo_tags = offsets_to_biluo_tags(doc, [(start, end, label)])\n",
|
| 199 |
+
" # If any tag is '-' -> it means partial mismatch\n",
|
| 200 |
+
" if any(t == \"-\" for t in biluo_tags):\n",
|
| 201 |
+
" print(f\"[Record {i}] Mismatch in offset for label '{label}', text='{span_text}'\")\n",
|
| 202 |
+
" except Exception as e:\n",
|
| 203 |
+
" print(f\"[Record {i}] Error converting offsets for '{span_text}': {e}\")\n"
|
| 204 |
+
]
|
| 205 |
+
},
|
| 206 |
+
{
|
| 207 |
+
"cell_type": "code",
|
| 208 |
+
"execution_count": 6,
|
| 209 |
+
"id": "a219e932-b161-4634-8914-5d5a9f7def54",
|
| 210 |
+
"metadata": {},
|
| 211 |
+
"outputs": [],
|
| 212 |
+
"source": [
|
| 213 |
+
"import json\n",
|
| 214 |
+
"import re\n",
|
| 215 |
+
"\n",
|
| 216 |
+
"def trim_and_fix_offsets(raw_data, context_key=\"additional_info\"):\n",
|
| 217 |
+
" \"\"\"\n",
|
| 218 |
+
" Attempt to fix leading/trailing whitespace in spans and recalc offsets.\n",
|
| 219 |
+
" Then do a local substring search to fix minor misalignments.\n",
|
| 220 |
+
" \"\"\"\n",
|
| 221 |
+
" fixed_data = []\n",
|
| 222 |
+
" for i, record in enumerate(raw_data):\n",
|
| 223 |
+
" text = record[context_key]\n",
|
| 224 |
+
" new_labels = []\n",
|
| 225 |
+
" for ann in record[\"label\"]:\n",
|
| 226 |
+
" label = ann[\"labels\"][0]\n",
|
| 227 |
+
" old_start, old_end = ann[\"start\"], ann[\"end\"]\n",
|
| 228 |
+
" original_substring = text[old_start:old_end]\n",
|
| 229 |
+
" trimmed_substring = original_substring.strip()\n",
|
| 230 |
+
" \n",
|
| 231 |
+
" # 1) Trim leading/trailing whitespace offsets\n",
|
| 232 |
+
" # Move start forward while it points to space\n",
|
| 233 |
+
" start = old_start\n",
|
| 234 |
+
" while start < old_end and text[start].isspace():\n",
|
| 235 |
+
" start += 1\n",
|
| 236 |
+
" # Move end backward while it points to space\n",
|
| 237 |
+
" end = old_end\n",
|
| 238 |
+
" while end > start and text[end - 1].isspace():\n",
|
| 239 |
+
" end -= 1\n",
|
| 240 |
+
" \n",
|
| 241 |
+
" # After naive trimming, see if the substring still matches\n",
|
| 242 |
+
" new_substring = text[start:end]\n",
|
| 243 |
+
" if new_substring == trimmed_substring:\n",
|
| 244 |
+
" # Great, we can trust these offsets directly\n",
|
| 245 |
+
" pass\n",
|
| 246 |
+
" else:\n",
|
| 247 |
+
" # Possibly there's hidden Unicode or the original offset was off.\n",
|
| 248 |
+
" # We'll do a local substring search around `old_start`.\n",
|
| 249 |
+
" # We'll search for `trimmed_substring` in a window of +/- 30 chars.\n",
|
| 250 |
+
" window_size = 30\n",
|
| 251 |
+
" \n",
|
| 252 |
+
" # Define a safe search window in the text\n",
|
| 253 |
+
" search_start = max(0, old_start - window_size)\n",
|
| 254 |
+
" search_end = min(len(text), old_end + window_size)\n",
|
| 255 |
+
" window_text = text[search_start:search_end]\n",
|
| 256 |
+
" \n",
|
| 257 |
+
" # Try to find the first occurrence of trimmed_substring in that window\n",
|
| 258 |
+
" local_pos = window_text.find(trimmed_substring)\n",
|
| 259 |
+
" if local_pos != -1:\n",
|
| 260 |
+
" # Recalc absolute offset\n",
|
| 261 |
+
" start = search_start + local_pos\n",
|
| 262 |
+
" end = start + len(trimmed_substring)\n",
|
| 263 |
+
" new_substring = text[start:end]\n",
|
| 264 |
+
" else:\n",
|
| 265 |
+
" # We failed to find it in the local region\n",
|
| 266 |
+
" print(f\"[Record {i}] Can't find '{trimmed_substring}' near offset {old_start}-{old_end}\")\n",
|
| 267 |
+
" # We'll leave this annotation as-is or skip it\n",
|
| 268 |
+
" start, end = old_start, old_end\n",
|
| 269 |
+
" new_substring = original_substring\n",
|
| 270 |
+
"\n",
|
| 271 |
+
" new_labels.append({\n",
|
| 272 |
+
" \"start\": start,\n",
|
| 273 |
+
" \"end\": end,\n",
|
| 274 |
+
" \"text\": new_substring,\n",
|
| 275 |
+
" \"labels\": [label]\n",
|
| 276 |
+
" })\n",
|
| 277 |
+
" \n",
|
| 278 |
+
" # Update the record with the new label data\n",
|
| 279 |
+
" new_record = dict(record)\n",
|
| 280 |
+
" new_record[\"label\"] = new_labels\n",
|
| 281 |
+
" fixed_data.append(new_record)\n",
|
| 282 |
+
" \n",
|
| 283 |
+
" return fixed_data\n",
|
| 284 |
+
"\n",
|
| 285 |
+
"\n",
|
| 286 |
+
"# Usage example:\n",
|
| 287 |
+
"# 1) Read your JSON\n",
|
| 288 |
+
"with open(\"data/ner-training/03-15-labeled.json\", \"r\", encoding=\"utf-8\") as f:\n",
|
| 289 |
+
" raw_data = json.load(f)\n",
|
| 290 |
+
"\n",
|
| 291 |
+
"# 2) Fix whitespace + do local substring search\n",
|
| 292 |
+
"fixed_data = trim_and_fix_offsets(raw_data, context_key=\"additional_info\")\n",
|
| 293 |
+
"\n",
|
| 294 |
+
"# 3) Write the fixed data back out\n",
|
| 295 |
+
"with open(\"data/ner-training/03-15-labeled-fixed.json\", \"w\", encoding=\"utf-8\") as out:\n",
|
| 296 |
+
" json.dump(fixed_data, out, indent=2, ensure_ascii=False)"
|
| 297 |
+
]
|
| 298 |
+
},
|
| 299 |
+
{
|
| 300 |
+
"cell_type": "code",
|
| 301 |
+
"execution_count": null,
|
| 302 |
+
"id": "d2a1a315-b7c5-4a91-875a-c540e05efe78",
|
| 303 |
+
"metadata": {},
|
| 304 |
+
"outputs": [],
|
| 305 |
+
"source": []
|
| 306 |
+
}
|
| 307 |
+
],
|
| 308 |
+
"metadata": {
|
| 309 |
+
"kernelspec": {
|
| 310 |
+
"display_name": "Python 3 (ipykernel)",
|
| 311 |
+
"language": "python",
|
| 312 |
+
"name": "python3"
|
| 313 |
+
},
|
| 314 |
+
"language_info": {
|
| 315 |
+
"codemirror_mode": {
|
| 316 |
+
"name": "ipython",
|
| 317 |
+
"version": 3
|
| 318 |
+
},
|
| 319 |
+
"file_extension": ".py",
|
| 320 |
+
"mimetype": "text/x-python",
|
| 321 |
+
"name": "python",
|
| 322 |
+
"nbconvert_exporter": "python",
|
| 323 |
+
"pygments_lexer": "ipython3",
|
| 324 |
+
"version": "3.12.9"
|
| 325 |
+
}
|
| 326 |
+
},
|
| 327 |
+
"nbformat": 4,
|
| 328 |
+
"nbformat_minor": 5
|
| 329 |
+
}
|