Class: Kotoshu::Suggestions::Strategies::CompositeStrategy

Inherits:
BaseStrategy
  • Object
show all
Defined in:
lib/kotoshu/suggestions/strategies/composite_strategy.rb

Overview

Composite strategy that chains multiple suggestion strategies. Implements the Composite Pattern for extensible suggestion generation.

This is MORE OOP than Spylls which has a procedural suggestion pipeline. Here, strategies are proper objects that can be added/removed/reordered.

Examples:

Using composite strategy

pipeline = CompositeStrategy.new(name: :pipeline)
pipeline.add(EditDistanceStrategy.new)
pipeline.add(PhoneticStrategy.new)
pipeline.add(NgramStrategy.new)
suggestions = pipeline.generate(context)

Instance Attribute Summary collapse

Attributes inherited from BaseStrategy

#config, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseStrategy

#calculate_ngram_similarity, #create_suggestion, #create_suggestion_set, #enabled?, #generate_ngrams, #get_config, #has_config?, #max_results, #priority

Constructor Details

#initialize(name:, strategies: [], **config) ⇒ CompositeStrategy

Returns a new instance of CompositeStrategy.

Parameters:

  • name (String, Symbol)

    Name of the composite

  • strategies (Array<BaseStrategy>) (defaults to: [])

    Initial strategies

  • config (Hash)

    Configuration options



24
25
26
27
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 24

def initialize(name:, strategies: [], **config)
  @strategies = strategies
  super(name: name, **config)
end

Instance Attribute Details

#strategiesObject (readonly)

Returns the value of attribute strategies.



19
20
21
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 19

def strategies
  @strategies
end

Class Method Details

.with_defaults(**config) ⇒ CompositeStrategy

Create a composite strategy with default algorithms.

Parameters:

  • config (Hash)

    Configuration

Returns:



134
135
136
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 134

def self.with_defaults(**config)
  new(name: :default, **config)
end

Instance Method Details

#add(strategy) ⇒ CompositeStrategy Also known as: <<

Add a strategy to the pipeline.

Parameters:

Returns:



33
34
35
36
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 33

def add(strategy)
  @strategies << strategy
  self
end

#any?Boolean

Check if the composite has any strategies.

Returns:

  • (Boolean)

    True if there are strategies



100
101
102
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 100

def any?
  @strategies.any?
end

#applicable_strategies(context) ⇒ Array<BaseStrategy>

Get strategies that can handle the given context.

Parameters:

  • context (Context)

    The suggestion context

Returns:



60
61
62
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 60

def applicable_strategies(context)
  @strategies.select { |s| s.handles?(context) }
end

#clearCompositeStrategy

Clear all strategies.

Returns:



51
52
53
54
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 51

def clear
  @strategies.clear
  self
end

#each_strategy {|strategy| ... } ⇒ Enumerator

Iterate over strategies.

Yields:

  • (strategy)

    Each strategy

Returns:

  • (Enumerator)

    Enumerator if no block given



108
109
110
111
112
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 108

def each_strategy(&block)
  return enum_for(:each_strategy) unless block_given?

  @strategies.each(&block)
end

#generate(context) ⇒ SuggestionSet

Generate suggestions by delegating to all child strategies.

Parameters:

  • context (Context)

    The suggestion context

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 68

def generate(context)
  # Create result set
  result = SuggestionSet.empty(max_size: context.max_results)

  # Process each applicable strategy
  applicable_strategies(context).each do |strategy|
    strategy_result = strategy.generate(context)
    result.merge!(strategy_result)
  end

  result
end

#handles?(context) ⇒ Boolean

Check if any strategy can handle the context.

Parameters:

  • context (Context)

    The suggestion context

Returns:

  • (Boolean)

    True if any strategy handles the context



85
86
87
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 85

def handles?(context)
  applicable_strategies(context).any?
end

#remove(strategy) ⇒ CompositeStrategy

Remove a strategy from the pipeline.

Parameters:

Returns:



43
44
45
46
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 43

def remove(strategy)
  @strategies.delete(strategy)
  self
end

#sizeInteger Also known as: count

Get the number of strategies.

Returns:

  • (Integer)

    Number of strategies



92
93
94
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 92

def size
  @strategies.size
end

#sort_by_priority!CompositeStrategy

Sort strategies by priority.

Returns:



117
118
119
120
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 117

def sort_by_priority!
  @strategies.sort_by!(&:priority)
  self
end

#to_sString Also known as: inspect

Convert to string.

Returns:

  • (String)

    String representation



125
126
127
# File 'lib/kotoshu/suggestions/strategies/composite_strategy.rb', line 125

def to_s
  "#{self.class.name}(name: #{@name}, strategies: #{@strategies.map(&:name).join(", ")})"
end