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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ef8edf3a-4fe9-4f5b-8e70-bd9bd42a609b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import re\n",
    "from termcolor import colored\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "e7b1e345-08d4-49f1-b5cc-b856e6f045d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "outs= json.load(open('Downloads/qwen3_out (2).json'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "795f3269-23f2-4843-848f-cd1ec43180ba",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'0': 'exclaiming', '1': 'argued', '2': 'sob'}"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outs[IDX]['candidates']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "14add67b-7bef-4301-b9c6-48f743d489a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"0\": \"no\", \"1\": \"no\", \"2\": \"no\"}'"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outs[IDX]['pred']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "37a381c6-0846-47c9-91d4-ae7216a221ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\x1b[32mrr\\x1b[0m'"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colored('rr','green')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 546,
   "id": "c385d973-bc75-4bab-9aea-d9113eee1fb7",
   "metadata": {},
   "outputs": [],
   "source": [
    "annotations += [{}] * len(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 343,
   "id": "b7ddfe82-565b-4d2c-a82b-02602dcf89ef",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<>:4: SyntaxWarning: invalid escape sequence '\\|'\n",
      "<>:6: SyntaxWarning: invalid escape sequence '\\|'\n",
      "<>:12: SyntaxWarning: invalid escape sequence '\\|'\n",
      "<>:14: SyntaxWarning: invalid escape sequence '\\|'\n",
      "<>:4: SyntaxWarning: invalid escape sequence '\\|'\n",
      "<>:6: SyntaxWarning: invalid escape sequence '\\|'\n",
      "<>:12: SyntaxWarning: invalid escape sequence '\\|'\n",
      "<>:14: SyntaxWarning: invalid escape sequence '\\|'\n",
      "/var/folders/_s/rl4c05mx7ldfyms14mh6s7_w0000gp/T/ipykernel_22336/4162419528.py:4: SyntaxWarning: invalid escape sequence '\\|'\n",
      "  c = re.sub('<\\|Q\\|>([^<]+)<\\|Q\\|>', lambda x: '\\033[1m\\x1b[34m' + x.group(1) +'\\x1b[0m', context)\n",
      "/var/folders/_s/rl4c05mx7ldfyms14mh6s7_w0000gp/T/ipykernel_22336/4162419528.py:6: SyntaxWarning: invalid escape sequence '\\|'\n",
      "  c = re.sub('(\\|([\\d]+)\\|([^\\|])+\\|[\\d]+\\|)', lambda x:f'\\033[1m{cs[preds[x.group(2)]]}' + x.group(1) +'\\x1b[0m', c)\n",
      "/var/folders/_s/rl4c05mx7ldfyms14mh6s7_w0000gp/T/ipykernel_22336/4162419528.py:12: SyntaxWarning: invalid escape sequence '\\|'\n",
      "  c = re.sub('<\\|Q\\|>([^<]+)<\\|Q\\|>', lambda x: '\\033[1m\\x1b[34m' + x.group(1) +'\\x1b[0m', context)\n",
      "/var/folders/_s/rl4c05mx7ldfyms14mh6s7_w0000gp/T/ipykernel_22336/4162419528.py:14: SyntaxWarning: invalid escape sequence '\\|'\n",
      "  c = re.sub('(\\|([\\d]+)\\|([^\\|])+\\|[\\d]+\\|)', lambda x:f'\\033[1m\\x1b[31m' + x.group(1) +'\\x1b[0m', c)\n"
     ]
    }
   ],
   "source": [
    "def print_c(context, preds) : \n",
    "    cs = {'no' : '\\x1b[31m', 'yes' : '\\x1b[32m'}\n",
    "    \n",
    "    c = re.sub('<\\|Q\\|>([^<]+)<\\|Q\\|>', lambda x: '\\033[1m\\x1b[34m' + x.group(1) +'\\x1b[0m', context)\n",
    "    \n",
    "    c = re.sub('(\\|([\\d]+)\\|([^\\|])+\\|[\\d]+\\|)', lambda x:f'\\033[1m{cs[preds[x.group(2)]]}' + x.group(1) +'\\x1b[0m', c)\n",
    "    print(c)\n",
    "\n",
    "def print_c(context) : \n",
    "    #cs = {'no' : '\\x1b[31m', 'yes' : '\\x1b[32m'}\n",
    "    \n",
    "    c = re.sub('<\\|Q\\|>([^<]+)<\\|Q\\|>', lambda x: '\\033[1m\\x1b[34m' + x.group(1) +'\\x1b[0m', context)\n",
    "    \n",
    "    c = re.sub('(\\|([\\d]+)\\|([^\\|])+\\|[\\d]+\\|)', lambda x:f'\\033[1m\\x1b[31m' + x.group(1) +'\\x1b[0m', c)\n",
    "    print(c)\n",
    "\n",
    "             "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 978,
   "id": "b2f9fed1-f2d1-4082-a62d-e9a53cbfefa3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "368 \n",
      "\n",
      "\n",
      "All sorts of complications might ensue from this hasty proceeding. Yet Dorothy, even in that moment of trouble, realized that she must keep her brain clear for whatever might transpire. Tavia, she felt, might do something reckless — well meant, no doubt, but none the less something that might put a weapon in the hands of the man against whom they hoped to proceed for the sake of Aunt Winnie.\n",
      "\n",
      "“Just what do you mean?” \u001b[1m\u001b[31m|0|snapped|0|\u001b[0m the man again, and he seemed master of the situation, even though Dorothy thought she detected a gleam of — was it fear? in his eyes. \u001b[1m\u001b[34m“I am not in the habit of being spoken to in that manner,”\u001b[0m he went on.\n",
      "\n",
      "“I am afraid I shall have to ask you to explain yourself. It is the first time I have ever been accused of wrongdoing.”\n"
     ]
    }
   ],
   "source": [
    "IDX = 368\n",
    "print(IDX, '\\n\\n')\n",
    "print_c(outs[IDX]['context'])\n",
    "\n",
    "preds = eval(outs[IDX]['pred']).copy()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 979,
   "id": "faf55c79-d96d-4fee-a046-27a0ddfe991e",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "annotations[IDX]['0'] = 'yes'\n",
    "annotations[IDX]['1'] = 'no'\n",
    "annotations[IDX]['2'] = 'no'\n",
    "annotations[IDX]['3'] = 'no'\n",
    " \n",
    "annotations[IDX] = { k : annotations[IDX][k] for k in preds}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 981,
   "id": "12203b4b-ca6a-4a1b-822e-827f41f6eb61",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('Desktop/annotations_gmichel.json','w') as f : \n",
    "    json.dump(annotations, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 963,
   "id": "3900b42a-75f8-4a50-ab7e-5239ff140a99",
   "metadata": {},
   "outputs": [],
   "source": [
    "### 200 examples took 45 minutes\n",
    "### 400 examples took 1h15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1021,
   "id": "6d736767-a400-4432-baeb-b3013dc41f8d",
   "metadata": {},
   "outputs": [],
   "source": [
    "corr_y = 0\n",
    "corr_n = 0\n",
    "total_y = 0 \n",
    "total_n = 0 \n",
    "\n",
    "errors = []\n",
    "for cnt, (gt, pred) in enumerate(zip(annotations, outs)) : \n",
    "    p = eval(pred['pred'])\n",
    "    assert len(gt) == len(p)\n",
    "    for k in p : \n",
    "        if any([gt[k] not in ['no', 'yes'], p[k] not in ['no', 'yes']]): \n",
    "            print(cnt)\n",
    "        \n",
    "        if gt[k] == p[k] : \n",
    "            if gt[k] == 'yes' : \n",
    "                corr_y += 1\n",
    "            else : \n",
    "                corr_n += 1\n",
    "\n",
    "        else : \n",
    "            errors.append((cnt, k, gt[k], p[k] ))\n",
    "        if gt[k] == 'yes' : \n",
    "            total_y +=1\n",
    "        else : \n",
    "            total_n +=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1022,
   "id": "ce296326-817a-48f2-87a3-12897ff46443",
   "metadata": {},
   "outputs": [],
   "source": [
    "tp = corr_y\n",
    "tn = corr_n\n",
    "fp = total_n - tn\n",
    "fn = total_y - tp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1023,
   "id": "164dcbdb-ffae-45d8-9ae3-04fb374a24a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Precision: 0.88\n",
      "Recall: 0.76\n"
     ]
    }
   ],
   "source": [
    "print(f'Precision: {tp / (tp+fp):.2f}')\n",
    "print(f'Recall: {tp / (tp+fn):.2f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1024,
   "id": "a9a3cfa3-09de-424e-b578-43d05c34241c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Acc: 0.89\n",
      "Acc + : 0.76\n",
      "Acc - : 0.95\n"
     ]
    }
   ],
   "source": [
    "print(f'Acc: {(corr_n+corr_y)/(total_n + total_y):0.2f}')\n",
    "\n",
    "print(f'Acc + : {corr_y/total_y:0.2f}')\n",
    "print(f'Acc - : {corr_n/total_n:0.2f}')\n",
    "\n",
    "#print(f'Prec : {corr_y/(total_y+total_n):0.2f}')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1025,
   "id": "853e4059-7a72-4a77-8dc9-3f8c8267ae64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, '0', 'yes', 'no')"
      ]
     },
     "execution_count": 1025,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "IDX = 0\n",
    "errors[IDX]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1085,
   "id": "c881415b-6ca2-43ea-91b9-ac8abdc1c6ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "idx, v_id, gt, pred\n",
      "(24, '0', 'yes', 'no') \n",
      "\n",
      "\n",
      "{'0': 'no'}\n",
      "\n",
      "\n",
      "\n",
      "Pollyanna gave her head a dubious shake.\n",
      "\n",
      "\"Well, I'm afraid maybe I don't know ALL of 'em,\" she \u001b[1m\u001b[31m|0|admitted|0|\u001b[0m. \u001b[1m\u001b[34m\"Are they all -- in books?\"\u001b[0m\n",
      "\n",
      "The boy nodded.\n"
     ]
    }
   ],
   "source": [
    "IDX += 1\n",
    "\n",
    "print('idx, v_id, gt, pred')\n",
    "print(errors[IDX], '\\n\\n')\n",
    "preds = eval(outs[errors[IDX][0]]['pred']).copy()\n",
    "print(preds)\n",
    "print('\\n\\n')\n",
    "print_c(outs[errors[IDX][0]]['context'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20b327ae-8b67-4590-ae63-1c849eba96bd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5829df94-48d5-4075-9767-8a5da3a208e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.org/simple, https://artifacts.deez.re/repository/python-research/simple\n",
      "\u001b[33mWARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x10fdca210>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /repository/python-research/simple/transformers/\u001b[0m\u001b[33m\n",
      "\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x10fdca490>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /repository/python-research/simple/transformers/\u001b[0m\u001b[33m\n",
      "\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x10fdca850>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /repository/python-research/simple/transformers/\u001b[0m\u001b[33m\n",
      "\u001b[0m\u001b[33mWARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x10fdcaad0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /repository/python-research/simple/transformers/\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "!pip install transformers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b00aefa2-fbd4-4441-a2b0-d0cf5dbbf0ea",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d2f953f-9b18-4294-8a4c-eb16816e5d9c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "2406125b-4b81-4a01-8648-cfef91b19d4f",
   "metadata": {},
   "source": [
    "#### Example 1: case where 'pouted' then 'giggled' --> giggled = false\n",
    "\n",
    "Bunting spoke in a muffled, angry tone. He was wiping his forehead with one hand, while with the other he mechanically squeezed the little packet of tobacco, for which, as he now remembered, he had forgotten to pay.\n",
    "\n",
    "Daisy pouted. <|Q|>“Oh, father, I think you might let me have a treat on my birthday! I told him that Saturday wasn’t a very good day — at least, so I’d heard — for Madame Tussaud’s. Then he said we could go early, while the fine folk are still having their dinners.”<|Q|> She turned to her stepmother, then |0|giggled|0| happily. “He particularly said you was to come, too. The lodger has a wonderful fancy for you, Ellen; if I was father, I’d feel quite jealous!”\n",
    "\n",
    "Her last words were cut across by a tap-tap on the door.\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96dd645d-f66e-4d74-a04e-6f6ccf48b3f6",
   "metadata": {},
   "source": [
    "#### Example 2: here snapped then went-on --> means that snapping continues so snap=yes\n",
    "\n",
    "“Just what do you mean?” |0|snapped|0| the man again, and he seemed master of the situation, even though Dorothy thought she detected a gleam of — was it fear? in his eyes. <|Q|>“I am not in the habit of being spoken to in that manner,”<|Q|> he went on."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21403b44-94ff-4e0a-82ad-dc56c7cd5832",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0442a33b-cadb-4517-971f-4d7ffafb3ecc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03cbdd05-a5a8-4668-8076-100b87a43e27",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}