Class: Vangrail::Screening

Inherits:
Struct
  • Object
show all
Defined in:
lib/vangrail/screening.rb

Overview

What Engine#screen returns. certain means what it means on a Result: false says a rail did not reach a decision about some document, so "nothing was rejected" is not evidence that nothing was wrong.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#certainObject

Returns the value of attribute certain

Returns:

  • (Object)

    the current value of certain



9
10
11
# File 'lib/vangrail/screening.rb', line 9

def certain
  @certain
end

#keptObject

Returns the value of attribute kept

Returns:

  • (Object)

    the current value of kept



9
10
11
# File 'lib/vangrail/screening.rb', line 9

def kept
  @kept
end

#reasonObject

Returns the value of attribute reason

Returns:

  • (Object)

    the current value of reason



9
10
11
# File 'lib/vangrail/screening.rb', line 9

def reason
  @reason
end

#rejectedObject

Returns the value of attribute rejected

Returns:

  • (Object)

    the current value of rejected



9
10
11
# File 'lib/vangrail/screening.rb', line 9

def rejected
  @rejected
end

Class Method Details

.replace_text(document, content) ⇒ Object

A context rail may rewrite a document rather than reject it, so the replacement has to go back into the shape the caller passed in.



61
62
63
64
65
66
# File 'lib/vangrail/screening.rb', line 61

def self.replace_text(document, content)
  return content.to_s unless document.is_a?(Hash)

  key = document.key?('text') ? 'text' : :text
  document.merge(key => content.to_s)
end

.run(engine, documents, **context) ⇒ Object

Screens a set of retrieved documents and reports what survived.

A document that fails is dropped rather than failing the whole turn. One poisoned wiki page should cost a reader that page, not their answer, and an application that refuses outright teaches its readers that the guardrail is the problem.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vangrail/screening.rb', line 37

def self.run(engine, documents, **context)
  kept = []
  rejected = []
  uncertain = nil

  Array(documents).each_with_index do |document, index|
    result = engine.check_context(text_of(document), **context, document: document, index: index)
    uncertain ||= result unless result.certain?
    if result.blocked?
      rejected << { document: document, result: result }
    else
      kept << (result.modified? ? replace_text(document, result.content) : document)
    end
  end

  new(kept: kept, rejected: rejected, certain: uncertain.nil?, reason: uncertain&.reason)
end

.text_of(document) ⇒ Object



55
56
57
# File 'lib/vangrail/screening.rb', line 55

def self.text_of(document)
  Cell.text_of(document)
end

Instance Method Details

#cellsObject



18
19
20
# File 'lib/vangrail/screening.rb', line 18

def cells
  kept.map { |document| Cell.data(Cell.text_of(document)) }
end

#certain?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/vangrail/screening.rb', line 10

def certain?
  certain
end

#rejected?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/vangrail/screening.rb', line 14

def rejected?
  !rejected.empty?
end

#to_hObject



22
23
24
25
26
27
28
29
# File 'lib/vangrail/screening.rb', line 22

def to_h
  {
    'kept' => kept.size,
    'rejected' => rejected.map { |r| r[:result].to_h },
    'certain' => certain?,
    'reason' => reason,
  }.compact
end