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

Extended by:
Logging::Helper, Settings::Helper
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



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

def concept_pattern
  keywords = settings[:entity_watchdog][:concept_keywords]
  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



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

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|
    entity_type = type_sym == :repository ? :repo : type_sym
    pattern = entity_type == :concept ? concept_pattern : ENTITY_PATTERNS[entity_type]
    next unless pattern

    text.scan(pattern).each do |match|
      entities << { type: entity_type, 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


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

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