Humanizr / Humanizer

Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities
Other
8.6k stars 956 forks source link

Pluralize / singularize verbs? #343

Open piranout opened 9 years ago

piranout commented 9 years ago

Are ubiquitous verbs on the horizon? This could make so many programs "sound" friendlier:

string PeopleInLinePhrase(int numberOfPeople) {
    int x = numberOfPeople;
    return string.Format(
        "There {0} {1} {2} in line ahead of you", 
        "is".ToQuantity(x), 
        x, 
        "person".ToQuantity(x));
    // result (maintains given capitalization of "is" and "person"):
    // x = 0: "are no people"
    // x = 1: "is 1 person"
    // x > 1: "are x people"
}

Regardless what the answer is, thanks for publishing this fantastic and eminently usable project!

MehdiK commented 9 years ago

English rules have proven rather complex to add to the library in the past (although not quite the same use case). To be honest with you I'm not sure when no should be used instead of 0. For is and are there is a relatively easy hack to make it work. Although not grammatically correct, we can add them to the reflector dictionary.

Small nitpicking: "person".ToQuantity(x) prefixes the word with the number; e.g. 2 people.

P.S. Thanks for the kind words. Glad you like it.

dbmercer commented 1 year ago

This would be cool. I currently use the following to handle this:

public static string SelectVerbByQuantity( int quantity, string singular, string plural ) => quantity == 1 ? singular : plural;