File size: 10,852 Bytes
784a7e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Augmentation by parapharsing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Init & Load Seed Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json, openai\n",
    "from tqdm import tqdm "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DOMAIN = \"drone-planning/\"\n",
    "# DOMAIN = \"clean-up/\"\n",
    "# DOMAIN = \"pick-and-place/\"\n",
    "with open(DOMAIN + \"train_seed.jsonl\") as f:\n",
    "    train_seed = [json.loads(line) for line in f]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "eng_seeds = {\n",
    "    seed['natural']: [] for seed in train_seed\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Augmentation Code\n",
    "prompting GPT-3 seems to work the best in this case"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# You need to set your OPENAI API key here\n",
    "# https://beta.openai.com/account/api-keys\n",
    "openai.api_key = \"TO_BE_SET\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def normalize(sentence):\n",
    "    # captialize first letter and add period at the end if not present\n",
    "    if sentence[0].islower():\n",
    "        sentence = sentence[0].upper() + sentence[1:]\n",
    "    if sentence[-1] != '.':\n",
    "        sentence = sentence + '.'\n",
    "    return sentence\n",
    "\n",
    "def parse_sentences_from_response(response):\n",
    "    lines = response.split('\\n')\n",
    "    # assert len(lines) == 5\n",
    "    assert len(lines) == 10\n",
    "    lines[0] = \"1.\" + lines[0]\n",
    "    paraphrases = []\n",
    "    for idx, line in enumerate(lines):\n",
    "        assert line.startswith(str(idx+1) + '. ')\n",
    "        sentence_start_idx = len(str(idx+1) + '. ')\n",
    "        paraphrases.append(line[sentence_start_idx:])\n",
    "    for paraphrase in paraphrases:\n",
    "        if paraphrase[-1] == ' ':\n",
    "            if paraphrase[-2] == '.':\n",
    "                paraphrase = paraphrase[:-1]\n",
    "            else:\n",
    "                paraphrase = paraphrase[:-2] + '.'\n",
    "    return paraphrases\n",
    "\n",
    "\n",
    "PROMPT = \"\"\"Rephrase the source sentence in 10 different ways. Make the outputs as diverse as possible.\n",
    "\n",
    "Source: \n",
    "SOURCE-TO-BE-PLACED\n",
    "\n",
    "Outputs:\n",
    "1.\"\"\"\n",
    "def rephrase_a_sentence(sentence):\n",
    "    response = openai.Completion.create(\n",
    "        model=\"text-davinci-002\",\n",
    "        prompt=PROMPT.replace(\"SOURCE-TO-BE-PLACED\", normalize(sentence)),\n",
    "        temperature=0.7,\n",
    "        max_tokens=512,\n",
    "        top_p=1,\n",
    "        best_of=1,\n",
    "        frequency_penalty=0.1,\n",
    "        presence_penalty=0\n",
    "        )\n",
    "    output = response['choices'][0]['text']\n",
    "    try:\n",
    "        paraphrases = parse_sentences_from_response(output)\n",
    "    except:\n",
    "        print(\"Error in parsing response\")\n",
    "        print(output)\n",
    "        return output, \"ERROR\"\n",
    "    return parse_sentences_from_response(output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "O = rephrase_a_sentence(\"Go to the red room or go to the green room to finally go to the blue room.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "O"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run Augmentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(eng_seeds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list(eng_seeds.keys())[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def paraphrase_done(eng_seeds):\n",
    "    for eng_seed, extended in tqdm(eng_seeds.items()):\n",
    "        if len(extended) == 0:\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "while not paraphrase_done(eng_seeds):\n",
    "    for eng_seed, extended in tqdm(eng_seeds.items()):\n",
    "        if len(extended) == 0:\n",
    "            extended += rephrase_a_sentence(eng_seed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "eng_seeds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dump as Training Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_seed[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(DOMAIN + \"syn-aug.train.jsonl\", 'w') as f:\n",
    "    for seed in train_seed:\n",
    "        f.write(json.dumps(seed) + '\\n')\n",
    "        for aug_eng in eng_seeds[seed['natural']]:\n",
    "                f.write(json.dumps({\n",
    "                    'natural': aug_eng,\n",
    "                    'canonical': seed['canonical'],\n",
    "                    'formula': seed['formula']\n",
    "                }) + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(DOMAIN + \"syn.train.jsonl\", 'w') as f:\n",
    "    for seed in train_seed:\n",
    "        f.write(json.dumps(seed) + '\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Normalize the natural language form "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if DOMAIN == \"clean-up/\":\n",
    "    # in clean up, golden natural language data comes without period at the end, no capitalization in the beginning\n",
    "    def clean_up_normalize(sentence):\n",
    "        if sentence[0].isupper():\n",
    "            sentence = sentence[0].lower() + sentence[1:]\n",
    "        if sentence[-1] == '.':\n",
    "            sentence = sentence[:-1]\n",
    "        return sentence\n",
    "\n",
    "    buffer = []\n",
    "    with open(DOMAIN + \"syn-aug.train.jsonl\", 'r') as f:\n",
    "        for l in f.readlines():\n",
    "            buffer.append(json.loads(l))\n",
    "    \n",
    "    with open(DOMAIN + \"syn-aug.train.jsonl\", 'w') as f:\n",
    "        for dp in buffer:\n",
    "            f.write(json.dumps({\n",
    "                'natural': clean_up_normalize(dp['natural']),\n",
    "                'canonical': dp['canonical'],\n",
    "                'formula': dp['formula']\n",
    "            }) + '\\n')\n",
    "\n",
    "if DOMAIN == \"pick-and-place/\":\n",
    "    # in pick and place, golden natural language data comes without period at the end, no capitalization in the beginning\n",
    "    def clean_up_normalize(sentence):\n",
    "        if sentence[0].isupper():\n",
    "            sentence = sentence[0].lower() + sentence[1:]\n",
    "        if sentence[-1] == '.':\n",
    "            sentence = sentence[:-1]\n",
    "        return sentence\n",
    "\n",
    "    buffer = []\n",
    "    with open(DOMAIN + \"syn-aug.train.jsonl\", 'r') as f:\n",
    "        for l in f.readlines():\n",
    "            buffer.append(json.loads(l))\n",
    "    \n",
    "    with open(DOMAIN + \"syn-aug.train.jsonl\", 'w') as f:\n",
    "        for dp in buffer:\n",
    "            f.write(json.dumps({\n",
    "                'natural': clean_up_normalize(dp['natural']),\n",
    "                'canonical': dp['canonical'],\n",
    "                'formula': dp['formula']\n",
    "            }) + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if DOMAIN == \"drone-planning/\":\n",
    "    # in clean up, golden natural language data comes with a \"space + period\" at the end, no capitalization in the beginning\n",
    "    def clean_up_normalize(sentence):\n",
    "        if sentence[0].isupper():\n",
    "            sentence = sentence[0].lower() + sentence[1:]\n",
    "        while sentence[-1] == ' ' or sentence[-1] == '.' or sentence[-1] == '!':\n",
    "            sentence = sentence[:-1]\n",
    "        sentence = sentence + '.'\n",
    "        sentence = sentence.replace('.', ' .')\n",
    "        sentence = sentence.replace(',', ' ,')\n",
    "        return sentence\n",
    "\n",
    "    buffer = []\n",
    "    # with open(DOMAIN + \"syn-aug.train.jsonl\", 'r') as f:\n",
    "    #     for l in f.readlines():\n",
    "    #         buffer.append(json.loads(l))\n",
    "    \n",
    "    # with open(DOMAIN + \"syn-aug.train.jsonl\", 'w') as f:\n",
    "    #     for dp in buffer:\n",
    "    #         f.write(json.dumps({\n",
    "    #             'natural': clean_up_normalize(dp['natural']),\n",
    "    #             'canonical': dp['canonical'],\n",
    "    #             'formula': dp['formula']\n",
    "    #         }) + '\\n')\n",
    "    with open(DOMAIN + \"syn.train.jsonl\", 'r') as f:\n",
    "        for l in f.readlines():\n",
    "            buffer.append(json.loads(l))\n",
    "    \n",
    "    with open(DOMAIN + \"syn.train.jsonl\", 'w') as f:\n",
    "        for dp in buffer:\n",
    "            f.write(json.dumps({\n",
    "                'natural': clean_up_normalize(dp['natural']),\n",
    "                'canonical': dp['canonical'],\n",
    "                'formula': dp['formula']\n",
    "            }) + '\\n')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "GPML",
   "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.7.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "75567ad983eac98a78c1e40a895e8d82557b42cf9969286235abec07ddbf9e7d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}