Is there any algorithm in c# to singularize - pluralize a word?

前端 未结 11 1837
臣服心动
臣服心动 2020-12-07 10:55

Is there any algorithm in c# to singularize - pluralize a word (in english) or does exist a .net library to do this (may be also in different languages)?

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 11:53

    I've created a tiny library for this in .net (C#), called Pluralizer (unsurprisingly).

    It's meant to work with full sentences, something like String.Format does.

    It basically works like this:

    var target = new Pluralizer();
    var str = "There {is} {_} {person}.";
    
    var single = target.Pluralize(str, 1);
    Assert.AreEqual("There is 1 person.", single);
    
    // Or use the singleton if you're feeling dirty:
    var several = Pluralizer.Instance.Pluralize(str, 47);
    Assert.AreEqual("There are 47 people.", several);
    

    It can also do way more than that. Read more about it on my blog. It's also available in NuGet.

提交回复
热议问题