File size: 3,698 Bytes
4b465b5 265465b 4b465b5 a8ea5b9 4b465b5 a8ea5b9 4b465b5 a8ea5b9 4b465b5 a8ea5b9 4b465b5 a8ea5b9 4b465b5 a8ea5b9 4b465b5 cd8df66 4b465b5 e13b873 265465b e13b873 a8ea5b9 e13b873 a8ea5b9 e13b873 7eb3a8d e13b873 a8ea5b9 e13b873 a8ea5b9 e13b873 4b465b5 |
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 |
{
"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
}
|