[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ann] double_metaphone.rb

Tim Fletcher

8/17/2006 2:35:00 PM

http://tfletcher.com/lib/double_me...

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...

Bug reports/fixes welcome!

7 Answers

Daniel Schierbeck

8/17/2006 5:10:00 PM

0

Tim Fletcher wrote:
> http://tfletcher.com/lib/double_me...
>
> 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

Daniel Schierbeck

8/17/2006 5:16:00 PM

0

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 Fletcher

8/18/2006 8:18:00 AM

0

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/...

Daniel Schierbeck

8/18/2006 8:34:00 AM

0

Tim Fletcher 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

Tim Fletcher

8/18/2006 9:40:00 AM

0

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_?

Paul Battley

8/18/2006 11:20:00 AM

0

On 18/08/06, Tim Fletcher <twoggle@gmail.com> 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/...

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/le...
2. http://raa.ruby-lang.org/projec...

Daniel Schierbeck

8/18/2006 1:21:00 PM

0

Tim Fletcher 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