Class: Botrytis::SemanticMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/botrytis/semantic_matcher.rb

Defined Under Namespace

Classes: StepArgument

Instance Method Summary collapse

Constructor Details

#initializeSemanticMatcher

Returns a new instance of SemanticMatcher.



8
9
10
# File 'lib/botrytis/semantic_matcher.rb', line 8

def initialize
  ensure_cache_directory if Botrytis.configuration.cache_enabled
end

Instance Method Details

#find_match(step_text, available_step_definitions) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/botrytis/semantic_matcher.rb', line 12

def find_match(step_text, available_step_definitions)
  patterns = available_step_definitions.map do  |step_def|
    {
      pattern: step_def.regexp_source,
      proc: step_def.proc,
      step_def: step_def
    }
  end

  # Filter out test verification steps for semantic matching
  # These are steps that end with "should have executed" or similar test patterns
  business_patterns = patterns.reject do |p|
    pattern_text = p[:pattern].to_s
    pattern_text.include?("should have executed") ||
    pattern_text.include?("configured for testing") ||
    pattern_text.include?("test") ||
    pattern_text.include?("verification")
  end

  # Use business patterns for LLM matching, but keep all patterns for final matching
  query_patterns = business_patterns.empty? ? patterns : business_patterns

  if Botrytis.configuration.cache_enabled
    cache_result = check_cache(step_text, patterns.map { |p| p[:pattern] })
    return cache_result if cache_result
  end

  match_result = query_llm(step_text, query_patterns.map { |p| p[:pattern] })

  save_to_cache(step_text, patterns.map { |p| p[:pattern] }, match_result) if Botrytis.configuration.cache_enabled

  if match_result.match_found == "yes" && match_result.confidence.to_f >= Botrytis.configuration.confidence_threshold

    # Handle different pattern formats from LLM response
    # The LLM might return escaped quotes or slightly different formats
    matching_pattern = patterns.find do |p| 
      original_pattern = p[:pattern]
      llm_pattern = match_result.best_match_pattern
      
      # Try exact match first
      original_pattern == llm_pattern ||
      # Try with/without surrounding slashes
      original_pattern == "/#{llm_pattern}/" ||
      original_pattern.gsub(/^\/|\/$/,'') == llm_pattern.gsub(/^\/|\/$/,'') ||
      # Try with unescaped quotes (LLM might escape them)
      original_pattern == llm_pattern.gsub(/\\\"/, '"') ||
      original_pattern.gsub(/^\/|\/$/,'') == llm_pattern.gsub(/^\/|\/$/,'').gsub(/\\\"/, '"')
    end

    if matching_pattern
      # Convert comma-separated parameter_values string to array
      parameter_values = if match_result.parameter_values.nil? || match_result.parameter_values.empty?
                          []
                        else
                          match_result.parameter_values.split(',').map(&:strip)
                        end
      return create_match_result(matching_pattern[:step_def], step_text, parameter_values)
    end
  end

  nil
end