-
Notifications
You must be signed in to change notification settings - Fork 0
/
Soundex.cs
84 lines (75 loc) · 1.62 KB
/
Soundex.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AlbumRecorder {
/// <summary>
/// Soundex algorithm for rough matching, from the Internet
/// </summary>
public class SoundEx {
/// <summary>
/// Encode a string to a Soundex
/// </summary>
public static string Encode(string s) {
StringBuilder output=new StringBuilder();
if(s.Length>0) {
output.Append(Char.ToUpper(s[0]));
// Stop at a maximum of 4 characters
for(int i=1; i<s.Length && output.Length<4; i++) {
string c=EncodeChar(s[i]);
// Ignore duplicated chars, except a duplication with the first char
if(i==1) {
output.Append(c);
} else if(c!=EncodeChar(s[i-1])) {
output.Append(c);
}
}
// Pad with zeros
for(int i=output.Length; i<4; i++) {
output.Append("0");
}
}
return output.ToString();
}
/// <summary>
/// See if two strings are roughly the same
/// </summary>
public static bool Equals(string s1, string s2) {
return Encode(s1) == Encode(s2);
}
/// <summary>
/// Encode a single character
/// </summary>
protected static string EncodeChar(char c) {
switch (Char.ToLower(c)) {
case 'b':
case 'f':
case 'p':
case 'v':
return "1";
case 'c':
case 'g':
case 'j':
case 'k':
case 'q':
case 's':
case 'x':
case 'z':
return "2";
case 'd':
case 't':
return "3";
case 'l':
return "4";
case 'm':
case 'n':
return "5";
case 'r':
return "6";
default:
return string.Empty;
}
}
}
}