Class: Jkproof::Sentence

Inherits:
Object
  • Object
show all
Defined in:
lib/jkproof/sentence.rb

Instance Method Summary collapse

Constructor Details

#initialize(buf, json_dictionary) ⇒ Sentence

Returns a new instance of Sentence.



12
13
14
15
16
17
18
19
20
21
# 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

Instance Method Details

#fetch_wrong_wordsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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