Class: TopSecret::Filters::NER

Inherits:
Object
  • Object
show all
Defined in:
lib/top_secret/filters/ner.rb

Overview

Applies Named Entity Recognition (NER) filtering based on tag and confidence score.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, tag:, min_confidence_score: nil) ⇒ NER

Returns a new instance of NER.

Parameters:

  • label (String)

    The label for redacted entities

  • tag (Symbol, String)

    The NER tag to match (e.g., :person, :location)

  • min_confidence_score (Float, nil) (defaults to: nil)

    Minimum score required for a match (defaults to TopSecret.min_confidence_score)



13
14
15
16
17
# File 'lib/top_secret/filters/ner.rb', line 13

def initialize(label:, tag:, min_confidence_score: nil)
  @label = label
  @tag = tag.upcase.to_s
  @min_confidence_score = min_confidence_score
end

Instance Attribute Details

#labelString (readonly)

Returns The label applied to matching entities.

Returns:

  • (String)

    The label applied to matching entities



8
9
10
# File 'lib/top_secret/filters/ner.rb', line 8

def label
  @label
end

Instance Method Details

#call(entities) ⇒ Array<String>

Filters and extracts entity texts matching the tag and score threshold.

Parameters:

  • entities (Array<Hash>)

    List of entity hashes with keys :tag, :score, and :text

Returns:

  • (Array<String>)

    Matched entity texts



23
24
25
26
# File 'lib/top_secret/filters/ner.rb', line 23

def call(entities)
  tags = entities.filter { _1.fetch(:tag) == tag && _1.fetch(:score) >= (min_confidence_score || TopSecret.min_confidence_score) }
  tags.map { _1.fetch(:text) }
end