Beispiel #1
0
        private string PublicSingularize(string word)
        {
            // words that we know of
            if (_userDictionary.ExistsInSecond(word))
            {
                return(_userDictionary.GetFirstValue(word));
            }

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

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

            if (IsNoOpWord(suffixWord))
            {
                return(prefixWord + suffixWord);
            }

            // handle the word that is the same as the plural form
            if (IsUninflective(suffixWord))
            {
                return(prefixWord + suffixWord);
            }

            // if word is one of the known singular words, then just return

            if (_knownSingluarWords.Contains(suffixWord.ToLowerInvariant()))
            {
                return(prefixWord + suffixWord);
            }

            // handle simple irregular verbs, e.g. was -> were
            if (_irregularVerbPluralizationService.ExistsInSecond(suffixWord))
            {
                return(prefixWord + _irregularVerbPluralizationService.GetFirstValue(suffixWord));
            }

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

            // handle singluarization for words ending with sis and pluralized to ses,
            // e.g. "ses" -> "sis"
            if (_wordsEndingWithSisPluralizationService.ExistsInSecond(suffixWord))
            {
                return(prefixWord + _wordsEndingWithSisPluralizationService.GetFirstValue(suffixWord));
            }

            // handle words ending with se, e.g. "ses" -> "se"
            if (_wordsEndingWithSePluralizationService.ExistsInSecond(suffixWord))
            {
                return(prefixWord + _wordsEndingWithSePluralizationService.GetFirstValue(suffixWord));
            }

            // handle words ending with sus, e.g. "suses" -> "sus"
            if (_wordsEndingWithSusPluralizationService.ExistsInSecond(suffixWord))
            {
                return(prefixWord + _wordsEndingWithSusPluralizationService.GetFirstValue(suffixWord));
            }

            string newSuffixWord;

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

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

            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "teeth"
            },
                                         s => s.Remove(s.Length - 4, 4) + "ooth", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "geese"
            },
                                         s => s.Remove(s.Length - 4, 4) + "oose", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "feet"
            },
                                         s => s.Remove(s.Length - 3, 3) + "oot", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "zoa"
            },
                                         s => s.Remove(s.Length - 2, 2) + "oon", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            // [cs]h and ss that take es as plural form, this is being moved up since the sses will be override by the ses
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "ches", "shes", "sses"
            },
                                         s => s.Remove(s.Length - 2, 2), Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }


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

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

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

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

            if (_wordsEndingWithInxAnxYnxPluralizationService.ExistsInSecond(suffixWord))
            {
                return(prefixWord + _wordsEndingWithInxAnxYnxPluralizationService.GetFirstValue(suffixWord));
            }

            // f, fe that take ves as plural form
            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "alves", "elves", "olves", "eaves", "arves"
            },
                                         s => s.Remove(s.Length - 3, 3) + "f", Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            if (TryInflectOnSuffixInWord(suffixWord,
                                         new List <string> {
                "nives", "lives", "wives"
            },
                                         s => s.Remove(s.Length - 3, 3) + "fe", 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> {
                "ays", "eys", "iys", "oys", "uys"
            },
                                         s => s.Remove(s.Length - 1, 1), Culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }

            //

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

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

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

            //



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

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

            if (suffixWord.EndsWith("oes", true, Culture))
            {
                return(prefixWord + suffixWord.Remove(suffixWord.Length - 2, 2));
            }

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

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

            // word is a singlar
            return(prefixWord + suffixWord);
        }