{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "_dev = pd.read_json(\"EN_dev_Anon.jsonl\", lines=True)\n", "_test = pd.read_json(\"EN_test_Anon.jsonl\", lines=True)\n", "_train = pd.read_json(\"EN_train_Anon.jsonl\", lines=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def process(df):\n", " df = df.copy()\n", " df.columns = df.columns.str.lower()\n", " df[\"violated\"] = df.violated_articles.str.len() != 0\n", " return df\n", "\n", "dev = process(_dev)\n", "test = process(_test)\n", "train = process(_train)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "d = {k: list(v) for k, v in train.groupby(\"violated\").indices.items()}\n", "dist = dict(dev.violated.value_counts().items())\n", "d_train = {k: v[0:dist[k]] for k, v in d.items()}\n", "d_remaining = {k: v[dist[k]:] for k, v in d.items()}\n", "\n", "new_rows = []\n", "for i in range(len(dev[\"violated\"])):\n", " label = i % 2 == 0\n", " new_rows.append(train.iloc[d_train[label].pop()])\n", "\n", "new_train = pd.concat([pd.DataFrame(new_rows), pd.DataFrame(train.iloc[i] for l in d_remaining.values() for i in l).sample(frac=1, random_state=42)])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "new_train.to_json(\"train.jsonl\", lines=True, orient=\"records\")\n", "test.to_json(\"test.jsonl\", lines=True, orient=\"records\")\n", "dev.to_json(\"dev.jsonl\", lines=True, orient=\"records\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "train = pd.read_json(\"data/train.jsonl\", lines=True, orient=\"records\")\n", "test = pd.read_json(\"data/test.jsonl\", lines=True, orient=\"records\")\n", "dev = pd.read_json(\"data/dev.jsonl\", lines=True, orient=\"records\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import re\n", "\n", "number = re.compile(\"^([0-9]+|CARDINAL)\\s?\\. \")\n", "train[\"text\"] = train[\"text\"].map(lambda r: [re.sub(number, \"\", line) for line in r])\n", "test[\"text\"] = test[\"text\"].map(lambda r: [re.sub(number, \"\", line) for line in r])\n", "dev[\"text\"] = dev[\"text\"].map(lambda r: [re.sub(number, \"\", line) for line in r])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "train.to_json(\"data/train.jsonl\", lines=True, orient=\"records\")\n", "test.to_json(\"data/test.jsonl\", lines=True, orient=\"records\")\n", "dev.to_json(\"data/dev.jsonl\", lines=True, orient=\"records\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.5 64-bit", "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.10.5" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a" } } }, "nbformat": 4, "nbformat_minor": 2 }