Class: Detergent::NodeScorer
- Inherits:
-
Object
- Object
- Detergent::NodeScorer
- Defined in:
- lib/detergent/node_scorer.rb
Overview
Scores nodes on how likely they are to be the page's main content.
Descendant statistics are computed bottom-up, with each node's stats built from its children's already-computed stats, so scoring every node in a document is O(n) instead of a full-subtree scan per node. A scorer instance is scoped to a single parse of a single document; discard it once the tree is mutated.
Constant Summary collapse
- ARTICLE_TAG_BONUS =
100- MAIN_TAG_BONUS =
50- MAIN_ROLE_BONUS =
25- POINTS_PER_PARAGRAPH =
5- CHARS_PER_TEXT_POINT =
100- LINK_DENSITY_PENALTY =
10- POINTS_PER_MEDIA =
10- LONG_PARAGRAPH_BONUS =
15- LONG_PARAGRAPH_LENGTH =
100- POINTS_PER_BLOCKQUOTE =
10- POINTS_PER_LIST =
5- SIDEBAR_PENALTY =
50- COMMENT_PENALTY =
50- AD_PENALTY =
70- SOCIAL_PENALTY =
70
Instance Method Summary collapse
-
#initialize ⇒ NodeScorer
constructor
A new instance of NodeScorer.
- #score(node) ⇒ Object
Constructor Details
#initialize ⇒ NodeScorer
Returns a new instance of NodeScorer.
28 29 30 31 |
# File 'lib/detergent/node_scorer.rb', line 28 def initialize @scores = {}.compare_by_identity @stats = {}.compare_by_identity end |
Instance Method Details
#score(node) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/detergent/node_scorer.rb', line 33 def score(node) cached = @scores[node] return cached unless cached.nil? return 0 unless node.element? @scores[node] = compute_score(node) end |