Class: VivlioStarter::CLI::IndexCommands::MainReferenceSuggester

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio_starter/cli/index/main_reference_suggester.rb

Overview

主要参照の候補を算出する

Defined Under Namespace

Classes: Chapter

Constant Summary collapse

HEADING =

見出し行(レベルつき)

/^(\#{1,6})[ \t]+(.+)$/
HEADING_WEIGHTS =

見出しレベル別の重み。h4 以下は重みを持たない(出現回数として数える)

{ 1 => 3, 2 => 2, 3 => 1 }.freeze
MIN_OCCURRENCES =

索引に出るページ番号が 1 つしかない語には主要参照が要らない。 太字にしても読者に伝わる情報が増えない(実測で該当は 1 語)。

2

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chapters) ⇒ MainReferenceSuggester

Returns a new instance of MainReferenceSuggester.



76
77
78
# File 'lib/vivlio_starter/cli/index/main_reference_suggester.rb', line 76

def initialize(chapters)
  @chapters = load_chapters(chapters)
end

Class Method Details

.suggest(terms, chapters) ⇒ Hash{String => String}

Returns 用語 → 章のベースネーム(候補なしの語は含まない).

Parameters:

  • terms (Array<Hash>)

    辞書エントリ('term' と任意の 'pattern')

  • chapters (Array<String>)

    章のベースネームまたはパス

Returns:

  • (Hash{String => String})

    用語 → 章のベースネーム(候補なしの語は含まない)



70
71
72
73
74
# File 'lib/vivlio_starter/cli/index/main_reference_suggester.rb', line 70

def self.suggest(terms, chapters)
  return {} if terms.empty? || chapters.empty?

  new(chapters).suggest(terms)
end

Instance Method Details

#suggest(terms) ⇒ Hash{String => String}

Returns:

  • (Hash{String => String})


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vivlio_starter/cli/index/main_reference_suggester.rb', line 81

def suggest(terms)
  terms.filter_map do |entry|
    name = entry['term'].to_s
    next if name.empty?

    pattern = TermPattern.for(entry)
    next if total_occurrences(pattern) < MIN_OCCURRENCES

    chapter = best_chapter(pattern)
    [name, chapter] if chapter
  end.to_h
end