-
-
Notifications
You must be signed in to change notification settings - Fork 11
Account for multiple filters matching on the same token #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module TopSecret | ||
| class Text | ||
| # Replaces matched values in text with their label placeholders. | ||
| # | ||
| # When a value is matched by a single filter, every occurrence is replaced | ||
| # with that label. When a value is matched by multiple filters, occurrences | ||
| # are labeled in filter order; if there are more labels than occurrences, | ||
| # the later labels win (preserving "custom filter overrides default" semantics). | ||
| class Substitution | ||
| def initialize(mapping) | ||
| @mapping = mapping | ||
| end | ||
|
|
||
| def apply!(output) | ||
| return output if labels_by_value.empty? | ||
|
|
||
| substitute_single_label_values!(output) | ||
| substitute_multi_label_values!(output) | ||
| output | ||
| end | ||
|
|
||
| private | ||
|
|
||
| attr_reader :mapping | ||
|
|
||
| def labels_by_value | ||
| @labels_by_value ||= mapping.each_with_object({}) do |(filter, value), hash| | ||
| (hash[value] ||= []) << "[#{filter}]" | ||
| end | ||
| end | ||
|
|
||
| def single_label_values | ||
| labels_by_value.select { |_, labels| labels.one? } | ||
| end | ||
|
|
||
| def multi_label_values | ||
| labels_by_value.reject { |_, labels| labels.one? } | ||
| end | ||
|
|
||
| def substitute_single_label_values!(output) | ||
| return if single_label_values.empty? | ||
|
|
||
| value_to_label = single_label_values.transform_values(&:first) | ||
| output.gsub!(Regexp.union(value_to_label.keys), value_to_label) | ||
| end | ||
|
|
||
| def substitute_multi_label_values!(output) | ||
| multi_label_values.each do |value, labels| | ||
| labels_for_each_occurrence(value, labels, output).each do |label| | ||
| output.sub!(value, label) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def labels_for_each_occurrence(value, labels, output) | ||
| occurrences = output.scan(value).size | ||
| return labels.last(1) if occurrences.zero? | ||
|
|
||
| labels.last(occurrences) | ||
|
|
||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -62,6 +62,59 @@ | |||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| context "when the same value matches multiple filters" do | ||||||||||||||||||||||||||||||||||||||
| let(:austin_person) { build_entity(text: "Austin", tag: :person) } | ||||||||||||||||||||||||||||||||||||||
| let(:austin_location) { build_entity(text: "Austin", tag: :location) } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| before do | ||||||||||||||||||||||||||||||||||||||
| stub_ner_entities(austin_person, austin_location) | ||||||||||||||||||||||||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should check to see what happens if we swap the order:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Named entity recognition. I learned about it from using MITIE Ruby. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it! You've probably explained it before, which makes me think, could this method use the full name instead of the acronym so it's more accessible to the reader?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case I wanted to be consistent with how MITIE Ruby names things, since it has a |
||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| it "labels each occurrence according to the filter that matched it" do | ||||||||||||||||||||||||||||||||||||||
| input = "My name is Austin, and I live in Austin TX." | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| result = TopSecret::Text.filter(input) | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(result.output).to eq( | ||||||||||||||||||||||||||||||||||||||
| "My name is [PERSON_1], and I live in [LOCATION_1] TX." | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| expect(result.mapping).to eq({ | ||||||||||||||||||||||||||||||||||||||
| PERSON_1: "Austin", | ||||||||||||||||||||||||||||||||||||||
| LOCATION_1: "Austin" | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||
|
stevepolitodesign marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| context "when the same value matches multiple non-NER filters" do | ||||||||||||||||||||||||||||||||||||||
| before do | ||||||||||||||||||||||||||||||||||||||
| stub_ner_entities | ||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| it "labels each occurrence according to the filter that matched it" do | ||||||||||||||||||||||||||||||||||||||
| ip_filter = TopSecret::Filters::Regex.new( | ||||||||||||||||||||||||||||||||||||||
| label: "IP_ADDRESS", | ||||||||||||||||||||||||||||||||||||||
| regex: /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/ | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| server_filter = TopSecret::Filters::Regex.new( | ||||||||||||||||||||||||||||||||||||||
| label: "SERVER", | ||||||||||||||||||||||||||||||||||||||
| regex: /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/ | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+113
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make it clearer that these are identical expressions, what do you think of this?
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| result = TopSecret::Text.filter( | ||||||||||||||||||||||||||||||||||||||
| "Primary 192.168.1.1, backup 192.168.1.1.", | ||||||||||||||||||||||||||||||||||||||
| custom_filters: [ip_filter, server_filter] | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+122
to
+125
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here.
Suggested change
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| expect(result.output).to eq( | ||||||||||||||||||||||||||||||||||||||
| "Primary [IP_ADDRESS_1], backup [SERVER_1]." | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| expect(result.mapping).to eq({ | ||||||||||||||||||||||||||||||||||||||
| IP_ADDRESS_1: "192.168.1.1", | ||||||||||||||||||||||||||||||||||||||
| SERVER_1: "192.168.1.1" | ||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| context "when a custom filter matches the same value as a default filter" do | ||||||||||||||||||||||||||||||||||||||
| it "uses the custom filter's label" do | ||||||||||||||||||||||||||||||||||||||
| custom_filter = TopSecret::Filters::Regex.new( | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.