Class: Html2rss::Scoring::ContainerAssessor

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

Overview

Observes an SST container and builds typed Observation score inputs.

Constant Summary collapse

PUBLISH_ITEMPROPS =

Microdata itemprop values treated as publish/update markers.

%w[datePublished dateModified].freeze
CONTENT_TOKEN_REGEXP =

Matches content-like tokens in class/id strings.

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.

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

Instance Method Summary collapse

Constructor Details

#initialize(text_classifier: LinkDestination::TextClassifier.new) ⇒ ContainerAssessor

Returns a new instance of ContainerAssessor.

Parameters:



24
25
26
# File 'lib/html2rss/scoring/container_assessor.rb', line 24

def initialize(text_classifier: LinkDestination::TextClassifier.new)
  @text_classifier = text_classifier
end

Instance Method Details

#call(container, selected_anchor, destination_facts:) ⇒ Observation

Parameters:

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/html2rss/scoring/container_assessor.rb', line 33

def call(container, selected_anchor, destination_facts:) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  title = entry_title(container, selected_anchor)
  tokens = "#{container.attrs.class_attr} #{container.attrs.id}"

  Observation.new(
    title_word_count: word_count(title),
    path_length: destination_facts&.url&.path.to_s.length,
    content_path: destination_facts&.content_path,
    publish_marker: publish_marker?(container),
    descriptive_context: descriptive_context?(container.visible_text, title),
    article_container: container.name == :article,
    content_tokens: tokens.match?(CONTENT_TOKEN_REGEXP),
    junk_tokens: tokens.match?(JUNK_TOKEN_REGEXP),
    utility_prefix_title: @text_classifier.utility_prefix?(title),
    recommended_title: @text_classifier.recommended?(title),
    utility_path: destination_facts&.utility_path,
    strong_post_suffix: destination_facts&.strong_post_suffix,
    shallow: destination_facts&.shallow,
    high_confidence_junk_path: destination_facts&.high_confidence_junk_path,
    high_confidence_utility_destination: destination_facts&.high_confidence_utility_destination,
    selected_anchor_present: !selected_anchor.nil?
  )
end