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|
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "42ac3dd1-7154-40ec-ae54-38e4389c5ea8",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import os\n",
|
11 |
+
"import random\n",
|
12 |
+
"import requests\n",
|
13 |
+
"import zipfile\n",
|
14 |
+
"\n",
|
15 |
+
"from io import BytesIO"
|
16 |
+
]
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"cell_type": "code",
|
20 |
+
"execution_count": 2,
|
21 |
+
"id": "1ba83fb8-8c38-427e-83c1-68da2b5b4bbd",
|
22 |
+
"metadata": {},
|
23 |
+
"outputs": [
|
24 |
+
{
|
25 |
+
"name": "stdout",
|
26 |
+
"output_type": "stream",
|
27 |
+
"text": [
|
28 |
+
"Downloading dataset from https://coltekin.github.io/offensive-turkish/offenseval2020-turkish.zip\n",
|
29 |
+
"Extracting files to './'...\n",
|
30 |
+
"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"
|
31 |
+
]
|
32 |
+
}
|
33 |
+
],
|
34 |
+
"source": [
|
35 |
+
"def download_and_extract_zip(url, extract_to=\"./\"):\n",
|
36 |
+
" try:\n",
|
37 |
+
" print(f\"Downloading dataset from {url}\")\n",
|
38 |
+
" response = requests.get(url)\n",
|
39 |
+
" response.raise_for_status()\n",
|
40 |
+
"\n",
|
41 |
+
" with zipfile.ZipFile(BytesIO(response.content)) as z:\n",
|
42 |
+
" print(f\"Extracting files to '{extract_to}'...\")\n",
|
43 |
+
" z.extractall(extract_to)\n",
|
44 |
+
" extracted_files = z.namelist()\n",
|
45 |
+
" print(f\"Extracted files: {extracted_files}\")\n",
|
46 |
+
" except Exception as e:\n",
|
47 |
+
" print(f\"An error occurred: {e}\")\n",
|
48 |
+
"\n",
|
49 |
+
"url = \"https://coltekin.github.io/offensive-turkish/offenseval2020-turkish.zip\" # Replace with the actual URL\n",
|
50 |
+
"download_and_extract_zip(url, \"./\")"
|
51 |
+
]
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"cell_type": "code",
|
55 |
+
"execution_count": 3,
|
56 |
+
"id": "b74682a7-ccf8-44ad-98a0-73636c35e10e",
|
57 |
+
"metadata": {},
|
58 |
+
"outputs": [],
|
59 |
+
"source": [
|
60 |
+
"original_train_file = \"./offenseval2020-turkish/offenseval-tr-training-v1/offenseval-tr-training-v1.tsv\"\n",
|
61 |
+
"original_test_file = \"./offenseval2020-turkish/offenseval-tr-testset-v1/offenseval-tr-testset-v1.tsv\"\n",
|
62 |
+
"orginal_label_test_file = \"./offenseval2020-turkish/offenseval-tr-testset-v1/offenseval-tr-labela-v1.tsv\""
|
63 |
+
]
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"cell_type": "code",
|
67 |
+
"execution_count": 4,
|
68 |
+
"id": "af3ba702-1341-4e70-8c74-87078eeaddf1",
|
69 |
+
"metadata": {},
|
70 |
+
"outputs": [],
|
71 |
+
"source": [
|
72 |
+
"def get_instances(filename: str):\n",
|
73 |
+
" instances = []\n",
|
74 |
+
" with open(filename, \"rt\") as f_p:\n",
|
75 |
+
" for line in f_p:\n",
|
76 |
+
" line = line.strip()\n",
|
77 |
+
" \n",
|
78 |
+
" if not line:\n",
|
79 |
+
" continue\n",
|
80 |
+
" \n",
|
81 |
+
" if line.startswith(\"id\"):\n",
|
82 |
+
" continue\n",
|
83 |
+
" \n",
|
84 |
+
" _, tweet, label = line.split(\"\\t\")\n",
|
85 |
+
"\n",
|
86 |
+
" instances.append([label, tweet])\n",
|
87 |
+
"\n",
|
88 |
+
" print(f\"Found {len(instances)} training instances.\")\n",
|
89 |
+
"\n",
|
90 |
+
" return instances"
|
91 |
+
]
|
92 |
+
},
|
93 |
+
{
|
94 |
+
"cell_type": "code",
|
95 |
+
"execution_count": 5,
|
96 |
+
"id": "a397f04d-354c-4d97-9f93-794929c5e51d",
|
97 |
+
"metadata": {},
|
98 |
+
"outputs": [],
|
99 |
+
"source": [
|
100 |
+
"def get_test_instances(filename: str, label_filename: str):\n",
|
101 |
+
" # E.g. 41993,NOT is mapped to \"41993\" -> \"NOT\"\n",
|
102 |
+
" id_label_mapping = {}\n",
|
103 |
+
" with open(label_filename, \"rt\") as f_p:\n",
|
104 |
+
" for line in f_p:\n",
|
105 |
+
" line = line.strip()\n",
|
106 |
+
"\n",
|
107 |
+
" if not line:\n",
|
108 |
+
" continue\n",
|
109 |
+
"\n",
|
110 |
+
" id_, label = line.split(\",\")\n",
|
111 |
+
"\n",
|
112 |
+
" id_label_mapping[id_] = label\n",
|
113 |
+
"\n",
|
114 |
+
" print(f\"Found {len(id_label_mapping)} labelled test instances\")\n",
|
115 |
+
"\n",
|
116 |
+
" instances = []\n",
|
117 |
+
" \n",
|
118 |
+
" with open(filename, \"rt\") as f_p:\n",
|
119 |
+
" for line in f_p:\n",
|
120 |
+
" line = line.strip()\n",
|
121 |
+
" \n",
|
122 |
+
" if not line:\n",
|
123 |
+
" continue\n",
|
124 |
+
" \n",
|
125 |
+
" if line.startswith(\"id\"):\n",
|
126 |
+
" continue\n",
|
127 |
+
" \n",
|
128 |
+
" id_, tweet = line.split(\"\\t\")\n",
|
129 |
+
"\n",
|
130 |
+
" label = id_label_mapping[id_]\n",
|
131 |
+
"\n",
|
132 |
+
" instances.append([label, tweet])\n",
|
133 |
+
" return instances\n",
|
134 |
+
"\n",
|
135 |
+
" assert len(id_label_mapping) == len(instances)"
|
136 |
+
]
|
137 |
+
},
|
138 |
+
{
|
139 |
+
"cell_type": "code",
|
140 |
+
"execution_count": 6,
|
141 |
+
"id": "06508ca8-649b-44ea-a8c6-09c2c2b434f4",
|
142 |
+
"metadata": {},
|
143 |
+
"outputs": [
|
144 |
+
{
|
145 |
+
"name": "stdout",
|
146 |
+
"output_type": "stream",
|
147 |
+
"text": [
|
148 |
+
"Found 31756 training instances.\n",
|
149 |
+
"Found 3528 labelled test instances\n"
|
150 |
+
]
|
151 |
+
}
|
152 |
+
],
|
153 |
+
"source": [
|
154 |
+
"original_train_instances = get_instances(original_train_file)\n",
|
155 |
+
"original_test_instances = get_test_instances(original_test_file, orginal_label_test_file)"
|
156 |
+
]
|
157 |
+
},
|
158 |
+
{
|
159 |
+
"cell_type": "code",
|
160 |
+
"execution_count": 7,
|
161 |
+
"id": "cd4a7942-42c9-4bdb-9079-c6039ff908c4",
|
162 |
+
"metadata": {},
|
163 |
+
"outputs": [],
|
164 |
+
"source": [
|
165 |
+
"# Shuffling is done in-place\n",
|
166 |
+
"random.seed(83607)\n",
|
167 |
+
"random.shuffle(original_train_instances)"
|
168 |
+
]
|
169 |
+
},
|
170 |
+
{
|
171 |
+
"cell_type": "code",
|
172 |
+
"execution_count": 8,
|
173 |
+
"id": "6ab61fde-e534-4ffe-9302-f80581e503eb",
|
174 |
+
"metadata": {},
|
175 |
+
"outputs": [],
|
176 |
+
"source": [
|
177 |
+
"train_instances = original_train_instances[:30_000]\n",
|
178 |
+
"dev_instances = original_train_instances[30_000:]"
|
179 |
+
]
|
180 |
+
},
|
181 |
+
{
|
182 |
+
"cell_type": "code",
|
183 |
+
"execution_count": 9,
|
184 |
+
"id": "84f9c044-7906-4c04-9154-70e2b8d55982",
|
185 |
+
"metadata": {},
|
186 |
+
"outputs": [],
|
187 |
+
"source": [
|
188 |
+
"def write_instances(instances: str, split_name: str):\n",
|
189 |
+
" with open(f\"{split_name}.txt\", \"wt\") as f_out:\n",
|
190 |
+
" for instance in instances:\n",
|
191 |
+
" label, tweet = instance\n",
|
192 |
+
"\n",
|
193 |
+
" # We stick to Flair format for classification tasks, which is basically FastText inspired ;)\n",
|
194 |
+
" new_label = \"__label__\" + label\n",
|
195 |
+
" f_out.write(f\"{new_label} {tweet}\\n\")"
|
196 |
+
]
|
197 |
+
},
|
198 |
+
{
|
199 |
+
"cell_type": "code",
|
200 |
+
"execution_count": 10,
|
201 |
+
"id": "0bf06e96-2b25-46ed-8a7e-0672e7aa6af8",
|
202 |
+
"metadata": {},
|
203 |
+
"outputs": [],
|
204 |
+
"source": [
|
205 |
+
"write_instances(train_instances, \"train\")\n",
|
206 |
+
"write_instances(dev_instances, \"dev\")\n",
|
207 |
+
"write_instances(original_test_instances, \"test\")"
|
208 |
+
]
|
209 |
+
}
|
210 |
+
],
|
211 |
+
"metadata": {
|
212 |
+
"kernelspec": {
|
213 |
+
"display_name": "Python 3 (ipykernel)",
|
214 |
+
"language": "python",
|
215 |
+
"name": "python3"
|
216 |
+
},
|
217 |
+
"language_info": {
|
218 |
+
"codemirror_mode": {
|
219 |
+
"name": "ipython",
|
220 |
+
"version": 3
|
221 |
+
},
|
222 |
+
"file_extension": ".py",
|
223 |
+
"mimetype": "text/x-python",
|
224 |
+
"name": "python",
|
225 |
+
"nbconvert_exporter": "python",
|
226 |
+
"pygments_lexer": "ipython3",
|
227 |
+
"version": "3.12.3"
|
228 |
+
}
|
229 |
+
},
|
230 |
+
"nbformat": 4,
|
231 |
+
"nbformat_minor": 5
|
232 |
+
}
|