feat: separate contents, abstract and body evidence
This commit is contained in:
@@ -36,18 +36,33 @@ def page_image_path(db, doc: KnowledgeDocument, page: int) -> Path | None:
|
||||
return path if path and path.is_relative_to(root) and path.is_file() else None
|
||||
|
||||
|
||||
def page_links(db, document_id: int | None, page: int | None) -> dict[str, str | None]:
|
||||
def page_links(db, document_id: int | None, page: int | None) -> dict:
|
||||
"""Stable citation links plus the page type, so callers can label the source."""
|
||||
empty = {"url": None, "image_url": None, "page_type": None, "section": None}
|
||||
if not document_id or not page:
|
||||
return {"url": None, "image_url": None}
|
||||
return empty
|
||||
doc = db.get(KnowledgeDocument, document_id)
|
||||
if not doc:
|
||||
return {"url": None, "image_url": None}
|
||||
return empty
|
||||
record = db.get(KnowledgePage, (document_id, page))
|
||||
if not record:
|
||||
return {"url": None, "image_url": None}
|
||||
return empty
|
||||
return {"url": text_url(document_id, page),
|
||||
"image_url": image_url(document_id, page) if page_image_path(db, doc, page) else None}
|
||||
"image_url": image_url(document_id, page) if page_image_path(db, doc, page) else None,
|
||||
"page_type": record.page_type or "unknown",
|
||||
"section": record.section or ""}
|
||||
|
||||
|
||||
def evidence_links(db, evidence) -> dict[str, str | None]:
|
||||
def evidence_links(db, evidence) -> dict:
|
||||
return page_links(db, evidence.document_id, evidence.page)
|
||||
|
||||
|
||||
def page_types(db, document_ids) -> dict[int, str]:
|
||||
"""Page type per page of the given documents, for labelling citations in bulk."""
|
||||
from sqlalchemy import select as _select
|
||||
ids = [item for item in dict.fromkeys(document_ids or []) if item]
|
||||
if not ids:
|
||||
return {}
|
||||
rows = db.execute(_select(KnowledgePage.document_id, KnowledgePage.page, KnowledgePage.page_type)
|
||||
.where(KnowledgePage.document_id.in_(ids))).all()
|
||||
return {(document_id, page): (page_type or "unknown") for document_id, page, page_type in rows}
|
||||
|
||||
Reference in New Issue
Block a user