File size: 30,996 Bytes
67be06c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bfd057e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "%ai reset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1c3aa2dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%ai -c\n",
    "I'm refactoring the following code to ingest and process items from a zotero lib.\n",
    "\n",
    "```python\n",
    "import re\n",
    "\n",
    "import requests\n",
    "import srsly\n",
    "from bs4 import BeautifulSoup\n",
    "from retry import retry\n",
    "from tqdm.auto import tqdm\n",
    "\n",
    "\n",
    "\n",
    "def get_pdf_text(arxiv_id):\n",
    "    url = \"http://147.189.194.113:80/extract\"\n",
    "\n",
    "    try:\n",
    "        response = requests.get(url, params={\"arxiv_id\": arxiv_id})\n",
    "        response = response.json()\n",
    "        if \"text\" in response:\n",
    "            return response['text']\n",
    "        return None\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        return None\n",
    "    \n",
    "\n",
    "\n",
    "def get_zotero_items():\n",
    "    \"\"\"fetch all items from zotero library\"\"\"\n",
    "\n",
    "    GROUP_ID = \"5004697\"\n",
    "    API_KEY = \"HFfTT8uuL7z9tN0L5VCHe7Ow\"\n",
    "    BASE_URL = f\"https://api.zotero.org/groups/{GROUP_ID}/items\"\n",
    "    LIMIT = 100\n",
    "\n",
    "    headers = {\"Zotero-API-Key\": API_KEY, \"Content-Type\": \"application/json\"}\n",
    "\n",
    "    items = []\n",
    "    start = 0\n",
    "\n",
    "    ITEM_TYPES = [\n",
    "        \"journalArticle\",\n",
    "        \"report\",\n",
    "        \"conferencePaper\",\n",
    "        \"manuscript\",\n",
    "        \"book\",\n",
    "        \"thesis\",\n",
    "    ]  # Replace or extend with desired item types\n",
    "\n",
    "    i = 1\n",
    "    while True:\n",
    "        i += 1\n",
    "        params = {\"limit\": LIMIT, \"start\": start}\n",
    "        response = requests.get(BASE_URL, headers=headers, params=params)\n",
    "        page_items = response.json()\n",
    "        if ITEM_TYPES:\n",
    "            params[\"itemType\"] = \",\".join(ITEM_TYPES)\n",
    "\n",
    "        if not page_items:\n",
    "            break\n",
    "\n",
    "        items.extend(page_items)\n",
    "        start += LIMIT\n",
    "        print(f\"# items fetched {len(items)}\")\n",
    "        # break # debug\n",
    "\n",
    "    return items\n",
    "\n",
    "\n",
    "def extract_arxiv_links_and_pdfs(items):\n",
    "    \"\"\"Extract mapping between arxiv & pdf urls for a list of zotero items\n",
    "\n",
    "    Args:\n",
    "        items (list): list of zotero items\n",
    "\n",
    "    Returns:\n",
    "        arxiv_info (dict): dictionary with arxiv links as keys and pdf links as values\n",
    "    \"\"\"\n",
    "    arxiv_info = {}\n",
    "    arxiv_pattern = re.compile(r\"arxiv.org/abs/(\\d+\\.\\d+)\")\n",
    "\n",
    "    for item in items:\n",
    "        data = item.get(\"data\", {})\n",
    "        attachments = item.get(\"links\", {}).get(\"attachment\", {})\n",
    "        arxiv_url = None\n",
    "        pdf_url = None\n",
    "\n",
    "        if \"url\" in data and \"arxiv.org\" in data[\"url\"]:\n",
    "            arxiv_match = arxiv_pattern.search(data[\"url\"])\n",
    "            if arxiv_match:\n",
    "                arxiv_url = data[\"url\"]\n",
    "\n",
    "        if attachments:\n",
    "            pdf_url = attachments[\"href\"]\n",
    "\n",
    "        if arxiv_url:\n",
    "            arxiv_info[arxiv_url] = pdf_url\n",
    "\n",
    "    return arxiv_info\n",
    "\n",
    "\n",
    "@retry(tries=3, delay=15, backoff=2)\n",
    "def get_arxiv_with_retry(arxiv_id):\n",
    "    return requests.get(f\"https://ar5iv.labs.arxiv.org/html/{arxiv_id}\")\n",
    "\n",
    "\n",
    "def get_arxiv(arxiv_ids):\n",
    "    \"\"\"\n",
    "    Fetch html version of arxiv papers\n",
    "    \"\"\"\n",
    "    html5ivs = {}\n",
    "    for i, arxiv_id in tqdm(enumerate(arxiv_ids)):\n",
    "        if \"v\" in arxiv_id:\n",
    "            arxiv_id = arxiv_id.split(\"v\")[0]\n",
    "        response = get_arxiv_with_retry(arxiv_id)\n",
    "        html5ivs[arxiv_id] = response.text\n",
    "        if response.status_code != 200:\n",
    "            html5ivs[arxiv_id] = \"ERROR\"\n",
    "    return html5ivs\n",
    "\n",
    "\n",
    "def extract_html_content(html):\n",
    "    \"\"\"\n",
    "    Extract content from arxiv html\n",
    "    \"\"\"\n",
    "    arxiv_id_match = re.search(r\"\\[(\\d+\\.\\d+(v\\d+)?)\\]\", html)\n",
    "    arxiv_id = arxiv_id_match.group(1) if arxiv_id_match else None\n",
    "    soup = BeautifulSoup(html, \"html.parser\")\n",
    "    result = []\n",
    "\n",
    "    # Extract paper title\n",
    "    paper_title = soup.find(\"h1\", class_=\"ltx_title ltx_title_document\").get_text(strip=True)\n",
    "\n",
    "    for math in soup.find_all(\"math\"):\n",
    "        math.decompose()\n",
    "    for cite in soup.find_all(\"cite\"):\n",
    "        cite.decompose()\n",
    "\n",
    "    # Extract abstract\n",
    "    abstract = soup.find(\"div\", class_=\"ltx_abstract\")\n",
    "    if abstract:\n",
    "        result.append(\n",
    "            {\n",
    "                \"content\": \" \".join(p.get_text(strip=True) for p in abstract.find_all(\"p\")).replace(\")\", \") \"),\n",
    "                \"title\": \"Abstract\",\n",
    "                \"paper_title\": paper_title,\n",
    "                \"content_type\": \"abstract\",\n",
    "            }\n",
    "        )\n",
    "\n",
    "    # Extract introduction\n",
    "    intro = soup.find(\"section\", id=\"S1\")\n",
    "    if intro:\n",
    "        section_title = \"Introduction\"\n",
    "        paragraphs = intro.find_all(\"div\", class_=\"ltx_para\")\n",
    "        for para in paragraphs:\n",
    "            result.append(\n",
    "                {\n",
    "                    \"content\": para.get_text(strip=True).replace(\")\", \") \"),\n",
    "                    \"title\": section_title,\n",
    "                    \"paper_title\": paper_title,\n",
    "                    \"content_type\": \"introduction\",\n",
    "                }\n",
    "            )\n",
    "\n",
    "    # Extract other sections (excluding introduction and conclusion)\n",
    "    sections = soup.find_all(\"section\", class_=\"ltx_section\")\n",
    "    for section in sections[1:-1]:\n",
    "        section_title = section.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True)\n",
    "        paragraphs = section.find_all(\"div\", class_=\"ltx_para\")\n",
    "        for para in paragraphs:\n",
    "            result.append(\n",
    "                {\n",
    "                    \"content\": para.get_text(strip=True).replace(\")\", \") \"),\n",
    "                    \"title\": section_title,\n",
    "                    \"paper_title\": paper_title,\n",
    "                    \"content_type\": \"body\",\n",
    "                }\n",
    "            )\n",
    "\n",
    "    # Extract conclusion\n",
    "    conclusion = soup.find(\"section\", id=\"S4\")\n",
    "    if conclusion:\n",
    "        result.append(\n",
    "            {\n",
    "                \"content\": \" \".join(\n",
    "                    p.get_text(strip=True) for p in conclusion.find_all(\"div\", class_=\"ltx_para\")\n",
    "                ).replace(\")\", \") \"),\n",
    "                \"title\": conclusion.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True),\n",
    "                \"paper_title\": paper_title,\n",
    "                \"content_type\": \"conclusion\",\n",
    "            }\n",
    "        )\n",
    "\n",
    "    for c in result:\n",
    "        c[\"arxiv_id\"] = arxiv_id\n",
    "\n",
    "    return result\n",
    "\n",
    "\n",
    "def pool_html_content(parsed, pool_size=3, step_size=2):\n",
    "    \"\"\"\n",
    "    Pool together individual bits of contents from \"body\" and \"introduction\" sections where the \"title\" is the same.\n",
    "    It pools them 'pool_size' by 'pool_size' as a sliding window with a step size of 'step_size'.\n",
    "    Keeps all other attributes for each element and includes abstract and conclusion without pooling.\n",
    "\n",
    "    Args:\n",
    "    parsed (list): List of dictionaries containing parsed content.\n",
    "    pool_size (int): Size of the pool for the sliding window.\n",
    "    step_size (int): Step size for the sliding window.\n",
    "\n",
    "    Returns:\n",
    "    list: A list of pooled contents including non-pooled abstract and conclusion.\n",
    "    \"\"\"\n",
    "    pooled_contents = []\n",
    "    i = 0\n",
    "    while i < len(parsed) - pool_size + 1:\n",
    "        current_window = parsed[i : i + pool_size]\n",
    "        # Check if all entries in the current window have the same title and are from 'body' or 'introduction'\n",
    "        if all(\n",
    "            entry[\"content_type\"] in [\"body\", \"introduction\"] and entry[\"title\"] == current_window[0][\"title\"]\n",
    "            for entry in current_window\n",
    "        ):\n",
    "            pooled_content = \"\\n\".join(entry[\"content\"] for entry in current_window)\n",
    "            if \"bit\" not in current_window[0][\"title\"][:5]:\n",
    "                title = current_window[0][\"title\"].lstrip(\"0123456789\")\n",
    "            pooled_content = f\"{current_window[0]['paper_title']}\\n{title}\\n\\n\" + pooled_content\n",
    "            pooled_entry = {\n",
    "                \"title\": title,\n",
    "                \"text\": pooled_content,\n",
    "                \"paper_title\": current_window[0][\"paper_title\"],\n",
    "                \"content_type\": current_window[0][\"content_type\"],\n",
    "                \"arxiv_id\": current_window[0][\"arxiv_id\"],\n",
    "            }\n",
    "            pooled_contents.append(pooled_entry)\n",
    "        i += step_size\n",
    "\n",
    "    # Include abstract and conclusion without pooling\n",
    "    for entry in parsed:\n",
    "        if entry[\"content_type\"] in [\"abstract\", \"conclusion\"]:\n",
    "            if \"content\" in entry:\n",
    "                entry[\"text\"] = entry[\"content\"]\n",
    "                del entry[\"content\"]\n",
    "            if \"bit\" not in entry[\"title\"][:5]:\n",
    "                entry[\"title\"] = entry[\"title\"].lstrip(\"0123456789\")\n",
    "            entry[\"text\"] = f\"{entry['paper_title']}\\n{entry['title']}\\n\\n\" + entry[\"text\"]\n",
    "            pooled_contents.append(entry)\n",
    "\n",
    "    return pooled_contents\n",
    "\n",
    "\n",
    "def parse_markdown_content(md_content, arxiv_id):\n",
    "    \"\"\"\n",
    "    Parses markdown content to identify and extract sections based on headers.\n",
    "    \"\"\"\n",
    "\n",
    "    lines = md_content.split(\"\\n\")\n",
    "    parsed = []\n",
    "    current_section = None\n",
    "    content = []\n",
    "    paper_title = None\n",
    "    current_title = None\n",
    "\n",
    "    # Identify sections based on headers\n",
    "    for line in lines:\n",
    "        if line.startswith(\"#\"):\n",
    "            if paper_title is None:\n",
    "                paper_title = line.lstrip(\"#\").lstrip(\"#\").strip()\n",
    "                continue\n",
    "            if content:\n",
    "                if not current_title:\n",
    "                    print(\"skipping pre-abstract!\")\n",
    "                parsed.append(\n",
    "                    {\n",
    "                        \"title\": current_title,\n",
    "                        \"text\": content,\n",
    "                        \"paper_title\": paper_title,\n",
    "                        \"content_type\": \"body\"\n",
    "                        if current_section not in [\"Abstract\", \"Introduction\", \"Conclusion\"]\n",
    "                        else current_section.lower(),\n",
    "                        \"arxiv_id\": arxiv_id,\n",
    "                    }\n",
    "                )\n",
    "                content = []\n",
    "\n",
    "            if \"Introduction\" in line:\n",
    "                current_section = \"Introduction\"\n",
    "            elif \"Abstract\" in line:\n",
    "                current_section = \"Abstract\"\n",
    "            elif \"Conclusion\" in line:\n",
    "                current_section = \"Conclusion\"\n",
    "            elif \"Reference\" in line:\n",
    "                break\n",
    "            else:\n",
    "                current_section = \"body\"\n",
    "\n",
    "            current_title = line.lstrip(\"#\").lstrip(\"#\").lstrip()\n",
    "\n",
    "            if \"bit\" not in current_title:\n",
    "                current_title = (\n",
    "                    current_title.lstrip(\"123456789\")\n",
    "                    .lstrip()\n",
    "                    .lstrip(\".\")\n",
    "                    .lstrip()\n",
    "                    .lstrip(\"123456789\")\n",
    "                    .lstrip()\n",
    "                    .lstrip(\".\")\n",
    "                    .lstrip()\n",
    "                )\n",
    "        else:\n",
    "            content.append(line)\n",
    "\n",
    "    if content:\n",
    "        parsed.append(\n",
    "            {\n",
    "                \"title\": current_section,\n",
    "                \"text\": content,\n",
    "                \"paper_title\": paper_title,\n",
    "                \"content_type\": \"body\"\n",
    "                if current_section not in [\"Abstract\", \"Introduction\", \"Conclusion\"]\n",
    "                else current_section.lower(),\n",
    "                \"arxiv_id\": arxiv_id,\n",
    "            }\n",
    "        )\n",
    "\n",
    "    return parsed\n",
    "\n",
    "\n",
    "def pool_markdown(parsed, pool_size=3, step_size=2):\n",
    "    \"\"\"\n",
    "    pool together markdown contents with sliding window of size 'pool_size' and step size of 'step_size'.\n",
    "\n",
    "    Args:\n",
    "        parsed (list): List of dictionaries containing parsed content.\n",
    "        pool_size (int): Size of the pool for the sliding window.\n",
    "        step_size (int): Step size for the sliding window.\n",
    "\n",
    "    Returns:\n",
    "        list: A list of pooled contents including non-pooled abstract and conclusion.\n",
    "    \"\"\"\n",
    "    cleaned_contents = []\n",
    "    for section in parsed:\n",
    "        cleaned_lines = [line for line in section[\"text\"] if line and \"|\" not in line and len(line) >= 40]\n",
    "        section[\"text\"] = cleaned_lines\n",
    "        cleaned_contents.append(section)\n",
    "\n",
    "    pooled_contents = []\n",
    "    for section in cleaned_contents:\n",
    "        if section[\"content_type\"] in [\"body\", \"introduction\"]:\n",
    "            i = 0\n",
    "            while i <= len(section[\"text\"]) - pool_size:\n",
    "                pooled_content = \"\\n\".join(section[\"text\"][i : i + pool_size])\n",
    "                pooled_content = f\"{section['paper_title']}\\n{section['title']}\\n\\n\" + pooled_content\n",
    "                pooled_entry = {\n",
    "                    \"title\": section[\"title\"],\n",
    "                    \"text\": pooled_content,\n",
    "                    \"paper_title\": section[\"paper_title\"],\n",
    "                    \"content_type\": section[\"content_type\"],\n",
    "                    \"arxiv_id\": section[\"arxiv_id\"],\n",
    "                }\n",
    "                pooled_contents.append(pooled_entry)\n",
    "                i += step_size\n",
    "        else:\n",
    "            section[\"text\"] = f\"{section['paper_title']}\\n\" + \"\\n\".join(section[\"text\"])\n",
    "            pooled_contents.append(section)\n",
    "    return pooled_contents\n",
    "\n",
    "\n",
    "def clean_and_pool_markdown_content(md, arxiv_id):\n",
    "    md = parse_markdown_content(md, arxiv_id)\n",
    "    return pool_markdown(md)\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    items = get_zotero_items()\n",
    "    print(f\"# of items fetched from zotero: {len(items)}\")\n",
    "    arxiv_info = extract_arxiv_links_and_pdfs(items)\n",
    "\n",
    "    # extract arxiv information ---\n",
    "    arxiv_ids = set()\n",
    "    for arxiv_url, pdf_url in arxiv_info.items():\n",
    "        arxiv_ids.add(arxiv_url.split(\"/\")[-1])\n",
    "    print(f\"# of arxiv papers: {len(arxiv_ids)}\")\n",
    "\n",
    "    html5ivs = get_arxiv(arxiv_ids)\n",
    "\n",
    "    broken = []\n",
    "    extracted = {}\n",
    "    extracted_from_pdf = {}\n",
    "    broken_tex = {}\n",
    "\n",
    "    pbar = tqdm(range(len(html5ivs)))\n",
    "    for arxiv_id, x in html5ivs.items():\n",
    "        try:\n",
    "            extracted[arxiv_id] = extract_html_content(x)\n",
    "        except Exception as e:\n",
    "            # print(e)\n",
    "            print(f'getting pdf text for {arxiv_id}...')\n",
    "            pdf_text = get_pdf_text(arxiv_id) \n",
    "            if pdf_text is not None:\n",
    "                extracted_from_pdf[arxiv_id] = pdf_text # get_pdf_text(arxiv_id) # <- original\n",
    "            else:\n",
    "                broken.append(arxiv_id)\n",
    "        pbar.update(1)\n",
    "    pbar.close()\n",
    "\n",
    "    print(f\"# of arxiv papers extracted: {len(extracted)}\")\n",
    "    print(f\"# of arxiv papers extracted from pdf: {len(extracted_from_pdf)}\")\n",
    "    print(f\"# of broken links: {len(broken)}\")\n",
    "\n",
    "    pooled = [pool_html_content(x) for _, x in extracted.items()]\n",
    "    pooled += [\n",
    "        clean_and_pool_markdown_content(x, arxiv_id) for arxiv_id, x in extracted_from_pdf.items()\n",
    "    ]  # No pdf content yet, no op for now\n",
    "    flat_pool = [item for sublist in pooled for item in sublist]\n",
    "\n",
    "    id_to_abstract = {}\n",
    "    for doc in pooled:\n",
    "        for entry in doc:\n",
    "            if entry[\"content_type\"] == \"abstract\":\n",
    "                id_to_abstract[entry[\"arxiv_id\"]] = entry[\"text\"]\n",
    "                break\n",
    "\n",
    "    srsly.write_json(\"id_to_abstract.json\", id_to_abstract)\n",
    "    srsly.write_json(\"flatpool.json\", flat_pool)\n",
    "```\n",
    "go through it and reply OK."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24e7ef23",
   "metadata": {},
   "source": [
    "OK. I've reviewed the code you provided for ingesting and processing items from a Zotero library."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84628bb2",
   "metadata": {},
   "source": [
    "OK. I've reviewed the code you provided for ingesting and processing items from a Zotero library."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bada6931",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%ai\n",
    "Let's work step by step. I currently have\n",
    "\n",
    "```\n",
    "import os\n",
    "import re\n",
    "\n",
    "import dotenv\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "from retry import retry\n",
    "from tqdm.auto import tqdm\n",
    "\n",
    "dotenv.load_dotenv()\n",
    "\n",
    "\n",
    "########################################################\n",
    "### GET ZOTERO ITEMS\n",
    "########################################################\n",
    "@retry(tries=3, delay=8)\n",
    "def _fetch_one_zotero_batch(url, headers, params):\n",
    "    \"\"\"\n",
    "    Fetch articles from Zotero API\n",
    "    \"\"\"\n",
    "    response = requests.get(url, headers=headers, params=params)\n",
    "    response.raise_for_status()\n",
    "    return response.json()\n",
    "\n",
    "\n",
    "def get_zotero_items(debug=False):\n",
    "    \"\"\"\n",
    "    fetch items from zotero library\n",
    "    \"\"\"\n",
    "\n",
    "    GROUP_ID = os.getenv(\"GROUP_ID\")\n",
    "    API_KEY = os.getenv(\"API_KEY\")\n",
    "    BASE_URL = f\"https://api.zotero.org/groups/{GROUP_ID}/items\"\n",
    "    LIMIT = 100\n",
    "\n",
    "    headers = {\"Zotero-API-Key\": API_KEY, \"Content-Type\": \"application/json\"}\n",
    "\n",
    "    items = []\n",
    "    start = 0\n",
    "\n",
    "    i = 1\n",
    "    while True:\n",
    "        i += 1\n",
    "        params = {\"limit\": LIMIT, \"start\": start}\n",
    "        page_items = _fetch_one_zotero_batch(BASE_URL, headers, params)\n",
    "\n",
    "        if not page_items:\n",
    "            break\n",
    "\n",
    "        items.extend(page_items)\n",
    "        start += LIMIT\n",
    "        print(f\"# items fetched {len(items)}\")\n",
    "\n",
    "        if debug:\n",
    "            break\n",
    "\n",
    "    return items\n",
    "\n",
    "\n",
    "########################################################\n",
    "### EXTRACT ARXIV LINKS AND PDFs\n",
    "########################################################\n",
    "def get_arxiv_items(items):\n",
    "    arxiv_items = []\n",
    "    arxiv_pattern = re.compile(r\"arxiv.org/abs/(\\d+\\.\\d+)\")\n",
    "\n",
    "    for item in items:\n",
    "        data = item.get(\"data\", {})\n",
    "        attachments = item.get(\"links\", {}).get(\"attachment\", {})\n",
    "\n",
    "        arxiv_url = None\n",
    "        pdf_url = None\n",
    "\n",
    "        if \"url\" in data and \"arxiv.org\" in data[\"url\"]:\n",
    "            arxiv_match = arxiv_pattern.search(data[\"url\"])\n",
    "            if arxiv_match:\n",
    "                arxiv_url = data[\"url\"]\n",
    "\n",
    "        if attachments:\n",
    "            pdf_url = attachments[\"href\"]\n",
    "\n",
    "        if arxiv_url:\n",
    "            arxiv_items.append(\n",
    "                {\n",
    "                    \"arxiv_id\": arxiv_url.split(\"/\")[-1],\n",
    "                    \"arxiv_url\": arxiv_url,\n",
    "                    \"pdf_url\": pdf_url,\n",
    "                    \"added_by\": item[\"meta\"][\"createdByUser\"][\"username\"],\n",
    "                    \"date_added\": data.get(\"dateAdded\", \"\"),\n",
    "                }\n",
    "            )\n",
    "\n",
    "    return arxiv_items\n",
    "\n",
    "\n",
    "@retry(tries=3, delay=15, backoff=2)\n",
    "def fetch_arxiv_html(arxiv_id):\n",
    "    url = f\"https://ar5iv.labs.arxiv.org/html/{arxiv_id.split('v')[0]}\"\n",
    "    response = requests.get(url)\n",
    "    return response.text if response.status_code == 200 else None\n",
    "\n",
    "\n",
    "def get_arxiv_html(arxiv_items):\n",
    "    for item in tqdm(arxiv_items):\n",
    "        html = fetch_arxiv_html(item[\"arxiv_id\"])\n",
    "        if html:\n",
    "            item[\"raw_html\"] = html\n",
    "        else:\n",
    "            print(f\"failed to fetch html for {item['arxiv_id']}\")\n",
    "\n",
    "    return arxiv_items\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    items = get_zotero_items(debug=True)\n",
    "    print(f\"# of items fetched from zotero: {len(items)}\")\n",
    "    arxiv_items = get_arxiv_items(items)\n",
    "    print(f\"# of arxiv papers: {len(arxiv_items)}\")\n",
    "    arxiv_htmls = get_arxiv_html(arxiv_items)  # raw html files\n",
    "--\n",
    "review for any mistake until now. What shall we do next."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60ab1dfc",
   "metadata": {},
   "source": [
    "The code looks good so far. You've implemented functions to fetch items from Zotero, extract ArXiv-related items, and fetch the HTML content for ArXiv papers. \n",
    "\n",
    "Next steps could include:\n",
    "\n",
    "1. Parse the HTML content to extract structured information from the papers.\n",
    "2. Implement error handling for failed HTML fetches.\n",
    "3. Add a function to extract text from PDFs for papers where HTML fetch fails.\n",
    "4. Store the extracted data in a suitable format (e.g., JSON, database).\n",
    "5. Implement content pooling as in your original code.\n",
    "\n",
    "Which of these would you like to focus on first?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "696318f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%aip\n",
    "Let's work on step 1. Let's follow the original code as much as possible.\n",
    "Note: these are arxiv htmls. Let's also keep full body text."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e7e4a5a6",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'arxiv_htmls' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[6], line 18\u001b[0m\n\u001b[1;32m     15\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mprocess_arxiv_items\u001b[39m(arxiv_items):\n\u001b[1;32m     16\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m [\u001b[38;5;28mdict\u001b[39m(item, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mextract_html_content(item[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_html\u001b[39m\u001b[38;5;124m\"\u001b[39m])) \u001b[38;5;28;01mfor\u001b[39;00m item \u001b[38;5;129;01min\u001b[39;00m arxiv_items \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mraw_html\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01min\u001b[39;00m item]\n\u001b[0;32m---> 18\u001b[0m processed_items \u001b[38;5;241m=\u001b[39m process_arxiv_items(\u001b[43marxiv_htmls\u001b[49m)\n",
      "\u001b[0;31mNameError\u001b[0m: name 'arxiv_htmls' is not defined"
     ]
    }
   ],
   "source": [
    "def extract_html_content(html):\n",
    "    soup = BeautifulSoup(html, \"html.parser\")\n",
    "    arxiv_id = re.search(r\"\\[(\\d+\\.\\d+(v\\d+)?)\\]\", html).group(1) if re.search(r\"\\[(\\d+\\.\\d+(v\\d+)?)\\]\", html) else None\n",
    "    paper_title = soup.find(\"h1\", class_=\"ltx_title ltx_title_document\").get_text(strip=True)\n",
    "    [m.decompose() for m in soup.find_all([\"math\", \"cite\"])]\n",
    "    abstract = \" \".join(p.get_text(strip=True) for p in soup.find(\"div\", class_=\"ltx_abstract\").find_all(\"p\")).replace(\")\", \") \") if soup.find(\"div\", class_=\"ltx_abstract\") else \"\"\n",
    "    sections = soup.find_all(\"section\", class_=\"ltx_section\")\n",
    "    body = []\n",
    "    for section in sections:\n",
    "        title = section.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True)\n",
    "        content = \" \".join(p.get_text(strip=True).replace(\")\", \") \") for p in section.find_all(\"div\", class_=\"ltx_para\"))\n",
    "        body.append({\"title\": title, \"content\": content})\n",
    "    return {\"arxiv_id\": arxiv_id, \"paper_title\": paper_title, \"abstract\": abstract, \"body\": body, \"full_text\": soup.get_text(strip=True)}\n",
    "\n",
    "def process_arxiv_items(arxiv_items):\n",
    "    return [dict(item, **extract_html_content(item[\"raw_html\"])) for item in arxiv_items if \"raw_html\" in item]\n",
    "\n",
    "processed_items = process_arxiv_items(arxiv_htmls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "70a3caaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%aip\n",
    "the above doesn't work and missed may details of the original implementation. Do not focus on pooling now\n",
    "just do extract_html_content better\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4040257d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def extract_html_content(html):\n",
    "    soup, result = BeautifulSoup(html, \"html.parser\"), []\n",
    "    arxiv_id = re.search(r\"\\[(\\d+\\.\\d+(v\\d+)?)\\]\", html)\n",
    "    arxiv_id = arxiv_id.group(1) if arxiv_id else None\n",
    "    paper_title = soup.find(\"h1\", class_=\"ltx_title ltx_title_document\").get_text(strip=True)\n",
    "    [m.decompose() for m in soup.find_all([\"math\", \"cite\"])]\n",
    "    abstract = soup.find(\"div\", class_=\"ltx_abstract\")\n",
    "    if abstract:\n",
    "        result.append({\"content\": \" \".join(p.get_text(strip=True) for p in abstract.find_all(\"p\")).replace(\")\", \") \"), \"title\": \"Abstract\", \"paper_title\": paper_title, \"content_type\": \"abstract\"})\n",
    "    intro = soup.find(\"section\", id=\"S1\")\n",
    "    if intro:\n",
    "        for para in intro.find_all(\"div\", class_=\"ltx_para\"):\n",
    "            result.append({\"content\": para.get_text(strip=True).replace(\")\", \") \"), \"title\": \"Introduction\", \"paper_title\": paper_title, \"content_type\": \"introduction\"})\n",
    "    for section in soup.find_all(\"section\", class_=\"ltx_section\")[1:-1]:\n",
    "        section_title = section.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True)\n",
    "        for para in section.find_all(\"div\", class_=\"ltx_para\"):\n",
    "            result.append({\"content\": para.get_text(strip=True).replace(\")\", \") \"), \"title\": section_title, \"paper_title\": paper_title, \"content_type\": \"body\"})\n",
    "    conclusion = soup.find(\"section\", id=\"S4\")\n",
    "    if conclusion:\n",
    "        result.append({\"content\": \" \".join(p.get_text(strip=True) for p in conclusion.find_all(\"div\", class_=\"ltx_para\")).replace(\")\", \") \"), \"title\": conclusion.find(\"h2\", class_=\"ltx_title ltx_title_section\").get_text(strip=True), \"paper_title\": paper_title, \"content_type\": \"conclusion\"})\n",
    "    for c in result:\n",
    "        c[\"arxiv_id\"] = arxiv_id\n",
    "    return result\n",
    "\n",
    "processed_items = [extract_html_content(item[\"raw_html\"]) for item in arxiv_items if \"raw_html\" in item]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0242f021",
   "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.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}