Created
August 7, 2011 21:14
-
-
Save cantlin/1130801 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Suggester | |
class << self | |
def variants_for word | |
[word.downcase, word.capitalize, word.squeeze] | |
end | |
def suggestions_for word | |
suggestions = self.vowel_suggestions_for word | |
suggestion_variants = [] | |
suggestions.each do |suggestion| | |
variants = self.variants_for suggestion | |
variants.each do |variant| | |
unless suggestions.include? variant | |
suggestion_variants << variant | |
end | |
end | |
end | |
suggestions + suggestion_variants | |
end | |
def vowel_suggestions_for word | |
vowels = ['a', 'e', 'i', 'o', 'u'] | |
suggestions = [word] | |
word.chars.each_with_index do |char, index| | |
next unless vowels.include? char.downcase | |
vowels.each do |vowel| | |
suggestions.each do |suggestion| | |
suggestion[index] = vowel | |
unless suggestions.include? suggestion | |
suggestions << suggestion | |
end | |
end | |
end | |
end | |
suggestions | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment