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

前端 未结 11 1835
臣服心动
臣服心动 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:32

    Not much documentation from MSDN on the specific usage of the PluralizationService class so here is a unit test class (NUnit) to show basic usage. Notice the odd test case at the bottom that shows the service isn't perfect when it comes to non-standard plural forms.

    [TestFixture]
    public class PluralizationServiceTests
    {
        [Test]
        public void Test01()
        {
            var service = PluralizationService.CreateService(CultureInfo.CurrentCulture);
    
            Assert.AreEqual("tigers", service.Pluralize("tiger"));
            Assert.AreEqual("processes", service.Pluralize("process"));
            Assert.AreEqual("fungi", service.Pluralize("fungus"));
    
            Assert.AreNotEqual("syllabi", service.Pluralize("syllabus")); // wrong pluralization
        }
    }
    

提交回复
热议问题