[ann] double_metaphone.rb

D

Daniel Schierbeck

Tim said:
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
 
D

Daniel Schierbeck

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
 
T

Tim Fletcher

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

Daniel Schierbeck

Tim said:
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
 
T

Tim Fletcher

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

Paul Battley

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

Daniel Schierbeck

Tim said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top