Double_metaphone.rb

http://tfletcher.com/lib/double_metaphone.rb

Adapted from the PHP version. There’s at least one person looking for
it:

http://groups.google.com/group/comp.lang.ruby/search?group=comp.lang.ruby&q=double+metaphone

Bug reports/fixes welcome!

I do have one comment though: perhaps have the key be a symbol instead
of a string, since we’re probably not gonna alter it. E.g.

DoubleMetaphone[“foo”] => [:F, nil]

It may just be a personal preference, though.

Cheers,
Daniel

Tim F. wrote:

http://tfletcher.com/lib/double_metaphone.rb

Adapted from the PHP version. There’s at least one person looking for
it:

Very cool! Maybe cook up a gem?

I’ve played with it for a few minutes, and look!

require ‘double_metaphone’

module SpellChecker
def self.words
@words ||= {}
end

 def self.add_word(word)
   word.downcase!
   raise ArgumentError unless word =~ /^[a-z]+$/
   words[word.to_sym] = DoubleMetaphone[word]
 end

 def self.valid? word
   words.has_key? word.to_sym
 end

 def self.check(word)
   word.downcase!

   if valid? word
     puts "`#{word}' is valid"
   else
     puts "`#{word}' is invalid. did you mean one of these?"
     key = DoubleMetaphone[word]
     words.select{|w, k| k == key}.each do |alt|
       puts " * #{alt.first}"
     end
   end
 end

end

A bit rough, but hey.

Thanks mate!

Daniel

Tim F. wrote:

As for symbols/strings, the string is already there and generated, so
might as well return it. It’s easy enough to use to_sym, if that’s what
your application needs/prefers.

The string gets GC’ed, though. If you’re going to store lots of keys, it
may be better to store them as symbols.

Cheers,
Daniel

Not sure it’s worth a gem, as it’s quite easy just to drop in.

As for symbols/strings, the string is already there and generated, so
might as well return it. It’s easy enough to use to_sym, if that’s what
your application needs/prefers.

Forgot to mention: if anyone’s looking for the regular (non Double)
Ruby metaphone, it’s at http://po-ru.com/projects/metaphone/

Not sure whether you’re referring to the function or the memoization -
is it the memo hash keys you think should be stored as symbols, or
the double_metaphone return values?

On 18/08/06, Tim F. [email protected] wrote:

Not sure it’s worth a gem, as it’s quite easy just to drop in.

As for symbols/strings, the string is already there and generated, so
might as well return it. It’s easy enough to use to_sym, if that’s what
your application needs/prefers.

Forgot to mention: if anyone’s looking for the regular (non Double)
Ruby metaphone, it’s at http://po-ru.com/projects/metaphone/

Oh, that’s one of mine. Perhaps we should package Metaphone, Double
Metaphone, and Levenshtein[1] together - perhaps with Soundex[2] too.

Paul.

  1. http://po-ru.com/projects/levenshtein/
  2. http://raa.ruby-lang.org/project/soundex/

Tim F. wrote:

Not sure whether you’re referring to the function or the memoization -
is it the memo hash keys you think should be stored as symbols, or
the double_metaphone return values?

Both.

Cheers,
Daniel

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs