@ckirby/opera-info
Advanced tools
Comparing version 1.3.3 to 1.3.4
import type { OperaData, TargetOpera } from "./types.js"; | ||
export declare function anonymize(hint: string, opera: Partial<TargetOpera> & OperaData): Promise<string>; | ||
export declare function anonymizeComposer(composer: string, target: string): string; | ||
export declare function makeTitlePattern(titles: string[], flags?: string): RegExp; | ||
export declare function anonymizeTitle(titles: string[], target: string, replacement?: string): string; | ||
export declare function capitalize(source: string, target: string): string; |
@@ -36,3 +36,3 @@ import re from "@ckirby/block-re"; | ||
} | ||
export function anonymizeTitle(titles, target, replacement = "this opera") { | ||
export function makeTitlePattern(titles, flags = "g") { | ||
const patterns = titles.map((title) => { | ||
@@ -42,4 +42,6 @@ const [first, ...rest] = title.split(/,\s+/); | ||
}); | ||
const composerPattern = RegExp(patterns.join("|"), "g"); | ||
target = target.replace(composerPattern, replacement); | ||
return RegExp(patterns.join("|"), flags); | ||
} | ||
export function anonymizeTitle(titles, target, replacement = "this opera") { | ||
target = target.replace(makeTitlePattern(titles), replacement); | ||
target = target.replace(re `/(?<=\b${replacement})'s?(?!\w)/`, "'s"); | ||
@@ -46,0 +48,0 @@ target = target.replace(re `/(?:an?|the)\sopera\s(?=${replacement})/`, ""); |
import sbd from "sbd"; | ||
import { listString } from "@ckirby/mr-lister"; | ||
import { anonymize, anonymizeComposer } from "./anonymizer.js"; | ||
import { anonymize, anonymizeComposer, makeTitlePattern, } from "./anonymizer.js"; | ||
export async function makeHints(opera) { | ||
@@ -45,7 +45,12 @@ const composerHints = await makeComposerHints(opera); | ||
} | ||
const extract = await anonymize(getText(div), opera); | ||
const sentences = sbd.sentences(capitalize(extract, "the composer"), { | ||
const titlePattern = makeTitlePattern(opera.titles); | ||
const titleAmongCaps = new RegExp(`[A-Z]\\w+ ${titlePattern.source}|${titlePattern.source} [A-Z]\\w+`); | ||
const sentences = sbd | ||
.sentences(getText(div), { | ||
newline_boundaries: true, | ||
}); | ||
return sentences | ||
}) | ||
.filter((s) => !titleAmongCaps.test(s)); | ||
const hints = await Promise.all(sentences.map((s) => anonymize(s, opera))); | ||
return hints | ||
.map((s) => s.replace(/^the\b/, "The")) | ||
.map((s) => s.replace(/^He\b/, "The composer")) | ||
@@ -60,7 +65,13 @@ .map((s) => s.replace(/^His\b/, "The composer's")); | ||
} | ||
const extract = (await anonymize(getText(div), opera)).replace(/, sometimes called this opera,/g, ""); | ||
const sentences = sbd.sentences(capitalize(extract, "this opera"), { | ||
const titlePattern = makeTitlePattern(opera.titles); | ||
const titleAmongCaps = new RegExp(`[A-Z]\\w+ ${titlePattern.source}|${titlePattern.source} [A-Z]\\w+`); | ||
const sentences = sbd | ||
.sentences(getText(div), { | ||
newline_boundaries: true, | ||
}); | ||
return sentences; | ||
}) | ||
.filter((s) => !titleAmongCaps.test(s)); | ||
const hints = await Promise.all(sentences.map((s) => anonymize(s, opera))); | ||
return hints | ||
.map((s) => s.replace(/^this\b/, "This")) | ||
.map((s) => s.replace(/, sometimes called this opera,/g, "")); | ||
} | ||
@@ -67,0 +78,0 @@ function getText(el) { |
{ | ||
"name": "@ckirby/opera-info", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"description": "information about operas", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4071206
28150