File size: 7,376 Bytes
99a8882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e7219917",
   "metadata": {},
   "source": [
    "# Create test dataset, by excluding certain queries from training and augmented datasets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "747fe513",
   "metadata": {},
   "source": [
    "## Load training and augmented datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e1e2c759",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import re\n",
    "df_train = pd.read_csv(\"train-data/sql_train.tsv\", sep='\\t')\n",
    "df_augment = pd.read_csv(\"train-data/data_augmentation_final.tsv\", sep='\\t')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6267d493",
   "metadata": {},
   "source": [
    "## Establish array of team names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "a59f9960",
   "metadata": {},
   "outputs": [],
   "source": [
    "team_array = [\n",
    "\"Atlanta Hawks\",\n",
    "\"Boston Celtics\",\n",
    "\"Cleveland Cavaliers\",\n",
    "\"New Orleans Pelicans\",\n",
    "\"Chicago Bulls\",\n",
    "\"Dallas Mavericks\",\n",
    "\"Denver Nuggets\",\n",
    "\"Golden State Warriors\",\n",
    "\"Houston Rockets\",\n",
    "\"Los Angeles Clippers\",\n",
    "\"Los Angeles Lakers\",\n",
    "\"Miami Heat\",\n",
    "\"Milwaukee Bucks\",\n",
    "\"Minnesota Timberwolves\",\n",
    "\"Brooklyn Nets\",\n",
    "\"New York Knicks\",\n",
    "\"Orlando Magic\",\n",
    "\"Indiana Pacers\",\n",
    "\"Philadelphia 76ers\",\n",
    "\"Phoenix Suns\",\n",
    "\"Portland Trail Blazers\",\n",
    "\"Sacramento Kings\",\n",
    "\"San Antonio Spurs\",\n",
    "\"Oklahoma City Thunder\",\n",
    "\"Toronto Raptors\",\n",
    "\"Utah Jazz\",\n",
    "\"Memphis Grizzlies\",\n",
    "\"Washington Wizards\",\n",
    "\"Detroit Pistons\",\n",
    "\"Charlotte Hornets\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "87dbf2dd",
   "metadata": {},
   "source": [
    "## Define regex expression to build new columns in the dataframes with the team names masked out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "188544ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Which NBA teams were established after the year 2000? List their names and founding years, sorted from newest to oldest\n",
      "What is the most points the <TEAM> have scored at home?\n",
      "What is the second-highest number of points the <TEAM> have ever scored in a single home game?\n",
      "How many home games did the <TEAM> win in the 2017 season?\n",
      "What is the average number of assists by the <TEAM> in home wins?\n",
      "Which game had the highest total points scored by both teams when the <TEAM> played at home?\n",
      "What is the <TEAM>' largest lead in a home game during the 2016 season?\n",
      "How many times did the <TEAM> score over 120 points at home?\n",
      "What is the highest three-point percentage the <TEAM> achieved in an away game?\n",
      "What was the average points difference in home games won by the <TEAM>?\n"
     ]
    }
   ],
   "source": [
    "# Compile a regex pattern for matching any team name\n",
    "team_pattern = re.compile(\"|\".join(map(re.escape, sorted(team_array, key=len, reverse=True))))\n",
    "\n",
    "def normalize_query(query):\n",
    "    \"\"\"Replace any known team name with <TEAM>.\"\"\"\n",
    "    return team_pattern.sub(\"<TEAM>\", query)\n",
    "\n",
    "# Assuming `df_train` and `df_augmented` are your two dataframes\n",
    "df_train[\"template\"] = df_train[\"natural_query\"].apply(normalize_query)\n",
    "df_augment[\"template\"] = df_augment[\"natural_query\"].apply(normalize_query)\n",
    "\n",
    "# Test performance of regex masking\n",
    "count = 0\n",
    "for index, row in df_train.iterrows():\n",
    "    print(row['template'])\n",
    "    count += 1\n",
    "    if count == 10:\n",
    "        break\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9ff879f",
   "metadata": {},
   "source": [
    "## Create new dataframe to hold test data, delete entries from test set in train and augment dataframes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ffb238d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1044\n",
      "786\n",
      "1870\n",
      "1367\n",
      "150\n"
     ]
    }
   ],
   "source": [
    "# Create new dataframes\n",
    "#df_test = pd.DataFrame(columns=['natural_query', 'sql_query', 'result'])\n",
    "#df_new_train = df_train.copy(deep=True)\n",
    "#df_new_augment = df_augment.copy(deep=True)\n",
    "\n",
    "# Sample some test queries from the original training set\n",
    "test_samples = df_train.sample(n=150)  # Or whatever number you want\n",
    "\n",
    "# Get the set of template forms of the test samples\n",
    "test_templates = set(test_samples[\"template\"])\n",
    "\n",
    "# Filter out those entries from the train and augmented sets\n",
    "df_train_filtered = df_train[~df_train[\"template\"].isin(test_templates)].drop(columns=[\"template\"])\n",
    "df_augment_filtered = df_augment[~df_augment[\"template\"].isin(test_templates)].drop(columns=[\"template\"])\n",
    "\n",
    "print(len(df_train))\n",
    "print(len(df_train_filtered))\n",
    "print(len(df_augment))\n",
    "print(len(df_augment_filtered))\n",
    "print(len(test_samples))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84896991",
   "metadata": {},
   "source": [
    "## Check distribution of new datasets to ensure they match original"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9d3587ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "36\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "for index, row in test_samples.iterrows():\n",
    "    if len(row['sql_query']) <= 90:\n",
    "        count += 1\n",
    "\n",
    "print(count)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2484fabf",
   "metadata": {},
   "source": [
    "## Save new train and test dataframes to tsv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c546ba58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150\n",
      "2213\n"
     ]
    }
   ],
   "source": [
    "df = pd.concat([df_train_filtered, df_augment_filtered])\n",
    "print(len(test_samples))\n",
    "print(len(df))\n",
    "test_samples.to_csv('./train-data/test_set.tsv', index=False, sep='\\t', columns=['natural_query', 'sql_query', 'result'])\n",
    "df.to_csv('./train-data/train_set.tsv', index=False, sep='\\t', columns=['natural_query', 'sql_query', 'result'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}