Class: Summarization::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/summarization/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.available_strategiesObject



11
12
13
# File 'lib/summarization/base.rb', line 11

def available_strategies
  DiscoursePluginRegistry.summarization_strategies
end

.can_request_summary_for?(user) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
# File 'lib/summarization/base.rb', line 34

def can_request_summary_for?(user)
  return false unless user

  user_group_ids = user.group_ids

  SiteSetting.custom_summarization_allowed_groups_map.any? do |group_id|
    user_group_ids.include?(group_id)
  end
end

.can_see_summary?(target, user) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/summarization/base.rb', line 25

def can_see_summary?(target, user)
  return false if SiteSetting.summarization_strategy.blank?

  has_cached_summary = SummarySection.exists?(target: target, meta_section_id: nil)
  return has_cached_summary if user.nil?

  has_cached_summary || can_request_summary_for?(user)
end

.find_strategy(strategy_model) ⇒ Object



15
16
17
# File 'lib/summarization/base.rb', line 15

def find_strategy(strategy_model)
  available_strategies.detect { |s| s.model == strategy_model }
end

.selected_strategyObject



19
20
21
22
23
# File 'lib/summarization/base.rb', line 19

def selected_strategy
  return if SiteSetting.summarization_strategy.blank?

  find_strategy(SiteSetting.summarization_strategy)
end

Instance Method Details

#configuration_hintObject

If we don’t meet the conditions to enable this strategy, we’ll display this hint as an error to admins.

Raises:

  • (NotImplemented)


60
61
62
# File 'lib/summarization/base.rb', line 60

def configuration_hint
  raise NotImplemented
end

#correctly_configured?Boolean

Some strategies could require other conditions to work correctly, like site settings. This method gets called when admins attempt to select it, checking if we met those conditions.

Returns:

  • (Boolean)

Raises:

  • (NotImplemented)


49
50
51
# File 'lib/summarization/base.rb', line 49

def correctly_configured?
  raise NotImplemented
end

#display_nameObject

Strategy name to display to admins in the available strategies dropdown.

Raises:

  • (NotImplemented)


54
55
56
# File 'lib/summarization/base.rb', line 54

def display_name
  raise NotImplemented
end

#modelObject

Returns the string we’ll store in the selected strategy site setting.

Raises:

  • (NotImplemented)


91
92
93
# File 'lib/summarization/base.rb', line 91

def model
  raise NotImplemented
end

#summarize(content) ⇒ Object

The idea behind this method is “give me a collection of texts, and I’ll handle the summarization to the best of my capabilities.”. It’s important to emphasize the “collection of texts” part, which implies it’s not tied to any model and expects the “content” to be a hash instead.

context to help the strategy produce a better result. Keys present in the content hash:

- resource_path (optional): Helps the strategy build links to the content in the summary (e.g. "/t/-/:topic_id/POST_NUMBER")
- content_title (optional): Provides guidance about what the content is about.
- contents (required): Array of hashes with content to summarize (e.g. [{ poster: "asd", id: 1, text: "This is a text" }])
  All keys are required.

will get called with partial summarized text as its generated.

Parameters:

  • content (Hash)
    • Includes the content to summarize, plus additional

  • &on_partial_blk (Block - Optional)
    • If the strategy supports it, the passed block

Raises:

  • (NotImplemented)


86
87
88
# File 'lib/summarization/base.rb', line 86

def summarize(content)
  raise NotImplemented
end