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:
swaitsandClaude Fable 5.2 committed 2026-11-18 11:01:50 -07:00
1 parent 308c0e1ed3
commit 6cec404e21
71 files changed
+8634 -1502

No files matched your search

+143
View File
@@ -0,0 +1,143 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { classified, finishSimilar, keywordsOf, mergeCandidates, queryPlan, stem, voteSimilar } from './similar.ts';
import type { SimilarCandidate } from './similar.ts';
import { parseMarcXml } from '../sources/marc.ts';
import { parseSimilar } from '../sources/openlibrary.ts';
const FIX = join(import.meta.dirname, '..', 'sources', 'fixtures');
const c = (title: string, lcc: string[], subjects: string[] = []): SimilarCandidate => ({ title, subjects, lcc });
test('stem folds plurals and -ing', () => {
assert.equal(stem('hikes'), stem('hiking'));
assert.equal(stem('mountains'), stem('mountain'));
assert.equal(stem('birds'), stem('bird'));
});
test('keywords drop stop and generic words, numbers and short words', () => {
const k = keywordsOf('Beyond Trails in the Wind River Mountains of Wyoming');
assert.deepEqual([...k.title].sort(), ['mountains', 'river', 'trails', 'wind', 'wyoming']);
assert.equal(k.title[0], 'mountains'); // longest first
assert.deepEqual(keywordsOf('100 Classic Hikes Utah').title.sort(), ['hikes', 'utah']);
assert.deepEqual(keywordsOf('The Complete Guide to the New Edition').title, []);
});
test('subjects add extra keywords, without noise or repeats of title words', () => {
const k = keywordsOf('Guns of August', undefined, ['World War, 1914-1918', 'Accessible book', 'Campaigns', 'Guns']);
assert.ok(k.extra.includes('campaigns'));
assert.ok(!k.extra.includes('accessible'));
assert.ok(!k.extra.includes('guns'));
});
test('query plan: widest first, fewer keywords after, at most 3, never one word for a multi-word title', () => {
const plan = queryPlan(['a1', 'b2', 'c3', 'd4', 'e5']);
assert.deepEqual(plan.map((p) => p.length), [5, 4, 3]);
assert.deepEqual(plan[2], ['a1', 'b2', 'c3']);
assert.deepEqual(queryPlan(['aa', 'bb']), [['aa', 'bb']]);
assert.deepEqual(queryPlan([]), []);
});
test('the same book from two searches counts once', () => {
const m = mergeCandidates([[c('Hiking Utah', ['GV199.42.U8 A1'])], [c('hiking utah', ['GV199.42.U8 B2'], ['Utah'])]]);
assert.equal(m.length, 1);
assert.equal(m[0]!.lcc.length, 2);
assert.equal(classified(m), 1);
});
test('votes by class key; two agreeing books win', () => {
const kw = keywordsOf('Hiking Utah Canyons');
const v = voteSimilar(kw, [
c('Hiking Utah canyons', ['GV199.42.U8 B1 2001']),
c('Utah canyons hiking guide', ['GV199.42.U8 C2 2005']),
c('Utah hiking', ['GV199.42.U8 D3 2010']),
c('Canyons of the Southwest', ['F788 .E5']),
]);
assert.ok(v);
assert.equal(v.key, 'GV199.42.U8');
assert.equal(v.support, 3);
});
test('one unrelated popular book does not capture a generic title', () => {
const kw = keywordsOf('Summer Guns');
const v = voteSimilar(kw, [
c('Guns of August', ['D530 .T8']),
c('Gun control', ['HV7436 .G86']),
c('Summer nights', ['PS3566 .A1']),
c('Summer rain', ['QC925 .R3']),
]);
assert.equal(v, null);
});
test('a single candidate needs to match most of the keywords', () => {
const kw = keywordsOf('Alpine Wildflowers Colorado');
assert.equal(voteSimilar(kw, [c('Alpine wildflowers of Colorado', ['QK144 .A4'])])?.key, 'QK144');
assert.equal(voteSimilar(kw, [c('Alpine skiing', ['GV854 .A4']), c('Colorado history', ['F776 .H5'])]), null);
});
test('candidates matching too few keywords are ignored', () => {
const kw = keywordsOf('Alpine Wildflowers Colorado');
assert.equal(voteSimilar(kw, [c('Alpine skiing', ['GV854 .A4']), c('Alpine lakes', ['GB1 .A1']), c('Alpine huts', ['GV854 .B1'])]), null);
});
test('scattered fine keys fall back to the class, and scattered classes return nothing', () => {
const kw = keywordsOf('Bread Baking Pizza');
const v = voteSimilar(kw, [
c('Bread baking', ['TX769 .A1 2000']),
c('Pizza and bread', ['TX770.P58 B1 2001']),
c('Baking bread, pizza', ['TX770.P58 C1 2002']),
c('Baking pizza', ['TX770.P6 D1 2003']),
]);
assert.ok(v);
assert.match(v.key, /^TX7/);
assert.equal(voteSimilar(kw, [c('Bread baking', ['TX769 .A1']), c('Pizza bread', ['QA76 .B1']), c('Baking pizza', ['HD9 .C1'])]), null);
});
test('neighbouring classes do not cancel each other out', () => {
const kw = keywordsOf('Western Birds Field');
const v = voteSimilar(kw, [
c('Western birds', ['QL683.W4 A1']), c('Field birds western', ['QL683.W4 B1']),
c('Birds field', ['QL681 .C1']), c('Birds of the field', ['QL681 .D1']),
]);
assert.ok(v);
assert.match(v.key, /^QL68[13]/);
});
test('author-numbered literature classes are never borrowed', () => {
const kw = keywordsOf('Dark Summer Novel');
assert.equal(voteSimilar(kw, [c('Dark summer', ['PS3562 .A1']), c('Summer dark novel', ['PS3562 .A2'])]), null);
});
test('candidates without a parseable call number do not vote', () => {
const kw = keywordsOf('Hiking Utah');
assert.equal(voteSimilar(kw, [c('Hiking Utah', ['CPB Box no. 1']), c('Utah hiking', [])]), null);
});
test('finish: own author cutter and year after the borrowed key', () => {
assert.deepEqual(finishSimilar('GV199.42.W8', { authors: ['Nancy Pallister'], title: 'Beyond Trails', year: 2025 })!.callNumber, 'GV199.42.W8 P35 2025');
assert.equal(finishSimilar('TX769', { authors: ['Ken Forkish'], title: 'Flour', year: 2012 })!.callNumber, 'TX769.F67 2012');
assert.equal(finishSimilar('QL683', { authors: [], title: 'A Field Guide' })!.callNumber, 'QL683.F54');
assert.equal(finishSimilar('???', { authors: [], title: 'x' }), null);
});
test('real responses: Wind River Mountains votes for hiking in Wyoming, nonsense for nothing', () => {
const ol = parseSimilar(readFileSync(join(FIX, 'ol-similar-mountains-wyoming-trails.json'), 'utf8'));
const loc = parseMarcXml(readFileSync(join(FIX, 'loc-similar-mountains-wyoming.xml'), 'utf8'));
const cands = mergeCandidates([
ol.map((d) => ({ title: d.title, ...(d.subtitle ? { subtitle: d.subtitle } : {}), subjects: d.subjects, lcc: d.lcc })),
loc.map((r) => ({ title: r.meta.title ?? '', subjects: [...(r.meta.subjects ?? []), ...r.topics], lcc: r.lcc.map((x) => x.raw) })),
]);
const v = voteSimilar(keywordsOf('Beyond Trails in the Wind River Mountains of Wyoming'), cands);
assert.ok(v);
assert.match(v.key, /^GV199\.42/);
assert.ok(v.support >= 2);
assert.equal(voteSimilar(keywordsOf('Xyzzy Plugh Quux Handbook'), cands), null);
});
test('real responses: bread books vote for TX769', () => {
const loc = parseMarcXml(readFileSync(join(FIX, 'loc-similar-artisan-flour.xml'), 'utf8'));
const cands = loc.map((r) => ({ title: r.meta.title ?? '', subjects: [...(r.meta.subjects ?? []), ...r.topics], lcc: r.lcc.map((x) => x.raw) }));
const v = voteSimilar(keywordsOf('Artisan Flour Water Bread'), cands);
assert.equal(v?.key, 'TX769');
});