class Phonetic::NYSIIS

This class implements original version of NYSIIS algorithm. @example

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

Constants

FIRST_CHAR_TABLE
LAST_CHAR_TABLE
LAST_TABLE
REMAINING_TABLE

Public Class Methods

encode_word(word, options = { trim: true }) click to toggle source

Convert word to its NYSIIS code

# 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