backend: print the 参考文献 list as a bibliography, one entry per work

The list was printing each citation's quote under a `引用 #53` tag: no authors,
no year, no pages, no DOI — the quoted passage where the entry should be — and
it numbered *citations*, so a paper with 86 citations to 65 works listed 21 of
them two or three times under different numbers. The same article was `[5]` in
one paragraph and `[17]` in another.

Two rules were wrong, so two are stated here:

**A work is numbered where the reader first meets it.** A citation is a place
in the text; an entry is a work. `citations` still has one row per place, and
each row now carries the number of its *work*, so the marker in the text, the
tooltip on it and the entry in the list are the same number. The document grew
a `references` list — the entries, in order — and `reference_style`, which
names the style that wrote them.

**How a reference prints is a property of a style, not of a citation.** The new
`app/citation_styles` package holds one module per style; `springer.py`
implements Springer's basic style, whose shape is
`Names: Title. <where it sits> (year). <identifier>` with a branch per document
type — a journal with its volume(issue) and pages, a book with publisher and
place, a chapter inside somebody else's book, a patent with its number, a page
whose URL is its source.

Three of its rules are decisions rather than transcription, and each is
documented where it applies: up to six contributors are named and more become
three plus `et al.`; a journal prints under its 刊名缩写 when the writer stored
one, because no algorithm abbreviates an arbitrary title correctly; and a CJK
name keeps its printed form even with halves filled in, because dotting 「三」
into an initial is wrong in Chinese and insufficient in English.

Nothing is invented to fill a gap. An entry says what it knows and reports the
rest as `missing` (「缺 责任者、出版年」), and the document warns about the
incomplete ones by number: an entry that is short because fields are empty
looks like a fact about the document rather than a library that needs attention.

`document_text` had the same bug in the MCP surface and now prints the entries,
with in-text markers taken from the document's numbering rather than from each
sentence's local position — which is what made the text an agent reads disagree
with the list under it.

`scripts/check_citation_styles.py` checks the formatter with no server and no
database: one reference of every type, and the exact entry the style calls for.
This commit is contained in:
2026-09-22 21:50:27 +08:00
parent 15955f552d
commit 76708ce767
8 changed files with 1305 additions and 46 deletions
+98 -15
View File
@@ -21,6 +21,12 @@ all three behaviours the feature needs:
Deleting is done in Python rather than with ``ON DELETE CASCADE`` because TiDB
parses foreign keys and then ignores them; the ORM's cascades are the only
thing that actually removes dependent rows.
The 参考文献 list is assembled in the same pass, and by the same rule: a work is
numbered where the reader first meets it, however many times it is cited
afterwards. The entries themselves are written by :mod:`app.citation_styles`,
because how a reference prints is a property of a style rather than of a
citation.
"""
from collections import defaultdict
@@ -31,6 +37,7 @@ from sqlalchemy import func, or_, select
from sqlalchemy import update as sa_update
from sqlalchemy.orm import Session
from app.citation_styles import DEFAULT_STYLE, format_reference, missing_fields
from app.crud import reference as reference_crud
from app.crud.filters import LIKE_ESCAPE, like_pattern
from app.models import (
@@ -47,6 +54,7 @@ from app.schemas.paper import (
PaperDocumentRead,
PaperListItem,
PaperRead,
PaperReferenceRead,
ParagraphRead,
ParagraphUpdate,
SentenceCreate,
@@ -329,9 +337,71 @@ def build_document(db: Session, paper: Paper) -> PaperDocumentRead:
# --- 3. the union, in ascending position order --------------------------
positions = sorted(set(placement_by_sort) | set(sentences_by_sort))
# --- 4. the literature the document cites, in one query -----------------
#
# The 参考文献 list is assembled here rather than by the client, for the same
# reason the paragraph order is: "which work is number 7" is a rule, and two
# implementations of a rule eventually disagree. Formatting needs the rows
# themselves — authors, container, pages, DOI — so they are loaded up front,
# once, with their contributors.
cited_ids = {
citation.reference_id
for sentence in paper.sentences
for citation in sentence.citations
if citation.reference_id is not None
}
cited_references = {
row.id: row for row in reference_crud.get_many(db, sorted(cited_ids))
}
if len(cited_references) != len(cited_ids):
# Only reachable if a citation points at a row the library no longer has,
# which the foreign key is there to prevent — so it is a warning rather
# than a crash, and the entries below print as unlinked.
warnings.append(
f"有 {len(cited_ids) - len(cited_references)} 条引用指向的文献已不在库中,"
"参考文献表中只保留了引用位置。"
)
paragraphs: list[ParagraphRead] = []
citations: list[PaperCitationRead] = []
citation_index = 0
references: list[PaperReferenceRead] = []
#: Work → its entry in the list above. Keyed by reference id, because that is
#: what "the same work" means; a citation with no id at all is its own work
#: and is keyed by its citation id so it still gets one entry and one number.
entry_by_work: dict[object, PaperReferenceRead] = {}
def entry_for(citation: PaperSentenceReference) -> PaperReferenceRead:
"""The entry this citation belongs to, numbering it on first sight.
Numbering on first *sight* rather than on first citation is the whole
rule: the entries come out in the order the reader meets the works, and a
work met again keeps the number it already had.
"""
key: object = (
("reference", citation.reference_id)
if citation.reference_id is not None
else ("citation", citation.id)
)
existing = entry_by_work.get(key)
if existing is not None:
return existing
reference = (
cited_references.get(citation.reference_id)
if citation.reference_id is not None
else None
)
entry = PaperReferenceRead(
index=len(references) + 1,
reference_id=citation.reference_id,
text=format_reference(reference),
missing=missing_fields(reference),
citations=[],
)
entry_by_work[key] = entry
references.append(entry)
return entry
for position in positions:
placement = placement_by_sort.get(position)
@@ -363,30 +433,43 @@ def build_document(db: Session, paper: Paper) -> PaperDocumentRead:
sentences=sentences,
)
# Citations are numbered in reading order, so a marker in the text and
# its entry in the 参考文献 list cannot disagree.
# Citations are numbered in reading order and by work, so a marker in the
# text and its entry in the 参考文献 list cannot disagree.
for sentence in sentences:
for citation in sentence.citations:
citation_index += 1
citations.append(
PaperCitationRead(
index=citation_index,
id=citation.id,
reference_id=citation.reference_id,
quote=citation.quote,
sentence_id=sentence.id,
sentence_content=sentence.content,
paper_template_filed_sort=position,
paragraph_name=paragraph.name,
)
entry = entry_for(citation)
row = PaperCitationRead(
index=entry.index,
id=citation.id,
reference_id=citation.reference_id,
quote=citation.quote,
sentence_id=sentence.id,
sentence_content=sentence.content,
paper_template_filed_sort=position,
paragraph_name=paragraph.name,
)
citations.append(row)
entry.citations.append(row)
paragraphs.append(paragraph)
unlinked = sum(1 for entry in references if entry.reference_id is None)
if unlinked:
warnings.append(
f"有 {unlinked} 处引用还没有关联文献库条目,参考文献表中缺少它们的条目。"
)
incomplete = [entry for entry in references if entry.missing]
if incomplete:
numbers = "、".join(f"[{entry.index}]" for entry in incomplete)
warnings.append(f"参考文献 {numbers} 的字段不完整,条目里会缺少相应信息。")
return PaperDocumentRead(
paper=read(paper),
paragraphs=paragraphs,
citations=citations,
references=references,
reference_style=DEFAULT_STYLE,
warnings=warnings,
)