class Spellcard::Checker

Public Instance Methods

find_typo(text) click to toggle source
# File lib/spellcard/checker.rb, line 7
def find_typo(text)
  array = word_array(text)
  array.each.inject(Array.new) do |result, word|
    if is_english_word(word) == false
      result << word
    else
      result
    end
  end      
end
is_english_word(word) click to toggle source
# File lib/spellcard/checker.rb, line 22
def is_english_word(word)
  begin
    uri = 'http://public.dejizo.jp/NetDicV09.asmx/SearchDicItemLite?' +
      'Dic=EJdict&Scope=HEADWORD&Merge=AND&Prof=XHTML&PageSize=20' + 
      '&PageIndex=0&Match=EXACT&Word=' + word      
    doc = Nokogiri::XML(open(uri).read)
    hash = Hash.from_xml(doc.to_s)
    count = hash["SearchDicItemResult"]["TotalHitCount"].to_i
    if count == 0
      false
    else
      true
    end
  rescue
    true
  end
end
word_array(text) click to toggle source
# File lib/spellcard/checker.rb, line 18
def word_array(text)
  text.split(' ').map { |word| word.gsub(/,/, "").gsub(/\./, "") }
end