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code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "027adfa9-5e64-474b-9a95-12e5c28c90a7",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import csv\n",
|
11 |
+
"import random\n",
|
12 |
+
"import requests"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"cell_type": "code",
|
17 |
+
"execution_count": 2,
|
18 |
+
"id": "e9fff288-d062-4c27-b9dc-db8579bbd3cf",
|
19 |
+
"metadata": {},
|
20 |
+
"outputs": [],
|
21 |
+
"source": [
|
22 |
+
"random.seed(83607)"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 3,
|
28 |
+
"id": "f85d8d2c-eca9-481d-b848-4d43a072b5fb",
|
29 |
+
"metadata": {},
|
30 |
+
"outputs": [],
|
31 |
+
"source": [
|
32 |
+
"# We use this as v1 of our dataset\n",
|
33 |
+
"revision = \"0ecb2228e6c290dd22836024f32e559cc9b9711e\"\n",
|
34 |
+
"original_dataset_file = \"gold_standard_v1.csv\""
|
35 |
+
]
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"cell_type": "code",
|
39 |
+
"execution_count": 4,
|
40 |
+
"id": "9c94cf8c-2185-4ca5-9191-02dd06c2fa0d",
|
41 |
+
"metadata": {},
|
42 |
+
"outputs": [],
|
43 |
+
"source": [
|
44 |
+
"# Download it - the simple way\n",
|
45 |
+
"url = f\"https://raw.githubusercontent.com/lucijakrusic/SentiAnno/{revision}/gold_standard.csv\"\n",
|
46 |
+
"r = requests.get(url, allow_redirects=True)\n",
|
47 |
+
"\n",
|
48 |
+
"if r:\n",
|
49 |
+
" with open(original_dataset_file, \"wb\") as f_out:\n",
|
50 |
+
" f_out.write(r.content)"
|
51 |
+
]
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"cell_type": "code",
|
55 |
+
"execution_count": 5,
|
56 |
+
"id": "1457fbb4-aeab-4c2e-a283-bcc12406ef3e",
|
57 |
+
"metadata": {},
|
58 |
+
"outputs": [],
|
59 |
+
"source": [
|
60 |
+
"# E.g.\n",
|
61 |
+
"# {\"negative\": [...], \"positive\": [...]\n",
|
62 |
+
"label_sentences_mapping = {}\n",
|
63 |
+
"\n",
|
64 |
+
"num_examples = 0\n",
|
65 |
+
"\n",
|
66 |
+
"with open(original_dataset_file, \"rt\") as csv_file:\n",
|
67 |
+
" csv_reader = csv.reader(csv_file, delimiter=',')\n",
|
68 |
+
"\n",
|
69 |
+
" # Skip header\n",
|
70 |
+
" next(csv_reader, None)\n",
|
71 |
+
"\n",
|
72 |
+
" for line in csv_reader:\n",
|
73 |
+
" assert len(line) == 5\n",
|
74 |
+
"\n",
|
75 |
+
" sentence = line[2]\n",
|
76 |
+
" label = line[-1]\n",
|
77 |
+
"\n",
|
78 |
+
" current_example = [label, sentence]\n",
|
79 |
+
" \n",
|
80 |
+
" if label in label_sentences_mapping:\n",
|
81 |
+
" label_sentences_mapping[label].append(current_example)\n",
|
82 |
+
" else:\n",
|
83 |
+
" label_sentences_mapping[label] = [current_example]\n",
|
84 |
+
"\n",
|
85 |
+
" num_examples += 1"
|
86 |
+
]
|
87 |
+
},
|
88 |
+
{
|
89 |
+
"cell_type": "code",
|
90 |
+
"execution_count": 6,
|
91 |
+
"id": "be5eda4e-bc2b-4f24-a584-7d7b34987f73",
|
92 |
+
"metadata": {},
|
93 |
+
"outputs": [
|
94 |
+
{
|
95 |
+
"name": "stdout",
|
96 |
+
"output_type": "stream",
|
97 |
+
"text": [
|
98 |
+
"Label negative has 447 sentences\n",
|
99 |
+
"Label mixed has 56 sentences\n",
|
100 |
+
"Label positive has 81 sentences\n",
|
101 |
+
"Label neutral has 345 sentences\n"
|
102 |
+
]
|
103 |
+
}
|
104 |
+
],
|
105 |
+
"source": [
|
106 |
+
"for label, sentences in label_sentences_mapping.items():\n",
|
107 |
+
" print(\"Label\", label, \"has\", len(sentences), \"sentences\")"
|
108 |
+
]
|
109 |
+
},
|
110 |
+
{
|
111 |
+
"cell_type": "code",
|
112 |
+
"execution_count": 7,
|
113 |
+
"id": "e218106a-8f23-41b0-96e5-b57a7a83fc5f",
|
114 |
+
"metadata": {},
|
115 |
+
"outputs": [],
|
116 |
+
"source": [
|
117 |
+
"# We create 80 / 10 / 10 splits, but for each label (to avoid over/under-representing labels\n",
|
118 |
+
"\n",
|
119 |
+
"train_examples = []\n",
|
120 |
+
"dev_examples = []\n",
|
121 |
+
"test_examples = []\n",
|
122 |
+
"\n",
|
123 |
+
"for _, sentences in label_sentences_mapping.items():\n",
|
124 |
+
" random.shuffle(sentences)\n",
|
125 |
+
"\n",
|
126 |
+
" split_1 = int(0.8 * len(sentences))\n",
|
127 |
+
" split_2 = int(0.9 * len(sentences))\n",
|
128 |
+
"\n",
|
129 |
+
" current_train_examples = sentences[:split_1]\n",
|
130 |
+
" current_dev_examples = sentences[split_1:split_2]\n",
|
131 |
+
" current_test_examples = sentences[split_2:]\n",
|
132 |
+
"\n",
|
133 |
+
" train_examples += current_train_examples\n",
|
134 |
+
" dev_examples += current_dev_examples\n",
|
135 |
+
" test_examples += current_test_examples"
|
136 |
+
]
|
137 |
+
},
|
138 |
+
{
|
139 |
+
"cell_type": "code",
|
140 |
+
"execution_count": 8,
|
141 |
+
"id": "5e1fc73f-1b9b-41eb-946b-872a1308712d",
|
142 |
+
"metadata": {},
|
143 |
+
"outputs": [
|
144 |
+
{
|
145 |
+
"name": "stdout",
|
146 |
+
"output_type": "stream",
|
147 |
+
"text": [
|
148 |
+
"Number of training examples: 741\n",
|
149 |
+
"Number of development examples: 93\n",
|
150 |
+
"Number of test examples: 95\n"
|
151 |
+
]
|
152 |
+
}
|
153 |
+
],
|
154 |
+
"source": [
|
155 |
+
"print(\"Number of training examples:\", len(train_examples))\n",
|
156 |
+
"print(\"Number of development examples:\", len(dev_examples))\n",
|
157 |
+
"print(\"Number of test examples:\", len(test_examples))"
|
158 |
+
]
|
159 |
+
},
|
160 |
+
{
|
161 |
+
"cell_type": "code",
|
162 |
+
"execution_count": 9,
|
163 |
+
"id": "5f359476-ecd5-4502-86ba-fee8bd8d3dcf",
|
164 |
+
"metadata": {},
|
165 |
+
"outputs": [],
|
166 |
+
"source": [
|
167 |
+
"assert num_examples == (len(train_examples) + len(dev_examples) + len(test_examples))"
|
168 |
+
]
|
169 |
+
},
|
170 |
+
{
|
171 |
+
"cell_type": "code",
|
172 |
+
"execution_count": 14,
|
173 |
+
"id": "29e1f3c9-3c3b-4541-840d-3db304966762",
|
174 |
+
"metadata": {},
|
175 |
+
"outputs": [],
|
176 |
+
"source": [
|
177 |
+
"def write_examples(examples: str, split_name: str):\n",
|
178 |
+
" # Shuffle again for more fun ;)\n",
|
179 |
+
" random.shuffle(examples)\n",
|
180 |
+
" with open(f\"{split_name}.txt\", \"wt\") as f_out:\n",
|
181 |
+
" for example in examples:\n",
|
182 |
+
" label, sentence = example\n",
|
183 |
+
"\n",
|
184 |
+
" # We stick to Flair format for classification tasks, which is basically FastText inspired ;)\n",
|
185 |
+
" new_label = \"__label__\" + label\n",
|
186 |
+
" f_out.write(f\"{new_label} {sentence}\\n\")"
|
187 |
+
]
|
188 |
+
},
|
189 |
+
{
|
190 |
+
"cell_type": "code",
|
191 |
+
"execution_count": 15,
|
192 |
+
"id": "502b3865-3efe-4730-9be8-ea675fd3feec",
|
193 |
+
"metadata": {},
|
194 |
+
"outputs": [],
|
195 |
+
"source": [
|
196 |
+
"write_examples(train_examples, \"train\")\n",
|
197 |
+
"write_examples(dev_examples, \"dev\")\n",
|
198 |
+
"write_examples(test_examples, \"test\")"
|
199 |
+
]
|
200 |
+
}
|
201 |
+
],
|
202 |
+
"metadata": {
|
203 |
+
"kernelspec": {
|
204 |
+
"display_name": "Python 3 (ipykernel)",
|
205 |
+
"language": "python",
|
206 |
+
"name": "python3"
|
207 |
+
},
|
208 |
+
"language_info": {
|
209 |
+
"codemirror_mode": {
|
210 |
+
"name": "ipython",
|
211 |
+
"version": 3
|
212 |
+
},
|
213 |
+
"file_extension": ".py",
|
214 |
+
"mimetype": "text/x-python",
|
215 |
+
"name": "python",
|
216 |
+
"nbconvert_exporter": "python",
|
217 |
+
"pygments_lexer": "ipython3",
|
218 |
+
"version": "3.12.3"
|
219 |
+
}
|
220 |
+
},
|
221 |
+
"nbformat": 4,
|
222 |
+
"nbformat_minor": 5
|
223 |
+
}
|