Class: Inspector

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

Overview

小説の状態を監視・検査する

Constant Summary collapse

INSPECT_LOG_NAME =
"調査ログ.txt"
LINE_LENGTH_THRESHOLD =
400
BRACKETS_RETURN_COUNT_THRESHOLD =
7
END_TOUTEN_COUNT_THRESHOLD =
50
ERROR =
1
WARNING =
2
INFO =
4
ALL =
ERROR | WARNING | INFO
KLASS_TAG =
{ ERROR => "エラー", WARNING =>  "警告", INFO => "INFO" }
IGNORE_INDENT_CHAR =
"((「『〈《≪【〔―・※[〝\n"
AUTO_INDENT_THRESHOLD_RATIO =

括弧等を除く全ての行のうちこの割合以上字下げされてなければ強制字下げする

0.5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(setting) ⇒ Inspector

Returns a new instance of Inspector.



36
37
38
39
40
41
42
43
# File 'lib/inspector.rb', line 36

def initialize(setting)
  @setting = setting
  @messages = []
  @error = false
  @warning = false
  @info = false
  @subtitle = ""
end

Instance Attribute Details

#messages=(value) ⇒ Object (writeonly)

Sets the attribute messages

Parameters:

  • value

    the value to set the attribute messages to.



25
26
27
# File 'lib/inspector.rb', line 25

def messages=(value)
  @messages = value
end

#subtitle=(value) ⇒ Object (writeonly)

Sets the attribute subtitle

Parameters:

  • value

    the value to set the attribute subtitle to.



25
26
27
# File 'lib/inspector.rb', line 25

def subtitle=(value)
  @subtitle = value
end

Class Method Details

.read_messages(setting) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/inspector.rb', line 27

def self.read_messages(setting)
  inspect_log = File.join(setting.archive_path, INSPECT_LOG_NAME)
  if File.exist?(inspect_log)
    File.read(inspect_log)
  else
    nil
  end
end

Instance Method Details

#countup_return_in_brackets(data) ⇒ Object

カギ括弧内の改行状況を調べる



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/inspector.rb', line 176

def countup_return_in_brackets(data)
  return if @setting.enable_auto_join_in_brackets
  max = 0
  brackets_num = 0
  brackets_num_over_threshould = 0
  total = 0
  ConverterBase::OPENCLOSE_REGEXPS.each do |openclose|
    data.scan(openclose) do |match|
      cnt = match[0].count("\n")
      brackets_num += 1
      total += cnt
      next if cnt < BRACKETS_RETURN_COUNT_THRESHOLD
      brackets_num_over_threshould += 1
      if cnt > max
        max = cnt
      end
    end
  end
  info("カギ括弧内の改行状況:\n" +
       "検出したカギ括弧数: #{brackets_num}、そのうち#{BRACKETS_RETURN_COUNT_THRESHOLD}個以上改行を含む数: #{brackets_num_over_threshould}\n" + 
       "1つのカギ括弧内で最大の改行数: #{max}、全カギ括弧内での改行合計: #{total}")
end

#display(klass = ALL, target = $stdout) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/inspector.rb', line 70

def display(klass = ALL, target = $stdout)
  target.puts @messages.map { |msg|
    if msg =~ /^\[(.+)\]/
      key = KLASS_TAG.key($1)
      if key && (klass & key) != 0
        next msg
      end
    end
    nil
  }.compact.join("\n\n")
end

#display_summary(target = $stdout) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/inspector.rb', line 61

def display_summary(target = $stdout)
  target.print "小説状態の調査結果を #{Inspector::INSPECT_LOG_NAME} に出力しました("
  target.print KLASS_TAG.values.map { |klass_type|
    num = @messages.count { |msg| msg =~ /^\[#{klass_type}\]/ }
    "#{klass_type}#{num}"
  }.join("")
  target.puts ""
end

#empty?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/inspector.rb', line 45

def empty?
  @messages.empty?
end

#error(message) ⇒ Object



104
105
106
107
# File 'lib/inspector.rb', line 104

def error(message)
  log("[#{KLASS_TAG[ERROR]}] #{message}")
  @error = true
end

#error?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/inspector.rb', line 49

def error?
  @error
end

#info(message) ⇒ Object



94
95
96
97
# File 'lib/inspector.rb', line 94

def info(message)
  log("[#{KLASS_TAG[INFO]}] #{message}")
  @info = true
end

#info?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/inspector.rb', line 57

def info?
  @info
end

#inspect_end_touten_conditions(data) ⇒ Object

行末読点の状況を調べる



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/inspector.rb', line 157

def inspect_end_touten_conditions(data)
  return if @setting.enable_auto_join_line
  num = 0
  data.scan(/、\n /) do
    num += 1
  end
  if num > 0
    msg = "#{num}個の行末読点を発見しました。"
    if num >= END_TOUTEN_COUNT_THRESHOLD
      msg << "作者による手動改行により改行が多くなっています。" + \
             "setting.ini の enable_auto_join_line を true にすることをお薦めします。"
    end
    info(msg)
  end
end

#inspect_indent(data) ⇒ Object

行頭字下げをするべきか調べる



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/inspector.rb', line 202

def inspect_indent(data)
  target_line_count = 0
  dont_indent_line_count = 0
  data.scan(/^[^#{IGNORE_INDENT_CHAR}]/).tap { |a|
    target_line_count = a.size
  }.each { |line|
    head = line[0]
    if head != " " && head != " "
      dont_indent_line_count += 1
    end
  }
  ratio = dont_indent_line_count / target_line_count.to_f
  return ratio > AUTO_INDENT_THRESHOLD_RATIO
end

#inspect_invalid_openclose_brackets(data, brackets, stack) ⇒ Object

かぎ括弧のとじ開きの異常部分を調査



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/inspector.rb', line 139

def inspect_invalid_openclose_brackets(data, brackets, stack)
  brackets.each do |bracket|
    buffer = data.dup
    while buffer =~ /#{bracket}/
      match_before = $`.dup
      match_after = $'.dup
      before = ConverterBase.rebuild_brackets(match_before, stack)
      after = ConverterBase.rebuild_brackets(match_after, stack)
      error("かぎ括弧(#{bracket})が正しく閉じていません。\n" +
            omit_message((before[-15..-1] || before) + bracket + after))
      buffer = match_before
    end
  end
end

#log(message) ⇒ Object



90
91
92
# File 'lib/inspector.rb', line 90

def log(message)
  @messages << message
end

#omit_message(strings) ⇒ Object



109
110
111
112
# File 'lib/inspector.rb', line 109

def omit_message(strings)
  navigation = "in #{@subtitle}" if @subtitle.to_s.length > 0
  "≫≫≫ 該当箇所 #{navigation}\n..." + strings[0...36].gsub("\n", "\\n") + "..."
end

#save(path = nil) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/inspector.rb', line 82

def save(path = nil)
  path = File.join(@setting.archive_path, INSPECT_LOG_NAME) if path.nil?
  open(path, "w") do |fp|
    fp.puts "--- ログ出力 #{Time.now} ---"
    display(ALL, fp)
  end
end

#validate_joined_inner_brackets(raw_strings, joined_strings, brackets) ⇒ Object

連結したかぎ括弧が正常かどうか



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/inspector.rb', line 117

def validate_joined_inner_brackets(raw_strings, joined_strings, brackets)
  error_result = false
  case
  # 連結前の文章の改行具合を調べて、改行が閾値を超えた場合意図的な改行とみなす
  when raw_strings.count("\n") >= BRACKETS_RETURN_COUNT_THRESHOLD
    warning("改行が規定の回数を超えて検出されました。" +
            "作者による意図的な改行とみなし、連結を中止しました。\n" +
            omit_message(raw_strings))
    error_result = true
  # 連結した文章があまりにも長い場合、特殊な用途で使われている可能性がある
  when joined_strings.length >= LINE_LENGTH_THRESHOLD
    warning("連結結果が長過ぎます。連結を中止しました。" +
            "特殊な用途(手紙形式)等でかぎ括弧が使われている可能性があります。\n" +
          omit_message(raw_strings))
    error_result = true
  end
  error_result
end

#warning(message) ⇒ Object



99
100
101
102
# File 'lib/inspector.rb', line 99

def warning(message)
  log("[#{KLASS_TAG[WARNING]}] #{message}")
  @warning = true
end

#warning?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/inspector.rb', line 53

def warning?
  @warning
end