-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathPhoneticSearchSoundexAlgorithm.java
More file actions
93 lines (78 loc) · 1.59 KB
/
PhoneticSearchSoundexAlgorithm.java
File metadata and controls
93 lines (78 loc) · 1.59 KB
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
85
86
87
88
89
90
91
92
93
package com.howtodoinjava.examples;
public class Soundex
{
public static String getGode(String s)
{
char[] x = s.toUpperCase().toCharArray();
char firstLetter = x[0];
//RULE [ 2 ]
//Convert letters to numeric code
for (int i = 0; i < x.length; i++) {
switch (x[i]) {
case 'B':
case 'F':
case 'P':
case 'V': {
x[i] = '1';
break;
}
case 'C':
case 'G':
case 'J':
case 'K':
case 'Q':
case 'S':
case 'X':
case 'Z': {
x[i] = '2';
break;
}
case 'D':
case 'T': {
x[i] = '3';
break;
}
case 'L': {
x[i] = '4';
break;
}
case 'M':
case 'N': {
x[i] = '5';
break;
}
case 'R': {
x[i] = '6';
break;
}
default: {
x[i] = '0';
break;
}
}
}
//Remove duplicates
//RULE [ 1 ]
String output = "" + firstLetter;
//RULE [ 3 ]
for (int i = 1; i < x.length; i++)
if (x[i] != x[i - 1] && x[i] != '0')
output += x[i];
//RULE [ 4 ]
//Pad with 0's or truncate
output = output + "0000";
return output.substring(0, 4);
}
}
Let’s see how it to use above algorithm.
class Main {
public static void main(String[] args)
{
String name1 = "beer";
String name2 = "bear";
String name3 = "bearer";
System.out.println(Soundex.getGode(name1));
System.out.println(Soundex.getGode(name2));
System.out.println(Soundex.getGode(name3));
}
}