-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
393 lines (308 loc) · 12.8 KB
/
Copy pathparser.py
File metadata and controls
393 lines (308 loc) · 12.8 KB
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
"""
Refract - Reference Parser v2
Supports multiple citation styles:
1. Numbered: [1], [2], [3]
2. Author-Year: (Zhou et al., 2013), (Hastie & Tibshirani, 1990)
3. Inline Author-Year: Zhou et al. (2013), Hastie and Tibshirani (1990)
"""
import re
try:
import fitz # PyMuPDF
except ImportError:
fitz = None
from dataclasses import dataclass, field
from enum import Enum
class CitationStyle(Enum):
NUMBERED = "numbered"
AUTHOR_YEAR = "author_year"
UNKNOWN = "unknown"
@dataclass
class ParsedReference:
"""A single parsed reference from the bibliography."""
key: str
raw_text: str
authors: list[str] = field(default_factory=list)
year: str | None = None
extracted_title: str | None = None
citation_contexts: list[str] = field(default_factory=list)
search_query: str | None = None
@dataclass
class ParseResult:
"""Result of parsing a PDF."""
style: CitationStyle
references: dict[str, ParsedReference]
body_text: str
full_text: str
total_refs: int
error: str | None = None
# ─── Text Extraction ─────────────────────────────────────────
def extract_text(pdf_path: str) -> str:
if fitz is None:
raise ImportError("PyMuPDF (fitz) is required for PDF extraction. Install with: pip install PyMuPDF")
doc = fitz.open(pdf_path)
pages = [page.get_text() for page in doc]
doc.close()
return "\n".join(pages)
# ─── Citation Style Detection ────────────────────────────────
def detect_citation_style(text: str) -> CitationStyle:
sample = text[:15000]
numbered_pattern = r"\[(\d+(?:\s*[,\-–]\s*\d+)*)\]"
numbered_hits = len(re.findall(numbered_pattern, sample))
author_year_pattern = (
r"[A-Z][a-z]+"
r"(?:\s+(?:et\s+al\.|&\s+[A-Z][a-z]+|and\s+[A-Z][a-z]+))?"
r"\s*[\(,]\s*\d{4}[a-z]?\s*\)?"
)
author_year_hits = len(re.findall(author_year_pattern, sample))
if author_year_hits > numbered_hits * 1.5 and author_year_hits > 10:
return CitationStyle.AUTHOR_YEAR
elif numbered_hits > 5:
return CitationStyle.NUMBERED
elif author_year_hits > 3:
return CitationStyle.AUTHOR_YEAR
return CitationStyle.UNKNOWN
# ─── Reference Section Detection ─────────────────────────────
def find_reference_section(text: str) -> tuple[str | None, int]:
patterns = [
r"\n\s*(References|Bibliography|Works Cited|Literature Cited|REFERENCES)\s*\n",
]
best_match = None
for pat in patterns:
for match in re.finditer(pat, text):
best_match = match
if best_match:
return text[best_match.start():], best_match.start()
return None, -1
# ─── Numbered Reference Parsing ──────────────────────────────
def parse_numbered_references(ref_section: str) -> dict[str, ParsedReference]:
entries = re.split(r"\n\s*\[(\d+)\]\s*", ref_section)
refs = {}
i = 1
while i < len(entries) - 1:
num = entries[i]
content = re.sub(r"\s+", " ", entries[i + 1].strip())
key = f"[{num}]"
ref = ParsedReference(key=key, raw_text=content)
ref.extracted_title = _extract_title_from_entry(content)
ref.year = _extract_year(content)
ref.authors = _extract_authors_from_entry(content)
ref.search_query = _build_search_query(ref)
refs[key] = ref
i += 2
return refs
# ─── Author-Year Reference Parsing ───────────────────────────
def parse_author_year_references(ref_section: str) -> dict[str, ParsedReference]:
refs = {}
entries = _split_bibliography_entries(ref_section)
for entry_text in entries:
entry_text = re.sub(r"\s+", " ", entry_text).strip()
if len(entry_text) < 20:
continue
year = _extract_year(entry_text)
authors = _extract_authors_from_entry(entry_text)
title = _extract_title_from_entry(entry_text)
if not year and not authors:
continue
key = _make_author_year_key(authors, year)
base_key = key
suffix = 1
while key in refs:
key = f"{base_key}_{suffix}"
suffix += 1
ref = ParsedReference(
key=key,
raw_text=entry_text,
authors=authors,
year=year,
extracted_title=title,
)
ref.search_query = _build_search_query(ref)
refs[key] = ref
return refs
def _split_bibliography_entries(ref_section: str) -> list[str]:
lines = ref_section.split("\n")
start = 0
for i, line in enumerate(lines):
if re.match(r"^\s*(References|Bibliography|REFERENCES)", line.strip()):
start = i + 1
break
text = "\n".join(lines[start:])
# Strategy 1: blank-line separated
chunks = re.split(r"\n\s*\n", text)
if len(chunks) > 3:
return [c.strip() for c in chunks if c.strip()]
# Strategy 2: each entry starts with Author surname
entry_pattern = r"\n(?=[A-Z][a-zà-ü]+(?:\s+[A-Z]\.|\s*,))"
chunks = re.split(entry_pattern, text)
if len(chunks) > 3:
return [c.strip() for c in chunks if c.strip()]
# Strategy 3: split on period + newline + capital letter
chunks = re.split(r"(?<=\.)\s*\n\s*(?=[A-Z])", text)
return [c.strip() for c in chunks if c.strip()]
# ─── Citation Context Extraction ─────────────────────────────
def find_citation_contexts_numbered(body_text: str, ref_key: str, window: int = 300) -> list[str]:
num = ref_key.strip("[]")
pattern = re.compile(
rf"\[(?:\d+\s*[,\-–]\s*)*{re.escape(num)}(?:\s*[,\-–]\s*\d+)*\]"
)
return _extract_contexts(body_text, pattern, window)
def find_citation_contexts_author_year(
body_text: str, authors: list[str], year: str | None, window: int = 300
) -> list[str]:
if not authors or not year:
return []
first_author = _get_surname(authors[0])
if not first_author or len(first_author) < 2:
return []
fa = re.escape(first_author)
yr = re.escape(year)
patterns = [
rf"{fa}\s+et\s+al\.?\s*[\(,]\s*{yr}",
rf"\(\s*{fa}\s+et\s+al\.?,?\s*{yr}\s*\)",
rf"{fa}\s+(?:and|&)\s+[A-Z][a-z]+\s*[\(,]\s*{yr}",
rf"\(\s*{fa}\s+(?:and|&)\s+[A-Z][a-z]+,?\s*{yr}\s*\)",
rf"{fa}\s*\(\s*{yr}\s*\)",
rf"\(\s*{fa},?\s*{yr}\s*\)",
rf"{fa}(?:\s+et\s+al\.?)?,?\s*{yr}",
]
all_contexts = []
seen_positions = set()
for pat_str in patterns:
pattern = re.compile(pat_str, re.IGNORECASE)
for match in pattern.finditer(body_text):
pos = match.start()
if any(abs(pos - sp) < 100 for sp in seen_positions):
continue
seen_positions.add(pos)
start = max(0, pos - window)
end = min(len(body_text), match.end() + window)
snippet = re.sub(r"\s+", " ", body_text[start:end].strip())
all_contexts.append(snippet)
return all_contexts[:5]
def _extract_contexts(text: str, pattern: re.Pattern, window: int) -> list[str]:
contexts = []
for match in pattern.finditer(text):
start = max(0, match.start() - window)
end = min(len(text), match.end() + window)
snippet = re.sub(r"\s+", " ", text[start:end].strip())
contexts.append(snippet)
return contexts[:5]
# ─── Helper Functions ────────────────────────────────────────
def _get_surname(author_str: str) -> str:
"""Extract surname from an author string like 'Zhou H.' or 'Durham T. J.'."""
author_str = author_str.strip().rstrip(".,")
if not author_str:
return ""
# If comma present: "Zhou, H." → surname before comma
if "," in author_str:
return author_str.split(",")[0].strip()
parts = author_str.split()
if not parts:
return author_str
# Walk from the end, skip initials (single letter with optional period)
for i in range(len(parts) - 1, -1, -1):
p = parts[i].rstrip(".")
if len(p) == 1 and p.isupper():
continue # This is an initial, skip
return parts[i]
# All parts are initials — return the first one
return parts[0]
def _extract_year(text: str) -> str | None:
match = re.search(r"\(?\b((?:19|20)\d{2})[a-z]?\b\)?", text)
return match.group(1) if match else None
def _extract_authors_from_entry(text: str) -> list[str]:
year_match = re.search(r"\(?\b(?:19|20)\d{2}[a-z]?\b\)?", text)
if not year_match:
return []
author_part = text[:year_match.start()].strip().rstrip(".(,")
# Step 1: split on major separators: " & ", " and ", "; "
# But also ", & " and ", and "
chunks = re.split(r"\s*(?:,\s*&\s*|\s+&\s+|,\s*and\s+|\s+and\s+|;\s*)", author_part)
# Step 2: within each chunk, further split authors separated by commas
# Pattern: "Zhou H., Li L." → two authors separated by ", " before a capital
# We split on ", " followed by a capital letter (start of new surname)
authors = []
for chunk in chunks:
# Split: ", " followed by uppercase letter (new author)
# But NOT ", " followed by an initial like "A." (which is part of same author)
sub_authors = re.split(r",\s+(?=[A-Z][a-z])", chunk)
for a in sub_authors:
a = a.strip().strip(".,")
if a and len(a) > 1 and not a.isdigit():
authors.append(a)
return authors
def _extract_title_from_entry(text: str) -> str | None:
quoted = re.search(r'["\u201c](.+?)["\u201d]', text)
if quoted:
return quoted.group(1).strip()
year_match = re.search(r"\(?\b(?:19|20)\d{2}[a-z]?\b\)?\.?\s*", text)
if not year_match:
return None
after_year = text[year_match.end():].strip()
after_year = re.sub(r"^[.,;:\s]+", "", after_year)
title_match = re.match(
r"(.+?)\.\s+(?:[A-Z]|In\s|Proceedings|Journal|The\s|IEEE|SIAM|Biometri|Nature|Chapman|CRC|Cambridge|Springer|John\s+Wiley|North-Holland|PMLR|ACM|Curran)",
after_year,
)
if title_match:
title = title_match.group(1).strip()
if 10 < len(title) < 300:
return title
dot_pos = after_year.find(".")
if dot_pos > 10:
return after_year[:dot_pos].strip()
return None
def _make_author_year_key(authors: list[str], year: str | None) -> str:
if not authors:
first = "Unknown"
else:
first = _get_surname(authors[0])
first = re.sub(r"[^a-zA-Z]", "", first)
if not first:
first = "Unknown"
yr = year or "XXXX"
return f"{first}{yr}"
def _build_search_query(ref: ParsedReference) -> str:
if ref.extracted_title:
return ref.extracted_title[:200]
query_parts = []
if ref.authors:
query_parts.append(_get_surname(ref.authors[0]))
if ref.year:
query_parts.append(ref.year)
query_parts.append(ref.raw_text[:80].strip())
return " ".join(query_parts)[:200]
# ─── Main Pipeline ───────────────────────────────────────────
def process_pdf(pdf_path: str) -> ParseResult:
full_text = extract_text(pdf_path)
style = detect_citation_style(full_text)
ref_section, ref_start = find_reference_section(full_text)
if not ref_section:
return ParseResult(
style=style, references={}, body_text=full_text,
full_text=full_text, total_refs=0,
error="Could not locate reference section",
)
body_text = full_text[:ref_start] if ref_start > 0 else full_text
if style == CitationStyle.NUMBERED:
refs = parse_numbered_references(ref_section)
for key, ref in refs.items():
ref.citation_contexts = find_citation_contexts_numbered(body_text, key)
else:
refs = parse_author_year_references(ref_section)
for key, ref in refs.items():
ref.citation_contexts = find_citation_contexts_author_year(
body_text, ref.authors, ref.year
)
# Fallback: if author-year found too few, try numbered
if style != CitationStyle.NUMBERED and len(refs) < 3:
numbered_refs = parse_numbered_references(ref_section)
if len(numbered_refs) > len(refs):
refs = numbered_refs
style = CitationStyle.NUMBERED
for key, ref in refs.items():
ref.citation_contexts = find_citation_contexts_numbered(body_text, key)
return ParseResult(
style=style, references=refs, body_text=body_text,
full_text=full_text, total_refs=len(refs),
)