Considering scene release dates when resolving actor aliases in tool.
This commit is contained in:
+121
-3
@@ -63,7 +63,13 @@ async function fetchActorAliases(actorIds, sourceEntityIds = []) {
|
||||
|
||||
// alias_id is null when the scene credits the actor by their main name
|
||||
const sceneNames = await knex('releases_actors')
|
||||
.select('releases_actors.actor_id', 'releases_actors.alias_id', 'releases.entity_id', knex.raw('count(*)::integer as scenes'))
|
||||
.select(
|
||||
'releases_actors.actor_id',
|
||||
'releases_actors.alias_id',
|
||||
'releases.entity_id',
|
||||
knex.raw('count(*)::integer as scenes'),
|
||||
knex.raw('coalesce(array_agg(releases.date ORDER BY releases.date) FILTER (WHERE releases.date IS NOT NULL), \'{}\') as dates'),
|
||||
)
|
||||
.join('releases', 'releases.id', 'releases_actors.release_id')
|
||||
.whereIn('releases_actors.actor_id', aliasedActorIds)
|
||||
.groupBy('releases_actors.actor_id', 'releases_actors.alias_id', 'releases.entity_id');
|
||||
@@ -90,6 +96,7 @@ async function fetchActorAliases(actorIds, sourceEntityIds = []) {
|
||||
aliasId: sceneName.alias_id,
|
||||
lineage: lineages[sceneName.entity_id],
|
||||
scenes: sceneName.scenes,
|
||||
dates: sceneName.dates,
|
||||
})),
|
||||
}];
|
||||
}));
|
||||
@@ -103,7 +110,7 @@ async function fetchActorAliases(actorIds, sourceEntityIds = []) {
|
||||
*/
|
||||
function getEntityNames(actorAliases, sourceLineage) {
|
||||
const names = new Map();
|
||||
const getName = (aliasId) => names.get(aliasId) || names.set(aliasId, { aliasId, scenes: 0, scoped: false }).get(aliasId);
|
||||
const getName = (aliasId) => names.get(aliasId) || names.set(aliasId, { aliasId, scenes: 0, dates: [], scoped: false }).get(aliasId);
|
||||
|
||||
actorAliases.aliases.forEach((alias) => {
|
||||
if (isRelatedEntity(alias.lineage, sourceLineage)) {
|
||||
@@ -113,16 +120,127 @@ function getEntityNames(actorAliases, sourceLineage) {
|
||||
|
||||
actorAliases.sceneNames.forEach((sceneName) => {
|
||||
if (isRelatedEntity(sceneName.lineage, sourceLineage)) {
|
||||
getName(sceneName.aliasId).scenes += sceneName.scenes;
|
||||
const name = getName(sceneName.aliasId);
|
||||
|
||||
name.scenes += sceneName.scenes;
|
||||
name.dates = name.dates.concat(sceneName.dates);
|
||||
}
|
||||
});
|
||||
|
||||
return Array.from(names.values()).sort((nameA, nameB) => (nameB.scenes - nameA.scenes) || (nameB.scoped - nameA.scoped));
|
||||
}
|
||||
|
||||
// names spelled the same way are the same name, e.g. LaSirena69 and Lasirena69, whether different scoped aliases or the main name
|
||||
function groupEntityNames(entityNames, mainSlug, aliasesById) {
|
||||
return Object.values(entityNames.reduce((acc, entityName) => {
|
||||
const slug = entityName.aliasId === null ? mainSlug : aliasesById[entityName.aliasId].slug;
|
||||
const group = acc[slug] || { slug, scenes: 0, dates: [], scoped: false, names: [] };
|
||||
|
||||
group.scenes += entityName.scenes;
|
||||
group.dates = group.dates.concat(entityName.dates);
|
||||
group.scoped = group.scoped || entityName.scoped;
|
||||
group.names.push(entityName); // entity names are sorted best first, so is each group
|
||||
|
||||
return { ...acc, [slug]: group };
|
||||
}, {}))
|
||||
.map((group) => ({
|
||||
...group,
|
||||
dates: group.dates.map((date) => new Date(date)).sort((dateA, dateB) => dateA - dateB),
|
||||
}))
|
||||
.sort((groupA, groupB) => (groupB.scenes - groupA.scenes) || (groupB.scoped - groupA.scoped));
|
||||
}
|
||||
|
||||
const dayMs = 24 * 60 * 60 * 1000;
|
||||
|
||||
/*
|
||||
* one name took over from all others, which were retired
|
||||
* the newest name must have been used on its own for long enough since each other name was last used
|
||||
* names used interchangeably keep getting used until recently, so there's no cut-off
|
||||
*/
|
||||
function getRetiredCutoff(nameGroups, { retiredScenes, retiredDays }) {
|
||||
const datedGroups = nameGroups.filter((group) => group.scenes > 0);
|
||||
|
||||
// a name without dated scenes can't be placed in time
|
||||
if (datedGroups.length < 2 || datedGroups.some((group) => group.dates.length === 0)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const lastDate = (group) => group.dates.at(-1);
|
||||
const [current, ...others] = datedGroups.toSorted((groupA, groupB) => lastDate(groupB) - lastDate(groupA));
|
||||
|
||||
const retirements = others.map((group) => ({
|
||||
group,
|
||||
lastUsed: lastDate(group),
|
||||
scenesSince: current.dates.filter((date) => date > lastDate(group)).length,
|
||||
daysSince: Math.floor((lastDate(current) - lastDate(group)) / dayMs),
|
||||
}));
|
||||
|
||||
if (retirements.some((retirement) => retirement.scenesSince < retiredScenes || retirement.daysSince < retiredDays)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { group: current, retirements };
|
||||
}
|
||||
|
||||
function normalizeName(value) {
|
||||
return value.toLowerCase().replace(/[^a-z0-9]/g, '');
|
||||
}
|
||||
|
||||
// the URL contains exactly one of the names, ignoring a name contained in a longer matching name, e.g. Kenia in Kenia Music
|
||||
function getUrlMatch(nameGroups, url) {
|
||||
if (!url) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const normalizedUrl = normalizeName(url.replace(/^\w+:\/\//, ''));
|
||||
const matches = nameGroups.filter((group) => normalizedUrl.includes(normalizeName(group.slug)));
|
||||
const longestMatches = matches.filter((match) => !matches.some((otherMatch) => otherMatch !== match && normalizeName(otherMatch.slug).includes(normalizeName(match.slug))));
|
||||
|
||||
return longestMatches.length === 1 ? longestMatches[0] : null;
|
||||
}
|
||||
|
||||
/*
|
||||
* the name an entity uses for an actor, in order of evidence:
|
||||
* 1. a cut-off, the other names were retired
|
||||
* 2. the profile URL contains the name
|
||||
* 3. the name dominates the scene credits
|
||||
* returns { group, reason, detail } or null when it's ambiguous
|
||||
*/
|
||||
function resolveEntityName(nameGroups, { url, dominance = 10, retiredScenes = 3, retiredDays = 180 } = {}) {
|
||||
if (nameGroups.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (nameGroups.length === 1) {
|
||||
return { group: nameGroups[0], reason: 'only' };
|
||||
}
|
||||
|
||||
const cutoff = getRetiredCutoff(nameGroups, { retiredScenes, retiredDays });
|
||||
|
||||
if (cutoff) {
|
||||
return { group: cutoff.group, reason: 'cutoff', detail: cutoff.retirements };
|
||||
}
|
||||
|
||||
const urlMatch = getUrlMatch(nameGroups, url);
|
||||
|
||||
if (urlMatch) {
|
||||
return { group: urlMatch, reason: 'url' };
|
||||
}
|
||||
|
||||
const [topGroup, nextGroup] = nameGroups;
|
||||
|
||||
if (topGroup.scenes > 0 && topGroup.scenes >= nextGroup.scenes * dominance) {
|
||||
return { group: topGroup, reason: 'dominance' };
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchActorAliases,
|
||||
fetchEntityLineages,
|
||||
getEntityNames,
|
||||
groupEntityNames,
|
||||
resolveEntityName,
|
||||
isRelatedEntity,
|
||||
};
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
/*
|
||||
* backfills actors_profiles.alias_id, the alias name a profile was found under
|
||||
* 1. profiles still held by an alias actor move to its main actor, unless the main actor already has a profile for that entity
|
||||
* 2. main actor profiles get the alias their entity is known to use, when it's the only name the entity uses, or it clearly dominates
|
||||
* names are compared by slug, e.g. LaSirena69 and Lasirena69 are the same name, whether as different scoped aliases or the main name
|
||||
* 2. main actor profiles get the name their entity uses, compared by slug so e.g. LaSirena69 and Lasirena69 are one name
|
||||
* decided by a cut-off where the other names were retired, else the profile URL containing the name, else a dominant share of scene credits
|
||||
* dry run by default, --apply to write
|
||||
*/
|
||||
|
||||
@@ -12,7 +12,7 @@ const yargs = require('yargs');
|
||||
const { hideBin } = require('yargs/helpers');
|
||||
|
||||
const knex = require('../knex');
|
||||
const { fetchActorAliases, getEntityNames } = require('../actor-aliases');
|
||||
const { fetchActorAliases, getEntityNames, groupEntityNames, resolveEntityName } = require('../actor-aliases');
|
||||
|
||||
const args = yargs(hideBin(process.argv))
|
||||
.option('apply', {
|
||||
@@ -25,6 +25,16 @@ const args = yargs(hideBin(process.argv))
|
||||
default: 10,
|
||||
describe: 'How many times more scenes a name needs than the next to be considered the name the entity uses',
|
||||
})
|
||||
.option('retired-scenes', {
|
||||
type: 'number',
|
||||
default: 3,
|
||||
describe: 'How many scenes must use the newest name since another name was last used for it to count as retired',
|
||||
})
|
||||
.option('retired-days', {
|
||||
type: 'number',
|
||||
default: 180,
|
||||
describe: 'How many days the newest name must be used since another name was last used for it to count as retired',
|
||||
})
|
||||
.option('verbose-list', {
|
||||
alias: 'list',
|
||||
type: 'boolean',
|
||||
@@ -94,7 +104,7 @@ async function assignAliases() {
|
||||
.pluck('alias_for');
|
||||
|
||||
const profiles = await knex('actors_profiles')
|
||||
.select('actors_profiles.id', 'actors_profiles.actor_id', 'actors_profiles.entity_id', 'actors.name as actor_name', 'actors.slug as actor_slug', 'entities.slug as entity_slug')
|
||||
.select('actors_profiles.id', 'actors_profiles.actor_id', 'actors_profiles.entity_id', 'actors_profiles.url', 'actors.name as actor_name', 'actors.slug as actor_slug', 'entities.slug as entity_slug')
|
||||
.join('actors', 'actors.id', 'actors_profiles.actor_id')
|
||||
.leftJoin('entities', 'entities.id', 'actors_profiles.entity_id')
|
||||
.whereIn('actors_profiles.actor_id', aliasedActorIds)
|
||||
@@ -113,33 +123,47 @@ async function assignAliases() {
|
||||
return { profile, outcome: 'unknown' };
|
||||
}
|
||||
|
||||
// entity names are sorted best first, so the first alias in a group is its best
|
||||
const nameGroups = Object.values(entityNames.reduce((acc, entityName) => {
|
||||
const slug = entityName.aliasId === null ? profile.actor_slug : aliasesById[entityName.aliasId].slug;
|
||||
const group = acc[slug] || { slug, scenes: 0, scoped: false, names: [] };
|
||||
const nameGroups = groupEntityNames(entityNames, profile.actor_slug, aliasesById);
|
||||
|
||||
group.scenes += entityName.scenes;
|
||||
group.scoped = group.scoped || entityName.scoped;
|
||||
group.names.push(entityName);
|
||||
const resolution = resolveEntityName(nameGroups, {
|
||||
url: profile.url,
|
||||
dominance: args.dominance,
|
||||
retiredScenes: args.retiredScenes,
|
||||
retiredDays: args.retiredDays,
|
||||
});
|
||||
|
||||
return { ...acc, [slug]: group };
|
||||
}, {})).sort((groupA, groupB) => (groupB.scenes - groupA.scenes) || (groupB.scoped - groupA.scoped));
|
||||
|
||||
const [topGroup, nextGroup] = nameGroups;
|
||||
const isDecisive = !nextGroup || (topGroup.scenes > 0 && topGroup.scenes >= nextGroup.scenes * args.dominance);
|
||||
|
||||
if (!isDecisive) {
|
||||
if (!resolution) {
|
||||
return { profile, outcome: 'ambiguous', nameGroups };
|
||||
}
|
||||
|
||||
// the main name, or an alias spelled the same way
|
||||
if (topGroup.slug === profile.actor_slug) {
|
||||
return { profile, outcome: 'main' };
|
||||
if (resolution.group.slug === profile.actor_slug) {
|
||||
return { profile, outcome: 'main', resolution };
|
||||
}
|
||||
|
||||
return { profile, outcome: 'assign', aliasId: topGroup.names.find((entityName) => entityName.aliasId !== null).aliasId };
|
||||
return {
|
||||
profile,
|
||||
outcome: 'assign',
|
||||
resolution,
|
||||
aliasId: resolution.group.names.find((entityName) => entityName.aliasId !== null).aliasId,
|
||||
};
|
||||
});
|
||||
|
||||
const getGroupName = (group, profile) => (group.names[0].aliasId === null ? profile.actor_name : aliasesById[group.names[0].aliasId].name);
|
||||
const formatDate = (date) => date.toISOString().slice(0, 7);
|
||||
|
||||
const getReason = ({ profile, resolution }) => {
|
||||
if (resolution.reason === 'cutoff') {
|
||||
return `retired ${resolution.detail.map((retirement) => `${getGroupName(retirement.group, profile)} after ${formatDate(retirement.lastUsed)}, ${retirement.scenesSince} scenes since`).join('; ')}`;
|
||||
}
|
||||
|
||||
if (resolution.reason === 'url') {
|
||||
return `URL ${profile.url}`;
|
||||
}
|
||||
|
||||
return resolution.reason;
|
||||
};
|
||||
|
||||
const byOutcome = (outcome) => outcomes.filter((entry) => entry.outcome === outcome);
|
||||
|
||||
console.log(`Step 2: ${profiles.length} profiles of actors with aliases`);
|
||||
@@ -148,20 +172,31 @@ async function assignAliases() {
|
||||
console.log(` ${byOutcome('ambiguous').length} ambiguous, the entity uses several names`);
|
||||
console.log(` ${byOutcome('unknown').length} without evidence`);
|
||||
|
||||
byOutcome('assign').forEach(({ profile, aliasId }) => console.log(` assign ${getProfileLabel(profile)}, as '${aliasesById[aliasId].name}'`));
|
||||
const reasonCounts = outcomes
|
||||
.filter((entry) => entry.resolution)
|
||||
.reduce((acc, entry) => ({ ...acc, [entry.resolution.reason]: (acc[entry.resolution.reason] || 0) + 1 }), {});
|
||||
|
||||
console.log(` decided by: ${Object.entries(reasonCounts).map(([reason, count]) => `${count} ${reason}`).join(', ')}`);
|
||||
|
||||
byOutcome('assign').forEach((entry) => console.log(` assign ${getProfileLabel(entry.profile)}, as '${aliasesById[entry.aliasId].name}' (${getReason(entry)})`));
|
||||
|
||||
// only main name decisions that overruled another name are worth reviewing
|
||||
byOutcome('main')
|
||||
.filter((entry) => entry.resolution.reason !== 'only')
|
||||
.forEach((entry) => console.log(` main ${getProfileLabel(entry.profile)} (${getReason(entry)})`));
|
||||
|
||||
byOutcome('ambiguous').forEach(({ profile, nameGroups }) => {
|
||||
const names = nameGroups.map((group) => {
|
||||
const name = group.names[0].aliasId === null ? profile.actor_name : aliasesById[group.names[0].aliasId].name;
|
||||
const dated = group.dates.length > 0 ? `, ${formatDate(group.dates[0])} to ${formatDate(group.dates.at(-1))}` : '';
|
||||
|
||||
return `${name} (${group.scenes} scenes${group.scoped ? ', scoped' : ''})`;
|
||||
return `${getGroupName(group, profile)} (${group.scenes} scenes${dated}${group.scoped ? ', scoped' : ''})`;
|
||||
});
|
||||
|
||||
console.log(` skip ${getProfileLabel(profile)}, ambiguous: ${names.join(', ')}`);
|
||||
});
|
||||
|
||||
if (args.list) {
|
||||
byOutcome('main').forEach(({ profile }) => console.log(` main ${getProfileLabel(profile)}`));
|
||||
byOutcome('main').filter((entry) => entry.resolution.reason === 'only').forEach(({ profile }) => console.log(` main ${getProfileLabel(profile)} (only)`));
|
||||
byOutcome('unknown').forEach(({ profile }) => console.log(` unknown ${getProfileLabel(profile)}`));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user