Spaces:
Runtime error
Runtime error
File size: 6,800 Bytes
b296808 6217acc b296808 da254c5 49cd01e b296808 6217acc b296808 6217acc b296808 49cd01e b296808 49cd01e b296808 3d9d194 b296808 49cd01e b296808 5b0a6ae 3d9d194 5b0a6ae b296808 03cfbf7 49cd01e b296808 49cd01e 3d9d194 49cd01e 3d9d194 49cd01e b296808 3d9d194 b296808 6217acc b296808 3d9d194 5b0a6ae d72bbae b296808 03cfbf7 b296808 3d9d194 5b0a6ae 6217acc | 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 | import re
from pathlib import Path
import pymupdf as PyMuPDF
import requests
def download_pdf_from_arxiv(arxiv_id):
if Path(f'arxiv_archive/{arxiv_id}.pdf').exists():
print(f"PDF for arXiv id {arxiv_id} already exists")
return
url = f'http://export.arxiv.org/pdf/{arxiv_id}'
response = requests.get(url)
if response.status_code == 200:
pdf_content = response.content
with open(f"arxiv_archive/{arxiv_id}.pdf", "wb") as f:
f.write(pdf_content)
else:
raise Exception(f"Failed to download pdf for arXiv id {arxiv_id}")
def extract_all_titles(pdf_path):
doc = PyMuPDF.open(pdf_path)
titles = []
should_break = False
for page_num in range(len(doc)):
if should_break:
break
page = doc.load_page(page_num)
blocks = page.get_text("dict")["blocks"]
page_titles = []
for block in blocks:
if 'image' in block:
continue
text = " ".join([span["text"] for line in block["lines"] for span in line["spans"]])
if_obvious_title, if_end = is_obvious_title(text)
if (if_obvious_title and title_len_constraint(text) and not if_double_space(text) and
not if_paper_header(text) and not is_formula(text) and not is_url(text)):
page_titles.append(text.strip())
if if_end:
should_break = True
break
titles += page_titles
return titles
def extract_bodies(pdf_path, all_chap_titles, all_titles):
doc = PyMuPDF.open(pdf_path)
bodies = []
is_body = False
chapter_body = []
should_break = False
for page_num in range(len(doc)):
if should_break:
break
page = doc.load_page(page_num)
blocks = page.get_text("dict")["blocks"]
for block in blocks:
if 'image' in block:
continue
text = " ".join([span["text"] for line in block["lines"] for span in line["spans"]])
if text in all_chap_titles:
if is_body:
bodies.append("\n".join(chapter_body))
chapter_body = []
if text == all_chap_titles[-1]:
should_break = True
break
is_body = True
if (is_body and is_body_text(text) and not is_table(text) and not is_image(text) and not is_formula(text)
and not is_url(text)) or text in all_titles:
chapter_body.append(text.strip())
return bodies
def extract_paper_title(pdf_path):
doc = PyMuPDF.open(pdf_path)
page = doc.load_page(0)
blocks = page.get_text("dict")["blocks"]
for block in blocks:
if 'image' in block:
continue
text = " ".join([span["text"] for line in block["lines"] for span in line["spans"]])
if (not is_body_text(text) and not is_table(text) and not is_image(text) and not is_formula(text)
and not is_url(text) and paper_title_len_constraint(text)):
return text.strip()
def select_chaptitle_from_alltitles(all_titles):
chap_titles = []
add = False
# extract intro pattern
for title in all_titles:
intro_pattern = check_intro_pattern(title)
if intro_pattern:
if intro_pattern == 2:
add = True
break
for title in all_titles:
if is_obvious_chapter_title(title, add):
chap_titles.append(title)
chap_titles.append(title)
return chap_titles
def is_obvious_title(text):
title_arabia_pattern = r'^\d[\.]?\d?[\.]?\s.+'
title_rome_pattern = r'^[IVABCDEF]+[\.]?\s.+'
# Acknowledgements or References
end_title = (r"^(a\s?cknowledgements|r\s?eferences|r\s?eference|a\s?cknowledgement|appendix|appendices|appendixes"
r"|appendixs|b\s?ibliography|biblio|a\s?cknowledgments|a\s?cknowledgment)$")
if_end = True if bool(re.match(end_title, text, re.IGNORECASE)) else False
return bool(re.match(title_arabia_pattern, text)) or bool(re.match(title_rome_pattern, text)) or if_end, if_end
def if_double_space(text):
flag = False
if " " in text:
# might occur some misformat title
pattern = r"^[\da-zA-Z\s\:'\?]+"
if not re.match(pattern, text):
flag = True
return flag
def if_paper_header(text):
pattern = r'^\d[\.]?\s.+(?:et.\sal)$'
return bool(re.match(pattern, text))
def is_obvious_chapter_title(text, add=False):
# add: True if the title has pattern like "1. introduction"
if add:
title_arabia_pattern = r'^\d\.\s.+'
title_rome_pattern = r'^[IV]+\.\s.+'
else:
title_arabia_pattern = r'^\d\s.+'
title_rome_pattern = r'^[IV]+\s.+'
return bool(re.match(title_arabia_pattern, text)) or bool(re.match(title_rome_pattern, text))
def check_intro_pattern(text):
ara_intro_pattern = r'^\d[\.]?\s\s?i\s?ntroduction'
rom_intro_pattern = r'^[IVABCDEF]+[\.]?\s\s?i\s?ntroduction'
if bool(re.match(ara_intro_pattern, text, re.IGNORECASE)) or bool(re.match(rom_intro_pattern, text, re.IGNORECASE)):
add_ara_intro_pattern = r'^\d\.\s\s?i\s?ntroduction'
add_rom_intro_pattern = r'^[IVABCDEF]+\.\s\s?i\s?ntroduction'
if bool(re.match(add_ara_intro_pattern, text, re.IGNORECASE)) or bool(re.match(add_rom_intro_pattern, text, re.IGNORECASE)):
return 2
return 1
return 0
def title_len_constraint(text):
return 5 < len(text) < 55
def paper_title_len_constraint(text):
return 10 < len(text) <= 100
def is_body_text(text):
return len(text) > 200 or text.endswith(".") or text.endswith("?") or text.endswith("!") or text.endswith("-")
def is_table(text):
# table_pattern = r"^Table\s\d+\."
return "\t" in text or " | " in text
def is_image(text):
# figure_pattern = r"^Figure\s\d+\."
return text.startswith("Image:") or text.startswith("Figure:")
def is_formula(text):
# ignore numbers
all_num_pattern = r"^[\.\d\−\s]+$"
return text.startswith("Formula:") or text.startswith("Equation:") or bool(re.match(all_num_pattern, text))
def is_url(text):
url_pattern = r"^(?:\d*\s)?https?://\S+"
return bool(re.match(url_pattern, text)) or text.startswith('arXiv:')
# Examples:
# arxiv_id = "./arxiv_archive/2106.09685.pdf"
# download_pdf_from_arxiv(1811.09243)
# all_titles = extract_all_titles(arxiv_id)
# all_chap_titles = select_chaptitle_from_alltitles(all_titles)
# bodies = extract_bodies(arxiv_id, all_chap_titles, all_titles)
# arxiv_id = "2106.09685"
# path = f"arxiv_archive/{arxiv_id}.pdf"
# download_pdf_from_arxiv(arxiv_id)
# paper_title = extract_paper_title(path)
# print(paper_title) |