Making RUST-RL years ago I struggled for a long time trying to get grammar right in the message log. I made some a(foo) function that appended a or an before the word and it ended up being right some of the time but it wasn’t really reliable at all. I had some other similar function for pluralisation, pl("apple"), which was even less reliable, and then eventually I swapped over to just specifically declaring what the plural was of everything because it was less effort.
I used inflect for the same problem in Python previously and had a look to see if I could find a crate that did anything remotely similar. There were a few but they weren’t very good, so I got massively sidetracked for months porting the library verbatim to Rust. At the time Python’s inflect was one huge thousands-of-lines file and I didn’t want to waste any more time making anything look nice or doing it
My port is inflect-rs, found here. It was sat at version 0.0.0 for a really long time because I couldn’t be bothered getting around to finishing it off after it achieved all that I needed personally at the time, but recently I felt like going back to it, so I spent the last few days converting it to the 7.5.0 version of the Python library, and then fixing some issues that exist in that version.
The full README has all the details but this is the synopsis.
Simple example with pluralization and word-representation of numbers:
use inflect_rs::Engine;
let p = Engine::new();
let count = 1;
let sentence = format!(
"There {} {} {} by the door.",
p.plural_verb_with_count("was", count),
p.number_to_words(count).unwrap(),
p.plural_noun_with_count("person", count),
);
assert_eq!(sentence, "There was one person by the door.");
When count = 243, the same code will generate:
There were two hundred and forty-three people by the door.