Class: Html2rss::Scoring::LinkResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/scoring/link_resolver.rb

Overview

Resolves href strings into memoized LinkDestination::DestinationFacts for one page base URL.

Constant Summary collapse

HREF_BASE_PATTERN =

Captures the href portion before a fragment for memoization keys.

/\A([^#]*)/
CONTENT_TOKEN_REGEXP =

Matches content-like tokens in class/id strings (from PathClassifier vocabulary).

begin
  words = LinkDestination::PathClassifier::SEGMENT_SETS.fetch(:content)
  /(?:^|\s|[-_])(#{Regexp.union(words.to_a).source})(?:\s|[-_]|$)/i
end.freeze
JUNK_TOKEN_REGEXP =

Matches utility/junk tokens in class/id strings (from PathClassifier vocabulary).

begin
  words = LinkDestination::PathClassifier::SEGMENT_SETS.fetch(:utility)
  /(?:^|\s|[-_])(#{Regexp.union(words.to_a).source})(?:\s|[-_]|$)/i
end.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ LinkResolver

Returns a new instance of LinkResolver.

Parameters:



24
25
26
27
28
29
# File 'lib/html2rss/scoring/link_resolver.rb', line 24

def initialize(base_url)
  @base_url = base_url
  @by_href = {}
  @by_node = {}.compare_by_identity
  @text_classifier = LinkDestination::TextClassifier.new
end

Instance Attribute Details

#text_classifierLinkDestination::TextClassifier (readonly)



32
33
34
# File 'lib/html2rss/scoring/link_resolver.rb', line 32

def text_classifier
  @text_classifier
end

Instance Method Details

#destination_facts(node_or_href) ⇒ LinkDestination::DestinationFacts?

Parameters:

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/html2rss/scoring/link_resolver.rb', line 37

def destination_facts(node_or_href)
  return @by_node[node_or_href] if node_or_href.is_a?(SST::Node) && @by_node.key?(node_or_href)

  href = extract_href(node_or_href)
  return unless href

  facts = (@by_href[href] ||= build_facts(href))
  @by_node[node_or_href] = facts if node_or_href.is_a?(SST::Node)
  facts
rescue ArgumentError
  nil
end

Parameters:

  • text (String, #to_s)

Returns:

  • (Boolean)


63
# File 'lib/html2rss/scoring/link_resolver.rb', line 63

def recommended_text?(text) = @text_classifier.recommended?(text)

#utility_prefix_text?(text) ⇒ Boolean

Parameters:

  • text (String, #to_s)

Returns:

  • (Boolean)


58
# File 'lib/html2rss/scoring/link_resolver.rb', line 58

def utility_prefix_text?(text) = @text_classifier.utility_prefix?(text)

#utility_text?(text) ⇒ Boolean

Parameters:

  • text (String, #to_s)

Returns:

  • (Boolean)


53
# File 'lib/html2rss/scoring/link_resolver.rb', line 53

def utility_text?(text) = @text_classifier.utility?(text)