Class: Html2rss::AutoSource::Discovery::SemanticAnchorCandidates::Candidate

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

Overview

One anchor plus the facts needed to decide whether it represents content.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anchor, context) ⇒ Candidate

Returns a new instance of Candidate.

Parameters:

  • anchor (Nokogiri::XML::Node)

    anchor candidate

  • context (Context)

    semantic container context



80
81
82
83
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 80

def initialize(anchor, context)
  @anchor = anchor
  @context = context
end

Instance Attribute Details

#anchorObject (readonly)

Returns the value of attribute anchor.



76
77
78
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 76

def anchor
  @anchor
end

Instance Method Details

#anchor_identity_attributesHash

Returns anchor identity attributes used to build AnchorFacts.

Returns:

  • (Hash)

    anchor identity attributes used to build AnchorFacts



105
106
107
108
109
110
111
112
113
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 105

def anchor_identity_attributes
  {
    anchor:,
    text:,
    url: destination_facts.url,
    destination: destination_facts.destination,
    segments: destination_facts.segments
  }
end

#anchor_signalsHtml2rss::AutoSource::LinkHeuristics::AnchorSignals

Returns ranking signals.



116
117
118
119
120
121
122
123
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 116

def anchor_signals
  LinkHeuristics::AnchorSignals.new(
    meaningful_text: meaningful_text?,
    content_like_destination: content_like_destination?,
    heading_anchor: heading_anchor?,
    heading_text_match: heading_text_match?
  )
end

#content_like_destination?Boolean

Returns true when the destination route has content signals.

Returns:

  • (Boolean)

    true when the destination route has content signals



131
132
133
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 131

def content_like_destination?
  destination_facts.content_path
end

#destination_factsHtml2rss::AutoSource::LinkHeuristics::DestinationFacts?

Returns destination facts.

Returns:



95
96
97
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 95

def destination_facts
  @destination_facts ||= @context.destination_facts(@anchor)
end

#factsAnchorFacts?

Returns ranked anchor facts when the anchor is eligible.

Returns:

  • (AnchorFacts, nil)

    ranked anchor facts when the anchor is eligible



86
87
88
89
90
91
92
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 86

def facts
  return unless destination_facts
  return if noise_anchor?
  return unless representative_content_anchor?

  AnchorFacts.from_candidate(self)
end

#heading_anchor?Boolean

Returns true when the anchor is inside the selected heading.

Returns:

  • (Boolean)

    true when the anchor is inside the selected heading



136
137
138
139
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 136

def heading_anchor?
  heading = @context.heading
  heading && (@anchor == heading || Html2rss::Html::Navigator.descendant_of?(@anchor, heading))
end

#heading_text_match?Boolean

Returns true when anchor text exactly matches heading text.

Returns:

  • (Boolean)

    true when anchor text exactly matches heading text



142
143
144
145
146
147
148
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 142

def heading_text_match?
  heading_text = @context.heading_text

  meaningful_text? &&
    heading_text.match?(/\p{Alnum}/) &&
    heading_text == text
end

#meaningful_text?Boolean

Returns true when visible anchor text has words.

Returns:

  • (Boolean)

    true when visible anchor text has words



126
127
128
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 126

def meaningful_text?
  @meaningful_text ||= text.match?(/\p{Alnum}/)
end

#textString

Returns visible anchor text.

Returns:

  • (String)

    visible anchor text



100
101
102
# File 'lib/html2rss/auto_source/discovery/semantic_anchor_candidates.rb', line 100

def text
  @text ||= @context.visible_text(@anchor)
end