DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Soundex.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Globalization;
19 using System.Text;
20 
21 namespace Deveel.Data.Text {
22  public abstract class Soundex {
23  public static Soundex Default = new DefaultSoundex();
24 
25  public virtual int Difference(string s1, string s2) {
26  throw new NotSupportedException();
27  }
28 
29  public abstract string Compute(string s);
30 
31  protected virtual string EncodeChar(char c) {
32  switch (Char.ToUpperInvariant(c)) {
33  case 'B':
34  case 'F':
35  case 'P':
36  case 'V':
37  return "1";
38  case 'C':
39  case 'G':
40  case 'J':
41  case 'K':
42  case 'Q':
43  case 'S':
44  case 'X':
45  case 'Z':
46  return "2";
47  case 'D':
48  case 'T':
49  return "3";
50  case 'L':
51  return "4";
52  case 'M':
53  case 'N':
54  return "5";
55  case 'R':
56  return "6";
57  default:
58  return string.Empty;
59  }
60  }
61 
62  #region DefaultSoundex
63 
64  private class DefaultSoundex : Soundex {
65  public override string Compute(string s) {
66  if (String.IsNullOrEmpty(s))
67  return String.Empty;
68 
69  int startIndex;
70  for (startIndex = 0; startIndex < s.Length && !char.IsLetter(s[startIndex]); startIndex++) {
71  }
72 
73  if (startIndex >= s.Length)
74  return String.Empty;
75 
76  var output = new StringBuilder();
77 
78  output.Append(Char.ToUpperInvariant(s[startIndex]));
79 
80  // Stop at a maximum of 4 characters.
81  for (int i = startIndex + 1; i < s.Length && output.Length < 4; i++) {
82  string c = EncodeChar(s[i]);
83 
84  // Ignore duplicated chars.
85  if (c != EncodeChar(s[i - 1])) {
86  output.Append(c);
87  }
88  }
89 
90  // Pad with zeros.
91  output.Append(new String('0', 4 - output.Length));
92 
93  return output.ToString();
94  }
95  }
96 
97  #endregion
98  }
99 }
virtual int Difference(string s1, string s2)
Definition: Soundex.cs:25
override string Compute(string s)
Definition: Soundex.cs:65
virtual string EncodeChar(char c)
Definition: Soundex.cs:31