import { normalizeText } from './normalize.js';
/**
* Whether text reads as English (or the Nigerian Pidgin the emergency patterns
* cover), so it can be screened for emergency signs (H-D). Text that doesn't
* ("Mama mi ko le mi") is reported as not screened, with the emergency number.
* A rough test on purpose: most words must be everyday English, Pidgin or
* health words, or names from the text it is checked against, and none may be
* a common Yoruba, Igbo or Hausa word: "Please help, mama mi ko le mi since
* morning" is mostly English, but the part that matters isn't (N8).
*/
const EVERYDAY = `a about above after again against all almost also always am an and any anything are around as ask asked at away back bad
badly be because been before being below better between big both but by call called came can cannot cant come comes coming could day days
did didnt do does doesnt doing done dont down during each early even ever every everything feel feeling feels felt few find fine first for
from get gets getting give given go goes going gone good got had has hasnt have having he her here hers herself him himself his how i if
im in into is isnt it its just keep keeps kept know last late less let like little long look looks looked lot lots made make makes many may
me mean means might mine more morning most much must my myself need needs never new next night no not nothing now of off often oh ok okay
on once one only or other our out over own please put quite really right said same saw say says see seem seems seen she should since so some
something sometimes soon still such sure take taken takes taking tell than thank thanks that thats the their them then there these they
thing things think this those though through time times to today tomorrow tonight too took two under until up us use used very want wanted
was wasnt way we week well went were what when where whether which while who why will with without woke wont would yes yesterday yet you
your yours hour hours minute minutes since started start starting stopped stop stopping won wouldnt couldnt shouldnt hes shes theyre youre
mum mom mother dad father mama papa daddy mummy wife husband son daughter baby child children grandma grandmother grandpa sister brother
friend auntie aunty uncle old young man woman boy girl problem problems question questions worried worry wrong matter help helps advice check
checks result results test tests safe normal usual instead together either neither answer`;
const PIDGIN = `dey don fit no wan wetin abeg abi sabi una oga pikin wahala na go come am di dem make sef sha ehn o oo shey dis dat comot
belle bodi body small plenty well well baba`;
const HEALTH = `ache aches aching allergic allergy appetite arm arms back bleed bleeding bleeds blood bloody body bones bowel brain breath
breathe breathing breathless bruise bruises bruising burn burning chest chills choke choking cold collapse collapsed confused confusion
constipation cough coughing cramp cramps diarrhoea diarrhea dizzy dizziness doctor dose drowsy ear ears eye eyes face faint fainted fainting
fall fell fever fit fits flu foot feet hand hands head headache heart heartbeat hospital hot hurt hurts hurting ill infection itch itching
itchy jaw joint joints kidney leg legs lips liver lump lungs medicine medicines migraine mouth muscle muscles nausea neck nose numb numbness
pain painful pains pale palpitations pee period pharmacist pills poo pressure pulse rash runny seizure shake shaking shivering short sick
skin sleep sleepy slow sneezing sore speech spinning stiff stomach stool stools sugar sweat sweating sweats swallow swelling swollen
symptoms tablet tablets temperature throat tight tingling tired tongue tooth toothache unconscious unwell urine vision vomit vomited vomiting
vomits weak weakness wheeze wheezing wound wounds yellow`;
// More of the commonest English words, so an ordinary question ("Can tramadol, at high doses, cause seizures?") reads as English.
const COMMON = `able across act actually add added age ago ahead air alone along already alright although among amount another anyone anyway
area aside asking awake bed best bit body born bottle box break bring brought buy care careful carry case cause caused causes causing
change changed cheap clear close cost course cut daily dangerous dear different difficult doing dont drink drinking drop drug drugs
due easy eat eating effect effects else end enough evening everyone exactly except expensive fast few fine finish food free full
gave general give giving glass happen happened happening happens hard harm have heavy high higher hold home hope hot however hurt idea
important instead keep kind knee knew known large later least leave left level life light likely living low lower lunch main maybe meal
medical medication medications might mind miss missed month months move moving near nearly never next nice none normal note number
okay once open order others outside packet part past pay people per perhaps person pick place plan point possible prescribed prescription
pretty prevent prevents price probably quick quickly rather reason red regular remember rest return risk risky room run safe safely
same school serious set several should side sign signs simple single size slowly soft soon sort sound speak spend stay strong strength
stuff such suddenly suppose talk talking taste than therefore thought three tiny together told top total tried true try trying turn
type under understand unless usually wait waiting wake walk walking water weight while whole wife within word work working worse worst
year years young`;
const KNOWN: ReadonlySet<string> = new Set(`${EVERYDAY} ${PIDGIN} ${HEALTH} ${COMMON}`.split(/\s+/).filter(Boolean));
/** A word, or the word it comes from ("doses", "caused", "thinning", "slowly"). */
const known = (word: string, alsoKnown: ReadonlySet<string>) => [word, word.replace(/s$/, ''), word.replace(/es$/, ''), word.replace(/ed$/, ''),
word.replace(/d$/, ''), word.replace(/ing$/, ''), word.replace(/ing$/, 'e'), word.replace(/ly$/, '')]
.some((form) => form.length >= 2 && (KNOWN.has(form) || alsoKnown.has(form)));
/** Everyday Yoruba, Igbo and Hausa words that are neither English nor Pidgin. */
// Not here: words that are also names or forms of address in Nigerian English (Ese, Mo, Omo, Iya, Ojo, Owo, Nne, Nna; L5).
const NOT_ENGLISH: ReadonlySet<string> = new Set(`mi ko le ara ya ti ni wa je se wo lo gbo egbon aburo oogun ori inu sugbon nitori
kini bawo nibo osi otun ahu nwa adighi biko nke ciwo sosai yana tana wannan numfashi iku ume`.split(/\s+/).filter((word) => !KNOWN.has(word)));
/** An everyday English (or Pidgin) word, or a form of one: never read as a misspelt medicine name (M3). */
export const isEnglishWord = (word: string) => known(word, new Set());
/** Mostly words in English (or Pidgin), counting any word in `alsoKnown` (medicine names, the text it draws on). */
export function readsAsEnglish(text: string, alsoKnown: ReadonlySet<string> = new Set()): boolean {
const words = normalizeText(text).split(/[^a-z]+/).filter((word) => word.length >= 2);
if (!words.length) return false;
if (words.some((word) => NOT_ENGLISH.has(word) && !alsoKnown.has(word))) return false;
return words.filter((word) => known(word, alsoKnown)).length / words.length >= 0.5;
}