Add QR codes to cards, stop inventing call number parts
Cards now carry a QR code holding the book's own record (call number, title, authors, ISBN, year), and the scanner reads it back to find or add that book. Call number selection no longer pads catalogue numbers with generated cutters. A number is "exact" only when it comes verbatim from a record for this edition; a bare class is a hint that steers the other-edition and similar-books lookups; a record fetched by LCCN must list the book's ISBN to count as this edition. "Look up again" bypasses every cached result, and shelf order breaks ties by author then title. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
6cec404e21
commit
ad2956e868
56 files changed
+4020
-156
No files matched your search
@@ -1,10 +1,12 @@
|
||||
import type { BookMeta, Candidate, ResolveInput, Resolution, SearchResult } from '../../shared/types.ts';
|
||||
import { normalizeIsbn } from '../../shared/isbn.ts';
|
||||
import { chooseCallNumber } from './choose.ts';
|
||||
import { classKey, parseCallNumber } from '../../shared/callnumber.ts';
|
||||
import { bareClassResult, chooseCallNumber, findHint } from './choose.ts';
|
||||
import type { Chosen } from './choose.ts';
|
||||
import { createLoc } from '../sources/loc.ts';
|
||||
import { createOpenLibrary } from '../sources/openlibrary.ts';
|
||||
import type { OlInfo } from '../sources/openlibrary.ts';
|
||||
import { classified, finishSimilar, keywordsOf, mergeCandidates, queryPlan, voteSimilar } from './similar.ts';
|
||||
import { classified, finishSimilar, keywordsOf, mergeCandidates, searchWords, voteSimilar } from './similar.ts';
|
||||
import type { SimilarCandidate } from './similar.ts';
|
||||
import type { MarcFields } from '../sources/marc.ts';
|
||||
import { TemporaryError } from '../sources/http.ts';
|
||||
@@ -20,6 +22,8 @@ export interface ResolveOpts {
|
||||
tolerateLocFailure?: boolean;
|
||||
/** Called when the similar-books tier chose the number, for example "GV199.42.W8 (4 of 9 candidates)". */
|
||||
onSimilar?: (detail: string) => void;
|
||||
/** Ignore every cached source answer (and write fresh ones), so "Look up again" really asks again. */
|
||||
fresh?: boolean;
|
||||
}
|
||||
|
||||
export interface Resolver {
|
||||
@@ -47,6 +51,9 @@ function mergeMeta(parts: Partials): BookMeta | null {
|
||||
return meta;
|
||||
}
|
||||
|
||||
/** A work-level or title-match record belongs to some edition, not necessarily this one: its LCCN is not this book's. */
|
||||
const withoutLccn = (m: Partial<BookMeta>): Partial<BookMeta> => { const { lccn: _l, ...rest } = m; return rest; };
|
||||
|
||||
const lccOf = (recs: MarcFields[]): Candidate[] => recs.flatMap((r) => r.lcc);
|
||||
|
||||
export function createResolver(deps: {
|
||||
@@ -54,16 +61,25 @@ export function createResolver(deps: {
|
||||
locIntervalMs?: number;
|
||||
sleep?: (ms: number) => Promise<void>;
|
||||
}): Resolver {
|
||||
const ol = createOpenLibrary({ fetch: deps.fetch, cache: deps.cache });
|
||||
// While a fresh lookup runs, reads miss; writes still go through, so the new answers replace the old ones.
|
||||
let bypass = 0;
|
||||
const cache: SourceCache = { get: (k) => (bypass > 0 ? null : deps.cache.get(k)), set: (k, v) => deps.cache.set(k, v) };
|
||||
const ol = createOpenLibrary({ fetch: deps.fetch, cache });
|
||||
const sleep = deps.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)));
|
||||
const loc = createLoc({
|
||||
fetch: deps.fetch, cache: deps.cache,
|
||||
fetch: deps.fetch, cache,
|
||||
intervalMs: deps.locIntervalMs ?? 6000,
|
||||
sleep,
|
||||
});
|
||||
const OL_GAP_MS = 1000;
|
||||
|
||||
async function resolve(input: ResolveInput, opts?: ResolveOpts): Promise<Resolution> {
|
||||
if (!opts?.fresh) return lookup(input, opts);
|
||||
bypass++;
|
||||
try { return await lookup(input, opts); } finally { bypass--; }
|
||||
}
|
||||
|
||||
async function lookup(input: ResolveInput, opts?: ResolveOpts): Promise<Resolution> {
|
||||
// In tolerant mode the first LoC failure switches the LoC tiers off for the rest of this lookup.
|
||||
let locError: unknown = null;
|
||||
const locCall = async (fn: () => Promise<MarcFields[]>): Promise<MarcFields[]> => {
|
||||
@@ -90,7 +106,7 @@ export function createResolver(deps: {
|
||||
if (required) throw r.e;
|
||||
return null;
|
||||
}
|
||||
return r.v;
|
||||
return r.v ? { ...r.v, meta: withoutLccn(r.v.meta) } : null;
|
||||
};
|
||||
if (isbn) {
|
||||
workP = ol.work(isbn).then((v) => ({ v }), (e: unknown) => ({ e }));
|
||||
@@ -102,14 +118,28 @@ export function createResolver(deps: {
|
||||
// Library of Congress: by LCCN when known, else by ISBN.
|
||||
const lccn = input.lccn ?? edition?.meta.lccn;
|
||||
let locRecs: MarcFields[] = [];
|
||||
let exactSource: 'loc-isbn' | 'loc-lccn' = 'loc-isbn';
|
||||
if (lccn) {
|
||||
locRecs = await locCall(() => loc.byLccn(lccn));
|
||||
if (locRecs.length > 0) exactSource = 'loc-lccn';
|
||||
let locCands: Candidate[] = [];
|
||||
// Numbers from an LCCN record that is not verifiably this edition: they are another edition's, like a title match.
|
||||
let lccnOtherCands: Candidate[] = [];
|
||||
let exactSource: 'loc-isbn' | 'loc-lccn' = isbn ? 'loc-isbn' : 'loc-lccn';
|
||||
const tagged = (recs: MarcFields[], source: 'loc-isbn' | 'loc-lccn'): Candidate[] =>
|
||||
lccOf(recs).map((c) => ({ ...c, source }));
|
||||
// The ISBN comes first: its record is for this edition by construction. The LCCN is the second route, asked
|
||||
// only when the ISBN gave no number, or none with evidence of being complete (a 050 with only $a).
|
||||
if (isbn) {
|
||||
locRecs = await locCall(() => loc.byIsbn(isbn));
|
||||
locCands = tagged(locRecs, 'loc-isbn');
|
||||
}
|
||||
if (isbn && lccOf(locRecs).length === 0) {
|
||||
const recs = await locCall(() => loc.byIsbn(isbn));
|
||||
if (recs.length > 0) { locRecs = recs; exactSource = 'loc-isbn'; }
|
||||
if (lccn && !locCands.some((c) => c.complete === true)) {
|
||||
const recs = await locCall(() => loc.byLccn(lccn));
|
||||
// Same edition only if the book has no ISBN (the LCCN is then its own identifier) or the record lists this ISBN.
|
||||
const mine = recs.filter((r) => !isbn || r.isbns.includes(isbn));
|
||||
const others = recs.filter((r) => !mine.includes(r));
|
||||
if (mine.length > 0) {
|
||||
if (locRecs.length === 0) locRecs = mine;
|
||||
locCands = [...locCands, ...tagged(mine, 'loc-lccn')];
|
||||
}
|
||||
lccnOtherCands = lccOf(others);
|
||||
}
|
||||
|
||||
const olEditionCands: Candidate[] = edition?.lcc ?? [];
|
||||
@@ -122,11 +152,10 @@ export function createResolver(deps: {
|
||||
});
|
||||
notify(metaOf());
|
||||
|
||||
// Tiers 1 and 2.
|
||||
let chosen = chooseCallNumber(
|
||||
{ locExact: lccOf(locRecs), olEdition: olEditionCands, locTitle: [], olWork: [] },
|
||||
forChoose(metaOf()), exactSource,
|
||||
);
|
||||
// Tiers 1 and 2: this edition's own records, used verbatim. A bare class there is only a hint.
|
||||
const own = { locExact: locCands, olEdition: olEditionCands };
|
||||
let chosen: Chosen | null = chooseCallNumber({ ...own, locTitle: [], olWork: [] }, forChoose(metaOf()), exactSource);
|
||||
const hint = chosen ? null : findHint(own, exactSource);
|
||||
|
||||
// Tier 3: LoC by title and author.
|
||||
let locTitleRecs: MarcFields[] = [];
|
||||
@@ -135,26 +164,26 @@ export function createResolver(deps: {
|
||||
const m = metaOf([work?.meta]);
|
||||
const title = m?.title ?? input.title;
|
||||
const author = m?.authors[0] ?? input.author ?? '';
|
||||
// Without an author a title search cannot be verified, so the tier is skipped.
|
||||
// Without an author a title search cannot be verified, so that search is skipped.
|
||||
if (title && author) {
|
||||
const found = await locCall(() => loc.byTitle(title, author));
|
||||
locTitleRecs = found.filter((r) => recordMatches(r, title, author));
|
||||
chosen = chooseCallNumber(
|
||||
{ locExact: [], olEdition: [], locTitle: lccOf(locTitleRecs), olWork: [] },
|
||||
forChoose(m), exactSource,
|
||||
);
|
||||
}
|
||||
chosen = chooseCallNumber(
|
||||
{ locExact: [], olEdition: [], locTitle: [...lccnOtherCands, ...lccOf(locTitleRecs)], olWork: [] },
|
||||
forChoose(m), exactSource, hint,
|
||||
);
|
||||
}
|
||||
|
||||
// Tier 4, and the work-level part of the metadata merge.
|
||||
work ??= await getWork(!chosen);
|
||||
// Without an ISBN or LCCN, the best title hit also supplies metadata.
|
||||
const hitMeta = !isbn && !input.lccn ? locTitleRecs[0]?.meta : undefined;
|
||||
const hitMeta = !isbn && !input.lccn && locTitleRecs[0] ? withoutLccn(locTitleRecs[0].meta) : undefined;
|
||||
const meta = metaOf([hitMeta, work?.meta]);
|
||||
notify(meta);
|
||||
chosen ??= chooseCallNumber(
|
||||
{ locExact: [], olEdition: [], locTitle: [], olWork: work?.lcc ?? [] },
|
||||
forChoose(meta), exactSource,
|
||||
forChoose(meta), exactSource, hint,
|
||||
);
|
||||
|
||||
// Tier 5: borrow the class from similar books. Last resort, and only for a book with a title.
|
||||
@@ -163,7 +192,7 @@ export function createResolver(deps: {
|
||||
const kw = keywordsOf(meta.title, meta.subtitle, meta.subjects);
|
||||
const groups: SimilarCandidate[][] = [];
|
||||
let found = 0;
|
||||
const plan = kw.title.length >= 2 ? queryPlan(kw.title) : queryPlan([...kw.title, ...kw.extra].slice(0, 3), 1);
|
||||
const plan = searchWords(kw);
|
||||
for (const [i, words] of plan.entries()) {
|
||||
if (i > 0) await sleep(OL_GAP_MS);
|
||||
try {
|
||||
@@ -176,7 +205,7 @@ export function createResolver(deps: {
|
||||
found = classified(groups.flat());
|
||||
if (found >= 5) break;
|
||||
}
|
||||
const locWords = kw.title.slice(0, 3);
|
||||
const locWords = (plan[0] ?? []).slice(0, 3);
|
||||
if (locWords.length > 0) {
|
||||
for (const words of locWords.length > 2 ? [locWords, locWords.slice(0, 2)] : [locWords]) {
|
||||
const recs = await locCall(() => loc.byKeywords(words));
|
||||
@@ -187,7 +216,18 @@ export function createResolver(deps: {
|
||||
if (classified(groups.at(-1)!) >= 5 || locError) break;
|
||||
}
|
||||
}
|
||||
const vote = voteSimilar(kw, mergeCandidates(groups));
|
||||
// With a class hint and nobody in that class found so far, one more LoC search on the title index alone.
|
||||
if (hint && !locError && locWords.length >= 2 && !groups.flat().some((c) => c.lcc.some((l) => {
|
||||
const cn = parseCallNumber(l);
|
||||
return cn !== null && cn.cutters.length > 0 && classKey(cn) === hint.cls;
|
||||
}))) {
|
||||
const recs = await locCall(() => loc.byTitleWords(locWords));
|
||||
groups.push(recs.map((r) => ({
|
||||
title: r.meta.title ?? '', ...(r.meta.subtitle ? { subtitle: r.meta.subtitle } : {}),
|
||||
subjects: [...(r.meta.subjects ?? []), ...r.topics], lcc: r.lcc.map((c) => c.raw),
|
||||
})));
|
||||
}
|
||||
const vote = voteSimilar(kw, mergeCandidates(groups), hint ? { hint: hint.cls } : {});
|
||||
if (vote) {
|
||||
const done = finishSimilar(vote.key, forChoose(meta));
|
||||
if (done) {
|
||||
@@ -197,6 +237,13 @@ export function createResolver(deps: {
|
||||
}
|
||||
}
|
||||
|
||||
// A bare class and nothing better: the class and this edition's year, with no generated cutter.
|
||||
// Unless Open Library just failed and another attempt is coming: then ask again rather than settle for it.
|
||||
if (!chosen && hint) {
|
||||
if (olError && !opts?.tolerateLocFailure) throw olError;
|
||||
chosen = bareClassResult(hint, forChoose(meta).year);
|
||||
}
|
||||
|
||||
const dewey = locRecs.map((r) => r.dewey).find(Boolean) ?? edition?.dewey ?? work?.dewey
|
||||
?? locTitleRecs.map((r) => r.dewey).find(Boolean) ?? null;
|
||||
|
||||
|
||||
Reference in new issue
Block a user