// Calculate's Lawrence Phillip's basic metaphone (a more advanced code for matching names)
1
2 class Metaphone
3 TRANSFORMATIONS = [[/\A[gkp]n/ , 'n'],
4 [/\Ax/ , 's'],
5 [/\Awh/ , 'w'],
6 [/mb\z/ , 'm'],
7 [/sch/ , 'sk'],
8 [/x/ , 'ks'],
9 [/cia/ , 'xia'],
10 [/ch/ , 'xh'],
11 [/c([iey])/ , 's\1'],
12 [/ck/ , 'k'],
13 [/c/ , 'k'],
14 [/dg([eiy])/ , 'j\1'],
15 [/d/ , 't'],
16 [/gh/ , ''],
17 [/gned/ , 'ned'],
18 [/gn((?![aeiou])|(\z))/ , 'n'],
19 [/g[eiy]/ , 'j'],
20 [/ph/ , 'f'],
21 [/[aeiou]h(?![aeoiu])/ , '\1'],
22 [/q/ , 'k'],
23 [/s(h|(ia)|(io))/ , 'x\1'],
24 [/t((ia)|(io))/, 'x\1'],
25 [/th/ , '0'],
26 [/v/ , 'f'],
27 [/w(?![aeiou])/ , ''],
28 [/y(?![aeiou])/ , ''],
29 [/z/ , 's']
30 ]
31
32 def self.create_metaphone(aWord)
33 word = aWord.to_s.downcase
34 TRANSFORMATIONS.each{|transform| word.gsub!(transform.first, transform.last)}
35 'a'.upto('z'){|letter| word.gsub!(letter*2, letter)}
36 return (word[0].chr + word[1..word.length-1].gsub(/[aeiou]/, '')).upcase
37 end
38 end