Class: Wp2txt::SectionStatsCollector
- Inherits:
-
Object
- Object
- Wp2txt::SectionStatsCollector
- Defined in:
- lib/wp2txt/section_extractor.rb
Overview
Collects section heading statistics across multiple articles Used for --section-stats mode
Instance Attribute Summary collapse
-
#section_counts ⇒ Object
readonly
Returns the value of attribute section_counts.
-
#total_articles ⇒ Object
readonly
Returns the value of attribute total_articles.
Instance Method Summary collapse
-
#initialize ⇒ SectionStatsCollector
constructor
A new instance of SectionStatsCollector.
-
#merge(other) ⇒ Object
Merge another collector's results into this one Used for combining results from parallel processing.
-
#process(article) ⇒ Object
Process an article and collect section heading statistics.
-
#to_hash(top_n: DEFAULT_TOP_N_SECTIONS) ⇒ Hash
Generate statistics output as a hash.
-
#to_json(top_n: DEFAULT_TOP_N_SECTIONS) ⇒ String
Generate JSON output.
-
#to_mergeable_hash ⇒ Hash
Export current state as a hash (for parallel processing).
-
#top_sections(n = 50) ⇒ Array<Hash>
Get top N sections by count.
Constructor Details
#initialize ⇒ SectionStatsCollector
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_counts ⇒ Object (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_articles ⇒ Object (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
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
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
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
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_hash ⇒ Hash
Export current state as a hash (for parallel processing)
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
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 |