{ "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 }