Class: Vangrail::Screening
- Inherits:
-
Struct
- Object
- Struct
- Vangrail::Screening
- 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
-
#certain ⇒ Object
Returns the value of attribute certain.
-
#kept ⇒ Object
Returns the value of attribute kept.
-
#reason ⇒ Object
Returns the value of attribute reason.
-
#rejected ⇒ Object
Returns the value of attribute rejected.
Class Method Summary collapse
-
.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.
-
.run(engine, documents, **context) ⇒ Object
Screens a set of retrieved documents and reports what survived.
- .text_of(document) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#certain ⇒ Object
Returns the value of attribute certain
9 10 11 |
# File 'lib/vangrail/screening.rb', line 9 def certain @certain end |
#kept ⇒ Object
Returns the value of attribute kept
9 10 11 |
# File 'lib/vangrail/screening.rb', line 9 def kept @kept end |
#reason ⇒ Object
Returns the value of attribute reason
9 10 11 |
# File 'lib/vangrail/screening.rb', line 9 def reason @reason end |
#rejected ⇒ Object
Returns the value of attribute 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 |
Instance Method Details
#cells ⇒ Object
18 19 20 |
# File 'lib/vangrail/screening.rb', line 18 def cells kept.map { |document| Cell.data(Cell.text_of(document)) } end |
#certain? ⇒ Boolean
10 11 12 |
# File 'lib/vangrail/screening.rb', line 10 def certain? certain end |
#rejected? ⇒ Boolean
14 15 16 |
# File 'lib/vangrail/screening.rb', line 14 def rejected? !rejected.empty? end |
#to_h ⇒ Object
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 |