Baseline: main branch state prior to Top-10 films expansion

This commit is contained in:
Hermes Agent committed 2026-11-02 12:34:51 +00:00
commit 6b15603e56
139 files changed
+329420

No files matched your search

+258
View File
@@ -0,0 +1,258 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
WORD_SPLIT_REGEX: () => WORD_SPLIT_REGEX,
WORD_TEST_REGEX: () => WORD_TEST_REGEX,
applyCasePattern: () => applyCasePattern,
detectCasePattern: () => detectCasePattern,
extractPreservedPatterns: () => extractPreservedPatterns,
normalizeApostrophes: () => normalizeApostrophes,
splitCamelCase: () => splitCamelCase,
stripDiacritics: () => stripDiacritics,
tokenizeIPA: () => tokenizeIPA,
tokenizeText: () => tokenizeText
});
module.exports = __toCommonJS(index_exports);
// src/case.ts
function applyCasePattern(word, pattern, original) {
if (pattern === "lower") {
const firstChar = word.codePointAt(0);
if (firstChar >= 97 && firstChar <= 122 && word === word.toLowerCase()) {
return word;
}
return word.toLowerCase();
}
switch (pattern) {
case "capitalized": {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
case "mixed": {
if (original !== void 0 && original.length > 0) {
return applyMixedCase(word, original);
}
return word.toLowerCase();
}
case "upper": {
return word.toUpperCase();
}
default: {
return word.toLowerCase();
}
}
}
function detectCasePattern(word) {
if (word.length === 0) {
return "lower";
}
const firstCode = word.codePointAt(0);
const firstChar = word[0];
const isFirstUpper = firstCode >= 65 && firstCode <= 90 || // A-Z (fast ASCII path)
firstCode > 127 && firstChar !== firstChar.toLowerCase();
if (!isFirstUpper && word === word.toLowerCase()) {
return "lower";
}
if (word.length === 1) {
if (word === "I") {
return "lower";
}
return isFirstUpper ? "capitalized" : "lower";
}
if (word === word.toUpperCase()) {
if (word.length <= 2) {
return "capitalized";
}
return "upper";
}
if (isFirstUpper) {
let restIsLower = true;
for (let i = 1; i < word.length; i++) {
const c = word.codePointAt(i);
if (c >= 65 && c <= 90 || // A-Z
c > 127 && word[i] !== word[i].toLowerCase()) {
restIsLower = false;
break;
}
}
if (restIsLower) {
return "capitalized";
}
}
return "mixed";
}
function splitCamelCase(word) {
if (word.length < 2) {
return null;
}
let hasInternalUpper = false;
for (let i = 1; i < word.length; i++) {
const c = word.codePointAt(i);
if (c >= 65 && c <= 90) {
hasInternalUpper = true;
break;
}
}
if (!hasInternalUpper) {
return null;
}
const parts = [];
let start = 0;
for (let i = 1; i < word.length; i++) {
const prevCode = word.codePointAt(i - 1);
const currCode = word.codePointAt(i);
if (prevCode >= 97 && prevCode <= 122 && currCode >= 65 && currCode <= 90) {
parts.push(word.slice(start, i));
start = i;
}
}
if (parts.length > 0) {
parts.push(word.slice(start));
return parts;
}
return null;
}
function applyMixedCase(translated, original) {
const lowerTranslated = translated.toLowerCase();
let result = "";
let i = 0;
for (const char of lowerTranslated) {
if (i < original.length) {
const origChar = original[i];
result += origChar === origChar.toUpperCase() ? char.toUpperCase() : char;
} else {
result += char;
}
i++;
}
return result;
}
// src/text.ts
var URL_REGEX = /(?:https?|ftp|file):\/\/[^\s<>"')\]]+/gi;
var EMAIL_REGEX = /[\w.%+-]{1,64}@(?:[a-z0-9-]{1,63}\.){1,10}[a-z]{2,63}/gi;
var COMMON_TLDS = "com|org|net|edu|gov|io|co|uk|de|fr|jp|au|ca|ru|ch|it|nl|se|no|es|mil|info|biz|tv|me|app|dev|ai|xyz|rs|site|tech|blog|news|club|lol|new|world|online|space|fun|live|shop|store|gg|fm|im|is|to|cc|ws|ly|gl|be|us|in|eu|asia|pro|cz|pl|fi|dk|pt|ie|nz|za|br|mx|ar|cl|kr|tw|hk|sg|id|th|vn|ph";
var BARE_DOMAIN_REGEX = new RegExp(
String.raw`\b(?:[a-z0-9][-a-z0-9]*\.)+(?:${COMMON_TLDS})\b(?:\/[^\s<>"')\]]*)?`,
"gi"
);
function extractPreservedPatterns(text) {
const preserved = /* @__PURE__ */ new Map();
let counter = 0;
let result = text;
if (text.includes("://")) {
result = result.replaceAll(URL_REGEX, (match) => {
const placeholder = `\0${counter++}\0`;
preserved.set(placeholder, match);
return placeholder;
});
}
if (result.includes("@")) {
result = result.replaceAll(EMAIL_REGEX, (match) => {
const placeholder = `\0${counter++}\0`;
preserved.set(placeholder, match);
return placeholder;
});
}
if (result.includes(".")) {
result = result.replaceAll(BARE_DOMAIN_REGEX, (match) => {
const placeholder = `\0${counter++}\0`;
preserved.set(placeholder, match);
return placeholder;
});
}
return { preserved, text: result };
}
var FANCY_APOSTROPHE = /[\u2018\u2019\u02BC]/g;
var COMBINING_MARKS = /[\u0300-\u036F]/g;
function normalizeApostrophes(text) {
return text.replaceAll(FANCY_APOSTROPHE, "'");
}
function stripDiacritics(text) {
return text.normalize("NFD").replaceAll(COMBINING_MARKS, "");
}
// src/tokenize.ts
var WORD_SPLIT_REGEX = /((?<!\d)[a-zA-Z\u00C0-\u024F']+(?!\d))/;
var WORD_TEST_REGEX = /^[a-zA-Z\u00C0-\u024F']+$/;
var IPA_SYMBOLS_SET = new Set("\u0259\u025D\u025A\u028C\xE6\u0251\u0254\u025B\u026A\u028A\xF0\u03B8\u0283\u0292\u014B\u0279\u0261");
function isIPAChar(char) {
const code = char.codePointAt(0);
if (code >= 65 && code <= 90 || code >= 97 && code <= 122) {
return true;
}
if (code === 8288) {
return true;
}
if (code === 712 || code === 716) {
return true;
}
if (IPA_SYMBOLS_SET.has(char)) {
return true;
}
return false;
}
function tokenizeIPA(text) {
const tokens = [];
let i = 0;
while (i < text.length) {
if (isIPAChar(text[i])) {
let wordEnd = i + 1;
while (wordEnd < text.length && isIPAChar(text[wordEnd])) {
wordEnd++;
}
tokens.push({ isWord: true, text: text.slice(i, wordEnd) });
i = wordEnd;
} else {
let nonWordEnd = i + 1;
while (nonWordEnd < text.length && !isIPAChar(text[nonWordEnd])) {
nonWordEnd++;
}
tokens.push({ isWord: false, text: text.slice(i, nonWordEnd) });
i = nonWordEnd;
}
}
return tokens;
}
function tokenizeText(text) {
const normalized = normalizeApostrophes(text);
const parts = normalized.split(WORD_SPLIT_REGEX);
const tokens = [];
for (const part of parts) {
if (part.length > 0) {
tokens.push({ isWord: WORD_TEST_REGEX.test(part), text: part });
}
}
return tokens;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
WORD_SPLIT_REGEX,
WORD_TEST_REGEX,
applyCasePattern,
detectCasePattern,
extractPreservedPatterns,
normalizeApostrophes,
splitCamelCase,
stripDiacritics,
tokenizeIPA,
tokenizeText
});
+74
View File
@@ -0,0 +1,74 @@
/**
* Utilities for preserving case patterns during translation.
*/
/**
* Case pattern detected in a word.
* - 'lower': all lowercase (hello)
* - 'upper': all uppercase (HELLO)
* - 'capitalized': first letter uppercase (Hello)
* - 'mixed': mixed case like camelCase (GitHub, iPhone)
*/
type CasePattern = 'capitalized' | 'lower' | 'mixed' | 'upper';
/**
* Applies a case pattern to a word, optionally using original word for mixed case.
* Optimized: lowercase case returns word directly if already lowercase.
*/
declare function applyCasePattern(word: string, pattern: CasePattern, original?: string): string;
/**
* Detects the case pattern of a word.
* Optimized for the common case (lowercase) by checking it first.
*/
declare function detectCasePattern(word: string): CasePattern;
/**
* Splits a word at camelCase boundaries.
* e.g., "iCloud" -> ["i", "Cloud"], "MacBook" -> ["Mac", "Book"]
*/
declare function splitCamelCase(word: string): null | string[];
/**
* Extracts URLs and emails from text, replacing them with placeholders.
* Returns the modified text and a map to restore originals.
* Placeholders use non-alphanumeric characters to avoid being split by word regex.
*/
declare function extractPreservedPatterns(text: string): {
preserved: Map<string, string>;
text: string;
};
/**
* Normalizes various apostrophe characters to the standard straight apostrophe.
* Handles: ' (U+2019 right single quotation mark), ' (U+2018 left), ʼ (U+02BC modifier letter)
*/
declare function normalizeApostrophes(text: string): string;
/**
* Strips diacritics/accents from text, preserving base letters.
* Converts résumé→resume, naïve→naive, cliché→cliche, café→cafe.
* Uses Unicode NFD decomposition to separate base letters from combining marks.
*/
declare function stripDiacritics(text: string): string;
/**
* Tokenization utilities for splitting text into word and non-word tokens.
*/
/** Regex to split text into word and non-word tokens (includes accented Latin chars).
* Digit lookaround prevents matching letters in escape sequences like \u2014. */
declare const WORD_SPLIT_REGEX: RegExp;
/** Regex to test if a token is a word (includes accented Latin chars) */
declare const WORD_TEST_REGEX: RegExp;
/**
* Token with text content and word/non-word classification.
*/
interface TextToken {
isWord: boolean;
text: string;
}
/**
* Tokenizes IPA text into words and non-words (punctuation/whitespace).
*/
declare function tokenizeIPA(text: string): TextToken[];
/**
* Tokenizes Ingglish/English text into words and non-words.
* Words are sequences of letters and apostrophes.
*/
declare function tokenizeText(text: string): TextToken[];
export { type CasePattern, WORD_SPLIT_REGEX, WORD_TEST_REGEX, applyCasePattern, detectCasePattern, extractPreservedPatterns, normalizeApostrophes, splitCamelCase, stripDiacritics, tokenizeIPA, tokenizeText };
+74
View File
@@ -0,0 +1,74 @@
/**
* Utilities for preserving case patterns during translation.
*/
/**
* Case pattern detected in a word.
* - 'lower': all lowercase (hello)
* - 'upper': all uppercase (HELLO)
* - 'capitalized': first letter uppercase (Hello)
* - 'mixed': mixed case like camelCase (GitHub, iPhone)
*/
type CasePattern = 'capitalized' | 'lower' | 'mixed' | 'upper';
/**
* Applies a case pattern to a word, optionally using original word for mixed case.
* Optimized: lowercase case returns word directly if already lowercase.
*/
declare function applyCasePattern(word: string, pattern: CasePattern, original?: string): string;
/**
* Detects the case pattern of a word.
* Optimized for the common case (lowercase) by checking it first.
*/
declare function detectCasePattern(word: string): CasePattern;
/**
* Splits a word at camelCase boundaries.
* e.g., "iCloud" -> ["i", "Cloud"], "MacBook" -> ["Mac", "Book"]
*/
declare function splitCamelCase(word: string): null | string[];
/**
* Extracts URLs and emails from text, replacing them with placeholders.
* Returns the modified text and a map to restore originals.
* Placeholders use non-alphanumeric characters to avoid being split by word regex.
*/
declare function extractPreservedPatterns(text: string): {
preserved: Map<string, string>;
text: string;
};
/**
* Normalizes various apostrophe characters to the standard straight apostrophe.
* Handles: ' (U+2019 right single quotation mark), ' (U+2018 left), ʼ (U+02BC modifier letter)
*/
declare function normalizeApostrophes(text: string): string;
/**
* Strips diacritics/accents from text, preserving base letters.
* Converts résumé→resume, naïve→naive, cliché→cliche, café→cafe.
* Uses Unicode NFD decomposition to separate base letters from combining marks.
*/
declare function stripDiacritics(text: string): string;
/**
* Tokenization utilities for splitting text into word and non-word tokens.
*/
/** Regex to split text into word and non-word tokens (includes accented Latin chars).
* Digit lookaround prevents matching letters in escape sequences like \u2014. */
declare const WORD_SPLIT_REGEX: RegExp;
/** Regex to test if a token is a word (includes accented Latin chars) */
declare const WORD_TEST_REGEX: RegExp;
/**
* Token with text content and word/non-word classification.
*/
interface TextToken {
isWord: boolean;
text: string;
}
/**
* Tokenizes IPA text into words and non-words (punctuation/whitespace).
*/
declare function tokenizeIPA(text: string): TextToken[];
/**
* Tokenizes Ingglish/English text into words and non-words.
* Words are sequences of letters and apostrophes.
*/
declare function tokenizeText(text: string): TextToken[];
export { type CasePattern, WORD_SPLIT_REGEX, WORD_TEST_REGEX, applyCasePattern, detectCasePattern, extractPreservedPatterns, normalizeApostrophes, splitCamelCase, stripDiacritics, tokenizeIPA, tokenizeText };
+222
View File
@@ -0,0 +1,222 @@
// src/case.ts
function applyCasePattern(word, pattern, original) {
if (pattern === "lower") {
const firstChar = word.codePointAt(0);
if (firstChar >= 97 && firstChar <= 122 && word === word.toLowerCase()) {
return word;
}
return word.toLowerCase();
}
switch (pattern) {
case "capitalized": {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
case "mixed": {
if (original !== void 0 && original.length > 0) {
return applyMixedCase(word, original);
}
return word.toLowerCase();
}
case "upper": {
return word.toUpperCase();
}
default: {
return word.toLowerCase();
}
}
}
function detectCasePattern(word) {
if (word.length === 0) {
return "lower";
}
const firstCode = word.codePointAt(0);
const firstChar = word[0];
const isFirstUpper = firstCode >= 65 && firstCode <= 90 || // A-Z (fast ASCII path)
firstCode > 127 && firstChar !== firstChar.toLowerCase();
if (!isFirstUpper && word === word.toLowerCase()) {
return "lower";
}
if (word.length === 1) {
if (word === "I") {
return "lower";
}
return isFirstUpper ? "capitalized" : "lower";
}
if (word === word.toUpperCase()) {
if (word.length <= 2) {
return "capitalized";
}
return "upper";
}
if (isFirstUpper) {
let restIsLower = true;
for (let i = 1; i < word.length; i++) {
const c = word.codePointAt(i);
if (c >= 65 && c <= 90 || // A-Z
c > 127 && word[i] !== word[i].toLowerCase()) {
restIsLower = false;
break;
}
}
if (restIsLower) {
return "capitalized";
}
}
return "mixed";
}
function splitCamelCase(word) {
if (word.length < 2) {
return null;
}
let hasInternalUpper = false;
for (let i = 1; i < word.length; i++) {
const c = word.codePointAt(i);
if (c >= 65 && c <= 90) {
hasInternalUpper = true;
break;
}
}
if (!hasInternalUpper) {
return null;
}
const parts = [];
let start = 0;
for (let i = 1; i < word.length; i++) {
const prevCode = word.codePointAt(i - 1);
const currCode = word.codePointAt(i);
if (prevCode >= 97 && prevCode <= 122 && currCode >= 65 && currCode <= 90) {
parts.push(word.slice(start, i));
start = i;
}
}
if (parts.length > 0) {
parts.push(word.slice(start));
return parts;
}
return null;
}
function applyMixedCase(translated, original) {
const lowerTranslated = translated.toLowerCase();
let result = "";
let i = 0;
for (const char of lowerTranslated) {
if (i < original.length) {
const origChar = original[i];
result += origChar === origChar.toUpperCase() ? char.toUpperCase() : char;
} else {
result += char;
}
i++;
}
return result;
}
// src/text.ts
var URL_REGEX = /(?:https?|ftp|file):\/\/[^\s<>"')\]]+/gi;
var EMAIL_REGEX = /[\w.%+-]{1,64}@(?:[a-z0-9-]{1,63}\.){1,10}[a-z]{2,63}/gi;
var COMMON_TLDS = "com|org|net|edu|gov|io|co|uk|de|fr|jp|au|ca|ru|ch|it|nl|se|no|es|mil|info|biz|tv|me|app|dev|ai|xyz|rs|site|tech|blog|news|club|lol|new|world|online|space|fun|live|shop|store|gg|fm|im|is|to|cc|ws|ly|gl|be|us|in|eu|asia|pro|cz|pl|fi|dk|pt|ie|nz|za|br|mx|ar|cl|kr|tw|hk|sg|id|th|vn|ph";
var BARE_DOMAIN_REGEX = new RegExp(
String.raw`\b(?:[a-z0-9][-a-z0-9]*\.)+(?:${COMMON_TLDS})\b(?:\/[^\s<>"')\]]*)?`,
"gi"
);
function extractPreservedPatterns(text) {
const preserved = /* @__PURE__ */ new Map();
let counter = 0;
let result = text;
if (text.includes("://")) {
result = result.replaceAll(URL_REGEX, (match) => {
const placeholder = `\0${counter++}\0`;
preserved.set(placeholder, match);
return placeholder;
});
}
if (result.includes("@")) {
result = result.replaceAll(EMAIL_REGEX, (match) => {
const placeholder = `\0${counter++}\0`;
preserved.set(placeholder, match);
return placeholder;
});
}
if (result.includes(".")) {
result = result.replaceAll(BARE_DOMAIN_REGEX, (match) => {
const placeholder = `\0${counter++}\0`;
preserved.set(placeholder, match);
return placeholder;
});
}
return { preserved, text: result };
}
var FANCY_APOSTROPHE = /[\u2018\u2019\u02BC]/g;
var COMBINING_MARKS = /[\u0300-\u036F]/g;
function normalizeApostrophes(text) {
return text.replaceAll(FANCY_APOSTROPHE, "'");
}
function stripDiacritics(text) {
return text.normalize("NFD").replaceAll(COMBINING_MARKS, "");
}
// src/tokenize.ts
var WORD_SPLIT_REGEX = /((?<!\d)[a-zA-Z\u00C0-\u024F']+(?!\d))/;
var WORD_TEST_REGEX = /^[a-zA-Z\u00C0-\u024F']+$/;
var IPA_SYMBOLS_SET = new Set("\u0259\u025D\u025A\u028C\xE6\u0251\u0254\u025B\u026A\u028A\xF0\u03B8\u0283\u0292\u014B\u0279\u0261");
function isIPAChar(char) {
const code = char.codePointAt(0);
if (code >= 65 && code <= 90 || code >= 97 && code <= 122) {
return true;
}
if (code === 8288) {
return true;
}
if (code === 712 || code === 716) {
return true;
}
if (IPA_SYMBOLS_SET.has(char)) {
return true;
}
return false;
}
function tokenizeIPA(text) {
const tokens = [];
let i = 0;
while (i < text.length) {
if (isIPAChar(text[i])) {
let wordEnd = i + 1;
while (wordEnd < text.length && isIPAChar(text[wordEnd])) {
wordEnd++;
}
tokens.push({ isWord: true, text: text.slice(i, wordEnd) });
i = wordEnd;
} else {
let nonWordEnd = i + 1;
while (nonWordEnd < text.length && !isIPAChar(text[nonWordEnd])) {
nonWordEnd++;
}
tokens.push({ isWord: false, text: text.slice(i, nonWordEnd) });
i = nonWordEnd;
}
}
return tokens;
}
function tokenizeText(text) {
const normalized = normalizeApostrophes(text);
const parts = normalized.split(WORD_SPLIT_REGEX);
const tokens = [];
for (const part of parts) {
if (part.length > 0) {
tokens.push({ isWord: WORD_TEST_REGEX.test(part), text: part });
}
}
return tokens;
}
export {
WORD_SPLIT_REGEX,
WORD_TEST_REGEX,
applyCasePattern,
detectCasePattern,
extractPreservedPatterns,
normalizeApostrophes,
splitCamelCase,
stripDiacritics,
tokenizeIPA,
tokenizeText
};
+47
View File
@@ -0,0 +1,47 @@
{
"name": "@ingglish/normalize",
"version": "0.1.1",
"description": "Text normalization, case handling, and URL/email preservation for Ingglish",
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"source": "./src/index.ts",
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
}
}
},
"files": [
"dist"
],
"sideEffects": false,
"engines": {
"node": ">=16"
},
"scripts": {
"build": "tsup",
"build:fast": "tsup src/index.ts --format esm",
"lint": "eslint --cache src",
"test": "vitest run --no-color",
"prepublishOnly": "npm run build"
},
"author": "Paul Tarjan",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/ptarjan/ingglish.git",
"directory": "packages/normalize"
},
"homepage": "https://github.com/ptarjan/ingglish#readme",
"bugs": {
"url": "https://github.com/ptarjan/ingglish/issues"
}
}