Files
swaitsandClaude Fable 5.2 7ae1bcef0a Print the subject hierarchy as a tree on cards, remove the QR code
Each card now shows, under the call number, the Library of Congress
outline levels for its class (from the published outline, down to the
narrowest range) followed by the place or topic from the catalogue
record's subject heading, one level per line. Existing books get the
subject part filled in by a background job at startup that changes
nothing else about them.

The QR code and everything that read it are removed; the subjects line
on the card is replaced by the tree.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 14:06:50 -07:00

113 lines
4.8 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Parses the text of one Library of Congress Classification Outline PDF (from `pdftotext -layout`)
// into its number ranges with their captions. Used by gen-lcc-outline.ts; no I/O here.
/** [lo, hi, caption] or, for ranges bounded by cutters, [lo, hi, caption, cutterLo, cutterHi]. */
export type Range = [lo: number, hi: number, caption: string, cutterLo?: string, cutterHi?: string];
export interface Parsed {
/** Ranges by class letters (PS, GV, KBM), in the order they appear in the outline. */
ranges: Map<string, Range[]>;
/** Lines that looked like a range but could not be read; reported by the generator. */
problems: string[];
}
const NUM = String.raw`\(?(\d+(?:\.\d+)?)\)?`;
const START = new RegExp(String.raw`^${NUM}(?:\.([A-Z]\d*))?$`);
const END_NUMBER = new RegExp(String.raw`^([A-Z]{1,3}(?=[\d(]))?${NUM}(?:\.([A-Z]\d*))?$`);
const END_DECIMAL = /^\.(\d+)$/;
const END_CUTTER = /^\.?([A-Z]\d*)$/;
const NOTE = /^(Including|Class |For |See |Subarrange|Cf\.)/;
export interface Bounds { lo: number; hi: number; cutterLo?: string; cutterHi?: string }
/** The number part of a code such as `191.2-200.66`, `1569.C2`, `469.M21-.M38` or `1115-(1193)`; null if unreadable. */
export function parseBounds(code: string, letters = ''): Bounds | null {
const dash = code.indexOf('-');
const startText = dash < 0 ? code : code.slice(0, dash);
const s = START.exec(startText);
if (!s) return null;
const lo = Number(s[1]);
const out: Bounds = { lo, hi: lo };
if (s[2]) out.cutterLo = s[2];
if (dash < 0) { if (s[2]) out.cutterHi = s[2]; return out; }
const endText = code.slice(dash + 1);
let m: RegExpExecArray | null;
// "U46" after "DA990.U45" is a cutter; "Z1003.5" after "Z1" repeats the class letters. Tell them apart by the letters.
if ((m = END_NUMBER.exec(endText)) && (!m[1] || m[1] === letters) && Number(m[2]) >= lo) {
out.hi = Number(m[2]);
if (m[3]) out.cutterHi = m[3];
} else if ((m = END_DECIMAL.exec(endText))) {
out.hi = Number(`${Math.trunc(lo)}.${m[1]}`);
} else if ((m = END_CUTTER.exec(endText))) {
out.cutterHi = m[1]!;
} else return null;
if (out.hi < out.lo) return null;
return out;
}
/** A caption as published: one line of text, no stray marks; a lone full stop (a diacritic dot the PDF set on its own line) is dropped. */
export function cleanCaption(raw: string): string {
let s = raw.replace(/­/g, '-').replace(/\s+/g, ' ').trim();
s = s.replace(/(?<!-) \.(?= |$)/g, '').replace(/\s+/g, ' ').trim();
// Trailing debris only: commas, semicolons, colons and a lone full stop after a word; "etc." and "Co." style stops stay.
s = s.replace(/[,;:\s]+$/, '');
if (/[a-z]{3,}\.$/.test(s) && !/\betc\.$/.test(s)) s = s.slice(0, -1);
return s;
}
export function parseOutlineText(text: string): Parsed {
const ranges = new Map<string, Range[]>();
const problems: string[] = [];
let started = false;
let cur: { letters: string; index: number; capCol: number; caption: string } | null = null;
let inNote = false;
const finish = () => {
if (!cur) return;
const list = ranges.get(cur.letters)!;
const r = list[cur.index]!;
r[2] = cleanCaption(cur.caption);
cur = null;
};
for (const rawLine of text.split('\n')) {
const line = rawLine.replace(/\f/g, '').replace(/\s+$/, '');
if (/^(?:Sub)?[Cc]lass [A-Z]{1,3}\s*$/.test(line)) { finish(); started = true; inNote = false; continue; }
if (!started || !line.trim()) continue;
const codeLine = /^([A-Z]{1,3})((?:\(\d+\)|\d)\S*)\s+(\S.*)$/.exec(line) ?? /^([A-Z]{1,3})((?:\(\d+\)|\d)\S*)\s*$/.exec(line);
if (codeLine) {
finish();
inNote = false;
const [, letters, code, caption = ''] = codeLine as unknown as [string, string, string, string?];
const b = parseBounds(code, letters);
if (!b) { problems.push(line.trim()); continue; }
if (!ranges.has(letters)) ranges.set(letters, []);
const list = ranges.get(letters)!;
const row: Range = [b.lo, b.hi, ''];
if (b.cutterLo !== undefined || b.cutterHi !== undefined) { row[3] = b.cutterLo ?? ''; row[4] = b.cutterHi ?? ''; }
list.push(row);
cur = { letters, index: list.length - 1, capCol: line.length - caption.length, caption };
continue;
}
// Anything else: a wrapped caption, a note under a caption, or a heading without a range.
const indent = line.length - line.trimStart().length;
const t = line.trim();
if (!cur) continue;
if (indent <= cur.capCol) { finish(); inNote = false; continue; } // a heading line, not part of the range above
if (inNote) continue;
if (NOTE.test(t)) { inNote = true; continue; }
cur.caption += ' ' + t;
}
finish();
for (const [letters, list] of ranges) {
ranges.set(letters, list.filter((r) => {
if (r[2]) return true;
problems.push(`${letters}${r[0]}-${r[1]}: empty caption`);
return false;
}));
}
return { ranges, problems };
}