Class: Html2rss::AutoSource::Discovery::DomClustering::GroupScorer

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/auto_source/discovery/dom_clustering/group_scorer.rb

Overview

Scores candidate DOM groups using heading, time, date, and word-count signals.

Instance Method Summary collapse

Constructor Details

#initializeGroupScorer

Returns a new instance of GroupScorer.



10
11
12
13
# File 'lib/html2rss/auto_source/discovery/dom_clustering/group_scorer.rb', line 10

def initialize
  @text_words = {}.compare_by_identity
  @has_date = {}.compare_by_identity
end

Instance Method Details

#avg_words(nodes) ⇒ Float

Returns average visible word count across nodes.

Parameters:

  • nodes (Array<Nokogiri::XML::Node>)

Returns:

  • (Float)

    average visible word count across nodes



33
34
35
# File 'lib/html2rss/auto_source/discovery/dom_clustering/group_scorer.rb', line 33

def avg_words(nodes)
  nodes.sum { |n| text_words(n) } / nodes.size.to_f
end

#select_best_group(groups) ⇒ Array<Nokogiri::XML::Node>

Returns nodes from the highest-scoring group.

Parameters:

  • groups (Hash{String => Array<Nokogiri::XML::Node>})

    candidate DOM groups

Returns:

  • (Array<Nokogiri::XML::Node>)

    nodes from the highest-scoring group



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/html2rss/auto_source/discovery/dom_clustering/group_scorer.rb', line 17

def select_best_group(groups)
  best_nodes = []
  best_score = -1

  groups.each_value do |nodes|
    score = score_group(nodes)
    next if score.negative?

    (best_nodes = nodes) && (best_score = score) if score > best_score
  end

  best_nodes
end