public override string BuildKey(string word) { if (string.IsNullOrEmpty(word)) { return(string.Empty); } string upperName = word.ToUpper(); //Let's strip non A-Z characters upperName = Regex.Replace(upperName, "[^A-Z]", string.Empty, RegexOptions.Compiled); //step 1 TranslateFirstCharacters(ref upperName); //step 2 TranslateLastCharacters(ref upperName); //step 3 string firstCharacter = upperName.Substring(0, 1); string remainingName = upperName.Length > 1 ? upperName.Substring(1) : string.Empty; if (!string.IsNullOrEmpty(remainingName.Trim())) { //step 4 TranslateRemaining(ref remainingName); //step 6 - 8 ReplaceLastCharacter(ref remainingName); } //collapse repeating characters and append first character back on return(firstCharacter + SioHelpers.CollapseAdjacentRepeating(remainingName)); }
internal static string CollapseRepeatingConsonants(string name) { StringBuilder sb = new StringBuilder(); char prev = ' '; bool first = true; foreach (char c in name) { if (c != prev || first || SioHelpers.IsVowel(c)) { sb.Append(c); first = false; } prev = c; } return(sb.ToString()); }
public static void GenerateNysiisKey(string name, out string key, out string fullKey) { key = string.Empty; fullKey = string.Empty; if (string.IsNullOrEmpty(name)) { return; } string upperName = name.ToUpper(); //Let's strip non A-Z characters upperName = Regex.Replace(upperName, "[^A-Z]", string.Empty, RegexOptions.Compiled); //step 1 TranslateFirstCharacters(ref upperName); //step 2 TranslateLastCharacters(ref upperName); //step 3 string firstCharacter = upperName.Substring(0, 1); string remainingName = upperName.Length > 1 ? upperName.Substring(1) : string.Empty; if (!string.IsNullOrEmpty(remainingName.Trim())) { //step 4 TranslateRemaining(ref remainingName); //step 6 - 8 ReplaceLastCharacter(ref remainingName); } //collapse repeating characters and append first character back on fullKey = firstCharacter + SioHelpers.CollapseAdjacentRepeating(remainingName); //a true NYSIIS key is only 6 characters long key = fullKey.Substring(0, (fullKey.Length >= 6 ? 6 : fullKey.Length)); }
public int MatchRatingCompute(string name1, string name2) { //0 is an impossible rating, it will mean unrated if (string.IsNullOrEmpty(name1) || string.IsNullOrEmpty(name2)) { return(0); } string large; string small; if (name1.Length >= name2.Length) { large = name1.ToUpper(); small = name2.ToUpper(); } else { large = name2.ToUpper(); small = name1.ToUpper(); } int x = large.Length; int y = small.Length; //If the length difference between the encoded strings is 3 or greater, then no similarity comparison is done. if ((x - y) > 3) { return(0); } //Obtain the minimum rating value by calculating the length sum of the encoded strings and using table A int minRating = MinimumRating(x + y); //Process the encoded strings from left to right and remove any identical characters found from both strings respectively. for (int i = 0; i < small.Length;) { bool found = false; for (int j = 0; j < large.Length; j++) { if (small[i] == large[j]) { small = small.Remove(i, 1); large = large.Remove(j, 1); found = true; } } if (!found) { i++; } } large = SioHelpers.ReverseString(large); small = SioHelpers.ReverseString(small); //Process the unmatched characters from right to left and remove any identical characters found from both names respectively. for (int i = 0; i < small.Length;) { bool found = false; for (int j = 0; j < large.Length; j++) { if (small[i] == large[j]) { small = small.Remove(i, 1); large = large.Remove(j, 1); found = true; } } if (!found) { i++; } } //Subtract the number of unmatched characters from 6 in the longer string. This is the similarity rating. int rating = 6 - (large.Length); //If the similarity rating equal to or greater than the minimum rating then the match is considered good. if (rating >= minRating) { return(rating); } return(0); }