Normalize for resolveTerm
This commit is contained in:
parent
341581e485
commit
0931663f57
1 changed files with 14 additions and 14 deletions
28
script.js
28
script.js
|
|
@ -142,46 +142,46 @@ const specialCaseMap = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const resolveTerm = (term) => {
|
const resolveTerm = (term) => {
|
||||||
const normalized = term.replace(/’/g, "'");
|
const normalized = term.replace(/'/g, "'");
|
||||||
const special = specialCaseMap[normalized];
|
const special = specialCaseMap[normalized];
|
||||||
if (special && entryMap.has(special)) {
|
if (special && entryMap.has(special)) {
|
||||||
return special;
|
return special;
|
||||||
}
|
}
|
||||||
if (entryMap.has(term)) {
|
if (entryMap.has(normalized)) {
|
||||||
return term;
|
return normalized;
|
||||||
}
|
}
|
||||||
|
|
||||||
const lower = term.toLowerCase();
|
const lower = normalized.toLowerCase();
|
||||||
const candidates = [];
|
const candidates = [];
|
||||||
|
|
||||||
// Singularize
|
// Singularize
|
||||||
if (lower.endsWith("ies")) {
|
if (lower.endsWith("ies")) {
|
||||||
candidates.push(term.slice(0, -3) + "Y");
|
candidates.push(normalized.slice(0, -3) + "Y");
|
||||||
}
|
}
|
||||||
if (lower.endsWith("ves")) {
|
if (lower.endsWith("ves")) {
|
||||||
candidates.push(term.slice(0, -3) + "F");
|
candidates.push(normalized.slice(0, -3) + "F");
|
||||||
candidates.push(term.slice(0, -3) + "FE");
|
candidates.push(normalized.slice(0, -3) + "FE");
|
||||||
}
|
}
|
||||||
if (lower.endsWith("es")) {
|
if (lower.endsWith("es")) {
|
||||||
candidates.push(term.slice(0, -2));
|
candidates.push(normalized.slice(0, -2));
|
||||||
}
|
}
|
||||||
if (lower.endsWith("s")) {
|
if (lower.endsWith("s")) {
|
||||||
candidates.push(term.slice(0, -1));
|
candidates.push(normalized.slice(0, -1));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pluralize
|
// Pluralize
|
||||||
if (lower.endsWith("y") && !/[aeiou]y$/.test(lower)) {
|
if (lower.endsWith("y") && !/[aeiou]y$/.test(lower)) {
|
||||||
candidates.push(term.slice(0, -1) + "IES");
|
candidates.push(normalized.slice(0, -1) + "IES");
|
||||||
}
|
}
|
||||||
if (lower.endsWith("f")) {
|
if (lower.endsWith("f")) {
|
||||||
candidates.push(term.slice(0, -1) + "VES");
|
candidates.push(normalized.slice(0, -1) + "VES");
|
||||||
}
|
}
|
||||||
if (lower.endsWith("fe")) {
|
if (lower.endsWith("fe")) {
|
||||||
candidates.push(term.slice(0, -2) + "VES");
|
candidates.push(normalized.slice(0, -2) + "VES");
|
||||||
}
|
}
|
||||||
candidates.push(`${term}S`);
|
candidates.push(`${normalized}S`);
|
||||||
if (/(s|x|z|ch|sh)$/i.test(lower)) {
|
if (/(s|x|z|ch|sh)$/i.test(lower)) {
|
||||||
candidates.push(`${term}ES`);
|
candidates.push(`${normalized}ES`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return candidates.find((candidate) => entryMap.has(candidate)) || null;
|
return candidates.find((candidate) => entryMap.has(candidate)) || null;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue