Module: Legion::Extensions::Apollo::Helpers::EntityWatchdog

Defined in:
lib/legion/extensions/apollo/helpers/entity_watchdog.rb

Constant Summary collapse

ENTITY_PATTERNS =
{
  person:  /\b[A-Z][a-z]+(?:\s[A-Z][a-z]+)+\b/,
  service: %r{\bhttps?://[^\s]+\b},
  repo:    %r{\b[a-zA-Z0-9_-]+/[a-zA-Z0-9_.-]+\b}
}.freeze

Class Method Summary collapse

Class Method Details

.concept_patternObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/legion/extensions/apollo/helpers/entity_watchdog.rb', line 57

def concept_pattern
  keywords = if defined?(Legion::Settings)
               Legion::Settings.dig(:apollo, :entity_watchdog, :concept_keywords) || []
             else
               []
             end
  return nil if keywords.empty?

  Regexp.new("\\b(?:#{keywords.map { |k| Regexp.escape(k) }.join('|')})\\b", Regexp::IGNORECASE)
end

.detect_entities(text:, types: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/legion/extensions/apollo/helpers/entity_watchdog.rb', line 19

def detect_entities(text:, types: nil)
  return [] if text.nil? || text.empty?

  types = (types || default_types).map(&:to_sym)
  entities = []

  types.each do |type_sym|
    pattern = type_sym == :concept ? concept_pattern : ENTITY_PATTERNS[type_sym]
    next unless pattern

    text.scan(pattern).each do |match|
      entities << { type: type_sym, value: match.strip, confidence: Confidence.apollo_setting(:entity_watchdog, :detect_confidence, default: 0.5) }
    end
  end

  entities.uniq { |e| [e[:type], e[:value].downcase] }
end


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/legion/extensions/apollo/helpers/entity_watchdog.rb', line 37

def link_or_create(entities:, source_context: nil)
  return { success: true, linked: 0, created: 0 } if entities.nil? || entities.empty?

  linked = 0
  created = 0

  entities.each do |entity|
    existing = find_existing(entity)
    if existing
      bump_confidence(existing, source_context)
      linked += 1
    else
      create_candidate(entity, source_context)
      created += 1
    end
  end

  { success: true, linked: linked, created: created }
end

.logObject



15
16
17
# File 'lib/legion/extensions/apollo/helpers/entity_watchdog.rb', line 15

def log
  Legion::Logging
end