File size: 6,181 Bytes
d7db08a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff8c7b1
d7db08a
 
 
 
 
 
 
 
 
 
 
ff8c7b1
 
 
d7db08a
 
 
 
 
 
 
ff8c7b1
d7db08a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "027adfa9-5e64-474b-9a95-12e5c28c90a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import random\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e9fff288-d062-4c27-b9dc-db8579bbd3cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.seed(83607)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f85d8d2c-eca9-481d-b848-4d43a072b5fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# We use this as v1 of our dataset\n",
    "revision = \"0ecb2228e6c290dd22836024f32e559cc9b9711e\"\n",
    "original_dataset_file = \"gold_standard_v1.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9c94cf8c-2185-4ca5-9191-02dd06c2fa0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download it - the simple way\n",
    "url = f\"https://raw.githubusercontent.com/lucijakrusic/SentiAnno/{revision}/gold_standard.csv\"\n",
    "r = requests.get(url, allow_redirects=True)\n",
    "\n",
    "if r:\n",
    "    with open(original_dataset_file, \"wb\") as f_out:\n",
    "        f_out.write(r.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1457fbb4-aeab-4c2e-a283-bcc12406ef3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# E.g.\n",
    "# {\"negative\": [...], \"positive\": [...]\n",
    "label_sentences_mapping = {}\n",
    "\n",
    "num_examples = 0\n",
    "\n",
    "with open(original_dataset_file, \"rt\") as csv_file:\n",
    "    csv_reader = csv.reader(csv_file, delimiter=',')\n",
    "\n",
    "    # Skip header\n",
    "    next(csv_reader, None)\n",
    "\n",
    "    for line in csv_reader:\n",
    "        assert len(line) == 5\n",
    "\n",
    "        sentence = line[2]\n",
    "        label = line[-1]\n",
    "\n",
    "        current_example = [label, sentence]\n",
    "        \n",
    "        if label in label_sentences_mapping:\n",
    "            label_sentences_mapping[label].append(current_example)\n",
    "        else:\n",
    "            label_sentences_mapping[label] = [current_example]\n",
    "\n",
    "        num_examples += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "be5eda4e-bc2b-4f24-a584-7d7b34987f73",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Label negative has 447 sentences\n",
      "Label mixed has 56 sentences\n",
      "Label positive has 81 sentences\n",
      "Label neutral has 345 sentences\n"
     ]
    }
   ],
   "source": [
    "for label, sentences in label_sentences_mapping.items():\n",
    "    print(\"Label\", label, \"has\", len(sentences), \"sentences\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e218106a-8f23-41b0-96e5-b57a7a83fc5f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# We create 80 / 10 / 10 splits, but for each label (to avoid over/under-representing labels\n",
    "\n",
    "train_examples = []\n",
    "dev_examples = []\n",
    "test_examples = []\n",
    "\n",
    "for _, sentences in label_sentences_mapping.items():\n",
    "    random.shuffle(sentences)\n",
    "\n",
    "    split_1 = int(0.8 * len(sentences))\n",
    "    split_2 = int(0.9 * len(sentences))\n",
    "\n",
    "    current_train_examples = sentences[:split_1]\n",
    "    current_dev_examples = sentences[split_1:split_2]\n",
    "    current_test_examples = sentences[split_2:]\n",
    "\n",
    "    train_examples += current_train_examples\n",
    "    dev_examples += current_dev_examples\n",
    "    test_examples += current_test_examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "5e1fc73f-1b9b-41eb-946b-872a1308712d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of training examples: 741\n",
      "Number of development examples: 93\n",
      "Number of test examples: 95\n"
     ]
    }
   ],
   "source": [
    "print(\"Number of training examples:\", len(train_examples))\n",
    "print(\"Number of development examples:\", len(dev_examples))\n",
    "print(\"Number of test examples:\", len(test_examples))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5f359476-ecd5-4502-86ba-fee8bd8d3dcf",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert num_examples == (len(train_examples) + len(dev_examples) + len(test_examples))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "29e1f3c9-3c3b-4541-840d-3db304966762",
   "metadata": {},
   "outputs": [],
   "source": [
    "def write_examples(examples: str, split_name: str):\n",
    "    # Shuffle again for more fun ;)\n",
    "    random.shuffle(examples)\n",
    "    with open(f\"{split_name}.txt\", \"wt\") as f_out:\n",
    "        for example in examples:\n",
    "            label, sentence = example\n",
    "\n",
    "            # Fix!\n",
    "            sentence = sentence.replace(\"\\n\", \" \")\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} {sentence}\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "502b3865-3efe-4730-9be8-ea675fd3feec",
   "metadata": {},
   "outputs": [],
   "source": [
    "write_examples(train_examples, \"train\")\n",
    "write_examples(dev_examples, \"dev\")\n",
    "write_examples(test_examples, \"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
}