Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andy Vanasse

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Metaphone calculation

// Calculate's Lawrence Phillip's basic metaphone (a more advanced code for matching names)

class Metaphone
  TRANSFORMATIONS = [[/\A[gkp]n/  ,  'n'],   # gn, kn, or pn at the start turns into 'n'
                     [/\Ax/       ,  's'],   # x at the start turns into 's'
                     [/\Awh/      ,  'w'],   # wh at the start turns into 'w'
                     [/mb\z/      ,  'm'],   # mb at the end turns into 'm'
                     [/sch/       ,  'sk'],  # sch sounds like 'sk'
                     [/x/         ,  'ks'],
                     [/cia/       ,  'xia'], # the 'c' -cia- and -ch- sounds like 'x'
                     [/ch/        ,  'xh'],
                     [/c([iey])/  ,  's\1'], # the 'c' -ce-, -ci-, or -cy- sounds like 's'
                     [/ck/        ,  'k'],
                     [/c/         ,  'k'],
                     [/dg([eiy])/ ,  'j\1'], # the 'dg' in -dge-, -dgi-, or -dgy- sounds like 'j'
                     [/d/         ,  't'],
                     [/gh/        ,  ''],
                     [/gned/      ,  'ned'],
                     [/gn((?![aeiou])|(\z))/ ,  'n'],
                     [/g[eiy]/    ,  'j'],
                     [/ph/        ,  'f'],
                     [/[aeiou]h(?![aeoiu])/ ,  '\1'], # 'h' is silent after a vowel unless it's between vowels
                     [/q/         ,  'k'],
                     [/s(h|(ia)|(io))/ ,  'x\1'],
                     [/t((ia)|(io))/,  'x\1'],
                     [/th/        ,  '0'],
                     [/v/         ,  'f'],
                     [/w(?![aeiou])/ ,  ''],
                     [/y(?![aeiou])/ ,  ''],
                     [/z/         ,  's']
                    ]
                    
  def self.create_metaphone(aWord)
    word = aWord.to_s.downcase
    TRANSFORMATIONS.each{|transform| word.gsub!(transform.first, transform.last)}
    'a'.upto('z'){|letter| word.gsub!(letter*2, letter)}
    return (word[0].chr + word[1..word.length-1].gsub(/[aeiou]/, '')).upcase
  end
end

Soundex calculation

// Calculates a soundex for a name in (basic) conformance to the
// algorithm discussed at http://en.wikipedia.org/wiki/Soundex
// It's wrapped in a class primarily to contain the ALPHA_MAP.

class Soundex
  ALPHA_MAP = { 'a' => nil, 'b' => '1', 'c' => '2', 'd' => '3', 'e' => nil, 'f' => '1',
                'g' => '2', 'h' => nil, 'i' => nil, 'j' => '2', 'k' => '2', 'l' => '4',
                'm' => '5', 'n' => '5', 'o' => nil, 'p' => '1', 'q' => '2', 'r' => '6',
                's' => '2', 't' => '3', 'u' => nil, 'v' => '1', 'w' => nil, 'x' => '2',
                'y' => nil, 'z' => 2
              }
              
  def self.calculate(aName)
    ary = []
    # map all the letters in the supplied name
    aName.downcase.each_byte{|ltr| ary.push(ALPHA_MAP[ltr.chr])}
    # now drop out repeated values
    ary.length.downto(1){ |idx| ary[idx] = nil if ary[idx]==ary[idx-1] }
    # remove the nil elements
    ary.compact!
    # pad with zeroes
    0.upto(2){ ary.push('0')}
    # Replace the first value with the first letter of the supplied name
    ary[0] = aName[0].chr
    # return the first four elements of the array as a string
    return ary[0..3].to_s
  end
end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS