Text

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

#text#labels#english#forms

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) → str

The plural of word: "category" is "categories", "person" is "people", "sheep" stays "sheep". Case is kept ("Box" is "Boxes").

inflect_singular(word: str) → str

The singular of word: "categories" is "category", "people" is "person", "analyses" is "analysis".

inflect_count(word: str, count: int) → str

count with word in the right form: "1 item", "0 items", "3 people".

inflect_ordinal(n: int) → str

n as an ordinal: "1st", "2nd", "3rd", "11th", "22nd", "113th", "-1st".

inflect_article(word: str) → str

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".

Get it

$ quillo install inflect               # inflect.qwer into this folder$ quillo install inflect --example     # and its example, inflect.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/inflect.qwer$ curl -O https://quillolang.org/plugins/files/inflect.qwe

Then, in your program, all of it or only what you call:

main.qwe
use inflect.qweruse inflect_plural, inflect_singular from inflect      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
pluralizer = "0.6"

Example

inflect.qwe
// 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

inflect.qwer
//! 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)}
Thread
67Useful? Like it, or add to its thread below.

Thread 0 posts

On Inflection: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.