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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": null,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"from transformers import set_seed\n",
|
| 10 |
+
"import pandas as pd\n",
|
| 11 |
+
"import matplotlib.pyplot as plt\n",
|
| 12 |
+
"from collections import Counter\n",
|
| 13 |
+
"import numpy as np\n",
|
| 14 |
+
"import random\n",
|
| 15 |
+
"import torch"
|
| 16 |
+
]
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"cell_type": "markdown",
|
| 20 |
+
"metadata": {},
|
| 21 |
+
"source": [
|
| 22 |
+
"three fields in each prompt: question, bot, task\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"input to the model is:\n",
|
| 25 |
+
"```\n",
|
| 26 |
+
"<s>human\n",
|
| 27 |
+
"[question]\n",
|
| 28 |
+
"<s>bot\n",
|
| 29 |
+
"[bot]\n",
|
| 30 |
+
"```\n",
|
| 31 |
+
"where a question is\n",
|
| 32 |
+
"```\n",
|
| 33 |
+
"[program]\n",
|
| 34 |
+
"\n",
|
| 35 |
+
"[question]\n",
|
| 36 |
+
"```"
|
| 37 |
+
]
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"cell_type": "code",
|
| 41 |
+
"execution_count": null,
|
| 42 |
+
"metadata": {},
|
| 43 |
+
"outputs": [],
|
| 44 |
+
"source": [
|
| 45 |
+
"debug = ''\n",
|
| 46 |
+
"in_dir = f\"/Users/zzy/Documents/graph{debug}\"\n",
|
| 47 |
+
"out_dir = f\"/Users/zzy/Documents/graph{debug}/instruction\"\n",
|
| 48 |
+
"no_return_sample_num = 20 if len(debug) > 0 else 40000\n",
|
| 49 |
+
"\n",
|
| 50 |
+
"figsize = (24, 16)\n",
|
| 51 |
+
"fontsize = 28\n",
|
| 52 |
+
"fontsize_tick = 16\n",
|
| 53 |
+
"\n",
|
| 54 |
+
"def filter_df(df, n=None):\n",
|
| 55 |
+
" try:\n",
|
| 56 |
+
" n = n if n is not None else no_return_sample_num\n",
|
| 57 |
+
" return pd.concat([df[df.source.apply(lambda x: 'return ' in x)], df[df.source.apply(lambda x: 'return ' not in x)].sample(n)]).reset_index(drop=True)\n",
|
| 58 |
+
" except:\n",
|
| 59 |
+
" return df\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"def capitalize(s: str):\n",
|
| 62 |
+
" return s[0].upper() + s[1:]\n",
|
| 63 |
+
"\n",
|
| 64 |
+
"def replace_digit(s: str):\n",
|
| 65 |
+
" return s.replace('10', 'ten').replace('1', 'one').replace('2', 'two').replace('3', 'three').replace('4', 'four').replace('5', 'five').replace('6', 'six').replace('7', 'seven').replace('8', 'eight').replace('9', 'nine')\n",
|
| 66 |
+
"\n",
|
| 67 |
+
"def print_df(df, n=10):\n",
|
| 68 |
+
" for i in range(n):\n",
|
| 69 |
+
" print(df.loc[i].question)\n",
|
| 70 |
+
" print(df.loc[i].bot)\n",
|
| 71 |
+
" print('---'*10)\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"graph_type_map = {'AST': 'abstract syntax tree', 'DFG': 'data flow graph'}\n",
|
| 74 |
+
"NODE_TYPES = [\n",
|
| 75 |
+
" 'assignment expression',\n",
|
| 76 |
+
" 'basic block',\n",
|
| 77 |
+
" 'binary expression',\n",
|
| 78 |
+
" 'break statement',\n",
|
| 79 |
+
" 'call expression',\n",
|
| 80 |
+
" 'catch clause',\n",
|
| 81 |
+
" 'class expression',\n",
|
| 82 |
+
" 'compile unit',\n",
|
| 83 |
+
" 'conditional expression',\n",
|
| 84 |
+
" 'continue statement',\n",
|
| 85 |
+
" 'export statement',\n",
|
| 86 |
+
" 'for statement',\n",
|
| 87 |
+
" 'function expression',\n",
|
| 88 |
+
" 'identifier expression', \n",
|
| 89 |
+
" 'if statement',\n",
|
| 90 |
+
" 'import expression',\n",
|
| 91 |
+
" 'key value parameter',\n",
|
| 92 |
+
" 'literal expression',\n",
|
| 93 |
+
" 'member access',\n",
|
| 94 |
+
" 'new expression',\n",
|
| 95 |
+
" 'new with type expression',\n",
|
| 96 |
+
" 'object expression',\n",
|
| 97 |
+
" 'object property',\n",
|
| 98 |
+
" 'parameter',\n",
|
| 99 |
+
" 'Python delete',\n",
|
| 100 |
+
" 'Python with',\n",
|
| 101 |
+
" 'Python with expression clause',\n",
|
| 102 |
+
" 'Python yield expression',\n",
|
| 103 |
+
" 'range statement',\n",
|
| 104 |
+
" 'return statement',\n",
|
| 105 |
+
" 'scope',\n",
|
| 106 |
+
" 'spread collection expression',\n",
|
| 107 |
+
" 'spread dictionary expression',\n",
|
| 108 |
+
" 'super expression',\n",
|
| 109 |
+
" 'switch case',\n",
|
| 110 |
+
" 'switch statement',\n",
|
| 111 |
+
" 'this expression',\n",
|
| 112 |
+
" 'throw statement',\n",
|
| 113 |
+
" 'try statement',\n",
|
| 114 |
+
" 'tuple expression',\n",
|
| 115 |
+
" 'unary expression',\n",
|
| 116 |
+
" 'variable declaration',\n",
|
| 117 |
+
" 'while statement'\n",
|
| 118 |
+
"]"
|
| 119 |
+
]
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"cell_type": "code",
|
| 123 |
+
"execution_count": null,
|
| 124 |
+
"metadata": {},
|
| 125 |
+
"outputs": [],
|
| 126 |
+
"source": [
|
| 127 |
+
"# ph stands for place holder\n",
|
| 128 |
+
"ph1 = 'aohg981thgboir2bnjosi1839r8g9udnfv,mqwfo'\n",
|
| 129 |
+
"ph2 = 'io12i3ru9ginal90109ja-efi1-3gasd130gn0wa9'\n",
|
| 130 |
+
"ph3 = '2091rng09wegnb2p09jojmpzf,k[2e00-jmaa]'\n",
|
| 131 |
+
"ph4 = '0391gnea-g0-jr0aegbm[afk0-249jgps]waeg0'\n",
|
| 132 |
+
"ph5 = 'io1hngi0enriqgpgv]139gonpiamofj10onem;alf'\n",
|
| 133 |
+
"ph_list = [ph1, ph2, ph3, ph4, ph5]\n",
|
| 134 |
+
"punc_list = [\",\", \"?\", \".\", \";\", \"'s\"]\n",
|
| 135 |
+
"\n",
|
| 136 |
+
"def replace_place_holder(s, node_text, placeholder=\"{node}\"):\n",
|
| 137 |
+
" # this function injects a multi-line code snippet into the template\n",
|
| 138 |
+
"\n",
|
| 139 |
+
" if placeholder not in s:\n",
|
| 140 |
+
" return s\n",
|
| 141 |
+
"\n",
|
| 142 |
+
" # 1. remove the white spaces around {node} placeholder\n",
|
| 143 |
+
" s = s.replace(f\"{placeholder} \", f\"{placeholder}\").replace(f\" {placeholder}\", f\"{placeholder}\")\n",
|
| 144 |
+
" for punc in punc_list:\n",
|
| 145 |
+
" s = s.replace(f\"{placeholder}{punc} \", f\"{placeholder}{punc}\")\n",
|
| 146 |
+
" \n",
|
| 147 |
+
" # 2. injects the code, but first replace patterns like '\\n.' in both the code and template (the template may contain previously injected code)\n",
|
| 148 |
+
" for ph, punc in zip(ph_list, punc_list):\n",
|
| 149 |
+
" node_text = node_text.replace(f\"\\n{punc}\", ph)\n",
|
| 150 |
+
" s = s.replace(f\"\\n{punc}\", ph)\n",
|
| 151 |
+
" s = s.replace(placeholder, node_text)\n",
|
| 152 |
+
"\n",
|
| 153 |
+
" # 3. replace patterns like \"\\n.\" caused by the template\n",
|
| 154 |
+
" for punc in punc_list:\n",
|
| 155 |
+
" s = s.replace(f\"\\n{punc}\", f\"{punc}\\n\")\n",
|
| 156 |
+
"\n",
|
| 157 |
+
" # 4. replace the placerholders inserted in step 2\n",
|
| 158 |
+
" for ph, punc in zip(ph_list, punc_list):\n",
|
| 159 |
+
" s = s.replace(ph, f\"\\n{punc}\")\n",
|
| 160 |
+
" return s"
|
| 161 |
+
]
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"cell_type": "markdown",
|
| 165 |
+
"metadata": {},
|
| 166 |
+
"source": [
|
| 167 |
+
"## node classification"
|
| 168 |
+
]
|
| 169 |
+
},
|
| 170 |
+
{
|
| 171 |
+
"cell_type": "code",
|
| 172 |
+
"execution_count": null,
|
| 173 |
+
"metadata": {},
|
| 174 |
+
"outputs": [],
|
| 175 |
+
"source": [
|
| 176 |
+
"questions = [\n",
|
| 177 |
+
" 'In the {graph} of this {lang} program, what is the type of this node: {node}.',\n",
|
| 178 |
+
" 'Tell me the node type of {node} in the {graph} of this {lang} program.',\n",
|
| 179 |
+
" 'What is the node type of {node} in the {graph} of this {lang} program',\n",
|
| 180 |
+
" \"In the {graph} of the provided {lang} program, could you identify the type of the node {node}?\",\n",
|
| 181 |
+
" \"What kind of node is {node} in the {graph} of this {lang} program?\",\n",
|
| 182 |
+
" \"Can you classify the node {node} in the {graph} of this {lang} program?\",\n",
|
| 183 |
+
" \"What category does the node {node} fall under in the {graph} of this {lang} program?\",\n",
|
| 184 |
+
" \"Regarding the {graph} of this {lang} program, what is the classification of the node {node}?\",\n",
|
| 185 |
+
" \"In the context of the {graph} of this {lang} program, what is the nature of the node identified as {node}?\",\n",
|
| 186 |
+
" \"Could you tell me what the node {node} represents in the {graph} of this {lang} program?\",\n",
|
| 187 |
+
" \"I'm curious, what type of node is {node} in the {graph} of the {lang} program presented?\",\n",
|
| 188 |
+
" \"What is the designation of the node {node} within the {graph} of this {lang} program?\",\n",
|
| 189 |
+
" \"Could you specify the node type for {node} in the {graph} of this particular {lang} program?\",\n",
|
| 190 |
+
" 'Determine the node type of {node} in the {graph} of this {lang} program.',\n",
|
| 191 |
+
"]\n",
|
| 192 |
+
"answers = [\n",
|
| 193 |
+
" \"This node, {node}, is classified as a {answer}.\",\n",
|
| 194 |
+
" \"The node {node} is a {answer}.\",\n",
|
| 195 |
+
" \"This node is identified as a {answer}.\",\n",
|
| 196 |
+
" \"It's a {answer}.\",\n",
|
| 197 |
+
" \"Regarding the node {node}, it falls under the category of a {answer}.\",\n",
|
| 198 |
+
" \"{node} is classified as a {answer} in the {graph} of this program.\",\n",
|
| 199 |
+
" \"The classification of the node {node} is a {answer}.\",\n",
|
| 200 |
+
" \"Within the {graph} of this program, {node} is a {answer} type of node.\",\n",
|
| 201 |
+
" \"As for the node identified as {node}, it's considered a {answer}.\",\n",
|
| 202 |
+
" \"The node {node} is of the {answer} variety.\",\n",
|
| 203 |
+
" 'The type of this node is {answer}.',\n",
|
| 204 |
+
" 'The given node is a {answer}.',\n",
|
| 205 |
+
" \"{answer}.\",\n",
|
| 206 |
+
" \"{answer}\",\n",
|
| 207 |
+
"]\n",
|
| 208 |
+
"print(len(questions), len(set(questions)))\n",
|
| 209 |
+
"print(len(answers), len(set(answers)))\n",
|
| 210 |
+
"assert all(a.count('{answer}') == 1 for a in answers)\n",
|
| 211 |
+
"assert all(a.count('{graph}') == 1 for a in questions)\n",
|
| 212 |
+
"assert all(a.count('{lang}') == 1 for a in questions)\n",
|
| 213 |
+
"\n",
|
| 214 |
+
"bots = [a for a in answers]\n",
|
| 215 |
+
"prompts = [[q, b] for q in questions for b in bots]\n",
|
| 216 |
+
"print(len(prompts))"
|
| 217 |
+
]
|
| 218 |
+
},
|
| 219 |
+
{
|
| 220 |
+
"cell_type": "code",
|
| 221 |
+
"execution_count": null,
|
| 222 |
+
"metadata": {},
|
| 223 |
+
"outputs": [],
|
| 224 |
+
"source": [
|
| 225 |
+
"set_seed(0)\n",
|
| 226 |
+
"results = {}\n",
|
| 227 |
+
"for lang in ['Java', 'Python']:\n",
|
| 228 |
+
" for graph in ['DFG', 'AST']:\n",
|
| 229 |
+
" result = []\n",
|
| 230 |
+
" df = pd.read_json(f\"{in_dir}/{lang}_{graph}.jsonl\", lines=True)\n",
|
| 231 |
+
" if lang == 'Python':\n",
|
| 232 |
+
" df = filter_df(df)\n",
|
| 233 |
+
"\n",
|
| 234 |
+
" question, bot = [], []\n",
|
| 235 |
+
" for i in range(len(df)):\n",
|
| 236 |
+
"\n",
|
| 237 |
+
" # filter out nodes that occur multiple times in the source code\n",
|
| 238 |
+
" node_texts = df.loc[i]['text']\n",
|
| 239 |
+
" node_ids = df.loc[i]['node_ids']\n",
|
| 240 |
+
" source = df.loc[i]['source']\n",
|
| 241 |
+
" assert len(node_ids) == len(node_texts)\n",
|
| 242 |
+
"\n",
|
| 243 |
+
" occurrences = np.array([source.count(t) for t in node_texts])\n",
|
| 244 |
+
" nodes_single_occurrence = np.where(occurrences == 1)[0]\n",
|
| 245 |
+
"\n",
|
| 246 |
+
" # we sample 1 node from each program\n",
|
| 247 |
+
" nodes = np.random.choice(nodes_single_occurrence, 1)\n",
|
| 248 |
+
" for node in nodes:\n",
|
| 249 |
+
" node_text = node_texts[node]\n",
|
| 250 |
+
" node_id = node_ids[node]\n",
|
| 251 |
+
" node_type = NODE_TYPES[node_id]\n",
|
| 252 |
+
"\n",
|
| 253 |
+
" p = random.sample(prompts, 1)[0]\n",
|
| 254 |
+
" p = [s.replace('{graph}', f\"{graph_type_map[graph]}\").replace('{lang}', f\"{lang}\") for s in p]\n",
|
| 255 |
+
" \n",
|
| 256 |
+
" response = p[1]\n",
|
| 257 |
+
" # deal with answer first in the response before plugging in the node text to avoid replacing something in the code\n",
|
| 258 |
+
" if any(node_type.startswith(l) for l in 'aeio'):\n",
|
| 259 |
+
" response = response.replace(' a ', ' an ')\n",
|
| 260 |
+
" response = response.replace('{answer}', node_type)\n",
|
| 261 |
+
" response = capitalize(response)\n",
|
| 262 |
+
" \n",
|
| 263 |
+
" if '\\n' in node_text:\n",
|
| 264 |
+
" node_text = f\"\\n```\\n{node_text}\\n```\\n\"\n",
|
| 265 |
+
" assert ph1 not in node_text and ph2 not in node_text and ph3 not in node_text and ph4 not in node_text and ph5 not in node_text\n",
|
| 266 |
+
" q = replace_place_holder(p[0], node_text)\n",
|
| 267 |
+
" response = replace_place_holder(response, node_text)\n",
|
| 268 |
+
" else:\n",
|
| 269 |
+
" node_text = f\"`{node_text}`\"\n",
|
| 270 |
+
" q = p[0].replace('{node}', node_text)\n",
|
| 271 |
+
" response = response.replace('{node}', node_text)\n",
|
| 272 |
+
"\n",
|
| 273 |
+
" source = source.strip('\\n')\n",
|
| 274 |
+
" q = f\"```\\n{source}\\n```\\n\\n{q}\"\n",
|
| 275 |
+
" question.append(q)\n",
|
| 276 |
+
" bot.append(response)\n",
|
| 277 |
+
" result.append(node_id)\n",
|
| 278 |
+
"\n",
|
| 279 |
+
" df['question'] = question\n",
|
| 280 |
+
" df['bot'] = bot\n",
|
| 281 |
+
" df = df.drop(columns={'text', 'source', 'node_ids', 'edge_index'})\n",
|
| 282 |
+
" df.to_json(f\"{out_dir}/Node_Classification_{lang}_{graph}.jsonl\", orient='records', lines=True)\n",
|
| 283 |
+
" results[f\"{lang}-{graph}\"] = result"
|
| 284 |
+
]
|
| 285 |
+
},
|
| 286 |
+
{
|
| 287 |
+
"cell_type": "markdown",
|
| 288 |
+
"metadata": {},
|
| 289 |
+
"source": [
|
| 290 |
+
"## parent node"
|
| 291 |
+
]
|
| 292 |
+
},
|
| 293 |
+
{
|
| 294 |
+
"cell_type": "code",
|
| 295 |
+
"execution_count": null,
|
| 296 |
+
"metadata": {},
|
| 297 |
+
"outputs": [],
|
| 298 |
+
"source": [
|
| 299 |
+
"questions = [\n",
|
| 300 |
+
" 'In the {graph} of this {lang} program, what is the parent node of this {node_type}: {node}.',\n",
|
| 301 |
+
" 'What is the parent of {node_type} {node} in the {graph} of this {lang} program?',\n",
|
| 302 |
+
" 'What is the parent node of {node_type} {node} in the {graph} of this {lang} program?',\n",
|
| 303 |
+
" 'Based on the {graph} of this {lang} program, identify the parent of {node_type} {node}.',\n",
|
| 304 |
+
" 'Based on the {graph} of this {lang} program, identify the parent of this {node_type}: {node}.',\n",
|
| 305 |
+
" 'Identify the parent of {node_type} {node} in the {graph} of this {lang} program.',\n",
|
| 306 |
+
" \"In the {graph} of the {lang} program presented, what is the predecessor of {node_type} {node}?\",\n",
|
| 307 |
+
" \"What node acts as the parent to {node_type} {node} in the {graph} of the displayed {lang} program?\",\n",
|
| 308 |
+
" \"Can you determine the parent node of {node_type} {node} in the {graph} of this {lang} program?\",\n",
|
| 309 |
+
" \"Which node is directly above {node_type} {node} in the hierarchy of the {graph} of the provided {lang} program?\",\n",
|
| 310 |
+
" \"Whose child is {node_type} {node} within the {graph} of this {lang} program?\",\n",
|
| 311 |
+
" \"What is the immediate ancestor of the {node_type} {node} in the {graph} of this {lang} program?\",\n",
|
| 312 |
+
" \"Regarding the {graph} of this {lang} program, can you point out the parent of {node_type} {node}?\",\n",
|
| 313 |
+
" \"In terms of graph theory, what is the parent of the {node_type} {node} in the {graph} of this {lang} program?\",\n",
|
| 314 |
+
" \"Who has the parental role for {node_type} {node} in the {graph}'s topology of this {lang} program?\",\n",
|
| 315 |
+
" \"For {node_type} {node} in the {graph} of the given {lang} program, which node supplies the incoming edge?\",\n",
|
| 316 |
+
"]\n",
|
| 317 |
+
"answers1 = [\n",
|
| 318 |
+
" \"In the {graph} of the given {lang} program, the parent of the given {node_type} is {parent}, which is a {parent_type}.\",\n",
|
| 319 |
+
" \"This {node_type}'s parent is the {parent_type} {parent}.\",\n",
|
| 320 |
+
" \"The given {node_type}'s parent in the {graph} of this {lang} program is the {parent_type} {parent}.\",\n",
|
| 321 |
+
" \"The parent of {node_type} {node} in the {graph} of this {lang} program is identified as {parent}, categorized as a {parent_type}.\",\n",
|
| 322 |
+
" \"In the structure of the {graph} of this {lang} program, {node_type} {node} finds its parent in node {parent}, which is a {parent_type}.\",\n",
|
| 323 |
+
" \"Node {parent}, a {parent_type}, serves as the parent to {node_type} {node} in the {graph} of this {lang} program.\",\n",
|
| 324 |
+
" \"As per the hierarchy in the {graph}, the {parent_type} node {parent} is the direct predecessor to {node_type} {node}.\",\n",
|
| 325 |
+
" \"Upon inspection, it is clear that the parent of {node_type} {node} is the {parent_type} {parent}.\",\n",
|
| 326 |
+
" \"The {node_type} {node} is immediately descended from {parent}, a {parent_type} in the {graph} of this {lang} program.\",\n",
|
| 327 |
+
" \"{node_type} {node}'s parental node is determined to be {parent}, which falls into the category of {parent_type}.\",\n",
|
| 328 |
+
" \"For {node_type} {node}, its lineage traces back to the {parent_type} node {parent} as its parent.\",\n",
|
| 329 |
+
" \"Within the nodal arrangement of the {graph}, {parent} is the progenitor to {node_type} {node}, having the classification of a {parent_type}.\",\n",
|
| 330 |
+
" \"Tracing the edges leads to confirming {parent}, a {parent_type}, as the parent of {node_type} {node}.\"\n",
|
| 331 |
+
"]\n",
|
| 332 |
+
"answers2 = [\n",
|
| 333 |
+
" 'This {node_type} has no parent in the {graph} of this {lang} program.',\n",
|
| 334 |
+
" 'This {node_type} has no parent in the {graph} of the given {lang} program.',\n",
|
| 335 |
+
" 'There is no edge pointing to this {node_type} in the {graph}. Therefore it does not have any parent.',\n",
|
| 336 |
+
" 'There is no edge pointing to this {node_type} in the {graph} of the given {lang} program. Therefore it does not have any parent.',\n",
|
| 337 |
+
" \"Within the confines of the {graph} of this {lang} program, {node_type} {node} does not have a parent node.\",\n",
|
| 338 |
+
" \"{node_type} {node} stands without a parent in the {graph}'s structure.\",\n",
|
| 339 |
+
" \"No parent node is associated with {node_type} {node} in the {graph} of the provided {lang} program.\",\n",
|
| 340 |
+
" \"A review of the code establishes that there is no preceding node to {node_type} {node} in the {graph}; it has no parent.\",\n",
|
| 341 |
+
" \"The {node_type} designated as {node} appears to lack a parental connection within the {graph} of this code.\",\n",
|
| 342 |
+
" \"In terms of graph topology, {node_type} {node} is an orphan node with no parent.\",\n",
|
| 343 |
+
" \"There is no edge incoming to {node_type} {node}, indicating the absence of a parent in the {graph} of this {lang} program.\",\n",
|
| 344 |
+
" \"After analyzing the code, it becomes evident that {node_type} {node} lacks a directly linked parent node in the {graph}.\",\n",
|
| 345 |
+
" \"The {graph} denotes that {node_type} {node} is disconnected from any parental lineage.\",\n",
|
| 346 |
+
" \"As depicted in the code, {node_type} {node} exists without a parent node to claim in the {graph}.\",\n",
|
| 347 |
+
"]\n",
|
| 348 |
+
"answers3 = [\n",
|
| 349 |
+
" \"There are multiple parents of this {node_type} in the {graph}:\\n\",\n",
|
| 350 |
+
" \"This {node_type} has more than one parent in the {graph}:\\n\"\n",
|
| 351 |
+
"]\n",
|
| 352 |
+
"print(len(questions), len(set(questions)))\n",
|
| 353 |
+
"print(len(answers1), len(set(answers1)))\n",
|
| 354 |
+
"print(len(answers2), len(set(answers2)))\n",
|
| 355 |
+
"print(len(answers3), len(set(answers3)))\n",
|
| 356 |
+
"assert all(a.count('{graph}') == 1 for a in questions)\n",
|
| 357 |
+
"assert all(a.count('{lang}') == 1 for a in questions)\n",
|
| 358 |
+
"\n",
|
| 359 |
+
"bots = [a for a in answers1]\n",
|
| 360 |
+
"bots_none = [a for a in answers2]\n",
|
| 361 |
+
"bots_multiple = [a for a in answers3]\n",
|
| 362 |
+
"prompts = [[q, b] for q in questions for b in bots]\n",
|
| 363 |
+
"print(len(prompts))"
|
| 364 |
+
]
|
| 365 |
+
},
|
| 366 |
+
{
|
| 367 |
+
"cell_type": "code",
|
| 368 |
+
"execution_count": null,
|
| 369 |
+
"metadata": {},
|
| 370 |
+
"outputs": [],
|
| 371 |
+
"source": [
|
| 372 |
+
"set_seed(1)\n",
|
| 373 |
+
"\n",
|
| 374 |
+
"results = {}\n",
|
| 375 |
+
"for lang in ['Java', 'Python']:\n",
|
| 376 |
+
" for graph in ['DFG', 'AST']:\n",
|
| 377 |
+
" result = []\n",
|
| 378 |
+
" df = pd.read_json(f\"{in_dir}/{lang}_{graph}.jsonl\", lines=True)\n",
|
| 379 |
+
" if lang == 'Python':\n",
|
| 380 |
+
" df = filter_df(df)\n",
|
| 381 |
+
" \n",
|
| 382 |
+
" question, bot = [], []\n",
|
| 383 |
+
" for i in range(len(df)):\n",
|
| 384 |
+
"\n",
|
| 385 |
+
" # filter out nodes that occur multiple times in the source code\n",
|
| 386 |
+
" node_texts = df.loc[i]['text']\n",
|
| 387 |
+
" node_ids = df.loc[i]['node_ids']\n",
|
| 388 |
+
" source = df.loc[i]['source']\n",
|
| 389 |
+
" edge_index = torch.tensor(df.loc[i]['edge_index'])\n",
|
| 390 |
+
" assert len(node_ids) == len(node_texts)\n",
|
| 391 |
+
"\n",
|
| 392 |
+
" occurrences = np.array([source.count(t) for t in node_texts])\n",
|
| 393 |
+
" nodes_single_occurrence = np.where(occurrences == 1)[0]\n",
|
| 394 |
+
" nodes_with_parents = [n for n in nodes_single_occurrence if n in edge_index[:, 1]]\n",
|
| 395 |
+
" nodes_without_parents = [n for n in nodes_single_occurrence if n not in edge_index[:, 1]]\n",
|
| 396 |
+
" \n",
|
| 397 |
+
" # we roughly maintain a balanced distribution\n",
|
| 398 |
+
" if random.random() < 0.75 and len(nodes_with_parents) > 0:\n",
|
| 399 |
+
" node = random.sample(nodes_with_parents, 1)[0]\n",
|
| 400 |
+
" elif len(nodes_without_parents) > 0:\n",
|
| 401 |
+
" node = random.sample(nodes_without_parents, 1)[0]\n",
|
| 402 |
+
" else:\n",
|
| 403 |
+
" node = np.random.choice(nodes_single_occurrence, 1)[0]\n",
|
| 404 |
+
" \n",
|
| 405 |
+
" node_text = node_texts[node]\n",
|
| 406 |
+
" node_id = node_ids[node]\n",
|
| 407 |
+
" node_type = NODE_TYPES[node_id]\n",
|
| 408 |
+
" edge_to_node = [edge for edge in edge_index if edge[1] == node]\n",
|
| 409 |
+
"\n",
|
| 410 |
+
" p = (random.sample(prompts, 1)[0] + random.sample(bots_none, 1) + random.sample(bots_multiple, 1)).copy()\n",
|
| 411 |
+
" assert p[2] in bots_none\n",
|
| 412 |
+
" p = [s.replace('{graph}', f\"{graph_type_map[graph]}\").replace('{lang}', f\"{lang}\") for s in p]\n",
|
| 413 |
+
"\n",
|
| 414 |
+
" # deal with answer first in the response before plugging in the node text to avoid replacing something in the code\n",
|
| 415 |
+
" num_parents = len(edge_to_node)\n",
|
| 416 |
+
" response = p[2] if num_parents == 0 else (p[1] if num_parents == 1 else p[3])\n",
|
| 417 |
+
" response = capitalize(response.replace('{node_type}', node_type))\n",
|
| 418 |
+
"\n",
|
| 419 |
+
" if num_parents > 1:\n",
|
| 420 |
+
" # no problem here\n",
|
| 421 |
+
" for j in range(num_parents):\n",
|
| 422 |
+
" parent_node = edge_to_node[j][0]\n",
|
| 423 |
+
" parent_text = node_texts[parent_node]\n",
|
| 424 |
+
" parent_id = node_ids[parent_node]\n",
|
| 425 |
+
" parent_type = NODE_TYPES[parent_id]\n",
|
| 426 |
+
" if '\\n' in parent_text:\n",
|
| 427 |
+
" parent_text = f\"\\n```\\n{parent_text}\\n```\\n\"\n",
|
| 428 |
+
" response += f\"{parent_type}:{parent_text}\"\n",
|
| 429 |
+
" else:\n",
|
| 430 |
+
" parent_text = f\"`{parent_text}`\"\n",
|
| 431 |
+
" response += f\"{parent_type}: {parent_text}\\n\"\n",
|
| 432 |
+
" elif num_parents == 1:\n",
|
| 433 |
+
" parent_node = edge_to_node[0][0]\n",
|
| 434 |
+
" parent_text = node_texts[parent_node]\n",
|
| 435 |
+
" parent_id = node_ids[parent_node]\n",
|
| 436 |
+
" parent_type = NODE_TYPES[parent_id]\n",
|
| 437 |
+
" if any(parent_type.startswith(l) for l in 'aeio'):\n",
|
| 438 |
+
" response = response.replace(' a ', ' an ')\n",
|
| 439 |
+
" if '\\n' in parent_text:\n",
|
| 440 |
+
" parent_text = f\"\\n```\\n{parent_text}\\n```\\n\"\n",
|
| 441 |
+
" response = replace_place_holder(response, parent_text, \"{parent}\")\n",
|
| 442 |
+
" else:\n",
|
| 443 |
+
" parent_text = f\"`{parent_text}`\"\n",
|
| 444 |
+
" response = response.replace('{parent}', parent_text)\n",
|
| 445 |
+
" response = response.replace('{parent_type}', parent_type) \n",
|
| 446 |
+
" \n",
|
| 447 |
+
" # now deal with node text\n",
|
| 448 |
+
" assert response.count('{node}') <= 1\n",
|
| 449 |
+
" if '\\n' in node_text:\n",
|
| 450 |
+
" node_text = f\"\\n```\\n{node_text}\\n```\\n\"\n",
|
| 451 |
+
" # note that now response may contain \"\\n,\" patterns\n",
|
| 452 |
+
" q = replace_place_holder(p[0].replace('{node_type}', node_type), node_text)\n",
|
| 453 |
+
" response = replace_place_holder(response, node_text)\n",
|
| 454 |
+
" else:\n",
|
| 455 |
+
" node_text = f\"`{node_text}`\"\n",
|
| 456 |
+
" q = p[0].replace('{node_type}', node_type).replace('{node}', node_text)\n",
|
| 457 |
+
" response = response.replace('{node}', node_text)\n",
|
| 458 |
+
"\n",
|
| 459 |
+
" source = source.strip('\\n')\n",
|
| 460 |
+
" q = f\"```\\n{source}\\n```\\n\\n{q}\"\n",
|
| 461 |
+
" question.append(q)\n",
|
| 462 |
+
" bot.append(response)\n",
|
| 463 |
+
" result.append(num_parents)\n",
|
| 464 |
+
"\n",
|
| 465 |
+
" df['question'] = question\n",
|
| 466 |
+
" df['bot'] = bot\n",
|
| 467 |
+
" df = df.drop(columns={'text', 'source', 'node_ids', 'edge_index'})\n",
|
| 468 |
+
" df.to_json(f\"{out_dir}/Parent_{lang}_{graph}.jsonl\", orient='records', lines=True)\n",
|
| 469 |
+
" results[f\"{lang}-{graph}\"] = result"
|
| 470 |
+
]
|
| 471 |
+
},
|
| 472 |
+
{
|
| 473 |
+
"cell_type": "markdown",
|
| 474 |
+
"metadata": {},
|
| 475 |
+
"source": [
|
| 476 |
+
"## Children"
|
| 477 |
+
]
|
| 478 |
+
},
|
| 479 |
+
{
|
| 480 |
+
"cell_type": "code",
|
| 481 |
+
"execution_count": null,
|
| 482 |
+
"metadata": {},
|
| 483 |
+
"outputs": [],
|
| 484 |
+
"source": [
|
| 485 |
+
"questions = [\n",
|
| 486 |
+
" 'In the {graph} of this {lang} program, what are the children of this {node_type}: {node}.',\n",
|
| 487 |
+
" 'Identify all children of {node_type} {node} in the {graph} of this {lang} program.',\n",
|
| 488 |
+
" 'Find the child nodes of {node_type} {node} in the {graph} of this {lang} program.',\n",
|
| 489 |
+
" 'In the {graph} of this {lang} program, how many children does the {node_type} {node} have? What are they?',\n",
|
| 490 |
+
" \"How many children does {node_type} {node} have in the {graph} of this {lang} program? What are they?\",\n",
|
| 491 |
+
" 'Please find all children of {node_type} {node} in the {graph} of this {lang} program.',\n",
|
| 492 |
+
" 'Can you find all children of {node_type} {node} in the {graph} of this {lang} program?',\n",
|
| 493 |
+
" \"List all the descendant nodes of {node_type} {node} in the {graph} of this {lang} program.\",\n",
|
| 494 |
+
" \"What are the direct children of the {node_type} {node} in the {graph} of this {lang} program?\",\n",
|
| 495 |
+
" \"Can you enumerate the offspring of {node_type} {node} within the {graph} of this {lang} program?\",\n",
|
| 496 |
+
" \"Could you provide the list of child nodes attached to {node_type} {node} in the {graph} of this {lang} program?\",\n",
|
| 497 |
+
" \"Please identify the child nodes emanating from {node_type} {node} in the {graph} of this {lang} program.\",\n",
|
| 498 |
+
" \"Show me the child nodes of {node_type} {node} in the {graph} of this {lang} program.\",\n",
|
| 499 |
+
" \"What nodes are directly connected to {node_type} {node} as its children in the {graph} of this {lang} program?\",\n",
|
| 500 |
+
" \"I need to know all the child elements of {node_type} {node} in the {graph} of this {lang} program. Can you provide that?\",\n",
|
| 501 |
+
" \"Are there any nodes that directly derive from {node_type} {node} in the {graph} of this {lang} program?\",\n",
|
| 502 |
+
" \"Which nodes act as successors to the node tagged as {node_type} {node} in the {graph} of this {lang} program?\",\n",
|
| 503 |
+
" \"What are the adjacent nodes that are children of {node_type} {node} in the {graph} of this {lang} program?\",\n",
|
| 504 |
+
" \"Identify the nodes that are immediate successors of {node_type} {node} in the {graph} of this {lang} program.\",\n",
|
| 505 |
+
" \"Detail the nodes branching from {node_type} {node} in the {graph} of this {lang} program.\",\n",
|
| 506 |
+
" \"Reveal all nodes that are directly beneath {node_type} {node} in the topology of the {graph} of this {lang} program.\",\n",
|
| 507 |
+
"]\n",
|
| 508 |
+
"answers1 = [\n",
|
| 509 |
+
" \"The given {node_type} has {child_num} children in the {graph}, they are:\\n\",\n",
|
| 510 |
+
" \"This {node_type} has {child_num} children:\\n\",\n",
|
| 511 |
+
" \"{node_type} {node} has a total of {child_num} children in the {graph}, which are:\\n\",\n",
|
| 512 |
+
" \"There are {child_num} child nodes of {node_type} {node}, specifically:\\n\",\n",
|
| 513 |
+
" \"As for the children of {node_type} {node}, you will find {child_num} direct descendants:\\n\",\n",
|
| 514 |
+
" \"The count of {node_type} {node}'s children amounts to {child_num}. They include:\\n\",\n",
|
| 515 |
+
" \"Upon identification, {node_type} {node} appears to have {child_num} offspring, namely:\\n\",\n",
|
| 516 |
+
" \"{node_type} {node} is parent to the following {child_num} nodes:\\n\",\n",
|
| 517 |
+
" \"A list of the {child_num} children under {node_type} {node} is as follows:\\n\",\n",
|
| 518 |
+
" \"Directly under {node_type} {node}, there are {child_num} children listed as:\\n\",\n",
|
| 519 |
+
" \"{node_type} {node} holds the hierarchy over {child_num} child nodes, which are:\\n\",\n",
|
| 520 |
+
" \"{child_num} children spring from {node_type} {node}, which are given below:\\n\",\n",
|
| 521 |
+
"]\n",
|
| 522 |
+
"answers2 = [\n",
|
| 523 |
+
" \"This {node_type} does not have any child nodes in the {graph}.\",\n",
|
| 524 |
+
" \"This {node_type} does not have any children in the {graph}.\",\n",
|
| 525 |
+
" \"There are no children of this {node_type} in the {graph} of the given code.\",\n",
|
| 526 |
+
" \"The given {node_type} does not have any children in the {graph}.\",\n",
|
| 527 |
+
" \"After examining the code, it's determined that in the {graph} this {node_type} has no children.\",\n",
|
| 528 |
+
" \"{node_type} {node} stands alone with zero child nodes descending from it.\",\n",
|
| 529 |
+
" \"I've checked the {node_type} {node} and found it has no direct descendants.\",\n",
|
| 530 |
+
" \"There are no child nodes attached to {node_type} {node} in the {graph} of this program.\",\n",
|
| 531 |
+
" \"No descendants can be traced from this {node_type}.\",\n",
|
| 532 |
+
" \"The {node_type} {node} is devoid of child nodes within the {graph} of the code.\",\n",
|
| 533 |
+
" \"Upon inspection, no nodes emerge as children of {node_type} {node}.\",\n",
|
| 534 |
+
" \"{node_type} {node} exists without progeny in the hierarchical layout.\",\n",
|
| 535 |
+
" \"It appears {node_type} {node} has no children.\",\n",
|
| 536 |
+
"]\n",
|
| 537 |
+
"\n",
|
| 538 |
+
"print(len(questions), len(set(questions)))\n",
|
| 539 |
+
"print(len(answers1), len(set(answers1)))\n",
|
| 540 |
+
"print(len(answers2), len(set(answers2)))\n",
|
| 541 |
+
"assert all(a.count('{answer}') == 1 for a in answers)\n",
|
| 542 |
+
"assert all(a.count('{graph}') == 1 for a in questions)\n",
|
| 543 |
+
"assert all(a.count('{lang}') == 1 for a in questions)\n",
|
| 544 |
+
"\n",
|
| 545 |
+
"bots = [a for a in answers1]\n",
|
| 546 |
+
"bots_none = [a for a in answers2]\n",
|
| 547 |
+
"prompts = [[q, b] for q in questions for b in bots]\n",
|
| 548 |
+
"print(len(prompts))"
|
| 549 |
+
]
|
| 550 |
+
},
|
| 551 |
+
{
|
| 552 |
+
"cell_type": "code",
|
| 553 |
+
"execution_count": null,
|
| 554 |
+
"metadata": {},
|
| 555 |
+
"outputs": [],
|
| 556 |
+
"source": [
|
| 557 |
+
"set_seed(2)\n",
|
| 558 |
+
"\n",
|
| 559 |
+
"results = {}\n",
|
| 560 |
+
"for lang in ['Java', 'Python']:\n",
|
| 561 |
+
" for graph in ['DFG', 'AST']:\n",
|
| 562 |
+
" result = []\n",
|
| 563 |
+
" df = pd.read_json(f\"{in_dir}/{lang}_{graph}.jsonl\", lines=True)\n",
|
| 564 |
+
" if lang == 'Python':\n",
|
| 565 |
+
" df = filter_df(df)\n",
|
| 566 |
+
" \n",
|
| 567 |
+
" question, bot = [], []\n",
|
| 568 |
+
" selected_idx = []\n",
|
| 569 |
+
" for i in range(len(df)):\n",
|
| 570 |
+
"\n",
|
| 571 |
+
" # filter out nodes that occur multiple times in the source code\n",
|
| 572 |
+
" node_texts = df.loc[i]['text']\n",
|
| 573 |
+
" node_ids = df.loc[i]['node_ids']\n",
|
| 574 |
+
" source = df.loc[i]['source']\n",
|
| 575 |
+
" edge_index = torch.tensor(df.loc[i]['edge_index'])\n",
|
| 576 |
+
"\n",
|
| 577 |
+
" occurrences = np.array([source.count(t) for t in node_texts])\n",
|
| 578 |
+
" nodes_single_occurrence = np.where(occurrences == 1)[0]\n",
|
| 579 |
+
" nodes_with_children = [n for n in nodes_single_occurrence if n in edge_index[:, 0]]\n",
|
| 580 |
+
" nodes_without_children = [n for n in nodes_single_occurrence if n not in edge_index[:, 0]]\n",
|
| 581 |
+
" \n",
|
| 582 |
+
" # we roughly maintain a balanced distribution\n",
|
| 583 |
+
" if random.random() < 0.85 and len(nodes_with_children) > 0:\n",
|
| 584 |
+
" node = random.sample(nodes_with_children, 1)[0]\n",
|
| 585 |
+
" elif len(nodes_without_children) > 0:\n",
|
| 586 |
+
" node = random.sample(nodes_without_children, 1)[0]\n",
|
| 587 |
+
" else:\n",
|
| 588 |
+
" node = np.random.choice(nodes_single_occurrence, 1)[0]\n",
|
| 589 |
+
" \n",
|
| 590 |
+
" node_text = node_texts[node]\n",
|
| 591 |
+
" node_id = node_ids[node]\n",
|
| 592 |
+
" node_type = NODE_TYPES[node_id]\n",
|
| 593 |
+
" edge_from_node = [edge for edge in edge_index if edge[0] == node]\n",
|
| 594 |
+
"\n",
|
| 595 |
+
" p = (random.sample(prompts, 1)[0] + random.sample(bots_none, 1)).copy()\n",
|
| 596 |
+
" assert p[1] in bots\n",
|
| 597 |
+
" p = [s.replace('{graph}', f\"{graph_type_map[graph]}\").replace('{lang}', f\"{lang}\") for s in p]\n",
|
| 598 |
+
"\n",
|
| 599 |
+
" num_children = len(edge_from_node)\n",
|
| 600 |
+
" if num_children > 10:\n",
|
| 601 |
+
" continue\n",
|
| 602 |
+
" else:\n",
|
| 603 |
+
" selected_idx.append(i)\n",
|
| 604 |
+
" response = p[2] if num_children == 0 else p[1]\n",
|
| 605 |
+
" response = capitalize(response.replace('{node_type}', node_type))\n",
|
| 606 |
+
" if num_children == 1:\n",
|
| 607 |
+
" response = response.replace('{child_num}', \"1\").replace('children', 'child').replace('nodes', 'node').replace('they are', 'it is').replace(' are', ' is').replace('descendants', 'descendant').replace('They include', 'It is').replace(' spring ', ' springs ')\n",
|
| 608 |
+
" else:\n",
|
| 609 |
+
" response = response.replace('{child_num}', f\"{num_children}\")\n",
|
| 610 |
+
" \n",
|
| 611 |
+
" if '\\n' in node_text:\n",
|
| 612 |
+
" node_text = f\"\\n```\\n{node_text}\\n```\\n\"\n",
|
| 613 |
+
" q = replace_place_holder(p[0].replace('{node_type}', node_type), node_text)\n",
|
| 614 |
+
" response = replace_place_holder(response, node_text)\n",
|
| 615 |
+
" else:\n",
|
| 616 |
+
" node_text = f\"`{node_text}`\"\n",
|
| 617 |
+
" q = p[0].replace('{node_type}', node_type).replace('{node}', node_text)\n",
|
| 618 |
+
" response = response.replace('{node}', node_text)\n",
|
| 619 |
+
" \n",
|
| 620 |
+
" for j in range(num_children):\n",
|
| 621 |
+
" child_node = edge_from_node[j][1]\n",
|
| 622 |
+
" child_text = node_texts[child_node]\n",
|
| 623 |
+
" child_id = node_ids[child_node]\n",
|
| 624 |
+
" child_type = NODE_TYPES[child_id]\n",
|
| 625 |
+
"\n",
|
| 626 |
+
" if '\\n' in child_text:\n",
|
| 627 |
+
" child_text = f\"\\n```\\n{child_text}\\n```\\n\"\n",
|
| 628 |
+
" response += f\"{child_type}:{child_text}\"\n",
|
| 629 |
+
" else:\n",
|
| 630 |
+
" child_text = f\"`{child_text}`\"\n",
|
| 631 |
+
" response += f\"{child_type}: {child_text}\\n\"\n",
|
| 632 |
+
" if num_children != len(set((node_ids[e[1]], node_texts[e[1]]) for e in edge_from_node)):\n",
|
| 633 |
+
" response += \"Note that there are multiple children with the same node type and literal representation.\"\n",
|
| 634 |
+
"\n",
|
| 635 |
+
" source = source.strip('\\n')\n",
|
| 636 |
+
" q = f\"```\\n{source}\\n```\\n\\n{q}\"\n",
|
| 637 |
+
" question.append(q)\n",
|
| 638 |
+
" bot.append(response)\n",
|
| 639 |
+
" result.append(num_children)\n",
|
| 640 |
+
"\n",
|
| 641 |
+
" df = df.loc[selected_idx].reset_index(drop=True)\n",
|
| 642 |
+
" df['question'] = question\n",
|
| 643 |
+
" df['bot'] = bot\n",
|
| 644 |
+
" df = df.drop(columns={'text', 'source', 'node_ids', 'edge_index'})\n",
|
| 645 |
+
" df.to_json(f\"{out_dir}/Children_{lang}_{graph}.jsonl\", orient='records', lines=True)\n",
|
| 646 |
+
" results[f\"{lang}-{graph}\"] = result"
|
| 647 |
+
]
|
| 648 |
+
},
|
| 649 |
+
{
|
| 650 |
+
"cell_type": "markdown",
|
| 651 |
+
"metadata": {},
|
| 652 |
+
"source": [
|
| 653 |
+
"## Edge prediction"
|
| 654 |
+
]
|
| 655 |
+
},
|
| 656 |
+
{
|
| 657 |
+
"cell_type": "code",
|
| 658 |
+
"execution_count": null,
|
| 659 |
+
"metadata": {},
|
| 660 |
+
"outputs": [],
|
| 661 |
+
"source": [
|
| 662 |
+
"questions = [\n",
|
| 663 |
+
" \"In the {graph} of this {lang} program, is there {edge_or_link} from {node_type1} {node1} to {node_type2} {node2}?\",\n",
|
| 664 |
+
" 'In the {graph} of this {lang} program, is there {edge_or_link} pointing from {node_type1} {node1} to {node_type2} {node2}?',\n",
|
| 665 |
+
" \"Please tell me if there is {edge_or_link} pointing from {node_type1} {node1} to {node_type2} {node2} in the {graph} of this {lang} program.\",\n",
|
| 666 |
+
" 'Is there {edge_or_link} from {node_type1} {node1} to {node_type2} {node2} in the {graph} of this {lang} program?',\n",
|
| 667 |
+
" \"Does a connection exist from {node_type1} {node1} to {node_type2} {node2} in the {graph} of this {lang} program?\",\n",
|
| 668 |
+
" \"In the {graph} of this {lang} program, do we have an arrow leading from {node_type1} {node1} to {node_type2} {node2}?\",\n",
|
| 669 |
+
" \"Is it true that {node_type1} {node1} is a predecessor of {node_type2} {node2} in the {graph} of this {lang} program?\",\n",
|
| 670 |
+
"]\n",
|
| 671 |
+
"answers1 = [\n",
|
| 672 |
+
" \"Yes, that is the case.\",\n",
|
| 673 |
+
" \"Yes, there is {edge_or_link} from {node_type1} {node1} to {node_type2} {node2}.\",\n",
|
| 674 |
+
" \"Yes, there is {edge_or_link} from {node_type1} {node1} to {node_type2} {node2} in the {graph} of this code.\",\n",
|
| 675 |
+
" \"Yes, there is {edge_or_link} pointing from {node_type1} {node1} to {node_type2} {node2} in the {graph}.\",\n",
|
| 676 |
+
" \"Affirmative, there exists {edge_or_link} from {node_type1} {node1} to {node_type2} {node2}.\",\n",
|
| 677 |
+
" \"Yes, that is the case. {node1} is directly connected to {node2}.\",\n",
|
| 678 |
+
"]\n",
|
| 679 |
+
"answers2 = [\n",
|
| 680 |
+
" \"No, that is not the case.\",\n",
|
| 681 |
+
" \"No, {node_type1} {node1} is not linked to {node_type2} {node2} by any edge in the {graph} of the given code.\",\n",
|
| 682 |
+
" \"No, there is no {edge_or_link} from {node_type1} {node1} to {node_type2} {node2}.\",\n",
|
| 683 |
+
" \"No, such {edge_or_link} is absent from the {graph}.\",\n",
|
| 684 |
+
" \"The code does not show {node_type1} {node1} as a predecessor to {node_type2} {node2} in the {graph}.\",\n",
|
| 685 |
+
"]\n",
|
| 686 |
+
"\n",
|
| 687 |
+
"print(len(questions), len(set(questions)))\n",
|
| 688 |
+
"print(len(answers1), len(set(answers1)))\n",
|
| 689 |
+
"print(len(answers2), len(set(answers2)))\n",
|
| 690 |
+
"assert all(a.count('{graph}') == 1 for a in questions)\n",
|
| 691 |
+
"assert all(a.count('{lang}') == 1 for a in questions)\n",
|
| 692 |
+
"\n",
|
| 693 |
+
"bots = [a for a in answers1]\n",
|
| 694 |
+
"bots_none = [a for a in answers2]\n",
|
| 695 |
+
"prompts = [[q, b] for q in questions for b in bots]\n",
|
| 696 |
+
"print(len(prompts))"
|
| 697 |
+
]
|
| 698 |
+
},
|
| 699 |
+
{
|
| 700 |
+
"cell_type": "code",
|
| 701 |
+
"execution_count": null,
|
| 702 |
+
"metadata": {},
|
| 703 |
+
"outputs": [],
|
| 704 |
+
"source": [
|
| 705 |
+
"set_seed(3)\n",
|
| 706 |
+
"results = {}\n",
|
| 707 |
+
"for lang in ['Java', 'Python']:\n",
|
| 708 |
+
" for graph in ['DFG', 'AST']:\n",
|
| 709 |
+
" result = []\n",
|
| 710 |
+
" df = pd.read_json(f\"{in_dir}/{lang}_{graph}.jsonl\", lines=True)\n",
|
| 711 |
+
" if lang == 'Python':\n",
|
| 712 |
+
" df = filter_df(df)\n",
|
| 713 |
+
" \n",
|
| 714 |
+
" question, bot = [], []\n",
|
| 715 |
+
" for i in range(len(df)):\n",
|
| 716 |
+
"\n",
|
| 717 |
+
" # filter out nodes that occur multiple times in the source code\n",
|
| 718 |
+
" node_texts = df.loc[i]['text']\n",
|
| 719 |
+
" node_ids = df.loc[i]['node_ids']\n",
|
| 720 |
+
" source = df.loc[i]['source']\n",
|
| 721 |
+
" edge_index = df.loc[i]['edge_index']\n",
|
| 722 |
+
"\n",
|
| 723 |
+
" occurrences = np.array([source.count(t) for t in node_texts])\n",
|
| 724 |
+
" nodes_single_occurrence = np.where(occurrences == 1)[0]\n",
|
| 725 |
+
" edge_index_elligible = [e for e in edge_index if (e[0] in nodes_single_occurrence and e[1] in nodes_single_occurrence)]\n",
|
| 726 |
+
" if graph == 'AST' and random.random() > 0.1:\n",
|
| 727 |
+
" edge_index_elligible = [e for e in edge_index_elligible if (node_texts[e[1]] not in node_texts[e[0]])]\n",
|
| 728 |
+
" \n",
|
| 729 |
+
" # we make sure at least half the problems have positive answer\n",
|
| 730 |
+
" if random.random() < 0.75 and len(edge_index_elligible) > 0:\n",
|
| 731 |
+
" n1, n2 = random.sample(edge_index_elligible, 1)[0]\n",
|
| 732 |
+
" else:\n",
|
| 733 |
+
" n1, n2 = np.random.choice(nodes_single_occurrence, 2)\n",
|
| 734 |
+
" \n",
|
| 735 |
+
" n1_text, n2_text = node_texts[n1], node_texts[n2]\n",
|
| 736 |
+
" n1_type, n2_type = NODE_TYPES[node_ids[n1]], NODE_TYPES[node_ids[n2]]\n",
|
| 737 |
+
"\n",
|
| 738 |
+
" p = random.sample(prompts, 1)[0] + random.sample(bots_none, 1)\n",
|
| 739 |
+
" p = [s.replace('{graph}', f\"{graph_type_map[graph]}\").replace('{lang}', f\"{lang}\") for s in p]\n",
|
| 740 |
+
" edge_or_link = 'an edge' if random.random() < 0.5 else 'a link'\n",
|
| 741 |
+
" p = [s.replace('{edge_or_link}', edge_or_link) for s in p[:-1]] + [p[-1].replace('such {edge_or_link}', f\"such {edge_or_link}\").replace('{edge_or_link}', edge_or_link.split()[-1])]\n",
|
| 742 |
+
" \n",
|
| 743 |
+
" q, b = '', ''\n",
|
| 744 |
+
" b = p[1] if [n1, n2] in edge_index else p[2]\n",
|
| 745 |
+
" b = b.replace('{node_type1}', n1_type).replace('{node_type2}', n2_type)\n",
|
| 746 |
+
"\n",
|
| 747 |
+
" if '\\n' in n1_text:\n",
|
| 748 |
+
" n1_text = f\"\\n```\\n{n1_text}\\n```\\n\"\n",
|
| 749 |
+
" q = replace_place_holder(p[0].replace('{node_type1}', n1_type).replace('{node_type2}', n2_type), n1_text, \"{node1}\")\n",
|
| 750 |
+
" b = replace_place_holder(b, n1_text, \"{node1}\")\n",
|
| 751 |
+
" else:\n",
|
| 752 |
+
" n1_text = f\"`{n1_text}`\"\n",
|
| 753 |
+
" q = p[0].replace('{node_type1}', n1_type).replace('{node_type2}', n2_type).replace('{node1}', n1_text)\n",
|
| 754 |
+
" b = b.replace('{node1}', n1_text)\n",
|
| 755 |
+
" \n",
|
| 756 |
+
" if '\\n' in n2_text:\n",
|
| 757 |
+
" n2_text = f\"\\n```\\n{n2_text}\\n```\\n\"\n",
|
| 758 |
+
" q = replace_place_holder(q, n2_text, \"{node2}\")\n",
|
| 759 |
+
" b = replace_place_holder(b, n2_text, \"{node2}\")\n",
|
| 760 |
+
" else:\n",
|
| 761 |
+
" n2_text = f\"`{n2_text}`\"\n",
|
| 762 |
+
" q = q.replace('{node2}', n2_text)\n",
|
| 763 |
+
" b = b.replace('{node2}', n2_text)\n",
|
| 764 |
+
" \n",
|
| 765 |
+
" source = source.strip('\\n')\n",
|
| 766 |
+
" q = f\"```\\n{source}\\n```\\n\\n{q}\"\n",
|
| 767 |
+
" question.append(q)\n",
|
| 768 |
+
" bot.append(b)\n",
|
| 769 |
+
" result.append(int([n1, n2] in edge_index))\n",
|
| 770 |
+
"\n",
|
| 771 |
+
" df['question'] = question\n",
|
| 772 |
+
" df['bot'] = bot\n",
|
| 773 |
+
" df = df.drop(columns={'text', 'source', 'node_ids', 'edge_index'})\n",
|
| 774 |
+
" df.to_json(f\"{out_dir}/Edge_Prediction_{lang}_{graph}.jsonl\", orient='records', lines=True)\n",
|
| 775 |
+
" results[f\"{lang}-{graph}\"] = result"
|
| 776 |
+
]
|
| 777 |
+
}
|
| 778 |
+
],
|
| 779 |
+
"metadata": {
|
| 780 |
+
"kernelspec": {
|
| 781 |
+
"display_name": "py39",
|
| 782 |
+
"language": "python",
|
| 783 |
+
"name": "python3"
|
| 784 |
+
},
|
| 785 |
+
"language_info": {
|
| 786 |
+
"codemirror_mode": {
|
| 787 |
+
"name": "ipython",
|
| 788 |
+
"version": 3
|
| 789 |
+
},
|
| 790 |
+
"file_extension": ".py",
|
| 791 |
+
"mimetype": "text/x-python",
|
| 792 |
+
"name": "python",
|
| 793 |
+
"nbconvert_exporter": "python",
|
| 794 |
+
"pygments_lexer": "ipython3",
|
| 795 |
+
"version": "3.9.17"
|
| 796 |
+
}
|
| 797 |
+
},
|
| 798 |
+
"nbformat": 4,
|
| 799 |
+
"nbformat_minor": 2
|
| 800 |
+
}
|
node_type_embedding.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dafda5e624bc797d04774bf727448973daf5fa93f17478ba41892d5692d6e2e4
|
| 3 |
+
size 44815
|