Class: TopSecret::Text::Result

Inherits:
Object
  • Object
show all
Includes:
Mapping
Defined in:
lib/top_secret/text/result.rb

Overview

Holds the result of a redaction operation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • input (String)

    The original text

  • output (String)

    The redacted text

  • mapping (Hash)

    Map of labels to matched values



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

#inputString (readonly)

Returns The original unredacted input.

Returns:

  • (String)

    The original unredacted input



10
11
12
# File 'lib/top_secret/text/result.rb', line 10

def input
  @input
end

#mappingHash (readonly)

Returns Mapping of redacted labels to matched values.

Returns:

  • (Hash)

    Mapping of redacted labels to matched values



16
17
18
# File 'lib/top_secret/text/result.rb', line 16

def mapping
  @mapping
end

#outputString (readonly)

Returns The redacted output.

Returns:

  • (String)

    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

Parameters:

  • messages (Array<String>)

    Array of text messages to filter

  • custom_filters (Array) (defaults to: [])

    Additional custom filters to apply

  • filters (Hash)

    Optional filters to override defaults (only valid filter keys accepted)

Returns:

  • (Array<Result>)

    Array of individual Result objects for each message

Raises:

  • (ArgumentError)

    If invalid filter keys are provided



34
35
36
37
38
39
40
# File 'lib/top_secret/text/result.rb', line 34

def self.from_messages(messages, custom_filters: [], **filters)
  shared_model = TopSecret.model_path ? Mitie::NER.new(TopSecret.model_path) : nil

  messages.map do |message|
    TopSecret::Text.new(message, 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

Parameters:

  • individual_results (Array<Result>)

    Array of individual filter results

  • global_mapping (Hash)

    Global mapping from filter labels to original values

Returns:

  • (Array<Result>)

    Array of Result objects with globally consistent redaction and individual mappings



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