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

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

    Subsonic 3 has an Inflector class which impressed me by turning Person into People. I peeked at the source and found it naturally cheats a little with a hardcoded list but that's really the only way of doing it in English and how humans do it - we remember the singular and plural of each word and don't just apply a rule. As there's not masculine/feminine(/neutral) to add to the mix it's a lot simpler.

    Here's a snippet:

    AddSingularRule("^(ox)en", "$1");
    AddSingularRule("(vert|ind)ices$", "$1ex");
    AddSingularRule("(matr)ices$", "$1ix");
    AddSingularRule("(quiz)zes$", "$1");
    
    AddIrregularRule("person", "people");
    AddIrregularRule("man", "men");
    AddIrregularRule("child", "children");
    AddIrregularRule("sex", "sexes");
    AddIrregularRule("tax", "taxes");
    AddIrregularRule("move", "moves");
    
    AddUnknownCountRule("equipment");
    

    It accounts for some words not having plural equivalents, like the equipment example. As you can probably tell it does a simple Regex replace using $1.

    Update:
    It appears Subsonic's Inflector is infact the Castle ActiveRecord Inflector class!

提交回复
热议问题