Module: English::Soundex
- Defined in:
- lib/gems/english-0.3.1/lib/english/soundex.rb
Overview
Ruby implementation of the Soundex algorithm, as described by Knuth in volume 3 of The Art of Computer Programming.
Class Method Summary collapse
-
.soundex(str) ⇒ Object
Returns nil if the value couldn’t be calculated b/c of empty-string or invalid character.
-
.soundex_code(char) ⇒ Object
Support function for String#soundex.
Class Method Details
.soundex(str) ⇒ Object
Returns nil if the value couldn’t be calculated b/c of empty-string or invalid character.
"Ruby".soundex #=> "R100"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gems/english-0.3.1/lib/english/soundex.rb', line 27 def soundex(str) return nil if str.empty? str = str.upcase last_code = soundex_code(str[0,1]) soundex_code = str[0,1] for index in 1...(str.size) do return soundex_code if soundex_code.size == 4 code = soundex_code(str[index,1]) if code == "0" then last_code = nil elsif code == nil then return nil elsif code != last_code then soundex_code += code last_code = code end end return soundex_code + "000"[0,4-soundex_code.size] end |
.soundex_code(char) ⇒ Object
Support function for String#soundex. Returns code for a single character.
55 56 57 |
# File 'lib/gems/english-0.3.1/lib/english/soundex.rb', line 55 def soundex_code(char) char.tr! "AEIOUYWHBPFVCSKGJQXZDTLMNR", "00000000111122222222334556" end |