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

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

    Using Microsoft's Northwind example database:

     System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(new System.Globalization.CultureInfo("en-US"));
    

    Singularize does not Singularize "Order_Details" It returns "Order_Details" with the s at the end. What is the work around?

    0 讨论(0)
  • 2020-12-07 11:40

    I whipped one together based on the Rails pluralizer. You can see my blog post here, or on github here

    output = Formatting.Pluralization(100, "sausage"); 
    
    0 讨论(0)
  • 2020-12-07 11:42

    I cheated in Java - I wanted to be able to produce a correct string for "There were n something(s)", so I wrote the foll. little overloaded utility method:

    static public String pluralize(int val, String sng) {
        return pluralize(val,sng,(sng+"s"));
        }
    
    static public String pluralize(int val, String sng, String plu) {
        return (val+" "+(val==1 ? sng : plu)); 
        }
    

    invoked like so

    System.out.println("There were "+pluralize(count,"something"));
    System.out.println("You have broken "+pluralize(count,"knife","knives"));
    
    0 讨论(0)
  • 2020-12-07 11:46

    This page shows how to use PluralizationService of System.Data.Entity (.NET Framework 4.0)

    http://zquanghoangz.blogspot.it/2012/02/beginner-with-pluralizationservices.html

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题