Class: Phonetic::RefinedNYSIIS

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

Overview

This class implements Refined NYSIIS algorithm.

Examples:

Phonetic::RefinedNYSIIS.encode('Aumont') # => 'ANAD'
Phonetic::RefinedNYSIIS.encode('Schmidt') # => 'SNAD'
Phonetic::RefinedNYSIIS.encode('Phoenix') # => 'FANAC'

See Also:

Constant Summary collapse

FIRST_MAP =
{
  /[SZ]+$/ => '',
  /^MAC/ => 'MC',
  /^PF/ =>  'F',
  /IX$/  => 'IC',
  /EX$/  => 'EC',
  /(YE|EE|IE)$/ => 'Y',
  /(DT|RT|RD|NT|ND)$/ => 'D',
  /(.)EV/ => '\1EF'
}
SECOND_MAP =
{
  /([AEIOU])W/ => '\1',
  /[AEIOU]+/ => 'A',
  'GHT' => 'GT',
  'DG' => 'G',
  'PH' => 'F',
  'AH' => 'A',
  /(.)HA/ => '\1A',
  'KN' => 'N',
  'K' => 'C',
  /(.)M/ => '\1N',
  /(.)Q/ => '\1G',
  'SH' => 'S',
  'SCH' => 'S',
  'YW' => 'Y',
  /(.)Y(.)/ => '\1A\2',
  'WR' => 'R',
  /(.)Z/ => '\1S',
  /AY$/ => 'Y',
  /A+$/ => '',
  /[^\w\s]|(.)(?=\1)/ => ''
}

Class Method Summary collapse

Class Method Details

.encode(str, options = { trim: true }) ⇒ Object

Convert string to Refined NYSIIS code



46
47
48
# File 'lib/phonetic/refined_nysiis.rb', line 46

def self.encode(str, options = { trim: true })
  self.encode_word(str, options)
end

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

Convert word to its Refined NYSIIS code



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/phonetic/refined_nysiis.rb', line 51

def self.encode_word(word, options = { trim: true })
  return '' if !word or word.empty?
  trim = options[:trim]
  w = word.upcase.strip
  w.gsub! /\s([IV]+|[JS]R)$/, ''
  w.gsub! /[^A-Z]/, ''
  return if w.empty?
  FIRST_MAP.each{ |rx, v| w.gsub!(rx, v) }
  first_char = w[0]
  SECOND_MAP.each{ |rx, v| w.gsub!(rx, v) }
  w.gsub! /^A*/, first_char if vowel?(first_char)
  w = w[0..5] if trim
  w
end