Example #1
0
 internal static bool TryGetMatchedSuffixForWord(string word, IEnumerable <string> suffixes, CultureInfo culture, out string matchedSuffix)
 {
     matchedSuffix = null;
     if (PluralizationServiceUtil.DoesWordContainSuffix(word, suffixes, culture))
     {
         matchedSuffix = suffixes.First((string s) => word.EndsWith(s, true, culture));
         return(true);
     }
     return(false);
 }
Example #2
0
        internal static bool TryInflectOnSuffixInWord(string word, IEnumerable <string> suffixes, Func <string, string> operationOnWord, CultureInfo culture, out string newWord)
        {
            newWord = null;
            string text;

            if (PluralizationServiceUtil.TryGetMatchedSuffixForWord(word, suffixes, culture, out text))
            {
                newWord = operationOnWord(word);
                return(true);
            }
            return(false);
        }
Example #3
0
        private string InternalSingularize(string word)
        {
            // words that we know of
            if (_userDictionary.ExistsInSecond(word))
            {
                return(_userDictionary.GetFirstValue(word));
            }
            if (IsNoOpWord(word))
            {
                return(word);
            }
            string prefixWord, 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));
            }
            string newSuffixWord;

            // handle irregular inflections for common suffixes, e.g. "mouse" -> "mice"
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "men"
            }, (s) => s.Remove(s.Length - 2, 2) + "an", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "lice", "mice"
            }, (s) => s.Remove(s.Length - 3, 3) + "ouse", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "teeth"
            }, (s) => s.Remove(s.Length - 4, 4) + "ooth", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "geese"
            }, (s) => s.Remove(s.Length - 4, 4) + "oose", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "feet"
            }, (s) => s.Remove(s.Length - 3, 3) + "oot", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.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 (PluralizationServiceUtil.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
            // CONSIDER here is the only place we took the classical variants instead of the anglicized
            if (_classicalInflectionPluralizationService.ExistsInSecond(suffixWord))
            {
                return(prefixWord + _classicalInflectionPluralizationService.GetFirstValue(suffixWord));
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "trices"
            }, (s) => s.Remove(s.Length - 3, 3) + "x", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "eaux", "ieux"
            }, (s) => s.Remove(s.Length - 1, 1), _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "inges", "anges", "ynges"
            }, (s) => s.Remove(s.Length - 3, 3) + "x", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            // f, fe that take ves as plural form
            if (PluralizationServiceUtil.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 (PluralizationServiceUtil.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 (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "ays", "eys", "iys", "oys", "uys"
            }, (s) => s.Remove(s.Length - 1, 1), _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            // CONSIDER proper noun handling, Marys, Tonys, ignore for now
            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 (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "aos", "eos", "ios", "oos", "uos"
            }, (s) => suffixWord.Remove(suffixWord.Length - 1, 1), _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            // CONSIDER limitation on the lines below, e.g. crisis -> crises -> cris
            // all the word ending with sis, xis, cis, their plural form cannot be singluarized correctly,// since words ending with c and cis both will get pluralized to ces
            // after searching the dictionary, the number of cis is just too small(7) that
            // we treat them as special case
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "ces"
            }, (s) => s.Remove(s.Length - 1, 1), _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.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);
        }
Example #4
0
 private bool IsUninflective(string word)
 {
     return(PluralizationServiceUtil.DoesWordContainSuffix(word, _uninflectiveSuffixes, _culture) || (!word.ToLower(_culture).Equals(word) && word.EndsWith("ese", false, _culture)) || _uninflectiveWords.Contains(word.ToLowerInvariant()));
 }
Example #5
0
        private string InternalPluralize(string word)
        {
            // words that we know of
            if (_userDictionary.ExistsInFirst(word))
            {
                return(_userDictionary.GetSecondValue(word));
            }
            if (IsNoOpWord(word))
            {
                return(word);
            }
            string prefixWord, 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;

            // handle irregular inflections for common suffixes, e.g. "mouse" -> "mice"
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "man"
            }, (s) => s.Remove(s.Length - 2, 2) + "en", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "louse", "mouse"
            }, (s) => s.Remove(s.Length - 4, 4) + "ice", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "tooth"
            }, (s) => s.Remove(s.Length - 4, 4) + "eeth", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "goose"
            }, (s) => s.Remove(s.Length - 4, 4) + "eese", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "foot"
            }, (s) => s.Remove(s.Length - 3, 3) + "eet", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "zoon"
            }, (s) => s.Remove(s.Length - 3, 3) + "oa", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.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
            // CONSIDER here is the only place we took the classical variants instead of the anglicized
            if (_classicalInflectionPluralizationService.ExistsInFirst(suffixWord))
            {
                return(prefixWord + _classicalInflectionPluralizationService.GetSecondValue(suffixWord));
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "trix"
            }, (s) => s.Remove(s.Length - 1, 1) + "ces", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "eau", "ieu"
            }, (s) => s + "x", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            if (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "inx", "anx", "ynx"
            }, (s) => s.Remove(s.Length - 1, 1) + "ges", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            // [cs]h and ss that take es as plural form
            if (PluralizationServiceUtil.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 (PluralizationServiceUtil.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 (PluralizationServiceUtil.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 (PluralizationServiceUtil.TryInflectOnSuffixInWord(suffixWord, new List <string> {
                "ay", "ey", "iy", "oy", "uy"
            }, (s) => s + "s", _culture, out newSuffixWord))
            {
                return(prefixWord + newSuffixWord);
            }
            // CONSIDER proper noun handling, Marys, Tonys, ignore for now
            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 (PluralizationServiceUtil.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))
            {
                return(prefixWord + suffixWord + "es");
            }
            if (suffixWord.EndsWith("x", true, _culture))
            {
                return(prefixWord + suffixWord + "es");
            }
            // cats, bags, hats, speakers
            return(prefixWord + suffixWord + "s");
        }