Class: VivlioStarter::CLI::IndexCommands::TermSpread

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

Overview

語の広がりを測る

Defined Under Namespace

Classes: Entry

Constant Summary collapse

MIN_TOTAL_CHAPTERS =

全章数がこれ未満の本では判定しない。 5 章の本では 3 章に出るだけで比率 0.6 になり、誤検出しかしない。

6
MIN_CHAPTER_COUNT =

出現章数がこれ未満なら、比率がいくつでも一般語としない。 2 章の本で 2 章に出れば比率 1.0 だが、それは「広い」とは言わない。

3

Class Method Summary collapse

Class Method Details

.common_terms(spreads, ratio:) ⇒ Array<Entry>

一般語(=広く散らばりすぎている語)を選ぶ。 判定できない本(章が少ない)では 1 件も返さない——誤検出は 「機械が余計なことを言う」形で著者の信頼を削る。

Parameters:

  • spreads (Hash{String => Entry})
  • ratio (Float)

    この比率以上を一般語とみなす

Returns:

  • (Array<Entry>)

    広い順



75
76
77
78
79
80
81
82
83
84
# File 'lib/vivlio_starter/cli/index/term_spread.rb', line 75

def self.common_terms(spreads, ratio:)
  return [] if spreads.empty?

  total = spreads.values.first.total_chapters
  return [] if total < MIN_TOTAL_CHAPTERS

  spreads.values
         .select { it.chapter_count >= MIN_CHAPTER_COUNT && it.ratio >= ratio }
         .sort_by { [-it.chapter_count, it.term] }
end

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

各語の広がりを測る。

Parameters:

  • terms (Array<Hash>)

    辞書の用語('term' と任意の 'pattern')

  • chapters (Array<String>)

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

Returns:

  • (Hash{String => Entry})


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vivlio_starter/cli/index/term_spread.rb', line 57

def self.measure(terms, chapters)
  bodies = load_bodies(chapters)
  total = bodies.size

  terms.to_h do |entry|
    name = entry['term'].to_s
    pattern = term_regexp(entry)
    count = bodies.count { it.match?(pattern) }
    [name, Entry.new(term: name, chapter_count: count, total_chapters: total)]
  end
end