File size: 3,774 Bytes
c8edf13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "87270bc7-2da0-490e-be8a-8cb06f38507b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from flair.datasets.sequence_labeling import ColumnCorpus\n",
    "\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "08ebe948-4179-4a75-8c6b-0e2fcad293d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "columns = {0: \"text\", 2: \"ner\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "af10c49b-002a-41f4-afde-172ac07251c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-03-25 12:57:38,583 Reading data from .\n",
      "2024-03-25 12:57:38,585 Train: train.tsv\n",
      "2024-03-25 12:57:38,586 Dev: dev.tsv\n",
      "2024-03-25 12:57:38,586 Test: test.tsv\n"
     ]
    }
   ],
   "source": [
    "corpus = ColumnCorpus(\".\",\n",
    "                      column_format=columns,\n",
    "                      train_file=\"train.tsv\",\n",
    "                      dev_file=\"dev.tsv\",\n",
    "                      test_file=\"test.tsv\",\n",
    "                      comment_symbol=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "efd08709-9308-4244-a730-0d51b738520c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Corpus: 758 train + 94 dev + 96 test sentences'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(corpus)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6d857c95-ae9f-4aab-803a-0a7e03ff632d",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_path = Path(\"./prepared-data-and-code/\")\n",
    "\n",
    "indices_for_split = {\n",
    "    \"train\": base_path / \"train_indices.txt\",\n",
    "    \"dev\": base_path / \"valid_indices.txt\",\n",
    "    \"test\": base_path / \"test_indices.txt\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2529666f-793f-4d8c-a7aa-9b7d83975310",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_number_of_total_indices(filename: str) -> int:\n",
    "    indices_counter = 0\n",
    "    with open(filename, \"rt\") as f_p:\n",
    "        for line in f_p:\n",
    "            line = line.strip()\n",
    "            if not line:\n",
    "                continue\n",
    "            indices_counter += 1\n",
    "    return indices_counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "49a69247-ef66-445b-b9e8-adcaac282e14",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_indices = get_number_of_total_indices(indices_for_split[\"train\"])\n",
    "dev_indices = get_number_of_total_indices(indices_for_split[\"dev\"])\n",
    "test_indices = get_number_of_total_indices(indices_for_split[\"test\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fb22489b-07d2-4403-a30c-6d92f00d252e",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert len(corpus.train) == train_indices\n",
    "assert len(corpus.dev) == dev_indices\n",
    "assert len(corpus.test) == test_indices"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}