{ "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 }