Class: Vivlio::Starter::CLI::IndexCommands::HierarchicalIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio/starter/cli/index/hierarchical_index.rb

Overview

階層化索引ビルダー

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHierarchicalIndex

Returns a new instance of HierarchicalIndex.



25
26
27
28
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 25

def initialize
  @entries = Hash.new { |h, k| h[k] = [] }
  @hierarchy = Hash.new { |h, k| h[k] = Set[] }
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



23
24
25
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 23

def entries
  @entries
end

#hierarchyObject (readonly)

Returns the value of attribute hierarchy.



23
24
25
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 23

def hierarchy
  @hierarchy
end

Instance Method Details

#add_entry(term, link, parent: nil) ⇒ Object

エントリを追加

Parameters:

  • term (String)

    用語

  • link (String)

    リンク先

  • parent (String, nil) (defaults to: nil)

    親用語(階層化用)



34
35
36
37
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 34

def add_entry(term, link, parent: nil)
  @entries[term] << link
  @hierarchy[parent] << term if parent
end

#calculate_page_ranges(page_numbers) ⇒ Array<String>

ページ範囲を計算(連続ページをまとめる)

Parameters:

  • page_numbers (Array<Integer>)

    ページ番号の配列

Returns:

  • (Array<String>)

    範囲表記の配列(例: [“10-12”, “15”, “20-22”])



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 54

def calculate_page_ranges(page_numbers)
  return [] if page_numbers.empty?

  sorted = page_numbers.uniq.sort
  ranges = []
  range_start = sorted.first
  range_end = sorted.first

  sorted[1..].each do |num|
    if num == range_end + 1
      range_end = num
    else
      ranges << format_range(range_start, range_end)
      range_start = num
      range_end = num
    end
  end
  ranges << format_range(range_start, range_end)

  ranges
end

#children_of(term) ⇒ Object

用語の子用語を取得



89
90
91
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 89

def children_of(term)
  @hierarchy[term].to_a
end

#deduplicate_same_page!Object

同一ページの重複を排除HTMLファイル名が同じリンクをまとめる



41
42
43
44
45
46
47
48
49
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 41

def deduplicate_same_page!
  @entries.each do |term, links|
    # ファイル名ごとにグループ化
    grouped = links.group_by { |link| link.split('#').first }

    # 各ファイルの最初のリンクのみを保持
    @entries[term] = grouped.map { |_, group| group.first }
  end
end

#entry_countObject

エントリ数を取得



94
95
96
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 94

def entry_count
  @entries.size
end

#get_hierarchyHash

階層構造を取得

Returns:

  • (Hash)

    親用語 => 子用語の Set のハッシュ



78
79
80
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 78

def get_hierarchy
  @hierarchy.transform_values(&:to_a)
end

リンク総数を取得



99
100
101
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 99

def link_count
  @entries.values.sum(&:size)
end

#root_termsObject

ルートレベルの用語を取得(親を持たない用語)



83
84
85
86
# File 'lib/vivlio/starter/cli/index/hierarchical_index.rb', line 83

def root_terms
  all_children = @hierarchy.values.flatten.to_set
  @entries.keys.reject { all_children.include?(it) }
end