Class: Kotoshu::Suggestions::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/suggestions/pipeline.rb

Overview

Pipeline for composable suggestion strategies.

Allows chaining multiple suggestion strategies that execute in sequence, with optional early termination when a stage produces no results.

Examples:

Creating a pipeline

pipeline = Pipeline.new do |p|
  p.add :sym_spell
  p.add :phonetic
  p.add :ngram
end

Executing a pipeline

result = pipeline.execute(context, strategies)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|pipeline| ... } ⇒ Pipeline

Create a new pipeline.

Examples:

With block

pipeline = Pipeline.new do |p|
  p.add :sym_spell
  p.add :phonetic
end

Yields:

  • (pipeline)

    Optional block to add stages



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

def initialize
  @stages = []
  yield self if block_given?
end

Instance Attribute Details

#stagesArray<Symbol> (readonly)

Returns Ordered stage names.

Returns:

  • (Array<Symbol>)

    Ordered stage names



21
22
23
# File 'lib/kotoshu/suggestions/pipeline.rb', line 21

def stages
  @stages
end

Instance Method Details

#add(stage_name) ⇒ Pipeline

Add a stage to the pipeline.

Examples:

pipeline.add(:sym_spell)

Parameters:

  • stage_name (Symbol)

    Name of the stage

Returns:



45
46
47
48
# File 'lib/kotoshu/suggestions/pipeline.rb', line 45

def add(stage_name)
  @stages << stage_name
  self
end

#clearPipeline

Clear all stages.

Returns:



111
112
113
114
# File 'lib/kotoshu/suggestions/pipeline.rb', line 111

def clear
  @stages.clear
  self
end

#clonePipeline

Clone the pipeline.

Returns:

  • (Pipeline)

    New pipeline with same stages



119
120
121
# File 'lib/kotoshu/suggestions/pipeline.rb', line 119

def clone
  self.class.new.tap { |p| @stages.each { |s| p.add(s) } }
end

#execute(context, strategies = nil, early_termination: false) ⇒ SuggestionSet

Execute strategies through the pipeline.

Strategies are executed in sequence. If a strategy returns an empty SuggestionSet, subsequent strategies are still executed unless early_termination is enabled.

Examples:

strategies = { sym_spell: sym_spell_strategy, phonetic: phonetic_strategy }
result = pipeline.execute(context, strategies)

Parameters:

  • context (Context)

    The suggestion context

  • strategies (Hash) (defaults to: nil)

    Hash of stage_name => strategy_instance

  • early_termination (Boolean) (defaults to: false)

    Whether to stop on empty result

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kotoshu/suggestions/pipeline.rb', line 76

def execute(context, strategies = nil, early_termination: false)
  combined = SuggestionSet.empty

  @stages.each do |stage_name|
    strategy = if strategies.is_a?(Hash)
                 strategies[stage_name]
               else
                 strategies
               end

    next unless strategy

    result = strategy.generate(context)

    # Combine results
    combined = combine_results(combined, result)

    # Early termination on empty result
    break if early_termination && result.empty?
  end

  combined
end

#has_stage?(stage_name) ⇒ Boolean

Check if pipeline has a stage.

Parameters:

  • stage_name (Symbol)

    Stage name

Returns:

  • (Boolean)

    True if stage exists



104
105
106
# File 'lib/kotoshu/suggestions/pipeline.rb', line 104

def has_stage?(stage_name)
  @stages.include?(stage_name)
end

#remove(stage_name) ⇒ Pipeline

Remove a stage from the pipeline.

Examples:

pipeline.remove(:phonetic)

Parameters:

  • stage_name (Symbol)

    Name of the stage to remove

Returns:



57
58
59
60
# File 'lib/kotoshu/suggestions/pipeline.rb', line 57

def remove(stage_name)
  @stages.delete(stage_name)
  self
end