Skip to content

Commit

Permalink
Fix input creation in Scrambler to correctly reset IDs.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 555226103
  • Loading branch information
cpka145 authored and LIT team committed Aug 9, 2023
1 parent d7e5406 commit 8fa1ce5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lit_nlp/components/scrambler.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def generate(self,
if not text_keys:
return []

new_example = dict(example)
for text_key in text_keys:
new_example[text_key] = _scramble(example[text_key])
updates = {
text_key: _scramble(example[text_key])
for text_key in text_keys
}
new_example = utils.make_modified_input(example, updates, 'Scrambler')
return [new_example]
63 changes: 63 additions & 0 deletions lit_nlp/components/scrambler_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for lit_nlp.components.scrambler."""


from absl.testing import absltest
from lit_nlp.api import dataset as lit_dataset
from lit_nlp.api import types as lit_types
from lit_nlp.components import scrambler
from lit_nlp.lib import testing_utils


class ScramblerTest(absltest.TestCase):

def setUp(self):
super().setUp()
test_spec: lit_types.Spec = {
'sentence': lit_types.TextSegment(),
'_id': lit_types.TextSegment(),
'_meta': lit_types.JsonDict,
}
self.model = testing_utils.RegressionModelForTesting(test_spec)
# Dataset is only used for spec in Scrambler so define once
self.dataset = lit_dataset.Dataset(
spec=test_spec,
examples=[{'text': 'blank', '_id': 'a1b2c3', '_meta': {}}],
)
self.generator = scrambler.Scrambler()

def test_scrambler_id_changes(self):
example = {
'sentence': 'this is the sentence to be scrambled',
'_id': 'a1b2c3',
'_meta': {'parentId': '000000'},
}

config = {scrambler.FIELDS_TO_SCRAMBLE_KEY: ['sentence']}
generated_example = self.generator.generate(
example, self.model, self.dataset, config
)[0]
self.assertNotEqual(generated_example, example)
self.assertNotEqual(generated_example['_id'], example['_id'])
self.assertEqual(
set(generated_example['sentence'].split()),
set(example['sentence'].split()),
)
self.assertEqual(generated_example['_meta']['parentId'], example['_id'])


if __name__ == '__main__':
absltest.main()

0 comments on commit 8fa1ce5

Please sign in to comment.