File size: 7,262 Bytes
a4882cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "42ac3dd1-7154-40ec-ae54-38e4389c5ea8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import random\n",
    "import requests\n",
    "import zipfile\n",
    "\n",
    "from io import BytesIO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1ba83fb8-8c38-427e-83c1-68da2b5b4bbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading dataset from https://coltekin.github.io/offensive-turkish/offenseval2020-turkish.zip\n",
      "Extracting files to './'...\n",
      "Extracted files: ['offenseval2020-turkish/', 'offenseval2020-turkish/offenseval-tr-training-v1/', 'offenseval2020-turkish/offenseval-tr-training-v1/offenseval-annotation.txt', 'offenseval2020-turkish/offenseval-tr-training-v1/offenseval-tr-training-v1.tsv', 'offenseval2020-turkish/offenseval-tr-training-v1/readme-trainingset-tr.txt', 'offenseval2020-turkish/offenseval-tr-testset-v1/', 'offenseval2020-turkish/offenseval-tr-testset-v1/offenseval-tr-testset-v1.tsv', 'offenseval2020-turkish/offenseval-tr-testset-v1/offenseval-tr-labela-v1.tsv', 'offenseval2020-turkish/README.txt']\n"
     ]
    }
   ],
   "source": [
    "def download_and_extract_zip(url, extract_to=\"./\"):\n",
    "    try:\n",
    "        print(f\"Downloading dataset from {url}\")\n",
    "        response = requests.get(url)\n",
    "        response.raise_for_status()\n",
    "\n",
    "        with zipfile.ZipFile(BytesIO(response.content)) as z:\n",
    "            print(f\"Extracting files to '{extract_to}'...\")\n",
    "            z.extractall(extract_to)\n",
    "            extracted_files = z.namelist()\n",
    "            print(f\"Extracted files: {extracted_files}\")\n",
    "    except Exception as e:\n",
    "        print(f\"An error occurred: {e}\")\n",
    "\n",
    "url = \"https://coltekin.github.io/offensive-turkish/offenseval2020-turkish.zip\"  # Replace with the actual URL\n",
    "download_and_extract_zip(url, \"./\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b74682a7-ccf8-44ad-98a0-73636c35e10e",
   "metadata": {},
   "outputs": [],
   "source": [
    "original_train_file = \"./offenseval2020-turkish/offenseval-tr-training-v1/offenseval-tr-training-v1.tsv\"\n",
    "original_test_file = \"./offenseval2020-turkish/offenseval-tr-testset-v1/offenseval-tr-testset-v1.tsv\"\n",
    "orginal_label_test_file = \"./offenseval2020-turkish/offenseval-tr-testset-v1/offenseval-tr-labela-v1.tsv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "af3ba702-1341-4e70-8c74-87078eeaddf1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_instances(filename: str):\n",
    "    instances = []\n",
    "    with open(filename, \"rt\") as f_p:\n",
    "        for line in f_p:\n",
    "            line = line.strip()\n",
    "    \n",
    "            if not line:\n",
    "                continue\n",
    "    \n",
    "            if line.startswith(\"id\"):\n",
    "                continue\n",
    "    \n",
    "            _, tweet, label = line.split(\"\\t\")\n",
    "\n",
    "            instances.append([label, tweet])\n",
    "\n",
    "    print(f\"Found {len(instances)} training instances.\")\n",
    "\n",
    "    return instances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a397f04d-354c-4d97-9f93-794929c5e51d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_test_instances(filename: str, label_filename: str):\n",
    "    # E.g. 41993,NOT is mapped to \"41993\" -> \"NOT\"\n",
    "    id_label_mapping = {}\n",
    "    with open(label_filename, \"rt\") as f_p:\n",
    "        for line in f_p:\n",
    "            line = line.strip()\n",
    "\n",
    "            if not line:\n",
    "                continue\n",
    "\n",
    "            id_, label = line.split(\",\")\n",
    "\n",
    "            id_label_mapping[id_] = label\n",
    "\n",
    "    print(f\"Found {len(id_label_mapping)} labelled test instances\")\n",
    "\n",
    "    instances = []\n",
    "    \n",
    "    with open(filename, \"rt\") as f_p:\n",
    "        for line in f_p:\n",
    "            line = line.strip()\n",
    "    \n",
    "            if not line:\n",
    "                continue\n",
    "    \n",
    "            if line.startswith(\"id\"):\n",
    "                continue\n",
    "    \n",
    "            id_, tweet = line.split(\"\\t\")\n",
    "\n",
    "            label = id_label_mapping[id_]\n",
    "\n",
    "            instances.append([label, tweet])\n",
    "    return instances\n",
    "\n",
    "    assert len(id_label_mapping) == len(instances)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "06508ca8-649b-44ea-a8c6-09c2c2b434f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Found 31756 training instances.\n",
      "Found 3528 labelled test instances\n"
     ]
    }
   ],
   "source": [
    "original_train_instances = get_instances(original_train_file)\n",
    "original_test_instances = get_test_instances(original_test_file, orginal_label_test_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cd4a7942-42c9-4bdb-9079-c6039ff908c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Shuffling is done in-place\n",
    "random.seed(83607)\n",
    "random.shuffle(original_train_instances)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6ab61fde-e534-4ffe-9302-f80581e503eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_instances = original_train_instances[:30_000]\n",
    "dev_instances = original_train_instances[30_000:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "84f9c044-7906-4c04-9154-70e2b8d55982",
   "metadata": {},
   "outputs": [],
   "source": [
    "def write_instances(instances: str, split_name: str):\n",
    "    with open(f\"{split_name}.txt\", \"wt\") as f_out:\n",
    "        for instance in instances:\n",
    "            label, tweet = instance\n",
    "\n",
    "            # We stick to Flair format for classification tasks, which is basically FastText inspired ;)\n",
    "            new_label = \"__label__\" + label\n",
    "            f_out.write(f\"{new_label} {tweet}\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0bf06e96-2b25-46ed-8a7e-0672e7aa6af8",
   "metadata": {},
   "outputs": [],
   "source": [
    "write_instances(train_instances, \"train\")\n",
    "write_instances(dev_instances, \"dev\")\n",
    "write_instances(original_test_instances, \"test\")"
   ]
  }
 ],
 "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}