Sha256: 1e9ae20792e9262dbac2eb560a99ec79f1ab368074d126542e9711d3c2c00144

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 KB

Contents

require 'phonetic/algorithm'

module Phonetic
  # Caverphone created by the Caversham Project at the University of Otago.
  # @see http://caversham.otago.ac.nz/files/working/ctp060902.pdf Caverphone: Phonetic Matching algorithm by David Hood (2002)
  # This class implements this algorithm.
  # @example
  #    Phonetic::Caverphone.encode('Charmain') # => 'KMN111'
  #    Phonetic::Caverphone.encode('Ellett')   # => 'ALT111'
  #    Phonetic::Caverphone.encode('Siegmund') # => 'SKMNT1'
  class Caverphone < Algorithm
    MAP = {
      /^(cou|rou|tou|enou)gh/ => '\12f',
      /^gn/ => '2n',
      /mb$/ => 'mb',
      'cq' => '2q',
      /c([iey])/ => 's\1',
      'tch' => '2ch',
      /[cqx]/ => 'k',
      'v' => 'f',
      'dg' => '2g',
      /ti([oa])/ => 'si\1',
      'd' => 't',
      'ph' => 'fh',
      'b' => 'p',
      'sh' => 's2',
      'z' => 's',
      /^[aeiou]/ => 'A',
      /[aeiou]/ => '3',
      '3gh3' => '3kh3',
      'gh' => '22',
      'g' => 'k',
      /s+/ => 'S',
      /t+/ => 'T',
      /p+/ => 'P',
      /k+/ => 'K',
      /f+/ => 'F',
      /m+/ => 'M',
      /n+/ => 'N',
      'w3' => 'W3',
      /wy/ => 'Wy',
      'wh3' => 'Wh3',
      'why' => 'Why',
      'w' => '2',
      /^h/ => 'A',
      'h' => '2',
      'r3' => 'R3',
      'ry' => 'Ry',
      'r' => '2',
      'l3' => 'L3',
      'ly' => 'Ly',
      'l' => '2',
      'j' => 'y',
      'y3' => 'Y3',
      'y' => '2',
      '2' => '',
      '3' => ''
    }

    # Encode word to its Caverphone code
    def self.encode_word(word, options = {})
      w = word.strip.downcase.gsub(/[^a-z]/, '')
      MAP.each { |r, v| w.gsub!(r, v) }
      w = w + '1' * 6
      w[0..5]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phonetic-1.2.0 lib/phonetic/caverphone.rb
phonetic-1.1.0 lib/phonetic/caverphone.rb
phonetic-1.0.1 lib/phonetic/caverphone.rb
phonetic-1.0.0 lib/phonetic/caverphone.rb