class Jkproof::Sentence

Public Class Methods

new(buf, json_dictionary) click to toggle source
# File lib/jkproof/sentence.rb, line 12
def initialize(buf, json_dictionary)
  Dotenv.load
  @yahoo_api_key = ENV['YAHOO_API_KEY']
  @no_filter     = ENV['NO_FILTER'].blank? ? '' : ENV['NO_FILTER']

  set_dictionary(json_dictionary)

  @yahoo_words = []
  @buf         = buf
end

Public Instance Methods

fetch_wrong_words() click to toggle source
# File lib/jkproof/sentence.rb, line 23
def fetch_wrong_words
  error_messages        = []
  wrong_words           = []
  excluded_correct_word = @buf

  begin
    fetch_yahoo_lint_words
  rescue StandardError => e
    error_messages.push "yahoo api. (#{e})"
  end

  # 正しいワードを取り除く
  begin
    @dictionary_words.each do |word|
      # 誤りのある単語の文字数 > 正しい単語の文字数の場合、先に検知する
      wrongs = fetch_wrong_words_than_long_correct_word(word['correct'], word['wrongs'])
      wrongs.each do |wrong|
        if excluded_correct_word.include?(wrong)
          wrong_words.push(type: 'local', correct: word['correct'], wrong: wrong)
          excluded_correct_word = excluded_correct_word.gsub(wrong, '####')
        end
      end
      # 正しいワードを取り除く
      excluded_correct_word = excluded_correct_word.gsub(word['correct'], '****')
    end

    @dictionary_words.each do |word|
      correct_word = word['correct']
      word['wrongs'].each do |wrong|
        if excluded_correct_word.include?(wrong)
          wrong_words.push(type: 'local', wrong: wrong, correct: correct_word)
          excluded_correct_word = excluded_correct_word.gsub(wrong, '####')
        end
      end
    end
  rescue StandardError => e
    error_messages.push "yml or json dictionary. (#{e})"
  end

  messages = error_messages.empty? ? '' : "#{error_messages.count} ERROR(s) : #{error_messages.join(', ')}"
  words    = wrong_words.concat(@yahoo_words).uniq
  {
    message: messages,
    count:   words.size,
    type:    @type,
    words:   words
  }
end

Private Instance Methods

fetch_wrong_words_than_long_correct_word(correct_word, wrong_words) click to toggle source
# File lib/jkproof/sentence.rb, line 123
def fetch_wrong_words_than_long_correct_word(correct_word, wrong_words)
  return_words = []
  c_length     = correct_word.length
  wrong_words.each { |wrong_word| return_words.push(wrong_word) if c_length < wrong_word.length }
  return_words
end
fetch_yahoo_lint_words() click to toggle source
# File lib/jkproof/sentence.rb, line 97
def fetch_yahoo_lint_words
  if @yahoo_api_key.blank? || @yahoo_api_key.size < 10
    raise "There is no Yahoo API Key.Please set API Key in 'jkproof/.env'.(ref: https://e.developer.yahoo.co.jp/dashboard/)"
  end

  # ref: http://developer.yahoo.co.jp/webapi/jlp/kousei/v1/kousei.html
  uri          = URI.parse('https://jlp.yahooapis.jp/KouseiService/V1/kousei')
  http         = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  reqest       = Net::HTTP::Post.new(uri.path)
  reqest.set_form_data(
    appid: @yahoo_api_key,
    sentence: @buf,
    no_filter: @no_filter
  )
  s = http.request(reqest).body
  unless Hash.from_xml(s)['ResultSet']['Result'].nil?
    [Hash.from_xml(s)['ResultSet']['Result']].flatten.each do |r|
      next unless r['Surface']
      next unless r['ShitekiWord']

      @yahoo_words.push(type: 'Yahoo', correct: r['ShitekiWord'], wrong: r['Surface'])
    end
  end
end
set_dictionary(json_dictionary) click to toggle source
# File lib/jkproof/sentence.rb, line 74
def set_dictionary(json_dictionary)
  # JSON形式で辞書データが送られてきた場合
  unless json_dictionary.blank?
    @type = 'json'
    @dictionary_words = json_dictionary
    return
  end

  # ローカルの辞書データを使う場合
  yml_path = ENV['DICTIONARY_YML_PATH']
  begin
    if yml_path.blank?
      @dictionary_words = []
      @type = 'none'
    else
      @dictionary_words = YAML.load_file(yml_path)
      @type = 'yml'
    end
  rescue StandardError => e
    raise "#{e}(file_path: '#{yml_path}')"
  end
end