Class: Vivlio::Starter::CLI::Metrics::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio/starter/cli/metrics/formatter.rb

Overview

メトリクス出力フォーマッタ

Constant Summary collapse

BAR_WIDTH =
12
BAR_CHAR =
'#'
BAR_EMPTY_CHAR =
'.'
CHAPTER_LABEL_WIDTH =
30
SECTION_LABEL_WIDTH =
36
CHAR_COUNT_WIDTH =
6

Instance Method Summary collapse

Constructor Details

#initialize(config_loader) ⇒ Formatter

Returns a new instance of Formatter.



28
29
30
31
32
# File 'lib/vivlio/starter/cli/metrics/formatter.rb', line 28

def initialize(config_loader)
  @config = config_loader
  @thresholds = config_loader.volume_thresholds
  @labels = config_loader.labels
end

Instance Method Details

#format_basic_info(basic) ⇒ Object

基本情報セクションを生成する



47
48
49
50
51
52
53
# File 'lib/vivlio/starter/cli/metrics/formatter.rb', line 47

def format_basic_info(basic)
  <<~OUTPUT.chomp
    📊 文章統計 — 基本情報
    - 文字数: #{number_with_comma(basic.chars_no_newline)} 文字(改行除く)
    - 行数: #{number_with_comma(basic.lines)}  OUTPUT
end

#format_chapter_line(chapter, max_chars, show_sections) ⇒ Object

章行をフォーマットする(公開メソッド)



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vivlio/starter/cli/metrics/formatter.rb', line 99

def format_chapter_line(chapter, max_chars, show_sections)
  bar = render_bar(chapter.chars, max_chars)
  warning = chapter.warning ? " ⚠️ #{chapter.warning}" : ''

  if show_sections && chapter.sections.any?
    format_chapter_with_sections(chapter, bar, warning, max_chars)
  else
    label = padded_label(chapter_label(chapter))
    char_count = format_char_count(chapter.chars)
    "#{label} #{bar} #{char_count}#{warning}"
  end
end

#format_chapters(chapters, show_sections: false) ⇒ Object

章別分量セクションを生成する



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vivlio/starter/cli/metrics/formatter.rb', line 85

def format_chapters(chapters, show_sections: false)
  return '📚 章別の分量\n\n(対象章がありません)' if chapters.empty?

  max_chars = chapters.map(&:chars).max
  title = show_sections ? '📚 章別の分量(節詳細)' : '📚 章別の分量'

  lines = [title, '']
  chapters.each do |ch|
    lines << format_chapter_line(ch, max_chars, show_sections)
  end
  lines.join("\n")
end

#format_detailed_analysis(vocab, readability) ⇒ Object

詳細分析セクションを生成する



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vivlio/starter/cli/metrics/formatter.rb', line 66

def format_detailed_analysis(vocab, readability)
  <<~OUTPUT.chomp
    📈 詳細分析

    【語彙難度】
    - 漢字比率: #{kanji_evaluation(vocab.kanji_ratio)}#{vocab.kanji_ratio.round(1)}%) — 理想的な範囲 25〜35%
    - 平均語長: #{vocab.avg_word_length.round(1)} 文字

    【語彙多様度】
    - 語彙の豊かさ: #{ttr_evaluation(vocab.ttr)}(TTR: #{vocab.ttr.round(2)}      - 評価基準: 0.7 以上=非常に豊富、0.5〜0.7=豊富、0.3〜0.5=標準的、0.3 未満=単調

    【読解難度】
    - 評価: #{readability.label}#{readability_description(readability.label)}    - スコア: #{readability.score.round(1)}  OUTPUT
end

#format_full(basic, vocab, readability, chapters, show_sections: false) ⇒ Object

全体の出力を生成する



35
36
37
38
39
40
41
42
43
44
# File 'lib/vivlio/starter/cli/metrics/formatter.rb', line 35

def format_full(basic, vocab, readability, chapters, show_sections: false)
  lines = []
  lines << format_basic_info(basic)
  lines << format_sentence_structure(basic)
  lines << '---'
  lines << format_detailed_analysis(vocab, readability)
  lines << '---'
  lines << format_chapters(chapters, show_sections:)
  lines.join("\n\n")
end

#format_sentence_structure(basic) ⇒ Object

文構造セクションを生成する



56
57
58
59
60
61
62
63
# File 'lib/vivlio/starter/cli/metrics/formatter.rb', line 56

def format_sentence_structure(basic)
  <<~OUTPUT.chomp
    📊 文章統計 — 文構造
    - 文の数: #{basic.sentences} 文(平均 #{basic.avg_sentence_len.round(1)} 文字/文)
    - 節の数: #{basic.clauses} 節(平均 #{basic.avg_clause_len.round(1)} 文字/節)
    - 読点数: #{basic.commas}  OUTPUT
end