feat(glossy): improve pluralize

This commit is contained in:
swaits committed 2024-12-18 06:08:52 -07:00
1 parent b1aa11705c
commit ba8fdda1bf
2 files changed
+418 -12

No files matched your search

+110 -12
View File
@@ -10,29 +10,127 @@
// word: The singular word to pluralize
// Returns: The pluralized form of the word, or none if input is none
#let __pluralize(word) = {
// early exit with invalid input
// Early exit for invalid input
if word == none {
return none
}
// Define helper functions for checking endings
// Helper functions for checking endings
let ends_with = (suffix) => lower(word).ends-with(suffix)
let ends_with_any = (suffixes) => suffixes.any((suffix) => ends_with(suffix))
// Handle special cases for "es" endings (s, x, z, sh, ch)
if ends_with_any(( "s", "x", "z", "sh", "ch" )) {
// Map of irregular plurals for common words
let irregulars = (
"alumna": "alumnae",
"alumnus": "alumni",
"analysis": "analyses",
"appendix": "appendices",
"basis": "bases",
"cactus": "cacti",
"child": "children",
"crisis": "crises",
"criterion": "criteria",
"datum": "data",
"foot": "feet",
"fungus": "fungi",
"goose": "geese",
"man": "men",
"medium": "media",
"mouse": "mice",
"nucleus": "nuclei",
"oasis": "oases",
"person": "people",
"phenomenon": "phenomena",
"stimulus": "stimuli",
"thesis": "theses",
"tooth": "teeth",
"woman": "women",
)
// Set of plural-only words
let plural_only = (
"advice",
"alumni",
"bison",
"children",
"criteria",
"data",
"deer",
"feet",
"fish",
"geese",
"information",
"media",
"men",
"mice",
"money",
"moose",
"octopi",
"octopodes",
"people",
"scissors",
"series",
"sheep",
"species",
"teeth",
"trousers",
"women",
)
// Irregular suffixes
let f_to_ves = ("leaf", "loaf", "calf", "half", "wolf", "thief")
let fe_to_ves = ("wife", "knife", "life")
let o_to_os = ("photo", "piano", "halo", "radio", "video", "studio", "solo", "taco", "memo", "zero")
// Convenience
let w = lower(word)
// Check for words already plural
if plural_only.contains(w) {
return word
}
// Check for irregular singulars
if irregulars.keys().contains(w) {
return irregulars.at(w)
}
// Standard pluralization rules
if ends_with("iz") {
word + "zes"
} else if ends_with_any(("s", "x", "z", "sh", "ch")) {
word + "es"
} else if ends_with("y") and not ends_with_any(( "a", "e", "i", "o", "u" )) {
// Convert y to ies when y follows a consonant
word.slice(0, -1) + "ies"
} else if ends_with("y") {
if not ends_with_any(("ay", "ey", "iy", "oy", "uy")) {
word.slice(0, -1) + "ies"
} else {
word + "s"
}
} else if ends_with("f") {
// Convert f to ves
word.slice(0, -1) + "ves"
if f_to_ves.contains(w) {
word.slice(0, -1) + "ves"
} else {
word + "s"
}
} else if ends_with("fe") {
// Convert fe to ves
word.slice(0, -2) + "ves"
if fe_to_ves.contains(w) {
word.slice(0, -2) + "ves"
} else {
word + "s"
}
} else if ends_with("us") {
word.slice(0, -2) + "i"
} else if ends_with("is") {
word.slice(0, -2) + "es"
} else if ends_with("on") {
word + "s"
} else if ends_with("o") {
if o_to_os.contains(w) {
word + "s"
} else {
word + "es"
}
} else {
// Default case: just add s
word + "s"
}
}