Class: TopSecret::Text::Result
- Inherits:
-
Object
- Object
- TopSecret::Text::Result
- Includes:
- Mapping
- Defined in:
- lib/top_secret/text/result.rb
Overview
Holds the result of a redaction operation.
Instance Attribute Summary collapse
-
#input ⇒ String
readonly
The original unredacted input.
-
#mapping ⇒ Hash
readonly
Mapping of redacted labels to matched values.
-
#output ⇒ String
readonly
The redacted output.
Class Method Summary collapse
-
.from_messages(messages, custom_filters: [], **filters) ⇒ Array<Result>
Filters multiple messages individually using a shared model for performance.
-
.with_global_labels(individual_results, global_mapping) ⇒ Array<Result>
Creates Result objects with globally consistent labels applied to text.
Instance Method Summary collapse
-
#initialize(input, output, mapping) ⇒ Result
constructor
A new instance of Result.
Methods included from Mapping
#categories, #method_missing, #respond_to_missing?, #safe?, #sensitive?
Constructor Details
#initialize(input, output, mapping) ⇒ Result
Returns a new instance of Result.
21 22 23 24 25 |
# File 'lib/top_secret/text/result.rb', line 21 def initialize(input, output, mapping) @input = input @output = output @mapping = mapping end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class TopSecret::Mapping
Instance Attribute Details
#input ⇒ String (readonly)
Returns The original unredacted input.
10 11 12 |
# File 'lib/top_secret/text/result.rb', line 10 def input @input end |
#mapping ⇒ Hash (readonly)
Returns Mapping of redacted labels to matched values.
16 17 18 |
# File 'lib/top_secret/text/result.rb', line 16 def mapping @mapping end |
#output ⇒ String (readonly)
Returns The redacted output.
13 14 15 |
# File 'lib/top_secret/text/result.rb', line 13 def output @output end |
Class Method Details
.from_messages(messages, custom_filters: [], **filters) ⇒ Array<Result>
Filters multiple messages individually using a shared model for performance
34 35 36 37 38 39 40 |
# File 'lib/top_secret/text/result.rb', line 34 def self.(, custom_filters: [], **filters) shared_model = TopSecret.model_path ? Mitie::NER.new(TopSecret.model_path) : nil .map do || TopSecret::Text.new(, filters:, custom_filters:, model: shared_model).filter end end |
.with_global_labels(individual_results, global_mapping) ⇒ Array<Result>
Creates Result objects with globally consistent labels applied to text
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/top_secret/text/result.rb', line 47 def self.with_global_labels(individual_results, global_mapping) value_to_label = global_mapping.each_with_object({}) do |(filter, value), hash| hash[value] = "[#{filter}]" end pattern = Regexp.union(value_to_label.keys) individual_results.map do |result| output = result.input.gsub(pattern, value_to_label) filter_keys = output.scan(/\[([^\]]+)\]/).flatten.map(&:to_sym) mapping = global_mapping.slice(*filter_keys) new(result.input, output, mapping) end end |