Class: Phonetic::NYSIIS

Inherits:
Algorithm show all
Defined in:
lib/phonetic/nysiis.rb

Overview

This class implements original version of NYSIIS algorithm.

Examples:

Phonetic::NYSIIS.encode('Alexandra') # => 'ALAXANDR'
Phonetic::NYSIIS.encode('Aumont') # => 'AANAD'
Phonetic::NYSIIS.encode('Bonnie') # => 'BANY'

Constant Summary collapse

FIRST_CHAR_TABLE =
{
  /^MAC/ => 'MCC',
  /^KN/ => 'NN',
  /^K/ => 'C',
  /^(PH|PF)/ => 'FF',
  /^SCH/ => 'SSS'
}
LAST_CHAR_TABLE =
{
  /(EE|IE)$/ => 'Y',
  /(DT|RT|RD|NT|ND)$/ => 'D'
}
REMAINING_TABLE =
{
  'EV' => 'AF',
  /[AEIOU]+/ => 'A',
  'Q' => 'G',
  'Z' => 'S',
  'M' => 'N',
  'KN' => 'N',
  'K' => 'C',
  'SCH' => 'SSS',
  'PH' => 'FF',
  /([^AEIOU])H/ => '\1',
  /(.)H[^AEIOU]/ => '\1',
  /[AEIOU]W/ => 'A'
}
LAST_TABLE =
{
  /S$/ => '',
  /AY$/ => 'Y',
  /A$/ => ''
}

Class Method Summary collapse

Methods inherited from Algorithm

encode

Class Method Details

.encode_word(word, options = { trim: true }) ⇒ Object

Convert word to its NYSIIS code



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/phonetic/nysiis.rb', line 45

def self.encode_word(word, options = { trim: true })
  return if !word or word.empty?
  trim = options[:trim]
  w = word.upcase
  w.gsub!(/[^A-Z]/, '')
  return if w.empty?
  FIRST_CHAR_TABLE.each{ |rx, str| break if w.sub!(rx, str) }
  LAST_CHAR_TABLE.each{ |rx, str| w.sub!(rx, str) }
  first = w[0]
  w = w[1...w.size].to_s
  REMAINING_TABLE.each{ |rx, str| w.gsub!(rx, str) }
  LAST_TABLE.each{ |rx, str| w.gsub!(rx, str) }
  w.gsub!(/[^\w\s]|(.)(?=\1)/, '') # remove duplicates
  w = first + w
  w = w[0..5] if trim
  w
end