{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "6aa5fb66", "metadata": {}, "outputs": [], "source": [ "import os\n", "import requests" ] }, { "cell_type": "code", "execution_count": null, "id": "445679f5", "metadata": {}, "outputs": [], "source": [ "def convert_txt_to_jsonl(base_url, domain):\n", " \n", " urls = {\n", " \"train\": f\"{base_url}/{domain}/train.txt\",\n", " \"validation\": f\"{base_url}/{domain}/dev.txt\",\n", " \"test\": f\"{base_url}/{domain}/test.txt\",\n", " }\n", "\n", " os.makedirs(domain, exist_ok=True)\n", "\n", " for split, url in urls.items():\n", " text = requests.get(url).text.strip().splitlines()\n", " samples, tokens, tags = [], [], []\n", " guid = 0\n", "\n", " for line in text:\n", " if not line.strip():\n", " if tokens:\n", " samples.append({\"id\": str(guid), \"tokens\": tokens, \"ner_tags\": tags})\n", " guid += 1\n", " tokens, tags = [], []\n", " else:\n", " token, tag = line.split(\"\\t\")\n", " tokens.append(token)\n", " tags.append(tag)\n", " \n", " if tokens:\n", " samples.append({\"id\": str(guid), \"tokens\": tokens, \"ner_tags\": tags})\n", "\n", " with open(f\"{domain}/{split}.json\", \"w\", encoding=\"utf-8\") as f:\n", " for s in samples:\n", " f.write(f\"{s}\\n\")" ] }, { "cell_type": "code", "execution_count": null, "id": "739fc387", "metadata": {}, "outputs": [], "source": [ "domains = [\"conll2003\", \"politics\", \"science\", \"music\", \"literature\", \"ai\"]\n", "base_url = \"https://raw.githubusercontent.com/zliucr/CrossNER/main/ner_data\"\n", "\n", "for domain in domains:\n", " convert_txt_to_jsonl(domain)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.11" } }, "nbformat": 4, "nbformat_minor": 5 }