What I’m trying to do, is a program that has a hash with predefined
words and its corresponding meaning, then when I enter a word that is
found in the hash, I must return its meaning, and if not found in the
hash, should return the most similar word.
words={
“computer” => “an electronic device …”,
“cat” => “a small domesticated carnivorous …”,
“home” => “the place where one lives permanently …”,
“dog” => "a domesticated carnivorous mammal … "
}
like=""
print "Enter word to search: "
w=gets.chomp.to_s
word.each{|k,v|
if k==w
puts v
else
for i in 0…10
if k[i]==w[i]
like[i]=w[i]
end
end
end
}
################################################################
for example if I enter the word home, returns me its meaning:
home => “the place where one lives permanently …”
and if such entry hom, meaning me back home with any word and thus
similar to those found in the hash.
What I’m trying to do, is a program that has a hash with predefined
words and its corresponding meaning, then when I enter a word that is
found in the hash, I must return its meaning, and if not found in the
hash, should return the most similar word.
Hi Joao, firs of all, it’s not necessary put .to_s() in w, since gets()
means “get string”, so everything inputted for the user will be a
string. Second, I recommend you .downcase() the w variable, because the
user may input “CAT”, if you will have personal nouns in the dictionary
you’ll have to do something like the next showed. I also though a way to
find which is the closest word to what is tipped. Take a look, I tested
it a bit and worked.
#encoding: utf-8
words={
“computer” => “an electronic device …”,
“cat” => “a small domesticated carnivorous …”,
“home” => “the place where one lives permanently …”,
“dog” => "a domesticated carnivorous mammal … "
}
loop do
print "Enter word to search: "
w=gets.strip
#you can tipe ‘exit’ to go out
if w == ‘exit’ then exit end
resultado =
if words.has_key?(w) then words[w]
elsif words.has_key?(w.downcase) then words[w]
elsif words.has_key?(w.downcase.capitalize) then words[w]
else nil end
unless resultado
words[w] = “erase after…”
place = nil
words_in_array = words.sort
words_in_array.each_with_index do |value, index|
if value[0] == w then place = index end
break if place != nil
end
possible_words = [words_in_array[place.-(1)][0],
words_in_array[place.+(1)][0]]
resultado =
“The word doesn’t match, meaby did you mean: #{possible_words.join(’
or ')}. Try again”
words.delete(w)
end