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)

   1  
   2  class Metaphone
   3    TRANSFORMATIONS = [[/\A[gkp]n/  ,  'n'],   # gn, kn, or pn at the start turns into 'n'
   4                       [/\Ax/       ,  's'],   # x at the start turns into 's'
   5                       [/\Awh/      ,  'w'],   # wh at the start turns into 'w'
   6                       [/mb\z/      ,  'm'],   # mb at the end turns into 'm'
   7                       [/sch/       ,  'sk'],  # sch sounds like 'sk'
   8                       [/x/         ,  'ks'],
   9                       [/cia/       ,  'xia'], # the 'c' -cia- and -ch- sounds like 'x'
  10                       [/ch/        ,  'xh'],
  11                       [/c([iey])/  ,  's\1'], # the 'c' -ce-, -ci-, or -cy- sounds like 's'
  12                       [/ck/        ,  'k'],
  13                       [/c/         ,  'k'],
  14                       [/dg([eiy])/ ,  'j\1'], # the 'dg' in -dge-, -dgi-, or -dgy- sounds like 'j'
  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'], # 'h' is silent after a vowel unless it's between vowels
  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

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.

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