Module: Text::Metaphone

Extended by:
Metaphone
Included in:
Metaphone
Defined in:
lib/text/metaphone.rb,
lib/text/double_metaphone.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Rules

Instance Method Summary collapse

Instance Method Details

#double_metaphone(str) ⇒ Object

Returns the primary and secondary double metaphone tokens (the secondary will be nil if equal to the primary).



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/text/double_metaphone.rb', line 15

def double_metaphone(str)
  primary, secondary, current = [], [], 0
  original, length, last = "#{str}     ".upcase, str.length, str.length - 1
  if /^GN|KN|PN|WR|PS$/ =~ original[0, 2]
    current += 1
  end
  if 'X' == original[0, 1]
    primary << :S
    secondary << :S
    current += 1
  end
  while primary.length < 4 || secondary.length < 4
    break if current > str.length
    a, b, c = double_metaphone_lookup(original, current, length, last)
    primary << a if a
    secondary << b if b
    current += c if c
  end
  primary, secondary = primary.to_s[0, 4], secondary.to_s[0, 4]
  return primary, (primary == secondary ? nil : secondary)
end

#metaphone(str, options = {}) ⇒ Object

Returns the Metaphone representation of a string. If the string contains multiple words, each word in turn is converted into its Metaphone representation. Note that only the letters A-Z are supported, so any language-specific processing should be done beforehand.

If the :buggy option is set, alternate ‘buggy’ rules are used.



79
80
81
# File 'lib/text/metaphone.rb', line 79

def metaphone(str, options={})
  return str.strip.split(/\s+/).map { |w| metaphone_word(w, options) }.join(' ')
end