// English detection for the first scored cohort. // // The readability formulas, syllable heuristic, whitespace tokenizer and // Zipf list are English-specific. Detecting the language before scoring keeps // the other languages out of a percentile comparison they did not enter // fairly. This is a filter, not a scorer; it stays outside the dependency-free // scoring core. import { franc } from 'franc' const MIN_LENGTH = 40 const SAMPLE = 4_000 export const ENGLISH = 'eng' /** ISO 639-3 code, or undefined when the sample is too short or ambiguous. */ export function detectLanguage(text) { if (typeof text !== 'string') return undefined const sample = text.trim().slice(0, SAMPLE) if (sample.length < MIN_LENGTH) return undefined const code = franc(sample, { minLength: MIN_LENGTH }) return code === 'und' ? undefined : code }