File size: 2,537 Bytes
4bb0be0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "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
}