Class: Wp2txt::SectionStatsCollector

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

Overview

Collects section heading statistics across multiple articles Used for --section-stats mode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSectionStatsCollector

Returns a new instance of SectionStatsCollector.



308
309
310
311
312
# File 'lib/wp2txt/section_extractor.rb', line 308

def initialize
  @total_articles = 0
  @section_counts = Hash.new(0)
  @extractor = SectionExtractor.new
end

Instance Attribute Details

#section_countsObject (readonly)

Returns the value of attribute section_counts.



306
307
308
# File 'lib/wp2txt/section_extractor.rb', line 306

def section_counts
  @section_counts
end

#total_articlesObject (readonly)

Returns the value of attribute total_articles.



306
307
308
# File 'lib/wp2txt/section_extractor.rb', line 306

def total_articles
  @total_articles
end

Instance Method Details

#merge(other) ⇒ Object

Merge another collector's results into this one Used for combining results from parallel processing

Parameters:



354
355
356
357
358
359
360
361
362
363
364
# File 'lib/wp2txt/section_extractor.rb', line 354

def merge(other)
  if other.is_a?(SectionStatsCollector)
    @total_articles += other.total_articles
    other.section_counts.each { |name, count| @section_counts[name] += count }
  elsif other.is_a?(Hash)
    @total_articles += other[:total_articles] || other["total_articles"] || 0
    counts = other[:section_counts] || other["section_counts"] || {}
    counts.each { |name, count| @section_counts[name] += count }
  end
  self
end

#process(article) ⇒ Object

Process an article and collect section heading statistics

Parameters:

  • article (Article)

    The article to process



316
317
318
319
320
# File 'lib/wp2txt/section_extractor.rb', line 316

def process(article)
  @total_articles += 1
  headings = @extractor.extract_headings(article)
  headings.each { |h| @section_counts[h] += 1 }
end

#to_hash(top_n: DEFAULT_TOP_N_SECTIONS) ⇒ Hash

Generate statistics output as a hash

Parameters:

  • top_n (Integer) (defaults to: DEFAULT_TOP_N_SECTIONS)

    Number of top sections to include

Returns:

  • (Hash)

    Statistics hash



335
336
337
338
339
340
341
# File 'lib/wp2txt/section_extractor.rb', line 335

def to_hash(top_n: DEFAULT_TOP_N_SECTIONS)
  {
    "total_articles" => @total_articles,
    "section_counts" => @section_counts.sort_by { |_k, v| -v }.to_h,
    "top_sections" => top_sections(top_n)
  }
end

#to_json(top_n: DEFAULT_TOP_N_SECTIONS) ⇒ String

Generate JSON output

Parameters:

  • top_n (Integer) (defaults to: DEFAULT_TOP_N_SECTIONS)

    Number of top sections to include

Returns:

  • (String)

    JSON string



346
347
348
349
# File 'lib/wp2txt/section_extractor.rb', line 346

def to_json(top_n: DEFAULT_TOP_N_SECTIONS)
  require "json"
  JSON.pretty_generate(to_hash(top_n: top_n))
end

#to_mergeable_hashHash

Export current state as a hash (for parallel processing)

Returns:

  • (Hash)

    Hash with total_articles and section_counts



368
369
370
371
372
373
# File 'lib/wp2txt/section_extractor.rb', line 368

def to_mergeable_hash
  {
    total_articles: @total_articles,
    section_counts: @section_counts.dup
  }
end

#top_sections(n = 50) ⇒ Array<Hash>

Get top N sections by count

Parameters:

  • n (Integer) (defaults to: 50)

    Number of sections to return (default: 50)

Returns:

  • (Array<Hash>)

    Array of count: hashes



325
326
327
328
329
330
# File 'lib/wp2txt/section_extractor.rb', line 325

def top_sections(n = 50)
  @section_counts
    .sort_by { |_name, count| -count }
    .first(n)
    .map { |name, count| { "name" => name, "count" => count } }
end