Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
259 changes: 242 additions & 17 deletions packages/starpod/src/lib/cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,43 +49,268 @@ const tokens = (name: string) =>
.map((token) => token.replace(/[^\p{L}\p{N}]/gu, ''))
.filter(Boolean);

const SUFFIXES = new Set(['jr', 'sr', 'ii', 'iii', 'iv']);

/**
* Whether two names' tokens after the first share a surname: one name's last
* (suffixes like "III" aside) appears in the other's. A middle name the two
* happen to share ("Adam Michael Rackis", "Adam Michael Argyle") is not one.
*
* Alone, a surname has to be 4 letters or more to count, so short common ones
* don't pair strangers. Backing first names that already agree (`firsts`),
* any length counts ("Jen Lee" ~ "Jennifer Lee"), and so does an initial
* ("Adam A." ~ "Adam Argyle").
*/
function shareSurname(aRest: Array<string>, bRest: Array<string>, firsts = false) {
const last = (rest: Array<string>) =>
rest.filter((token) => !SUFFIXES.has(token)).at(-1) ?? '';
const a = last(aRest);
const b = last(bRest);
if (!a || !b) {
return false;
}
const min = firsts ? 1 : 4;
if ((a.length >= min && bRest.includes(a)) || (b.length >= min && aRest.includes(b))) {
return true;
}
return firsts && (a.length === 1 ? b.startsWith(a) : b.length === 1 && a.startsWith(b));
}

/**
* Do these two names refer to the same person? Loosely: their first names
* start the same way ("Robbie" ~ "RobbieTheWagner"), or they share a surname
* ("Chuck Carpenter" ~ "Charles William Carpenter III").
*/
function sameName(a: string, b: string) {
export function sameName(a: string, b: string) {
const [aFirst = '', ...aRest] = tokens(a);
const [bFirst = '', ...bRest] = tokens(b);
if (aFirst.length >= 3 && bFirst.length >= 3 && (aFirst.startsWith(bFirst) || bFirst.startsWith(aFirst))) {
return true;
}
return aRest.some((token) => token.length >= 4 && bRest.includes(token));
return shareSurname(aRest, bRest);
}

/**
* How surely two names are one person, 0 when they aren't:
* 4 the same name ("Shruti Kapoor");
* 3 first names that agree and a shared surname ("Rob Wagner" ~ "Robbie
* Wagner");
* 2 first names that agree where one name has nothing else ("Jason" ~
* "Jason Lengstorf", "RobbieTheWagner" ~ "Robbie Wagner");
* 1 only a shared surname ("Chuck Carpenter" ~ "Charles William Carpenter
* III"), counted only when `surnames` is on: within one episode's own cast,
* never across the site, where every Wagner would match.
* Two full names whose surnames differ ("Adam Rackis", "Adam Argyle") never
* match.
*/
function likeness(a: string, b: string, surnames: boolean) {
const [aFirst = '', ...aRest] = tokens(a);
const [bFirst = '', ...bRest] = tokens(b);
if (!aFirst || !bFirst) {
return 0;
}
if ([aFirst, ...aRest].join(' ') === [bFirst, ...bRest].join(' ')) {
return 4;
}
const firsts =
aFirst.length >= 3 &&
bFirst.length >= 3 &&
(aFirst.startsWith(bFirst) || bFirst.startsWith(aFirst));
if (firsts && shareSurname(aRest, bRest, true)) {
return 3;
}
const surname = shareSurname(aRest, bRest);
if (firsts && (!aRest.length || !bRest.length)) {
return 2;
}
return surnames && surname && !firsts ? 1 : 0;
}

/**
* Pairs each item with at most one candidate and each candidate with at most
* one item: the surest pairs first, and a pair that ties with another for
* the same item or candidate is ambiguous and goes to neither, so nothing is
* matched by a guess.
*/
function pair<T>(
items: Array<T>,
candidates: Array<string>,
score: (item: T, candidate: string) => number
) {
const pairs: Array<{ item: T; candidate: string; score: number }> = [];
for (const item of items) {
for (const candidate of new Set(candidates)) {
const value = score(item, candidate);
if (value) {
pairs.push({ item, candidate, score: value });
}
}
}
pairs.sort((a, b) => b.score - a.score);
const matched = new Map<T, string>();
const used = new Set<string>();
for (let start = 0; start < pairs.length; ) {
let end = start;
while (end < pairs.length && pairs[end].score === pairs[start].score) {
end++;
}
const open = pairs
.slice(start, end)
.filter(
({ item, candidate }) => !matched.has(item) && !used.has(candidate)
);
for (const one of open) {
const rivals = open.filter(
(other) =>
other !== one &&
(other.item === one.item || other.candidate === one.candidate)
);
if (!rivals.length) {
matched.set(one.item, one.candidate);
used.add(one.candidate);
}
}
start = end;
}
return matched;
}

/**
* Pairs each of `names` with at most one of `candidates`, and each candidate
* with at most one name, surest first (see `likeness` and `pair`). Returns
* name → candidate. Used to find who in a cast is which transcript speaker.
*/
export function matchNames(
names: Array<string>,
candidates: Array<string>,
{ surnames = false } = {}
) {
return pair([...new Set(names)], candidates, (name, candidate) =>
likeness(name, candidate, surnames)
);
}

/**
* Who on a page is speaking: `names` are the people a page shows (however
* each element names them), `speakers` the playing transcript's. Names that
* are surely one person ("RobbieTheWagner" on the rail, "Robbie Wagner" on a
* card) count as one, so all of them light; otherwise each speaker goes to
* at most one person, the surest, and never by a shared surname alone, so a
* host's lines light no namesake. Returns name → speaker.
*/
export function matchPageNames(names: Array<string>, speakers: Array<string>) {
const people: Array<Array<string>> = [];
// Full names first, so each person is grouped before a short alias
// ("Adam") that could be either of two of them tries to join one.
const longestFirst = [...new Set(names)].sort(
(a, b) => tokens(b).length - tokens(a).length
);
for (const name of longestFirst) {
const same = people.filter((person) =>
person.some((other) => likeness(name, other, false) >= 2)
);
// A name that could be either of two people stays on its own.
if (same.length === 1) {
same[0].push(name);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} else {
people.push([name]);
}
}
const matched = pair(people, speakers, (person, speaker) =>
Math.max(...person.map((name) => likeness(name, speaker, false)))
);
return new Map(
[...matched].flatMap(([person, speaker]) =>
person.map((name) => [name, speaker] as const)
)
);
}

/**
* Which transcript voice (0 or 1, the two most talkative speakers) a person
* speaks in, so their name on the stage wears the same typeface as their
* lines. People who don't speak as one of those two, or can't be matched, get
* no voice.
* lines. Pass the episode's whole `cast` so each speaker goes to the right one
* of them (`matchNames`); people who don't speak as one of those two, or
* can't be told apart, get no voice.
*/
export function voiceFor(
name: string,
cues: Array<TranscriptCue> | null
cues: Array<TranscriptCue> | null,
cast: Array<string> = [name]
): number | undefined {
if (!cues?.length) {
return undefined;
}
const speakers = [0, 1].map((voice) => ({
voice,
speaker: cues.find((cue) => cue.v === voice && cue.n)?.n
}));
// An exact name wins over a loose one, whichever voice comes first. A name
// with nothing left after normalizing never matches.
const exact = tokens(name).join(' ');
const match =
(exact
? speakers.find(({ speaker }) => speaker && tokens(speaker).join(' ') === exact)
: undefined) ?? speakers.find(({ speaker }) => speaker && sameName(name, speaker));
return match?.voice;
const speakers = [...new Set(cues.flatMap((cue) => (cue.n ? [cue.n] : [])))];
const speaker = matchNames(cast, speakers, { surnames: true }).get(name);
const voice = speaker ? cues.find((cue) => cue.n === speaker)?.v : undefined;
return voice === 0 || voice === 1 ? voice : undefined;
}

/**
* Which of a transcript's speakers one name is, on its own (no cast to weigh
* it against): exact, or first names that agree (`matchNames`, without
* surname-only matches). Undefined when none matches surely.
*/
export function speakerFor(name: string, speakers: Array<string>) {
return matchNames([name], speakers).get(name);
}

/** 1st, 2nd, 3rd, 4th … 11th, 12th, 13th … 21st. */
export function ordinal(n: number) {
const teen = n % 100 >= 11 && n % 100 <= 13;
const suffix = teen ? 'th' : ({ 1: 'st', 2: 'nd', 3: 'rd' }[n % 10] ?? 'th');
return `${n}${suffix}`;
}

/**
* The one line under a person's name that says something about them, the
* first that applies, in sentence case. On an episode (`appearance` given:
* which of their episodes this one is, oldest first): a host's tally, a
* guest's first time, or which time this is and where they first appeared.
* Anywhere else: their role and tally.
*/
export function personFact({
appearance,
count,
first,
host
}: {
/** Which of their episodes this one is, oldest first (1-based). */
appearance?: number;
/** How many episodes they are on. */
count: number;
/** Their first episode, as the site labels it ("Ep 112"). */
first?: string;
host: boolean;
}) {
const episodes = `${count} ${count === 1 ? 'episode' : 'episodes'}`;
if (host || appearance === undefined) {
return `${host ? 'Host' : 'Guest'} · ${episodes}`;
}
if (count <= 1) {
return 'First time on the show';
}
if (appearance === 1) {
return `First of ${count} appearances`;
}
return first
? `${ordinal(appearance)} appearance · first on ${first}`
: `${ordinal(appearance)} appearance`;
}

/**
* Whether `personFact`'s line already says how many episodes they are on
* (a first time says it's one), so a tally beside it would repeat it.
*/
export function factGivesCount({
appearance,
count,
host
}: {
appearance?: number;
count: number;
host: boolean;
}) {
return host || appearance === undefined || count <= 1 || appearance === 1;
}
17 changes: 2 additions & 15 deletions packages/starpod/src/lib/cues.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { decodeEntities } from './entities';
import type { Episode } from './rss';
import {
fetchTranscriptText,
Expand Down Expand Up @@ -161,16 +160,6 @@ export function jsonSegments(data: { segments?: Array<JsonSegment> } | null): Ar
});
}

/** A VTT/SRT cue's text: voice and styling tags removed, references decoded. */
export function cueText(raw: string): { text: string; speaker?: string } {
// WebVTT voice spans: <v Speaker>text</v>
const voice = raw.match(/^<v(?:\.[^\s>]*)?\s+([^>]+)>/);
return {
text: decodeEntities(raw.replace(/<[^>]+>/g, '')).trim(),
speaker: voice ? decodeEntities(voice[1]).trim() : undefined
};
}

/**
* Timed lines from the feed's `<podcast:transcript>` (VTT/SRT cues or JSON
* segments). Returns `null` when none is available.
Expand Down Expand Up @@ -212,10 +201,8 @@ async function fetchRssCues(
const segments = jsonSegments(JSON.parse(download.raw));
return segments.length ? mergeIntoLines(segments) : null;
}
const cues = parseCues(download.raw).map((cue) => ({
start: cue.start,
...cueText(cue.text)
}));
// Plain text, with the speaker from any WebVTT voice span.
const cues = parseCues(download.raw);
return cues.length ? mergeIntoLines(cues) : null;
} catch {
return null;
Expand Down
13 changes: 10 additions & 3 deletions packages/starpod/src/lib/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ const NAMED: Record<string, string> = {
amp: '&',
apos: "'",
gt: '>',
lrm: '\u200e',
lt: '<',
nbsp: ' ',
quot: '"'
quot: '"',
rlm: '\u200f'
};

/**
Expand All @@ -14,13 +16,18 @@ const NAMED: Record<string, string> = {
*/
export function decodeEntities(value: string): string {
return value.replace(
/&(?:#(\d+)|#x([\da-f]+)|(amp|apos|gt|lt|nbsp|quot));/gi,
/&(?:#(\d+)|#x([\da-f]+)|(amp|apos|gt|lrm|lt|nbsp|quot|rlm));/gi,
(reference, decimal?: string, hex?: string, name?: string) => {
if (name) {
return NAMED[name.toLowerCase()];
}
const codePoint = decimal ? Number(decimal) : parseInt(hex!, 16);
return codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : reference;
return codePoint > 0 && codePoint <= 0x10ffff ? String.fromCodePoint(codePoint) : reference;
}
);
}

/** Escape `&`, `<` and `>` so text reads as text in HTML or markdown. */
export function escapeHtml(value: string): string {
return value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
Loading
Loading