Class: GuardrailsRuby::Checks::HallucinatedEmails

Inherits:
GuardrailsRuby::Check show all
Defined in:
lib/guardrails_ruby/checks/hallucinated_emails.rb

Constant Summary collapse

EMAIL_PATTERN =
/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/

Instance Attribute Summary

Attributes inherited from GuardrailsRuby::Check

#options

Instance Method Summary collapse

Methods inherited from GuardrailsRuby::Check

check_name, direction, #initialize, lookup

Constructor Details

This class inherits a constructor from GuardrailsRuby::Check

Instance Method Details

#call(text, context: {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/guardrails_ruby/checks/hallucinated_emails.rb', line 11

def call(text, context: {})
  emails = text.scan(EMAIL_PATTERN)
  return pass! if emails.empty?

  source_context = context[:source_context] || ""
  source_emails = source_context.scan(EMAIL_PATTERN).map(&:downcase)

  hallucinated = emails.reject { |e| source_emails.include?(e.downcase) }

  if hallucinated.any?
    fail! "Potentially hallucinated emails: #{hallucinated.join(', ')}",
      action: @options.fetch(:action, :warn),
      matches: hallucinated
  else
    pass!
  end
end