Find a number for books the catalogues have no LC number for
Google Books joins Open Library and the Library of Congress as a source of title, authors, publisher and categories when no number is in hand. The similar-books tier widens its search step by step (other word forms, place and topic words from the subtitle and publisher, fewer words) when the first search finds no class, and needs at least two agreeing books before it accepts one. The result is marked approximate. Books with no number or an approximate one are looked up again at startup once the last lookup is a week old. An ISBN no catalogue knows that is within three digits of a book already here says it may be a misread of that book. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
ddf6020731
commit
a08fbff685
21 files changed
+750
-30
No files matched your search
@@ -5,9 +5,11 @@ 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 { createGoogleBooks } from '../sources/googlebooks.ts';
|
||||
import type { GbInfo } from '../sources/googlebooks.ts';
|
||||
import type { OlInfo } from '../sources/openlibrary.ts';
|
||||
import { classified, finishSimilar, keywordsOf, mergeCandidates, searchWords, voteSimilar } from './similar.ts';
|
||||
import type { SimilarCandidate } from './similar.ts';
|
||||
import { classified, cuesOf, finishSimilar, keywordsOf, mergeCandidates, placeQueries, placeWords, searchWords, shortQueries, variants, voteSimilar } from './similar.ts';
|
||||
import type { Keywords, SimilarCandidate, VoteOpts } from './similar.ts';
|
||||
import type { MarcFields } from '../sources/marc.ts';
|
||||
import { TemporaryError } from '../sources/http.ts';
|
||||
import { recordMatches } from './match.ts';
|
||||
@@ -82,6 +84,9 @@ function mergeMeta(parts: Partials): BookMeta | null {
|
||||
/** 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; };
|
||||
|
||||
/** The fewest books that must agree on a class when the search had to be widened. */
|
||||
export const MIN_AGREE = 2;
|
||||
|
||||
const lccOf = (recs: MarcFields[]): Candidate[] => recs.flatMap((r) => r.lcc);
|
||||
|
||||
export function createResolver(deps: {
|
||||
@@ -93,6 +98,7 @@ export function createResolver(deps: {
|
||||
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 gb = createGoogleBooks({ fetch: deps.fetch, cache });
|
||||
const sleep = deps.sleep ?? ((ms: number) => new Promise<void>((r) => setTimeout(r, ms)));
|
||||
const loc = createLoc({
|
||||
fetch: deps.fetch, cache,
|
||||
@@ -187,8 +193,11 @@ export function createResolver(deps: {
|
||||
} else if (edit === null && !isbn && !locError) edit = '';
|
||||
|
||||
const olEditionCands: Candidate[] = edition?.lcc ?? [];
|
||||
// Google Books (asked below, only while there is no number): the last source for a title, and a filler of gaps.
|
||||
let google: GbInfo | null = null;
|
||||
let googleError: unknown = null;
|
||||
const metaOf = (extra: Partials = []): BookMeta | null =>
|
||||
mergeMeta([edition?.meta, locRecs[0]?.meta, ...extra, input.title ? { title: input.title } : null]);
|
||||
mergeMeta([edition?.meta, locRecs[0]?.meta, ...extra, google?.meta, input.title ? { title: input.title } : null]);
|
||||
const forChoose = (m: BookMeta | null) => ({
|
||||
authors: m?.authors.length ? m.authors : input.author ? [input.author] : [],
|
||||
title: m?.title ?? input.title ?? '',
|
||||
@@ -201,6 +210,16 @@ export function createResolver(deps: {
|
||||
let chosen: Chosen | null = chooseCallNumber({ ...own, locTitle: [], olWork: [] }, forChoose(metaOf()), exactSource);
|
||||
const hint = chosen ? null : findHint(own, exactSource);
|
||||
|
||||
// Google Books: a title and authors for an ISBN neither Open Library nor the Library of Congress knows, and the
|
||||
// subtitle, publisher and categories they left out. It never gives a number, so it is asked only when there is none.
|
||||
if (!chosen && isbn) {
|
||||
try { google = await gb.byIsbn(isbn); } catch (e) {
|
||||
if (!(e instanceof TemporaryError)) throw e;
|
||||
googleError = e; // a number found below still stands; with none, the lookup is tried again
|
||||
}
|
||||
if (google) notify(metaOf());
|
||||
}
|
||||
|
||||
// Tier 3: LoC by title and author.
|
||||
let locTitleRecs: MarcFields[] = [];
|
||||
let titleSearched = false;
|
||||
@@ -277,12 +296,55 @@ export function createResolver(deps: {
|
||||
subjects: [...(r.meta.subjects ?? []), ...r.topics], lcc: r.lcc.map((c) => c.raw),
|
||||
})));
|
||||
}
|
||||
const vote = voteSimilar(kw, mergeCandidates(groups), hint ? { hint: hint.cls } : {});
|
||||
// The searches so far found no class: widen them, one step at a time, voting after each request and stopping at
|
||||
// the first vote that wins. At most 12 requests in all for the tier (3 Open Library + 2 Library of Congress
|
||||
// above, 1 more with a class hint, then 1 for word forms, up to 3 with place words, up to 2 shorter ones).
|
||||
const hintOpt = hint ? { hint: hint.cls } : {};
|
||||
const cues = cuesOf([meta.title, meta.subtitle, meta.publisher, ...(google?.categories ?? []), ...meta.subjects]);
|
||||
const strict: VoteOpts = hintOpt;
|
||||
// A widened vote: two books must agree (never one), they hold half the weight, and lead the rival by half again.
|
||||
const relaxed: VoteOpts = { ...hintOpt, minAgree: MIN_AGREE, minShare: 0.2, margin: 1.5, cues };
|
||||
const asGroup = (recs: MarcFields[]): SimilarCandidate[] => 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),
|
||||
}));
|
||||
let vote = voteSimilar(kw, mergeCandidates(groups), strict);
|
||||
let how = '';
|
||||
const widen = async (label: string, q: () => Promise<MarcFields[]>, kws: Keywords, o: VoteOpts): Promise<void> => {
|
||||
if (vote || locError) return;
|
||||
const recs = await locCall(q);
|
||||
similarRecs.push(...recs);
|
||||
groups.push(asGroup(recs));
|
||||
vote = voteSimilar(kws, mergeCandidates(groups), o);
|
||||
if (vote) how = label;
|
||||
};
|
||||
if (!vote && kw.own.length >= 2) {
|
||||
// b: other forms of the words (hike, hikes, hiking) in one query.
|
||||
await widen('other forms of the title words', () => loc.byWordForms(kw.own.slice(0, 3).map(variants)), kw, strict);
|
||||
}
|
||||
const ctx = { title: meta.title, subtitle: meta.subtitle, publisher: meta.publisher, authors: meta.authors, subjects: meta.subjects, categories: google?.categories ?? [] };
|
||||
const places = placeWords(ctx);
|
||||
// c: place and topic words from the subtitle, publisher, author and categories; they also add weight to a book that has them.
|
||||
const kwPlaces: Keywords = { ...kw, extra: [...new Set([...places, ...kw.extra])].slice(0, 6) };
|
||||
if (!vote) {
|
||||
for (const q of placeQueries(kw, ctx, places)) {
|
||||
await widen(`${q.join(' ')}, a place or topic from the record`, () => loc.byKeywords(q), kwPlaces, relaxed);
|
||||
}
|
||||
}
|
||||
// d: shorter searches from the title's most distinctive words.
|
||||
if (!vote) {
|
||||
for (const q of shortQueries(kw)) await widen(`${q.join(' ')}, a shorter search`, () => loc.byKeywords(q), kwPlaces, relaxed);
|
||||
}
|
||||
// e: what has been found, judged once more with the match threshold lowered to one title word. Still two books must agree.
|
||||
if (!vote) {
|
||||
vote = voteSimilar(kwPlaces, mergeCandidates(groups), { ...relaxed, minMatched: 1, minShare: 0.3, margin: 1.25 });
|
||||
if (vote) how = 'a lower match threshold';
|
||||
}
|
||||
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)`);
|
||||
opts?.onSimilar?.(`${vote.key} (${vote.support} of ${vote.counted} candidates${how ? `; widened: ${how}` : ''})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -327,9 +389,10 @@ 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;
|
||||
if (googleError) throw googleError;
|
||||
return {
|
||||
meta, callNumber: null, sortKey: null, confidence: null, source: null, dewey, tail, edition: edit,
|
||||
reason: meta ? 'No Library of Congress number found' : 'Not found in Open Library or the Library of Congress',
|
||||
reason: meta ? 'No Library of Congress number found' : 'Not found in Open Library, Google Books or the Library of Congress',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user