accuracy-claim-kit 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/METHOD.md +99 -0
- package/README.md +478 -2
- package/dist/agreement.d.ts +62 -0
- package/dist/agreement.js +141 -0
- package/dist/agreement.js.map +1 -0
- package/dist/assess.d.ts +36 -0
- package/dist/assess.js +954 -0
- package/dist/assess.js.map +1 -0
- package/dist/census.d.ts +44 -0
- package/dist/census.js +95 -0
- package/dist/census.js.map +1 -0
- package/dist/dates.d.ts +2 -0
- package/dist/dates.js +23 -0
- package/dist/dates.js.map +1 -0
- package/dist/display.d.ts +56 -0
- package/dist/display.js +341 -0
- package/dist/display.js.map +1 -0
- package/dist/exact.d.ts +48 -0
- package/dist/exact.js +102 -0
- package/dist/exact.js.map +1 -0
- package/dist/format.d.ts +29 -0
- package/dist/format.js +120 -0
- package/dist/format.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/normal.d.ts +12 -0
- package/dist/normal.js +100 -0
- package/dist/normal.js.map +1 -0
- package/dist/text.d.ts +58 -0
- package/dist/text.js +202 -0
- package/dist/text.js.map +1 -0
- package/dist/types.d.ts +186 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +44 -0
- package/dist/validate.js +156 -0
- package/dist/validate.js.map +1 -0
- package/dist/wilson.d.ts +41 -0
- package/dist/wilson.js +86 -0
- package/dist/wilson.js.map +1 -0
- package/package.json +76 -4
package/dist/format.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { shortestWithinNoise, valueDigits } from './census.js';
|
|
2
|
+
import { splitDouble } from './exact.js';
|
|
3
|
+
// Number formatting for statements. Bounds are rounded in the direction that
|
|
4
|
+
// never makes the evidence look better than it is: a lower limit is floored,
|
|
5
|
+
// an upper limit is ceiled. The rounding is exact: the double is split into its
|
|
6
|
+
// integer mantissa and power of two and the scaled value is floored or ceiled
|
|
7
|
+
// with BigInt arithmetic, so a printed figure can never sit on the wrong side
|
|
8
|
+
// of the number it describes (no fixed slack, no floating-point multiply).
|
|
9
|
+
/** floor or ceil of x * 100 * 10^decimals for a finite x >= 0, exactly. */
|
|
10
|
+
function scaledUnits(x, decimals, mode) {
|
|
11
|
+
const { mantissa, exponent } = splitDouble(x);
|
|
12
|
+
const numerator = mantissa * 100n * 10n ** BigInt(decimals);
|
|
13
|
+
if (exponent >= 0)
|
|
14
|
+
return numerator << BigInt(exponent);
|
|
15
|
+
const denominator = 1n << BigInt(-exponent);
|
|
16
|
+
const quotient = numerator / denominator;
|
|
17
|
+
return mode === 'ceil' && numerator % denominator !== 0n ? quotient + 1n : quotient;
|
|
18
|
+
}
|
|
19
|
+
function render(units, decimals) {
|
|
20
|
+
if (decimals === 0)
|
|
21
|
+
return `${String(units)}%`;
|
|
22
|
+
const scale = 10n ** BigInt(decimals);
|
|
23
|
+
const whole = units / scale;
|
|
24
|
+
const fraction = String(units % scale).padStart(decimals, '0');
|
|
25
|
+
return `${String(whole)}.${fraction}%`;
|
|
26
|
+
}
|
|
27
|
+
function checkProportion(x, decimals) {
|
|
28
|
+
if (typeof x !== 'number' || !Number.isFinite(x) || x < 0 || x > 1) {
|
|
29
|
+
throw new RangeError(`proportion must be a finite number from 0 to 1, got ${String(x)}`);
|
|
30
|
+
}
|
|
31
|
+
if (!Number.isInteger(decimals) || decimals < 0 || decimals > 20) {
|
|
32
|
+
throw new RangeError('decimals must be a whole number from 0 to 20');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/** A proportion as a percent, rounded down to `decimals` places. */
|
|
36
|
+
export function pctFloor(proportion, decimals) {
|
|
37
|
+
checkProportion(proportion, decimals);
|
|
38
|
+
return render(scaledUnits(proportion, decimals, 'floor'), decimals);
|
|
39
|
+
}
|
|
40
|
+
/** A proportion as a percent, rounded up to `decimals` places. */
|
|
41
|
+
export function pctCeil(proportion, decimals) {
|
|
42
|
+
checkProportion(proportion, decimals);
|
|
43
|
+
return render(scaledUnits(proportion, decimals, 'ceil'), decimals);
|
|
44
|
+
}
|
|
45
|
+
/** Nearest, for figures that are not bounds (the claim itself, a width). */
|
|
46
|
+
export function pctNearest(proportion, decimals) {
|
|
47
|
+
return `${(proportion * 100).toFixed(decimals)}%`;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A number to `decimals` places, cut in a chosen direction instead of rounded to
|
|
51
|
+
* the nearest, from the digits it is written with (0.57 is 0.57, not 0.5699...):
|
|
52
|
+
* 'down' never shows more than the number (0.9996 is 0.99, -0.456 is -0.46),
|
|
53
|
+
* 'up' never shows less (a required minimum of 0.8004 is 0.801).
|
|
54
|
+
*/
|
|
55
|
+
export function decimalCut(x, decimals, direction) {
|
|
56
|
+
const negative = x < 0;
|
|
57
|
+
const { digits, scale } = valueDigits(Math.abs(x));
|
|
58
|
+
let units;
|
|
59
|
+
if (scale <= decimals) {
|
|
60
|
+
units = digits * 10n ** BigInt(decimals - scale);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
const unit = 10n ** BigInt(scale - decimals);
|
|
64
|
+
const rest = digits % unit;
|
|
65
|
+
// the size of a negative number grows when it is cut down, a positive one when it is cut up
|
|
66
|
+
const grows = negative ? direction === 'down' : direction === 'up';
|
|
67
|
+
units = digits / unit + (grows && rest !== 0n ? 1n : 0n);
|
|
68
|
+
}
|
|
69
|
+
const whole = units / 10n ** BigInt(decimals);
|
|
70
|
+
const fraction = decimals === 0 ? '' : `.${String(units % 10n ** BigInt(decimals)).padStart(decimals, '0')}`;
|
|
71
|
+
return `${negative && units !== 0n ? '-' : ''}${String(whole)}${fraction}`;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A confidence level as a percent label, rounded down to at most 9 decimals
|
|
75
|
+
* and never 100%. It works on the shortest decimal text of the number within
|
|
76
|
+
* float noise (0.95 reads "95%", not "94.999999999%", and 1 - 0.07 reads "93%"),
|
|
77
|
+
* so a level a person typed or computed keeps the digits they meant.
|
|
78
|
+
*/
|
|
79
|
+
export function confidenceLabel(confidence) {
|
|
80
|
+
// 1 - 0.07 is 0.9299999999999999: the level a person wrote, 93%, is the shortest decimal within
|
|
81
|
+
// float noise of it. A level that noise would carry up to 1 keeps its own digits (never 100%).
|
|
82
|
+
const nearest = shortestWithinNoise(confidence);
|
|
83
|
+
let text = Number(nearest) < 1 ? nearest : String(confidence);
|
|
84
|
+
const at = text.indexOf('e');
|
|
85
|
+
if (at >= 0) {
|
|
86
|
+
// 1.5e-7 is 0.00000015: expand the digits the number is written with, never the binary expansion
|
|
87
|
+
const exponent = Number(text.slice(at + 1));
|
|
88
|
+
const mantissa = text.slice(0, at).replace('.', '');
|
|
89
|
+
text = `0.${'0'.repeat(Math.max(0, -exponent - 1))}${mantissa}`;
|
|
90
|
+
}
|
|
91
|
+
const digits = text.slice(2); // after "0."
|
|
92
|
+
const whole = Number(digits.slice(0, 2).padEnd(2, '0')); // percent, integer part
|
|
93
|
+
const fraction = digits.slice(2, 11).replace(/0+$/, '');
|
|
94
|
+
if (whole === 0 && fraction === '')
|
|
95
|
+
return 'under 0.000000001%';
|
|
96
|
+
return `${String(whole)}${fraction === '' ? '' : `.${fraction}`}%`;
|
|
97
|
+
}
|
|
98
|
+
const LEVELS = [1, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 1e-7, 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15];
|
|
99
|
+
/**
|
|
100
|
+
* Most decimals a percent figure can print before its last digit is finer
|
|
101
|
+
* than one tenth of the interval width. A figure with D decimals has a last
|
|
102
|
+
* digit worth 10^-D percentage points; it is supported while
|
|
103
|
+
* 10^-D >= (width in percentage points) / 10. Whole-percent figures (0
|
|
104
|
+
* decimals) are always allowed. `width` is the interval width as a
|
|
105
|
+
* proportion (high - low).
|
|
106
|
+
*/
|
|
107
|
+
export function maxDisplayDecimals(width) {
|
|
108
|
+
if (typeof width !== 'number')
|
|
109
|
+
throw new TypeError('width must be a number');
|
|
110
|
+
if (!Number.isFinite(width) || width < 0 || width > 1) {
|
|
111
|
+
throw new RangeError(`width must be a finite number from 0 to 1, got ${String(width)}`);
|
|
112
|
+
}
|
|
113
|
+
const unitFloor = (width * 100) / 10;
|
|
114
|
+
let d = 0;
|
|
115
|
+
// A relative slack of 1e-12 makes "exactly one tenth" count as supported.
|
|
116
|
+
while (d + 1 < LEVELS.length && LEVELS[d + 1] >= unitFloor * (1 - 1e-12))
|
|
117
|
+
d += 1;
|
|
118
|
+
return d;
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=format.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.js","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAExC,6EAA6E;AAC7E,6EAA6E;AAC7E,gFAAgF;AAChF,8EAA8E;AAC9E,8EAA8E;AAC9E,2EAA2E;AAE3E,2EAA2E;AAC3E,SAAS,WAAW,CAAC,CAAS,EAAE,QAAgB,EAAE,IAAsB;IACtE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;IAC7C,MAAM,SAAS,GAAG,QAAQ,GAAG,IAAI,GAAG,GAAG,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAA;IAC3D,IAAI,QAAQ,IAAI,CAAC;QAAE,OAAO,SAAS,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAA;IACvD,MAAM,WAAW,GAAG,EAAE,IAAI,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAA;IAC3C,MAAM,QAAQ,GAAG,SAAS,GAAG,WAAW,CAAA;IACxC,OAAO,IAAI,KA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G,GAAG,CAAC,GAAG,EAAE,CAAA;IACpC,IAAI,CAAC,GAAG,CAAC,CAAA;IACT,0EAA0E;IAC1E,OAAO,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,IAAK,MAAM,CAAC,CAAC,GAAG,CAAC,CAAY,IAAI,SAAS,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;QAAE,CAAC,IAAI,CAAC,CAAA;IAC5F,OAAO,CAAC,CAAA;AACV,CAAC","sourcesContent":["import { shortestWithinNoise, valueDigits } from './census.js'\nimport { splitDouble } from './exact.js'\n\n// Number formatting for statements. Bounds are rounded in the direction that\n// never makes the evidence look better than it is: a lower limit is floored,\n// an upper limit is ceiled. The rounding is exact: the double is split into its\n// integer mantissa and power of two and the scaled value is floored or ceiled\n// with BigInt arithmetic, so a printed figure can never sit on the wrong side\n// of the number it describes (no fixed slack, no floating-point multiply).\n\n/** floor or ceil of x * 100 * 10^decimals for a finite x >= 0, exactly. */\nfunction scaledUnits(x: number, decimals: number, mode: 'floor' | 'ceil'): bigint {\n const { mantissa, exponent } = splitDouble(x)\n const numerator = mantissa * 100n * 10n ** BigInt(decimals)\n if (exponent >= 0) return numerator << BigInt(exponent)\n const denominator = 1n << BigInt(-exponent)\n const quotient = numerator / denominator\n return mode === 'ceil' && numerator % denominator !== 0n ? quotient + 1n : quotient\n}\n\nfunction render(units: bigint, decimals: number): string {\n if (decimals === 0) return `${String(units)}%`\n const scale = 10n ** BigInt(decimals)\n const whole = units / scale\n const fraction = String(units % scale).padStart(decimals, '0')\n return `${String(whole)}.${fraction}%`\n}\n\nfunction checkProportion(x: number, decimals: number): void {\n if (typeof x !== 'number' || !Number.isFinite(x) || x < 0 || x > 1) {\n throw new RangeError(`proportion must be a finite number from 0 to 1, got ${String(x)}`)\n }\n if (!Number.isInteger(decimals) || decimals < 0 || decimals > 20) {\n throw new RangeError('decimals must be a whole number from 0 to 20')\n }\n}\n\n/** A proportion as a percent, rounded down to `decimals` places. */\nexport function pctFloor(proportion: number, decimals: number): string {\n checkProportion(proportion, decimals)\n return render(scaledUnits(proportion, decimals, 'floor'), decimals)\n}\n\n/** A proportion as a percent, rounded up to `decimals` places. */\nexport function pctCeil(proportion: number, decimals: number): string {\n checkProportion(proportion, decimals)\n return render(scaledUnits(proportion, decimals, 'ceil'), decimals)\n}\n\n/** Nearest, for figures that are not bounds (the claim itself, a width). */\nexport function pctNearest(proportion: number, decimals: number): string {\n return `${(proportion * 100).toFixed(decimals)}%`\n}\n\n/**\n * A number to `decimals` places, cut in a chosen direction instead of rounded to\n * the nearest, from the digits it is written with (0.57 is 0.57, not 0.5699...):\n * 'down' never shows more than the number (0.9996 is 0.99, -0.456 is -0.46),\n * 'up' never shows less (a required minimum of 0.8004 is 0.801).\n */\nexport function decimalCut(x: number, decimals: number, direction: 'down' | 'up'): string {\n const negative = x < 0\n const { digits, scale } = valueDigits(Math.abs(x))\n let units: bigint\n if (scale <= decimals) {\n units = digits * 10n ** BigInt(decimals - scale)\n } else {\n const unit = 10n ** BigInt(scale - decimals)\n const rest = digits % unit\n // the size of a negative number grows when it is cut down, a positive one when it is cut up\n const grows = negative ? direction === 'down' : direction === 'up'\n units = digits / unit + (grows && rest !== 0n ? 1n : 0n)\n }\n const whole = units / 10n ** BigInt(decimals)\n const fraction = decimals === 0 ? '' : `.${String(units % 10n ** BigInt(decimals)).padStart(decimals, '0')}`\n return `${negative && units !== 0n ? '-' : ''}${String(whole)}${fraction}`\n}\n\n/**\n * A confidence level as a percent label, rounded down to at most 9 decimals\n * and never 100%. It works on the shortest decimal text of the number within\n * float noise (0.95 reads \"95%\", not \"94.999999999%\", and 1 - 0.07 reads \"93%\"),\n * so a level a person typed or computed keeps the digits they meant.\n */\nexport function confidenceLabel(confidence: number): string {\n // 1 - 0.07 is 0.9299999999999999: the level a person wrote, 93%, is the shortest decimal within\n // float noise of it. A level that noise would carry up to 1 keeps its own digits (never 100%).\n const nearest = shortestWithinNoise(confidence)\n let text = Number(nearest) < 1 ? nearest : String(confidence)\n const at = text.indexOf('e')\n if (at >= 0) {\n // 1.5e-7 is 0.00000015: expand the digits the number is written with, never the binary expansion\n const exponent = Number(text.slice(at + 1))\n const mantissa = text.slice(0, at).replace('.', '')\n text = `0.${'0'.repeat(Math.max(0, -exponent - 1))}${mantissa}`\n }\n const digits = text.slice(2) // after \"0.\"\n const whole = Number(digits.slice(0, 2).padEnd(2, '0')) // percent, integer part\n const fraction = digits.slice(2, 11).replace(/0+$/, '')\n if (whole === 0 && fraction === '') return 'under 0.000000001%'\n return `${String(whole)}${fraction === '' ? '' : `.${fraction}`}%`\n}\n\nconst LEVELS = [1, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 1e-7, 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15]\n\n/**\n * Most decimals a percent figure can print before its last digit is finer\n * than one tenth of the interval width. A figure with D decimals has a last\n * digit worth 10^-D percentage points; it is supported while\n * 10^-D >= (width in percentage points) / 10. Whole-percent figures (0\n * decimals) are always allowed. `width` is the interval width as a\n * proportion (high - low).\n */\nexport function maxDisplayDecimals(width: number): number {\n if (typeof width !== 'number') throw new TypeError('width must be a number')\n if (!Number.isFinite(width) || width < 0 || width > 1) {\n throw new RangeError(`width must be a finite number from 0 to 1, got ${String(width)}`)\n }\n const unitFloor = (width * 100) / 10\n let d = 0\n // A relative slack of 1e-12 makes \"exactly one tenth\" count as supported.\n while (d + 1 < LEVELS.length && (LEVELS[d + 1] as number) >= unitFloor * (1 - 1e-12)) d += 1\n return d\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { wilsonInterval, zForConfidence, DEFAULT_CONFIDENCE } from './wilson.js';
|
|
2
|
+
export type { WilsonInterval, WilsonOptions } from './wilson.js';
|
|
3
|
+
export { percentAgreement, cohenKappa, gwetAC1 } from './agreement.js';
|
|
4
|
+
export type { AgreementResult, AgreementStatistic, DefinedAgreement, GwetOptions, Label, UndefinedAgreement, UndefinedAgreementReason, } from './agreement.js';
|
|
5
|
+
export { assessAccuracyClaim, DEFAULT_MIN_N, COMPARISON_TOLERANCE } from './assess.js';
|
|
6
|
+
export { maxDisplayDecimals } from './format.js';
|
|
7
|
+
export type { AccuracyAssessment, AccuracyClaimInput, AccuracyVerdict, AgreementInput, AssessOptions, Benchmark, Claim, ClaimMetric, GateChecks, GateStatus, LabelProvenance, LabelSource, PopulationScope, ReasonCode, Retractions, SampleCounts, SamplingMethod, Stratum, UnsupportedMetric, } from './types.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { wilsonInterval, zForConfidence, DEFAULT_CONFIDENCE } from './wilson.js';
|
|
2
|
+
export { percentAgreement, cohenKappa, gwetAC1 } from './agreement.js';
|
|
3
|
+
export { assessAccuracyClaim, DEFAULT_MIN_N, COMPARISON_TOLERANCE } from './assess.js';
|
|
4
|
+
export { maxDisplayDecimals } from './format.js';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAEhF,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAA;AAUtE,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AACtF,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA","sourcesContent":["export { wilsonInterval, zForConfidence, DEFAULT_CONFIDENCE } from './wilson.js'\nexport type { WilsonInterval, WilsonOptions } from './wilson.js'\nexport { percentAgreement, cohenKappa, gwetAC1 } from './agreement.js'\nexport type {\n AgreementResult,\n AgreementStatistic,\n DefinedAgreement,\n GwetOptions,\n Label,\n UndefinedAgreement,\n UndefinedAgreementReason,\n} from './agreement.js'\nexport { assessAccuracyClaim, DEFAULT_MIN_N, COMPARISON_TOLERANCE } from './assess.js'\nexport { maxDisplayDecimals } from './format.js'\nexport type {\n AccuracyAssessment,\n AccuracyClaimInput,\n AccuracyVerdict,\n AgreementInput,\n AssessOptions,\n Benchmark,\n Claim,\n ClaimMetric,\n GateChecks,\n GateStatus,\n LabelProvenance,\n LabelSource,\n PopulationScope,\n ReasonCode,\n Retractions,\n SampleCounts,\n SamplingMethod,\n Stratum,\n UnsupportedMetric,\n} from './types.js'\n"]}
|
package/dist/normal.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x with Phi(x) = 0.5 + q, for |q| <= 0.47575, from the displacement q itself.
|
|
3
|
+
* Taking q directly (not p = 0.5 + q) matters: for tiny q the sum 0.5 + q
|
|
4
|
+
* rounds away every digit of q. The refinement compares 0.5 * erf(x / sqrt 2)
|
|
5
|
+
* with q, so both sides keep full relative accuracy.
|
|
6
|
+
*/
|
|
7
|
+
export declare function centralQuantile(q: number): number;
|
|
8
|
+
/**
|
|
9
|
+
* Inverse standard normal CDF: the x with P(Z <= x) = p.
|
|
10
|
+
* Throws TypeError if p is not a number and RangeError unless 0 < p < 1.
|
|
11
|
+
*/
|
|
12
|
+
export declare function normalQuantile(p: number): number;
|
package/dist/normal.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Inverse of the standard normal CDF. Acklam's rational approximation
|
|
2
|
+
// (relative error about 1.15e-9) followed by one Halley refinement step that
|
|
3
|
+
// evaluates the CDF through erf / erfc written out below, so the result is
|
|
4
|
+
// accurate to a few units of 1e-16 across the whole open interval (0, 1).
|
|
5
|
+
// Below about 1e-300 the exp(x^2/2) factor of the refinement overflows; there
|
|
6
|
+
// the unrefined approximation (about 1e-9 relative) is returned.
|
|
7
|
+
const A = [
|
|
8
|
+
-3.969683028665376e1, 2.209460984245205e2, -2.759285104469687e2,
|
|
9
|
+
1.38357751867269e2, -3.066479806614716e1, 2.506628277459239,
|
|
10
|
+
];
|
|
11
|
+
const B = [
|
|
12
|
+
-5.447609879822406e1, 1.615858368580409e2, -1.556989798598866e2,
|
|
13
|
+
6.680131188771972e1, -1.328068155288572e1,
|
|
14
|
+
];
|
|
15
|
+
const C = [
|
|
16
|
+
-7.784894002430293e-3, -3.223964580411365e-1, -2.400758277161838,
|
|
17
|
+
-2.549732539343734, 4.374664141464968, 2.938163982698783,
|
|
18
|
+
];
|
|
19
|
+
const D = [
|
|
20
|
+
7.784695709041462e-3, 3.224671290700398e-1, 2.445134137142996, 3.754408661907416,
|
|
21
|
+
];
|
|
22
|
+
const P_LOW = 0.02425;
|
|
23
|
+
const SQRT_PI = Math.sqrt(Math.PI);
|
|
24
|
+
const SQRT_2 = Math.SQRT2;
|
|
25
|
+
const SQRT_2PI = Math.sqrt(2 * Math.PI);
|
|
26
|
+
/** erf(y) for 0 <= y <= about 5, from the all-positive series (no cancellation). */
|
|
27
|
+
function erfSeries(y) {
|
|
28
|
+
const y2 = y * y;
|
|
29
|
+
let term = y;
|
|
30
|
+
let sum = y;
|
|
31
|
+
for (let n = 1; n < 500; n++) {
|
|
32
|
+
term *= (2 * y2) / (2 * n + 1);
|
|
33
|
+
sum += term;
|
|
34
|
+
if (term < sum * 1e-17)
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
return (2 / SQRT_PI) * Math.exp(-y2) * sum;
|
|
38
|
+
}
|
|
39
|
+
/** erfc(y) for y >= 1 from the continued fraction with partial numerators k/2. */
|
|
40
|
+
function erfcContinuedFraction(y) {
|
|
41
|
+
let f = y;
|
|
42
|
+
for (let k = 100; k >= 1; k--) {
|
|
43
|
+
f = y + k / 2 / f;
|
|
44
|
+
}
|
|
45
|
+
return Math.exp(-y * y) / (SQRT_PI * f);
|
|
46
|
+
}
|
|
47
|
+
function erfcPositive(y) {
|
|
48
|
+
return y < 1 ? 1 - erfSeries(y) : erfcContinuedFraction(y);
|
|
49
|
+
}
|
|
50
|
+
/** Acklam's tail approximation for p in (0, P_LOW). */
|
|
51
|
+
function acklamTail(p) {
|
|
52
|
+
const q = Math.sqrt(-2 * Math.log(p));
|
|
53
|
+
return ((((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5]) /
|
|
54
|
+
((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* x with Phi(x) = 0.5 + q, for |q| <= 0.47575, from the displacement q itself.
|
|
58
|
+
* Taking q directly (not p = 0.5 + q) matters: for tiny q the sum 0.5 + q
|
|
59
|
+
* rounds away every digit of q. The refinement compares 0.5 * erf(x / sqrt 2)
|
|
60
|
+
* with q, so both sides keep full relative accuracy.
|
|
61
|
+
*/
|
|
62
|
+
export function centralQuantile(q) {
|
|
63
|
+
if (q === 0)
|
|
64
|
+
return 0;
|
|
65
|
+
const r = q * q;
|
|
66
|
+
let x = ((((((A[0] * r + A[1]) * r + A[2]) * r + A[3]) * r + A[4]) * r + A[5]) * q) /
|
|
67
|
+
(((((B[0] * r + B[1]) * r + B[2]) * r + B[3]) * r + B[4]) * r + 1);
|
|
68
|
+
const e = 0.5 * erfSeries(Math.abs(x) / SQRT_2) * (x < 0 ? -1 : 1) - q;
|
|
69
|
+
const u = e * SQRT_2PI * Math.exp((x * x) / 2);
|
|
70
|
+
if (Number.isFinite(u))
|
|
71
|
+
x = x - u / (1 + (x * u) / 2);
|
|
72
|
+
return x;
|
|
73
|
+
}
|
|
74
|
+
/** Quantile for p in (0, 0.5]; p = 0.5 gives exactly 0. */
|
|
75
|
+
function quantileLower(p) {
|
|
76
|
+
if (p >= P_LOW)
|
|
77
|
+
return centralQuantile(p - 0.5);
|
|
78
|
+
let x = acklamTail(p);
|
|
79
|
+
const e = 0.5 * erfcPositive(-x / SQRT_2) - p; // Phi(x) - p, accurate in the tail
|
|
80
|
+
const u = e * SQRT_2PI * Math.exp((x * x) / 2);
|
|
81
|
+
if (Number.isFinite(u)) {
|
|
82
|
+
x = x - u / (1 + (x * u) / 2);
|
|
83
|
+
}
|
|
84
|
+
return x;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Inverse standard normal CDF: the x with P(Z <= x) = p.
|
|
88
|
+
* Throws TypeError if p is not a number and RangeError unless 0 < p < 1.
|
|
89
|
+
*/
|
|
90
|
+
export function normalQuantile(p) {
|
|
91
|
+
if (typeof p !== 'number') {
|
|
92
|
+
throw new TypeError('p must be a number');
|
|
93
|
+
}
|
|
94
|
+
if (!(p > 0 && p < 1)) {
|
|
95
|
+
throw new RangeError(`p must be strictly between 0 and 1, got ${String(p)}`);
|
|
96
|
+
}
|
|
97
|
+
// 1 - p is exact for p in [0.5, 1), so the upper half mirrors the lower half.
|
|
98
|
+
return p > 0.5 ? -quantileLower(1 - p) : quantileLower(p);
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=normal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normal.js","sourceRoot":"","sources":["../src/normal.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,6EAA6E;AAC7E,2EAA2E;AAC3E,0EAA0E;AAC1E,8EAA8E;AAC9E,iEAAiE;AAEjE,MAAM,CAAC,GAAG;IACR,CAAC,mBAAmB,EAAE,mBAAmB,EAAE,CAAC,mBAAmB;IAC/D,kBAAkB,EAAE,CAAC,mBAAmB,EAAE,iBAAiB;CACnD,CAAA;AACV,MAAM,CAAC,GAAG;IACR,CAAC,mBAAmB,EAAE,mBAAmB,EAAE,CAAC,mBAAmB;IAC/D,mBAAmB,EAAE,CAAC,mBAAmB;CACjC,CAAA;AACV,MAAM,CAAC,GAAG;IACR,CAAC,oBAAoB,EAAE,CAAC,oBAAoB,EAAE,CAAC,iBAAiB;IAChE,CAAC,iBAAiB,EAAE,iBAAiB,EAAE,iBAAiB;CAChD,CAAA;AACV,MAAM,CAAC,GAAG;IACR,oBAAoB,EAAE,oBAAoB,EAAE,i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AC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA,CAAC,mCAAmC;IACjF,MAAM,CAAC,GAAG,CAAC,GAAG,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;IAC9C,IAAI,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;QACvB,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;IAC/B,CAAC;IACD,OAAO,CAAC,CAAA;AACV,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,CAAS;IACtC,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;QAC1B,MAAM,IAAI,SAAS,CAAC,oBAAoB,CAAC,CAAA;IAC3C,CAAC;IACD,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,UAAU,CAAC,2CAA2C,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;IAC9E,CAAC;IACD,8EAA8E;IAC9E,OAAO,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAA;AAC3D,CAAC","sourcesContent":["// Inverse of the standard normal CDF. Acklam's rational approximation\n// (relative error about 1.15e-9) followed by one Halley refinement step that\n// evaluates the CDF through erf / erfc written out below, so the result is\n// accurate to a few units of 1e-16 across the whole open interval (0, 1).\n// Below about 1e-300 the exp(x^2/2) factor of the refinement overflows; there\n// the unrefined approximation (about 1e-9 relative) is returned.\n\nconst A = [\n -3.969683028665376e1, 2.209460984245205e2, -2.759285104469687e2,\n 1.38357751867269e2, -3.066479806614716e1, 2.506628277459239,\n] as const\nconst B = [\n -5.447609879822406e1, 1.615858368580409e2, -1.556989798598866e2,\n 6.680131188771972e1, -1.328068155288572e1,\n] as const\nconst C = [\n -7.784894002430293e-3, -3.223964580411365e-1, -2.400758277161838,\n -2.549732539343734, 4.374664141464968, 2.938163982698783,\n] as const\nconst D = [\n 7.784695709041462e-3, 3.224671290700398e-1, 2.445134137142996, 3.754408661907416,\n] as const\nconst P_LOW = 0.02425\n\nconst SQRT_PI = Math.sqrt(Math.PI)\nconst SQRT_2 = Math.SQRT2\nconst SQRT_2PI = Math.sqrt(2 * Math.PI)\n\n/** erf(y) for 0 <= y <= about 5, from the all-positive series (no cancellation). */\nfunction erfSeries(y: number): number {\n const y2 = y * y\n let term = y\n let sum = y\n for (let n = 1; n < 500; n++) {\n term *= (2 * y2) / (2 * n + 1)\n sum += term\n if (term < sum * 1e-17) break\n }\n return (2 / SQRT_PI) * Math.exp(-y2) * sum\n}\n\n/** erfc(y) for y >= 1 from the continued fraction with partial numerators k/2. */\nfunction erfcContinuedFraction(y: number): number {\n let f = y\n for (let k = 100; k >= 1; k--) {\n f = y + k / 2 / f\n }\n return Math.exp(-y * y) / (SQRT_PI * f)\n}\n\nfunction erfcPositive(y: number): number {\n return y < 1 ? 1 - erfSeries(y) : erfcContinuedFraction(y)\n}\n\n/** Acklam's tail approximation for p in (0, P_LOW). */\nfunction acklamTail(p: number): number {\n const q = Math.sqrt(-2 * Math.log(p))\n return (\n (((((C[0] * q + C[1]) * q + C[2]) * q + C[3]) * q + C[4]) * q + C[5]) /\n ((((D[0] * q + D[1]) * q + D[2]) * q + D[3]) * q + 1)\n )\n}\n\n/**\n * x with Phi(x) = 0.5 + q, for |q| <= 0.47575, from the displacement q itself.\n * Taking q directly (not p = 0.5 + q) matters: for tiny q the sum 0.5 + q\n * rounds away every digit of q. The refinement compares 0.5 * erf(x / sqrt 2)\n * with q, so both sides keep full relative accuracy.\n */\nexport function centralQuantile(q: number): number {\n if (q === 0) return 0\n const r = q * q\n let x =\n ((((((A[0] * r + A[1]) * r + A[2]) * r + A[3]) * r + A[4]) * r + A[5]) * q) /\n (((((B[0] * r + B[1]) * r + B[2]) * r + B[3]) * r + B[4]) * r + 1)\n const e = 0.5 * erfSeries(Math.abs(x) / SQRT_2) * (x < 0 ? -1 : 1) - q\n const u = e * SQRT_2PI * Math.exp((x * x) / 2)\n if (Number.isFinite(u)) x = x - u / (1 + (x * u) / 2)\n return x\n}\n\n/** Quantile for p in (0, 0.5]; p = 0.5 gives exactly 0. */\nfunction quantileLower(p: number): number {\n if (p >= P_LOW) return centralQuantile(p - 0.5)\n let x = acklamTail(p)\n const e = 0.5 * erfcPositive(-x / SQRT_2) - p // Phi(x) - p, accurate in the tail\n const u = e * SQRT_2PI * Math.exp((x * x) / 2)\n if (Number.isFinite(u)) {\n x = x - u / (1 + (x * u) / 2)\n }\n return x\n}\n\n/**\n * Inverse standard normal CDF: the x with P(Z <= x) = p.\n * Throws TypeError if p is not a number and RangeError unless 0 < p < 1.\n */\nexport function normalQuantile(p: number): number {\n if (typeof p !== 'number') {\n throw new TypeError('p must be a number')\n }\n if (!(p > 0 && p < 1)) {\n throw new RangeError(`p must be strictly between 0 and 1, got ${String(p)}`)\n }\n // 1 - p is exact for p in [0.5, 1), so the upper half mirrors the lower half.\n return p > 0.5 ? -quantileLower(1 - p) : quantileLower(p)\n}\n"]}
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export declare const MAX_NAME_LENGTH = 200;
|
|
2
|
+
export declare const MAX_TEXT_LENGTH = 2000;
|
|
3
|
+
/** Most characters in a label (agreement pairs) or a stratum name. Very long equal-length strings collide in V8 Map and Set hashing. */
|
|
4
|
+
export declare const MAX_LABEL_LENGTH = 1000;
|
|
5
|
+
/**
|
|
6
|
+
* Folds a string for comparison: NFKC (so full-width and compatibility forms
|
|
7
|
+
* match their plain forms), lower case, typographic quotes and apostrophes to
|
|
8
|
+
* ' and ", every dash variant and the minus sign to -, every invisible
|
|
9
|
+
* default-ignorable character (including the soft hyphen) dropped, a Greek
|
|
10
|
+
* final sigma written as an ordinary one (lower-casing picks between them by
|
|
11
|
+
* the word around it), and any run of whitespace becomes one space. Look-alike
|
|
12
|
+
* letters from other scripts are NOT folded.
|
|
13
|
+
*/
|
|
14
|
+
export declare function foldText(text: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* What a message may not carry raw: controls (the tab too), line and paragraph
|
|
17
|
+
* separators and every invisible code point. `clip` writes them as \uXXXX.
|
|
18
|
+
*/
|
|
19
|
+
export declare const MESSAGE_UNSAFE: RegExp;
|
|
20
|
+
/** The code point as U+XXXX. */
|
|
21
|
+
export declare function codeLabel(char: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* For a string that is echoed into an explanation or a statement (names,
|
|
24
|
+
* population identifiers, stratum names): assertPlainText, then a RangeError
|
|
25
|
+
* for a tag character or variation selector, then the string without its other
|
|
26
|
+
* invisible characters. The errors name the field, the code point and where it
|
|
27
|
+
* sits, never the string.
|
|
28
|
+
*/
|
|
29
|
+
export declare function shownText(value: string, name: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* Throws RangeError when `value` holds a control or bidirectional-formatting
|
|
32
|
+
* character, naming the field, the code point and where it sits (never echoing
|
|
33
|
+
* the string itself). `lineAdvice` is added when the character is a line break,
|
|
34
|
+
* for a field a caller is likely to paste from wrapped text.
|
|
35
|
+
*/
|
|
36
|
+
export declare function assertPlainText(value: string, name: string, lineAdvice?: string): void;
|
|
37
|
+
/**
|
|
38
|
+
* True when the folded `name` appears in the folded `text` as its own word or
|
|
39
|
+
* phrase. The characters next to a match may not continue it:
|
|
40
|
+
* - a letter, digit or combining mark next to a name that starts or ends with
|
|
41
|
+
* one ("test" is not found inside "latest");
|
|
42
|
+
* - a "+" straight after the match ("HumanEval" is not "HumanEval+");
|
|
43
|
+
* - a percent sign (straight after, or after one space) when the name ends in a
|
|
44
|
+
* digit: "70" is not found in "70%", where it is the score;
|
|
45
|
+
* - one of - _ / . straight before or after the match when a letter or digit
|
|
46
|
+
* sits on the far side of it ("MMLU" is not "MMLU-Pro" or "mini-MMLU", but a
|
|
47
|
+
* full stop that ends a sentence, as in "on CIFAR-10.", is fine).
|
|
48
|
+
* A suffix set off by a space ("SWE-bench Verified") cannot be told apart from
|
|
49
|
+
* ordinary words; declare the full name for such a set.
|
|
50
|
+
*/
|
|
51
|
+
export declare function mentionsName(text: string, name: string): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Reads a required string of at most `max` characters that holds at least one
|
|
54
|
+
* letter or digit once folded: a name made only of punctuation or invisible
|
|
55
|
+
* characters would be "found" in almost any text. Returns the name as it will
|
|
56
|
+
* be echoed (see shownText).
|
|
57
|
+
*/
|
|
58
|
+
export declare function readName(value: unknown, name: string, max?: number): string;
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Small text helpers for scope wording. Every loop is linear in the input
|
|
2
|
+
// length, which is bounded by the caps below: a match is judged by the one or
|
|
3
|
+
// two code points next to it, never by re-reading the text around it.
|
|
4
|
+
export const MAX_NAME_LENGTH = 200;
|
|
5
|
+
export const MAX_TEXT_LENGTH = 2000;
|
|
6
|
+
/** Most characters in a label (agreement pairs) or a stratum name. Very long equal-length strings collide in V8 Map and Set hashing. */
|
|
7
|
+
export const MAX_LABEL_LENGTH = 1000;
|
|
8
|
+
// Typographic variants that copied web text carries, folded to the ASCII
|
|
9
|
+
// character a person would type. NFKC runs first, so full-width forms and the
|
|
10
|
+
// non-breaking hyphen (U+2011 becomes U+2010) are already plain by then.
|
|
11
|
+
const APOSTROPHES = /[‘’‛′ʼ]/g;
|
|
12
|
+
const DOUBLE_QUOTES = /[“”]/g;
|
|
13
|
+
const DASHES = /[‐-―⁃−⸺⸻]/g;
|
|
14
|
+
// Characters with no visible form of their own (zero-width space, joiners, word
|
|
15
|
+
// joiner, direction marks, BOM, variation selectors, tag characters, the Hangul
|
|
16
|
+
// and Mongolian fillers, the soft hyphen). They are dropped before comparison,
|
|
17
|
+
// so one next to a name cannot make it look like a clean word boundary
|
|
18
|
+
// ("MMLU", ZWSP, "-Pro") and one inside a name copied from the web does not
|
|
19
|
+
// break the match.
|
|
20
|
+
const INVISIBLE = /\p{Default_Ignorable_Code_Point}/gu;
|
|
21
|
+
// toLowerCase writes a Greek capital sigma as the final form when it ends a word
|
|
22
|
+
// and as the ordinary one otherwise, and "word" depends on the characters around
|
|
23
|
+
// it, so the same name folds two ways ("ΟΔΥΣΣΕΥΣ" alone, "ΟΔΥΣΣΕΥΣ's" in a sentence).
|
|
24
|
+
const FINAL_SIGMA = /\u03c2/g;
|
|
25
|
+
/**
|
|
26
|
+
* Folds a string for comparison: NFKC (so full-width and compatibility forms
|
|
27
|
+
* match their plain forms), lower case, typographic quotes and apostrophes to
|
|
28
|
+
* ' and ", every dash variant and the minus sign to -, every invisible
|
|
29
|
+
* default-ignorable character (including the soft hyphen) dropped, a Greek
|
|
30
|
+
* final sigma written as an ordinary one (lower-casing picks between them by
|
|
31
|
+
* the word around it), and any run of whitespace becomes one space. Look-alike
|
|
32
|
+
* letters from other scripts are NOT folded.
|
|
33
|
+
*/
|
|
34
|
+
export function foldText(text) {
|
|
35
|
+
const plain = text
|
|
36
|
+
.normalize('NFKC')
|
|
37
|
+
.toLowerCase()
|
|
38
|
+
.replace(FINAL_SIGMA, '\u03c3')
|
|
39
|
+
.replace(INVISIBLE, '')
|
|
40
|
+
.replace(APOSTROPHES, "'")
|
|
41
|
+
.replace(DOUBLE_QUOTES, '"')
|
|
42
|
+
.replace(DASHES, '-');
|
|
43
|
+
const words = plain.split(/\s+/);
|
|
44
|
+
return words.filter((word) => word !== '').join(' ');
|
|
45
|
+
}
|
|
46
|
+
// A string that is echoed into a statement or an explanation must read the same
|
|
47
|
+
// to a person as it does to the kit. C0 and C1 controls (newlines, NUL, escape),
|
|
48
|
+
// line and paragraph separators, and the bidirectional embedding, override and
|
|
49
|
+
// isolate characters can reorder or hide the text around them, so they throw.
|
|
50
|
+
// The horizontal tab is plain spacing and stays, like the direction marks
|
|
51
|
+
// (U+200E, U+200F) and joiners: scripts written right to left need them.
|
|
52
|
+
// eslint-disable-next-line no-control-regex
|
|
53
|
+
const UNSAFE_TEXT = /[\u0000-\u0008\u000a-\u001f\u007f-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]/u;
|
|
54
|
+
const LINE_BREAK = /[\n\r\u0085\u2028\u2029]/u;
|
|
55
|
+
// Invisible code points can carry text a reader does not see. Tag characters
|
|
56
|
+
// spell ASCII and variation selectors encode bytes, and neither has a use in a
|
|
57
|
+
// name, so a string that holds one throws. The other invisible characters (zero-width
|
|
58
|
+
// space, word joiner, BOM, the fillers, the invisible operators, the braille
|
|
59
|
+
// blank) are dropped from a string before it is echoed, so what is printed is what
|
|
60
|
+
// a person sees. The ones real writing needs stay: the joiner and non-joiner, the
|
|
61
|
+
// direction marks and Arabic letter mark, the soft hyphen, the grapheme joiner and
|
|
62
|
+
// the emoji presentation selectors. The braille blank is not default-ignorable but
|
|
63
|
+
// is blank.
|
|
64
|
+
const TEXT_CARRYING = /[︀-︍]|[\u{e0000}-\u{e007f}]|[\u{e0100}-\u{e01ef}]/u;
|
|
65
|
+
const HIDDEN_TEXT = /(?!\u00ad|\u034f|\u061c|[\u200c-\u200f]|\ufe0e|\ufe0f)\p{Default_Ignorable_Code_Point}|\u2800/gu;
|
|
66
|
+
/**
|
|
67
|
+
* What a message may not carry raw: controls (the tab too), line and paragraph
|
|
68
|
+
* separators and every invisible code point. `clip` writes them as \uXXXX.
|
|
69
|
+
*/
|
|
70
|
+
// eslint-disable-next-line no-control-regex
|
|
71
|
+
export const MESSAGE_UNSAFE = /[\u0000-\u001f\u007f-\u009f\u2028\u2029\u2800]|\p{Default_Ignorable_Code_Point}/gu;
|
|
72
|
+
/** The code point as U+XXXX. */
|
|
73
|
+
export function codeLabel(char) {
|
|
74
|
+
return `U+${char.codePointAt(0).toString(16).toUpperCase().padStart(4, '0')}`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* For a string that is echoed into an explanation or a statement (names,
|
|
78
|
+
* population identifiers, stratum names): assertPlainText, then a RangeError
|
|
79
|
+
* for a tag character or variation selector, then the string without its other
|
|
80
|
+
* invisible characters. The errors name the field, the code point and where it
|
|
81
|
+
* sits, never the string.
|
|
82
|
+
*/
|
|
83
|
+
export function shownText(value, name) {
|
|
84
|
+
assertPlainText(value, name);
|
|
85
|
+
const carrying = TEXT_CARRYING.exec(value);
|
|
86
|
+
if (carrying !== null) {
|
|
87
|
+
throw new RangeError(`${name} must not contain a tag character or variation selector, which can carry hidden text (${codeLabel(carrying[0])} at position ${String(carrying.index)})`);
|
|
88
|
+
}
|
|
89
|
+
return value.replace(HIDDEN_TEXT, '');
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Throws RangeError when `value` holds a control or bidirectional-formatting
|
|
93
|
+
* character, naming the field, the code point and where it sits (never echoing
|
|
94
|
+
* the string itself). `lineAdvice` is added when the character is a line break,
|
|
95
|
+
* for a field a caller is likely to paste from wrapped text.
|
|
96
|
+
*/
|
|
97
|
+
export function assertPlainText(value, name, lineAdvice) {
|
|
98
|
+
const found = UNSAFE_TEXT.exec(value);
|
|
99
|
+
if (found === null)
|
|
100
|
+
return;
|
|
101
|
+
const advice = lineAdvice !== undefined && LINE_BREAK.test(found[0]) ? `; ${lineAdvice}` : '';
|
|
102
|
+
throw new RangeError(`${name} must not contain a control or bidirectional-formatting character (${codeLabel(found[0])} at position ${String(found.index)})${advice}`);
|
|
103
|
+
}
|
|
104
|
+
const WORDISH = /[\p{L}\p{N}\p{M}]/u;
|
|
105
|
+
const HAS_LETTER_OR_DIGIT = /[\p{L}\p{N}]/u;
|
|
106
|
+
const DIGIT = /\p{N}/u;
|
|
107
|
+
/** Characters that join a name to a suffix or prefix without a space: MMLU-Pro, MMLU_pro, MMLU/pro, MMLU.pro. */
|
|
108
|
+
const JOINERS = '-_/.';
|
|
109
|
+
function isWordish(codePoint) {
|
|
110
|
+
return codePoint !== undefined && WORDISH.test(String.fromCodePoint(codePoint));
|
|
111
|
+
}
|
|
112
|
+
/** The code point that ends just before `index`, surrogate-aware; undefined at the start. */
|
|
113
|
+
function codePointBefore(text, index) {
|
|
114
|
+
if (index <= 0)
|
|
115
|
+
return undefined;
|
|
116
|
+
const low = text.charCodeAt(index - 1);
|
|
117
|
+
if (low >= 0xdc00 && low <= 0xdfff && index >= 2) {
|
|
118
|
+
const high = text.charCodeAt(index - 2);
|
|
119
|
+
if (high >= 0xd800 && high <= 0xdbff)
|
|
120
|
+
return text.codePointAt(index - 2);
|
|
121
|
+
}
|
|
122
|
+
return low;
|
|
123
|
+
}
|
|
124
|
+
/** The code point that starts at `index`, surrogate-aware; undefined past the end. */
|
|
125
|
+
function codePointAfter(text, index) {
|
|
126
|
+
return index >= text.length ? undefined : text.codePointAt(index);
|
|
127
|
+
}
|
|
128
|
+
const unitOf = (codePoint) => (codePoint > 0xffff ? 2 : 1);
|
|
129
|
+
const PLUS = 0x2b;
|
|
130
|
+
const PERCENT = 0x25;
|
|
131
|
+
const SPACE = 0x20;
|
|
132
|
+
const isJoiner = (codePoint) => codePoint !== undefined && JOINERS.includes(String.fromCodePoint(codePoint));
|
|
133
|
+
/**
|
|
134
|
+
* True when the folded `name` appears in the folded `text` as its own word or
|
|
135
|
+
* phrase. The characters next to a match may not continue it:
|
|
136
|
+
* - a letter, digit or combining mark next to a name that starts or ends with
|
|
137
|
+
* one ("test" is not found inside "latest");
|
|
138
|
+
* - a "+" straight after the match ("HumanEval" is not "HumanEval+");
|
|
139
|
+
* - a percent sign (straight after, or after one space) when the name ends in a
|
|
140
|
+
* digit: "70" is not found in "70%", where it is the score;
|
|
141
|
+
* - one of - _ / . straight before or after the match when a letter or digit
|
|
142
|
+
* sits on the far side of it ("MMLU" is not "MMLU-Pro" or "mini-MMLU", but a
|
|
143
|
+
* full stop that ends a sentence, as in "on CIFAR-10.", is fine).
|
|
144
|
+
* A suffix set off by a space ("SWE-bench Verified") cannot be told apart from
|
|
145
|
+
* ordinary words; declare the full name for such a set.
|
|
146
|
+
*/
|
|
147
|
+
export function mentionsName(text, name) {
|
|
148
|
+
const hay = foldText(text);
|
|
149
|
+
const needle = foldText(name);
|
|
150
|
+
if (needle === '')
|
|
151
|
+
return false;
|
|
152
|
+
const first = needle.codePointAt(0);
|
|
153
|
+
const lastStart = needle.length - unitOf(codePointBefore(needle, needle.length));
|
|
154
|
+
const last = needle.codePointAt(lastStart);
|
|
155
|
+
const needsStart = isWordish(first);
|
|
156
|
+
const needsEnd = isWordish(last);
|
|
157
|
+
const endsInDigit = DIGIT.test(String.fromCodePoint(last));
|
|
158
|
+
let from = 0;
|
|
159
|
+
for (;;) {
|
|
160
|
+
const at = hay.indexOf(needle, from);
|
|
161
|
+
if (at < 0)
|
|
162
|
+
return false;
|
|
163
|
+
from = at + 1;
|
|
164
|
+
const end = at + needle.length;
|
|
165
|
+
const before = codePointBefore(hay, at);
|
|
166
|
+
const after = codePointAfter(hay, end);
|
|
167
|
+
if (needsStart && isWordish(before))
|
|
168
|
+
continue;
|
|
169
|
+
if (needsEnd && isWordish(after))
|
|
170
|
+
continue;
|
|
171
|
+
if (after === PLUS)
|
|
172
|
+
continue;
|
|
173
|
+
// a numeral that is part of a percent figure ("70" in "70%") is the score, not a set
|
|
174
|
+
if (endsInDigit && (after === PERCENT || (after === SPACE && codePointAfter(hay, end + 1) === PERCENT)))
|
|
175
|
+
continue;
|
|
176
|
+
if (isJoiner(after) && isWordish(codePointAfter(hay, end + 1)))
|
|
177
|
+
continue;
|
|
178
|
+
if (isJoiner(before) && isWordish(codePointBefore(hay, at - (before !== undefined && before > 0xffff ? 2 : 1))))
|
|
179
|
+
continue;
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Reads a required string of at most `max` characters that holds at least one
|
|
185
|
+
* letter or digit once folded: a name made only of punctuation or invisible
|
|
186
|
+
* characters would be "found" in almost any text. Returns the name as it will
|
|
187
|
+
* be echoed (see shownText).
|
|
188
|
+
*/
|
|
189
|
+
export function readName(value, name, max = MAX_NAME_LENGTH) {
|
|
190
|
+
if (typeof value !== 'string')
|
|
191
|
+
throw new TypeError(`${name} must be a string`);
|
|
192
|
+
if (value.length > max)
|
|
193
|
+
throw new RangeError(`${name} must be at most ${max} characters`);
|
|
194
|
+
const shown = shownText(value, name);
|
|
195
|
+
const folded = foldText(value);
|
|
196
|
+
if (folded === '')
|
|
197
|
+
throw new RangeError(`${name} must not be blank`);
|
|
198
|
+
if (!HAS_LETTER_OR_DIGIT.test(folded))
|
|
199
|
+
throw new RangeError(`${name} must contain at least one letter or digit`);
|
|
200
|
+
return shown;
|
|
201
|
+
}
|
|
202
|
+
//# sourceMappingURL=text.js.map
|
package/dist/text.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../src/text.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,8EAA8E;AAC9E,sEAAsE;AAEtE,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,CAAA;AAClC,MAAM,CAAC,MAAM,eAAe,GAAG,IAAI,CAAA;AACnC,wIAAwI;AACxI,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,CAAA;AAEpC,yEAAyE;AACzE,8EAA8E;AAC9E,yEAAyE;AACzE,MAAM,WAAW,GAAG,UAAU,CAAA;AAC9B,MAAM,aAAa,GAAG,OAAO,CAAA;AAC7B,MAAM,MAAM,GAAG,YAAY,CAAA;AAC3B,gFAAgF;AAChF,gFAAgF;AAChF,+EAA+E;AAC/E,uEAAuE;AACvE,4EAA4E;AAC5E,mBAAmB;AACnB,MAAM,SAAS,GAAG,oCAAoC,CAAA;AACtD,iFAAiF;AACjF,iFAAiF;AACjF,sFAAsF;AACtF,MAAM,WAAW,GAAG,SAAS,CAAA;AAE7B;;;;;;;;GAQG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAY;IACnC,MAAM,KAAK,GAAG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AC","sourcesContent":["// Small text helpers for scope wording. Every loop is linear in the input\n// length, which is bounded by the caps below: a match is judged by the one or\n// two code points next to it, never by re-reading the text around it.\n\nexport const MAX_NAME_LENGTH = 200\nexport const MAX_TEXT_LENGTH = 2000\n/** Most characters in a label (agreement pairs) or a stratum name. Very long equal-length strings collide in V8 Map and Set hashing. */\nexport const MAX_LABEL_LENGTH = 1000\n\n// Typographic variants that copied web text carries, folded to the ASCII\n// character a person would type. NFKC runs first, so full-width forms and the\n// non-breaking hyphen (U+2011 becomes U+2010) are already plain by then.\nconst APOSTROPHES = /[‘’‛′ʼ]/g\nconst DOUBLE_QUOTES = /[“”]/g\nconst DASHES = /[‐-―⁃−⸺⸻]/g\n// Characters with no visible form of their own (zero-width space, joiners, word\n// joiner, direction marks, BOM, variation selectors, tag characters, the Hangul\n// and Mongolian fillers, the soft hyphen). They are dropped before comparison,\n// so one next to a name cannot make it look like a clean word boundary\n// (\"MMLU\", ZWSP, \"-Pro\") and one inside a name copied from the web does not\n// break the match.\nconst INVISIBLE = /\\p{Default_Ignorable_Code_Point}/gu\n// toLowerCase writes a Greek capital sigma as the final form when it ends a word\n// and as the ordinary one otherwise, and \"word\" depends on the characters around\n// it, so the same name folds two ways (\"ΟΔΥΣΣΕΥΣ\" alone, \"ΟΔΥΣΣΕΥΣ's\" in a sentence).\nconst FINAL_SIGMA = /\\u03c2/g\n\n/**\n * Folds a string for comparison: NFKC (so full-width and compatibility forms\n * match their plain forms), lower case, typographic quotes and apostrophes to\n * ' and \", every dash variant and the minus sign to -, every invisible\n * default-ignorable character (including the soft hyphen) dropped, a Greek\n * final sigma written as an ordinary one (lower-casing picks between them by\n * the word around it), and any run of whitespace becomes one space. Look-alike\n * letters from other scripts are NOT folded.\n */\nexport function foldText(text: string): string {\n const plain = text\n .normalize('NFKC')\n .toLowerCase()\n .replace(FINAL_SIGMA, '\\u03c3')\n .replace(INVISIBLE, '')\n .replace(APOSTROPHES, \"'\")\n .replace(DOUBLE_QUOTES, '\"')\n .replace(DASHES, '-')\n const words = plain.split(/\\s+/)\n return words.filter((word) => word !== '').join(' ')\n}\n\n// A string that is echoed into a statement or an explanation must read the same\n// to a person as it does to the kit. C0 and C1 controls (newlines, NUL, escape),\n// line and paragraph separators, and the bidirectional embedding, override and\n// isolate characters can reorder or hide the text around them, so they throw.\n// The horizontal tab is plain spacing and stays, like the direction marks\n// (U+200E, U+200F) and joiners: scripts written right to left need them.\n// eslint-disable-next-line no-control-regex\nconst UNSAFE_TEXT = /[\\u0000-\\u0008\\u000a-\\u001f\\u007f-\\u009f\\u2028\\u2029\\u202a-\\u202e\\u2066-\\u2069]/u\n\nconst LINE_BREAK = /[\\n\\r\\u0085\\u2028\\u2029]/u\n\n// Invisible code points can carry text a reader does not see. Tag characters\n// spell ASCII and variation selectors encode bytes, and neither has a use in a\n// name, so a string that holds one throws. The other invisible characters (zero-width\n// space, word joiner, BOM, the fillers, the invisible operators, the braille\n// blank) are dropped from a string before it is echoed, so what is printed is what\n// a person sees. The ones real writing needs stay: the joiner and non-joiner, the\n// direction marks and Arabic letter mark, the soft hyphen, the grapheme joiner and\n// the emoji presentation selectors. The braille blank is not default-ignorable but\n// is blank.\nconst TEXT_CARRYING = /[︀-︍]|[\\u{e0000}-\\u{e007f}]|[\\u{e0100}-\\u{e01ef}]/u\nconst HIDDEN_TEXT = /(?!\\u00ad|\\u034f|\\u061c|[\\u200c-\\u200f]|\\ufe0e|\\ufe0f)\\p{Default_Ignorable_Code_Point}|\\u2800/gu\n\n/**\n * What a message may not carry raw: controls (the tab too), line and paragraph\n * separators and every invisible code point. `clip` writes them as \\uXXXX.\n */\n// eslint-disable-next-line no-control-regex\nexport const MESSAGE_UNSAFE = /[\\u0000-\\u001f\\u007f-\\u009f\\u2028\\u2029\\u2800]|\\p{Default_Ignorable_Code_Point}/gu\n\n/** The code point as U+XXXX. */\nexport function codeLabel(char: string): string {\n return `U+${(char.codePointAt(0) as number).toString(16).toUpperCase().padStart(4, '0')}`\n}\n\n/**\n * For a string that is echoed into an explanation or a statement (names,\n * population identifiers, stratum names): assertPlainText, then a RangeError\n * for a tag character or variation selector, then the string without its other\n * invisible characters. The errors name the field, the code point and where it\n * sits, never the string.\n */\nexport function shownText(value: string, name: string): string {\n assertPlainText(value, name)\n const carrying = TEXT_CARRYING.exec(value)\n if (carrying !== null) {\n throw new RangeError(`${name} must not contain a tag character or variation selector, which can carry hidden text (${codeLabel(carrying[0])} at position ${String(carrying.index)})`)\n }\n return value.replace(HIDDEN_TEXT, '')\n}\n\n/**\n * Throws RangeError when `value` holds a control or bidirectional-formatting\n * character, naming the field, the code point and where it sits (never echoing\n * the string itself). `lineAdvice` is added when the character is a line break,\n * for a field a caller is likely to paste from wrapped text.\n */\nexport function assertPlainText(value: string, name: string, lineAdvice?: string): void {\n const found = UNSAFE_TEXT.exec(value)\n if (found === null) return\n const advice = lineAdvice !== undefined && LINE_BREAK.test(found[0]) ? `; ${lineAdvice}` : ''\n throw new RangeError(`${name} must not contain a control or bidirectional-formatting character (${codeLabel(found[0])} at position ${String(found.index)})${advice}`)\n}\n\nconst WORDISH = /[\\p{L}\\p{N}\\p{M}]/u\nconst HAS_LETTER_OR_DIGIT = /[\\p{L}\\p{N}]/u\nconst DIGIT = /\\p{N}/u\n/** Characters that join a name to a suffix or prefix without a space: MMLU-Pro, MMLU_pro, MMLU/pro, MMLU.pro. */\nconst JOINERS = '-_/.'\n\nfunction isWordish(codePoint: number | undefined): boolean {\n return codePoint !== undefined && WORDISH.test(String.fromCodePoint(codePoint))\n}\n\n/** The code point that ends just before `index`, surrogate-aware; undefined at the start. */\nfunction codePointBefore(text: string, index: number): number | undefined {\n if (index <= 0) return undefined\n const low = text.charCodeAt(index - 1)\n if (low >= 0xdc00 && low <= 0xdfff && index >= 2) {\n const high = text.charCodeAt(index - 2)\n if (high >= 0xd800 && high <= 0xdbff) return text.codePointAt(index - 2)\n }\n return low\n}\n\n/** The code point that starts at `index`, surrogate-aware; undefined past the end. */\nfunction codePointAfter(text: string, index: number): number | undefined {\n return index >= text.length ? undefined : text.codePointAt(index)\n}\n\nconst unitOf = (codePoint: number): number => (codePoint > 0xffff ? 2 : 1)\nconst PLUS = 0x2b\nconst PERCENT = 0x25\nconst SPACE = 0x20\nconst isJoiner = (codePoint: number | undefined): boolean => codePoint !== undefined && JOINERS.includes(String.fromCodePoint(codePoint))\n\n/**\n * True when the folded `name` appears in the folded `text` as its own word or\n * phrase. The characters next to a match may not continue it:\n * - a letter, digit or combining mark next to a name that starts or ends with\n * one (\"test\" is not found inside \"latest\");\n * - a \"+\" straight after the match (\"HumanEval\" is not \"HumanEval+\");\n * - a percent sign (straight after, or after one space) when the name ends in a\n * digit: \"70\" is not found in \"70%\", where it is the score;\n * - one of - _ / . straight before or after the match when a letter or digit\n * sits on the far side of it (\"MMLU\" is not \"MMLU-Pro\" or \"mini-MMLU\", but a\n * full stop that ends a sentence, as in \"on CIFAR-10.\", is fine).\n * A suffix set off by a space (\"SWE-bench Verified\") cannot be told apart from\n * ordinary words; declare the full name for such a set.\n */\nexport function mentionsName(text: string, name: string): boolean {\n const hay = foldText(text)\n const needle = foldText(name)\n if (needle === '') return false\n const first = needle.codePointAt(0)\n const lastStart = needle.length - unitOf(codePointBefore(needle, needle.length) as number)\n const last = needle.codePointAt(lastStart)\n const needsStart = isWordish(first)\n const needsEnd = isWordish(last)\n const endsInDigit = DIGIT.test(String.fromCodePoint(last as number))\n let from = 0\n for (;;) {\n const at = hay.indexOf(needle, from)\n if (at < 0) return false\n from = at + 1\n const end = at + needle.length\n const before = codePointBefore(hay, at)\n const after = codePointAfter(hay, end)\n if (needsStart && isWordish(before)) continue\n if (needsEnd && isWordish(after)) continue\n if (after === PLUS) continue\n // a numeral that is part of a percent figure (\"70\" in \"70%\") is the score, not a set\n if (endsInDigit && (after === PERCENT || (after === SPACE && codePointAfter(hay, end + 1) === PERCENT))) continue\n if (isJoiner(after) && isWordish(codePointAfter(hay, end + 1))) continue\n if (isJoiner(before) && isWordish(codePointBefore(hay, at - (before !== undefined && before > 0xffff ? 2 : 1)))) continue\n return true\n }\n}\n\n/**\n * Reads a required string of at most `max` characters that holds at least one\n * letter or digit once folded: a name made only of punctuation or invisible\n * characters would be \"found\" in almost any text. Returns the name as it will\n * be echoed (see shownText).\n */\nexport function readName(value: unknown, name: string, max = MAX_NAME_LENGTH): string {\n if (typeof value !== 'string') throw new TypeError(`${name} must be a string`)\n if (value.length > max) throw new RangeError(`${name} must be at most ${max} characters`)\n const shown = shownText(value, name)\n const folded = foldText(value)\n if (folded === '') throw new RangeError(`${name} must not be blank`)\n if (!HAS_LETTER_OR_DIGIT.test(folded)) throw new RangeError(`${name} must contain at least one letter or digit`)\n return shown\n}\n"]}
|