Class: Phonetic::Metaphone
- Defined in:
- lib/phonetic/metaphone.rb
Overview
Metaphone is a phonetic algorithm, published by Lawrence Philips in 1990, for indexing words by their English pronunciation. This class implements this algorithm.
Constant Summary collapse
- VOWELS =
'AEIOU'
- FRONT_VOWELS =
'EIY'
- VARSON =
'CSPTG'
Class Method Summary collapse
-
.encode_word(word, options = { size: 4 }) ⇒ Object
Encode word to its Metaphone code.
Methods inherited from Algorithm
Class Method Details
.encode_word(word, options = { size: 4 }) ⇒ Object
Encode word to its Metaphone code
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/phonetic/metaphone.rb', line 18 def self.encode_word(word, = { size: 4 }) code_size = [:size] || 4 w = word.upcase.gsub(/[^A-Z]/, '') return if w.empty? two = w[0, 2] w[0] = '' if two =~ /PN|AE|KN|GN|WR/ w[0] = 'S' if w[0] == 'X' w[1] = '' if two == 'WH' l = w.size = '' for n in 0..(l - 1) break unless .size < code_size symb = w[n] next unless symb == 'C' || n == 0 || w[n - 1] != symb case when vowel?(symb) && n == 0 = symb when symb == 'B' += symb if n != l - 1 || w[n - 1] != 'M' when symb == 'C' += encode_c(w, n) when symb == 'D' += encode_d(w, n) when symb == 'G' += encode_g(w, n) when symb == 'H' += encode_h(w, n) when symb =~ /[FJLMNR]/ += symb when symb == 'K' += encode_k(w, n) when symb == 'P' += w[n + 1] == 'H' ? 'F' : 'P' when symb == 'Q' += 'K' when symb == 'S' += encode_s(w, n) when symb == 'T' += encode_t(w, n) when symb == 'V' += 'F' when symb =~ /[WY]/ += symb if vowel?(w[n + 1]) when symb == 'X' += 'KS' when symb == 'Z' += 'S' end end end |