Redesign the interface around Scan and Library, classify uncatalogued books from similar ones
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
308c0e1ed3
commit
6cec404e21
71 files changed
+8634
-1502
No files matched your search
@@ -4,6 +4,8 @@ import { chooseCallNumber } 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 type { SimilarCandidate } from './similar.ts';
|
||||
import type { MarcFields } from '../sources/marc.ts';
|
||||
import { TemporaryError } from '../sources/http.ts';
|
||||
import { recordMatches } from './match.ts';
|
||||
@@ -16,6 +18,8 @@ export interface ResolveOpts {
|
||||
onMeta?: (meta: BookMeta) => void;
|
||||
/** Last-attempt mode: a Library of Congress failure is not fatal; use Open Library's numbers if it has any. */
|
||||
tolerateLocFailure?: boolean;
|
||||
/** Called when the similar-books tier chose the number, for example "GV199.42.W8 (4 of 9 candidates)". */
|
||||
onSimilar?: (detail: string) => void;
|
||||
}
|
||||
|
||||
export interface Resolver {
|
||||
@@ -51,11 +55,13 @@ export function createResolver(deps: {
|
||||
sleep?: (ms: number) => Promise<void>;
|
||||
}): Resolver {
|
||||
const ol = createOpenLibrary({ fetch: deps.fetch, cache: deps.cache });
|
||||
const sleep = deps.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)));
|
||||
const loc = createLoc({
|
||||
fetch: deps.fetch, cache: deps.cache,
|
||||
intervalMs: deps.locIntervalMs ?? 6000,
|
||||
sleep: deps.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms))),
|
||||
sleep,
|
||||
});
|
||||
const OL_GAP_MS = 1000;
|
||||
|
||||
async function resolve(input: ResolveInput, opts?: ResolveOpts): Promise<Resolution> {
|
||||
// In tolerant mode the first LoC failure switches the LoC tiers off for the rest of this lookup.
|
||||
@@ -151,6 +157,46 @@ export function createResolver(deps: {
|
||||
forChoose(meta), exactSource,
|
||||
);
|
||||
|
||||
// Tier 5: borrow the class from similar books. Last resort, and only for a book with a title.
|
||||
let olError: unknown = null;
|
||||
if (!chosen && meta?.title) {
|
||||
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);
|
||||
for (const [i, words] of plan.entries()) {
|
||||
if (i > 0) await sleep(OL_GAP_MS);
|
||||
try {
|
||||
const docs = await ol.similar(words);
|
||||
groups.push(docs.map((d) => ({ title: d.title, ...(d.subtitle ? { subtitle: d.subtitle } : {}), subjects: d.subjects, lcc: d.lcc })));
|
||||
} catch (e) {
|
||||
if (e instanceof TemporaryError) { olError = e; break; }
|
||||
throw e;
|
||||
}
|
||||
found = classified(groups.flat());
|
||||
if (found >= 5) break;
|
||||
}
|
||||
const locWords = kw.title.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));
|
||||
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),
|
||||
})));
|
||||
if (classified(groups.at(-1)!) >= 5 || locError) break;
|
||||
}
|
||||
}
|
||||
const vote = voteSimilar(kw, mergeCandidates(groups));
|
||||
if (vote) {
|
||||
const done = finishSimilar(vote.key, forChoose(meta));
|
||||
if (done) {
|
||||
chosen = { callNumber: done.callNumber, sortKey: done.sortKey, confidence: 'approximate', source: 'similar' };
|
||||
opts?.onSimilar?.(`${vote.key} (${vote.support} of ${vote.counted} candidates)`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const dewey = locRecs.map((r) => r.dewey).find(Boolean) ?? edition?.dewey ?? work?.dewey
|
||||
?? locTitleRecs.map((r) => r.dewey).find(Boolean) ?? null;
|
||||
|
||||
@@ -162,6 +208,7 @@ export function createResolver(deps: {
|
||||
}
|
||||
// Open Library had nothing and the LoC could not be asked: report that, not "no number found".
|
||||
if (locError) throw locError;
|
||||
if (olError) throw olError;
|
||||
return {
|
||||
meta, callNumber: null, sortKey: null, confidence: null, source: null, dewey,
|
||||
reason: meta ? 'No Library of Congress number found' : 'Not found in Open Library or the Library of Congress',
|
||||
|
||||
Reference in new issue
Block a user