Esempio n. 1
0
        private string PublicPluralize(string word)
        {
            // words that we know of
            if (_userDictionary.ExistsInFirst(word))
            {
                return(_userDictionary.GetSecondValue(word));
            }

            if (IsNoOpWord(word))
            {
                return(word);
            }

            string prefixWord;
            string suffixWord = GetSuffixWord(word, out prefixWord);

            // by me -> by me
            if (IsNoOpWord(suffixWord))
            {
                return(prefixWord + suffixWord);
            }

            // handle the word that do not inflect in the plural form
            if (IsUninflective(suffixWord))
            {
                return(prefixWord + suffixWord);
            }

            // if word is one of the known plural forms, then just return
            if (_knownPluralWords.Contains(suffixWord.ToLowerInvariant()) || IsPlural(suffixWord))
            {
                return(prefixWord + suffixWord);
            }

            // handle irregular plurals, e.g. "ox" -> "oxen"
            if (_irregularPluralsPluralizationService.ExistsInFirst(suffixWord))
            {
                return(prefixWord + _irregularPluralsPluralizationService.GetSecondValue(suffixWord));
            }

            string newSuffixWord;

            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "man"
            },
                                         s => s.Remove(s.Length - 2, 2) + "en", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            // handle irregular inflections for common suffixes, e.g. "mouse" -> "mice"
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "louse", "mouse"
            },
                                         s => s.Remove(s.Length - 4, 4) + "ice", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "tooth"
            },
                                         s => s.Remove(s.Length - 4, 4) + "eeth", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "goose"
            },
                                         s => s.Remove(s.Length - 4, 4) + "eese", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "foot"
            },
                                         s => s.Remove(s.Length - 3, 3) + "eet", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "zoon"
            },
                                         s => s.Remove(s.Length - 3, 3) + "oa", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "cis", "sis", "xis"
            },
                                         s => s.Remove(s.Length - 2, 2) + "es", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            // handle assimilated classical inflections, e.g. vertebra -> vertebrae
            if (_assimilatedClassicalInflectionPluralizationService.ExistsInFirst(suffixWord))
            {
                return(prefixWord + _assimilatedClassicalInflectionPluralizationService.GetSecondValue(suffixWord));
            }

            // Handle the classical variants of modern inflections
            //
            if (_classicalInflectionPluralizationService.ExistsInFirst(suffixWord))
            {
                return(prefixWord + _classicalInflectionPluralizationService.GetSecondValue(suffixWord));
            }

            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "trix"
            },
                                         s => s.Remove(s.Length - 1, 1) + "ces", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "eau", "ieu"
            },
                                         s => s + "x", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            if (_wordsEndingWithInxAnxYnxPluralizationService.ExistsInFirst(suffixWord))
            {
                return(prefixWord + _wordsEndingWithInxAnxYnxPluralizationService.GetSecondValue(suffixWord));
            }

            // [cs]h and ss that take es as plural form
            if (TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "ch", "sh", "ss"
            }, s => s + "es", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            // f, fe that take ves as plural form
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "alf", "elf", "olf", "eaf", "arf"
            },
                                         s => s.EndsWith("deaf", true, Culture) ? s : s.Remove(s.Length - 1, 1) + "ves", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "nife", "life", "wife"
            },
                                         s => s.Remove(s.Length - 2, 2) + "ves", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            // y takes ys as plural form if preceded by a vowel, but ies if preceded by a consonant, e.g. stays, skies
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "ay", "ey", "iy", "oy", "uy"
            },
                                         s => s + "s", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            //

            if (suffixWord.EndsWith("y", true, Culture))
            {
                return(prefixWord + suffixWord.Remove(suffixWord.Length - 1, 1) + "ies");
            }

            // handle some of the words o -> os, and [vowel]o -> os, and the rest are o->oes
            if (_oSuffixPluralizationService.ExistsInFirst(suffixWord))
            {
                return(prefixWord + _oSuffixPluralizationService.GetSecondValue(suffixWord));
            }

            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "ao", "eo", "io", "oo", "uo"
            },
                                         s => s + "s", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            if (suffixWord.EndsWith("o", true, Culture) || suffixWord.EndsWith("s", true, Culture))
            {
                return(prefixWord + suffixWord + "es");
            }

            if (suffixWord.EndsWith("x", true, Culture))
            {
                return(prefixWord + suffixWord + "es");
            }

            // cats, bags, hats, speakers
            return(prefixWord + suffixWord + "s");
        }