Inflection
English words in the right form for counts and labels: plurals and singulars, 3 items, ordinals such as 1st and 22nd, and a or an by how a word sounds.
Version 1.0.0 · 5 functions · 1 crate
Functions
What use inflect.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use inflect_plural, inflect_singular from inflect (Quillo 0.6).
inflect_plural(word: str) → strThe plural of word: "category" is "categories", "person" is "people", "sheep" stays "sheep". Case is kept ("Box" is "Boxes").
inflect_singular(word: str) → strThe singular of word: "categories" is "category", "people" is "person", "analyses" is "analysis".
inflect_count(word: str, count: int) → strcount with word in the right form: "1 item", "0 items", "3 people".
inflect_ordinal(n: int) → strn as an ordinal: "1st", "2nd", "3rd", "11th", "22nd", "113th", "-1st".
inflect_article(word: str) → strword with "a" or "an" before it, by how it sounds: "an apple", "a user", "an hour", "a one-off", "an FAQ", "an 8-hour day".
Get it
$ quillo install inflect # inflect.qwer into this folder$ quillo install inflect --example # and its example, inflect.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/inflect.qwer$ curl -O https://quillolang.org/plugins/files/inflect.qweThen, in your program, all of it or only what you call:
use inflect.qweruse inflect_plural, inflect_singular from inflect // Quillo 0.6: only theseIt uses this crate, which cargo fetches the first time the plugin is built:
pluralizer = "0.6"Example
// Labels that read well whatever the count: "1 comment", "3 replies", "your 2nd order", "an hour ago".//// quillo run inflect.qwe then open http://localhost:8080/labelsuse "inflect.qwer"route GET /labels = { comments: [0, 1, 3] | map((n) => inflect_count("comment", n)), replies: inflect_count("reply", 3), people: inflect_plural("person"), tables: [inflect_plural("category"), inflect_plural("Box"), inflect_singular("analyses"), inflect_singular("people")], orders: [1, 2, 3, 11, 22, 113] | map((n) => `your ${inflect_ordinal(n)} order`), articles: ["apple", "user", "hour", "FAQ", "URL", "8-hour day", "one-off"] | map((w) => inflect_article(w)),}route GET /plural ? { word: str } = { singular: inflect_singular(word), plural: inflect_plural(word) }$ quillo run inflect.qwe$ curl 'localhost:8080/labels'$ curl 'localhost:8080/plural?word=mouse'Source
//! English words in the right form for counts and labels: plurals and singulars ("person" and "people", "category" and "categories"), "3 items", "1st, 2nd, 23rd", and "a" or "an".//! version: 1.0.0//!//! [dependencies]//! pluralizer = "0.6"/// The plural of `word`: "category" is "categories", "person" is "people", "sheep" stays "sheep". Case is kept ("Box" is "Boxes").pub fn inflect_plural(word: &str) -> String { pluralizer::pluralize(word, 2, false)}/// The singular of `word`: "categories" is "category", "people" is "person", "analyses" is "analysis".pub fn inflect_singular(word: &str) -> String { pluralizer::pluralize(word, 1, false)}/// `count` with `word` in the right form: "1 item", "0 items", "3 people".pub fn inflect_count(word: &str, count: i64) -> String { format!("{} {}", count, pluralizer::pluralize(word, if count == 1 { 1 } else { 2 }, false))}/// `n` as an ordinal: "1st", "2nd", "3rd", "11th", "22nd", "113th", "-1st".pub fn inflect_ordinal(n: i64) -> String { let a = n.unsigned_abs(); let suffix = match (a % 10, a % 100) { (_, 11..=13) => "th", (1, _) => "st", (2, _) => "nd", (3, _) => "rd", _ => "th", }; format!("{}{}", n, suffix)}/// `word` with "a" or "an" before it, by how it sounds: "an apple", "a user", "an hour", "a one-off", "an FAQ", "an 8-hour day".pub fn inflect_article(word: &str) -> String { let w = word.trim(); let l = w.to_lowercase(); let first = l.chars().next().unwrap_or(' '); let an = if ["hour", "honest", "honor", "honour", "heir"].iter().any(|p| l.starts_with(p)) { true } else if ["uni", "use", "usu", "uti", "ufo", "eu", "ewe", "one", "once", "ure"].iter().any(|p| l.starts_with(p)) { false } else if first.is_ascii_digit() { // "an 8", "an 11", "an 18", "an 80": the numbers said with a vowel first. let digits: String = l.chars().take_while(|c| c.is_ascii_digit()).collect(); digits.starts_with('8') || digits == "11" || digits == "18" || (digits.len() % 3 == 2 && (digits.starts_with("11") || digits.starts_with("18"))) } else if w.len() > 1 && w.chars().take_while(|c| c.is_alphabetic()).all(|c| c.is_uppercase()) && w.chars().take(2).all(|c| c.is_alphabetic()) { // Initials are said letter by letter: "an FAQ", "an SQL query", "a URL". "AEFHILMNORSX".contains(w.chars().next().unwrap_or(' ')) && !w.starts_with("URL") && !w.starts_with("UK") && !w.starts_with("US") } else { "aeiou".contains(first) }; format!("{} {}", if an { "an" } else { "a" }, w)}Related
case.qwer · 1.0.0Text in snake_case, camelCase, PascalCase, kebab-case, CONSTANT_CASE, Title Case and Sentence case: column names as API keys and labels.7 views · 8 downloadsHumanize humanize.qwer · 1.0.0Durations, file sizes and relative times as people write them and back: 2h 30m, 1.5 GB, 2 GiB, 3 days ago, in 2 hours.7 views · 11 downloadsJapanese kana kana.qwer · 1.0.0Japanese kana: romaji typed as hiragana or katakana, kana written in Hepburn romaji, hiragana and katakana each to the other, half-width katakana from bank and till systems made full-width, and which scripts a text uses.6 views · 10 downloadsProfanity filter profanity.qwer · 1.0.0Offensive words found and masked in comments, usernames and chat, with l33t spellings and spaced-out letters caught, and a severity per kind for moderation queues.8 views · 8 downloadsSpam filter spam_filter.qwer · 1.0.0A spam filter that learns from your own messages: naive Bayes trained on what users mark, kept in a file, scoring comments and forms from 0 to 1.7 views · 7 downloadsSpellcheck spellcheck.qwer · 1.0.0Spelling checked against a word list you load, in any language: unknown words found with their places, suggestions ranked by closeness and frequency (SymSpell's method), text corrected for "did you mean", and your own words added.7 views · 8 downloadsThread 0 posts
On Inflection: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.