require "raspell"

# Show all installed dictionaries.
# Select one to use.
puts "Choose Dictionary:\n"
dicts = Aspell.list_dicts
raise "\nNo dictionary installed!\nYou have to install some dictionaries (see: www.aspell.net)" if dicts.empty?
dicts.each_with_index { |dict, num|
  puts " [#{num+1}] #{dict.name}    (code:#{dict.code}, jargon:#{dict.jargon}, size:#{dict.size})\n"
}
puts "enter number of dictionary:"
dictionary = dicts[gets.to_i-1]


# Create a new aspell instance
# Aspell.new(language, jargon, size, encoding) 
# It is possible to omit all parameters (just forget or nil as placeholder).
aspell = Aspell.new(dictionary.code, dictionary.jargon, dictionary.size.to_s)
# Set suggestion mode: one of [ULTRA|FAST|NORMAL|BADSPELLERS]
aspell.suggestion_mode = Aspell::ULTRA

puts "\n\nCurrent Dictionary config=====================\n"
Aspell::DictionaryOptions.each { |option|
  puts "  -#{option}: #{aspell.get_option(option)}\n"
}
puts "==============================================\n"


  
# some content to check - with many errors
content = ["This is a simpel sample texxt, with manny erors to find.",
           "To check this, we need an englishh diktionary!"]

# get a list of all misspelled words
misspelled = aspell.list_misspelled(content).sort
puts "\n\n#{misspelled.length} misspelled words:\n  -"+misspelled.join("\n  -")

# correct content
correctreadme = aspell.correct_lines(content) { |badword|
  puts "\nMisspelled: #{badword}\n"
  suggestions = aspell.suggest(badword)
  suggestions.each_with_index { |word, num|
    puts "  [#{num+1}] #{word}\n"
  }
  puts "Enter number or correct word: "
  input = gets.chomp
  if (input.to_i != 0)
    input = suggestions[input.to_i-1] 
  else
    if (!aspell.check(input))
      puts "\nthe word #{input} is not known inside dictionary." 
      #possible to add the word into private dictionary or into session
      #via aspell.add_to_personal or aspell.add_to_session
    end
  end
  input #return input
}

puts "\n\nThe correct text is:\n"+correctreadme.join("\n")

# It is possible to correct files directly via aspell.correct_file(filename)
