def test_dictionary_collection(self): dc = StenoDictionaryCollection() d1 = StenoDictionary() d1[('S', )] = 'a' d1[('T', )] = 'b' d2 = StenoDictionary() d2[('S', )] = 'c' d2[('W', )] = 'd' dc.set_dicts([d1, d2]) self.assertEqual(dc.lookup(('S', )), 'c') self.assertEqual(dc.lookup(('W', )), 'd') self.assertEqual(dc.lookup(('T', )), 'b') f = lambda k, v: v == 'c' dc.add_filter(f) self.assertIsNone(dc.lookup(('S', ))) self.assertEqual(dc.raw_lookup(('S', )), 'c') self.assertEqual(dc.lookup(('W', )), 'd') self.assertEqual(dc.lookup(('T', )), 'b') self.assertEqual(dc.reverse_lookup('c'), [('S', )]) dc.remove_filter(f) self.assertEqual(dc.lookup(('S', )), 'c') self.assertEqual(dc.lookup(('W', )), 'd') self.assertEqual(dc.lookup(('T', )), 'b') self.assertEqual(dc.reverse_lookup('c'), [('S', )]) dc.set(('S', ), 'e') self.assertEqual(dc.lookup(('S', )), 'e') self.assertEqual(d2[('S', )], 'e')
def test_dictionary_enabled(self): dc = StenoDictionaryCollection() d1 = StenoDictionary() d1.path = 'd1' d1[('TEFT', )] = 'test1' d1[('TEFGT', )] = 'Testing' d2 = StenoDictionary() d2[('TEFT', )] = 'test2' d2[('TEFT', '-G')] = 'Testing' d2.path = 'd2' dc.set_dicts([d2, d1]) self.assertEqual(dc.lookup(('TEFT', )), 'test2') self.assertEqual(dc.raw_lookup(('TEFT', )), 'test2') self.assertEqual(dc.casereverse_lookup('testing'), ['Testing']) self.assertCountEqual(dc.reverse_lookup('Testing'), [('TEFGT', ), ('TEFT', '-G')]) d2.enabled = False self.assertEqual(dc.lookup(('TEFT', )), 'test1') self.assertEqual(dc.raw_lookup(('TEFT', )), 'test1') self.assertEqual(dc.casereverse_lookup('testing'), ['Testing']) self.assertCountEqual(dc.reverse_lookup('Testing'), [('TEFGT', )]) d1.enabled = False self.assertEqual(dc.lookup(('TEST', )), None) self.assertEqual(dc.raw_lookup(('TEFT', )), None) self.assertEqual(dc.casereverse_lookup('testing'), None) self.assertCountEqual(dc.reverse_lookup('Testing'), [])
def test_dictionary_collection_longest_key(): k1 = ('S', ) k2 = ('S', 'T') k3 = ('S', 'T', 'R') dc = StenoDictionaryCollection() assert dc.longest_key == 0 d1 = StenoDictionary() d1.path = 'd1' d1[k1] = 'a' dc.set_dicts([d1]) assert dc.longest_key == 1 d1[k2] = 'a' assert dc.longest_key == 2 d2 = StenoDictionary() d2.path = 'd2' d2[k3] = 'c' dc.set_dicts([d2, d1]) assert dc.longest_key == 3 del d1[k2] assert dc.longest_key == 3 dc.set_dicts([d1]) assert dc.longest_key == 1 dc.set_dicts([]) assert dc.longest_key == 0
def test_dictionary_enabled(): dc = StenoDictionaryCollection() d1 = StenoDictionary() d1.path = 'd1' d1[('TEFT', )] = 'test1' d1[('TEFGT', )] = 'Testing' d2 = StenoDictionary() d2[('TEFT', )] = 'test2' d2[('TEFT', '-G')] = 'Testing' d2.path = 'd2' dc.set_dicts([d2, d1]) assert dc.lookup(('TEFT', )) == 'test2' assert dc.raw_lookup(('TEFT', )) == 'test2' assert dc.casereverse_lookup('testing') == ['Testing'] assert dc.reverse_lookup('Testing') == {('TEFT', '-G'), ('TEFGT', )} d2.enabled = False assert dc.lookup(('TEFT', )) == 'test1' assert dc.raw_lookup(('TEFT', )) == 'test1' assert dc.casereverse_lookup('testing') == ['Testing'] assert dc.reverse_lookup('Testing') == {('TEFGT', )} d1.enabled = False assert dc.lookup(('TEST', )) is None assert dc.raw_lookup(('TEFT', )) is None assert dc.casereverse_lookup('testing') == [] assert dc.reverse_lookup('Testing') == set()
def test_reverse_lookup(): dc = StenoDictionaryCollection() d1 = StenoDictionary() d1['PWAOUFL'] = 'beautiful' d1['WAOUFL'] = 'beautiful' d2 = StenoDictionary() d2['PW-FL'] = 'beautiful' d3 = StenoDictionary() d3['WAOUFL'] = 'not beautiful' # Simple test. dc.set_dicts([d1]) assert dc.reverse_lookup('beautiful') == {'PWAOUFL', 'WAOUFL'} # No duplicates. d2_copy = StenoDictionary() d2_copy.update(d2) dc.set_dicts([d2_copy, d2]) assert dc.reverse_lookup('beautiful') == {'PW-FL'} # Don't stop at the first dictionary with matches. dc.set_dicts([d2, d1]) assert dc.reverse_lookup('beautiful') == {'PW-FL', 'PWAOUFL', 'WAOUFL'} # Ignore keys overridden by a higher precedence dictionary. dc.set_dicts([d3, d2, d1]) assert dc.reverse_lookup('beautiful') == {'PW-FL', 'PWAOUFL'}
def test_reverse_lookup(self): dc = StenoDictionaryCollection() d1 = StenoDictionary() d1[('PWAOUFL', )] = 'beautiful' d1[('WAOUFL', )] = 'beautiful' d2 = StenoDictionary() d2[('PW-FL', )] = 'beautiful' d3 = StenoDictionary() d3[('WAOUFL', )] = 'not beautiful' # Simple test. dc.set_dicts([d1]) self.assertCountEqual(dc.reverse_lookup('beautiful'), [('PWAOUFL', ), ('WAOUFL', )]) # No duplicates. d2_copy = StenoDictionary() d2_copy.update(d2) dc.set_dicts([d2_copy, d2]) self.assertCountEqual(dc.reverse_lookup('beautiful'), [('PW-FL', )]) # Don't stop at the first dictionary with matches. dc.set_dicts([d2, d1]) self.assertCountEqual(dc.reverse_lookup('beautiful'), [('PWAOUFL', ), ('WAOUFL', ), ('PW-FL', )]) # Ignore keys overridden by a higher precedence dictionary. dc.set_dicts([d3, d2, d1]) self.assertCountEqual(dc.reverse_lookup('beautiful'), [('PWAOUFL', ), ('PW-FL', )])
def test_dictionary_collection_longest_key(self): k1 = ('S', ) k2 = ('S', 'T') k3 = ('S', 'T', 'R') dc = StenoDictionaryCollection() self.assertEqual(dc.longest_key, 0) d1 = StenoDictionary() d1._path = 'd1' d1[k1] = 'a' dc.set_dicts([d1]) self.assertEqual(dc.longest_key, 1) d1[k2] = 'a' self.assertEqual(dc.longest_key, 2) d2 = StenoDictionary() d2._path = 'd2' d2[k3] = 'c' dc.set_dicts([d2, d1]) self.assertEqual(dc.longest_key, 3) del d1[k2] self.assertEqual(dc.longest_key, 3) dc.set_dicts([d1]) self.assertEqual(dc.longest_key, 1) dc.set_dicts([]) self.assertEqual(dc.longest_key, 0)
def test_dictionary_collection_writeable(self): d1 = StenoDictionary() d1[('S', )] = 'a' d1[('T', )] = 'b' d2 = StenoDictionary() d2[('S', )] = 'c' d2[('W', )] = 'd' d2.readonly = True dc = StenoDictionaryCollection([d2, d1]) self.assertEqual(dc.first_writable(), d1) dc.set(('S', ), 'A') self.assertEqual(d1[('S', )], 'A') self.assertEqual(d2[('S', )], 'c')
def test_dictionary_collection_writeable(): d1 = StenoDictionary() d1[('S', )] = 'a' d1[('T', )] = 'b' d2 = StenoDictionary() d2[('S', )] = 'c' d2[('W', )] = 'd' d2.readonly = True dc = StenoDictionaryCollection([d2, d1]) assert dc.first_writable() == d1 dc.set(('S', ), 'A') assert d1[('S', )] == 'A' assert d2[('S', )] == 'c'
def test_dictionary_collection(self): dc = StenoDictionaryCollection() d1 = StenoDictionary() d1[('S', )] = 'a' d1[('T', )] = 'b' d1.path = 'd1' d2 = StenoDictionary() d2[('S', )] = 'c' d2[('W', )] = 'd' d2.path = 'd2' dc.set_dicts([d2, d1]) self.assertEqual(dc.lookup(('S', )), 'c') self.assertEqual(dc.lookup(('W', )), 'd') self.assertEqual(dc.lookup(('T', )), 'b') f = lambda k, v: v == 'c' dc.add_filter(f) self.assertIsNone(dc.lookup(('S', ))) self.assertEqual(dc.raw_lookup(('S', )), 'c') self.assertEqual(dc.lookup(('W', )), 'd') self.assertEqual(dc.lookup(('T', )), 'b') self.assertEqual(dc.reverse_lookup('c'), [('S', )]) dc.remove_filter(f) self.assertEqual(dc.lookup(('S', )), 'c') self.assertEqual(dc.lookup(('W', )), 'd') self.assertEqual(dc.lookup(('T', )), 'b') self.assertEqual(dc.reverse_lookup('c'), [('S', )]) dc.set(('S', ), 'e') self.assertEqual(dc.lookup(('S', )), 'e') self.assertEqual(d2[('S', )], 'e') dc.set(('S', ), 'f', path='d1') self.assertEqual(dc.lookup(('S', )), 'e') self.assertEqual(d1[('S', )], 'f') self.assertEqual(d2[('S', )], 'e') # Iterating on a StenoDictionaryCollection is # the same as iterating on its dictionaries' paths. self.assertEqual(list(dc), ['d2', 'd1']) # Test get and []. self.assertEqual(dc.get('d1'), d1) self.assertEqual(dc['d1'], d1) self.assertEqual(dc.get('invalid'), None) with self.assertRaises(KeyError): dc['invalid']
def test_dictionary(): d = StenoDictionary() assert len(d) == 0 assert not d d['S'] = 'a' assert 'S' in d assert len(d) == 1 assert d d['S/S/S/S'] = 'b' assert len(d) == 2 d['S/S'] = 'c' assert d['S/S'] == 'c' assert len(d) == 3 del d['S/S/S/S'] assert len(d) == 2 del d['S'] assert 'S' not in d assert len(d) == 1 assert d.reverse_lookup('c') == ['S/S'] assert d.casereverse_lookup('c') == ['c'] d.clear() assert len(d) == 0 assert not d assert d.reverse_lookup('c') == [] assert d.casereverse_lookup('c') == [] d['S/S'] = 'c'
def test_dictionary_update(update_from, start_empty): d = StenoDictionary() if not start_empty: d.update({ ('SPH*G', ): 'not something', ('STHEUPBG', ): 'something', ('EF', 'REU', 'TH*EUPBG'): 'everything', }) assert d[('STHEUPBG', )] == 'something' assert d[('EF', 'REU', 'TH*EUPBG')] == 'everything' assert d.reverse_lookup('not something') == [('SPH*G', )] assert d.longest_key == 3 d.update(update_from) assert d[('S-G', )] == 'something' assert d[('SPH-G', )] == 'something' assert d[('SPH*G', )] == 'Something' assert d[('SPH', 'THEUPBG')] == 'something' if not start_empty: assert d[('STHEUPBG', )] == 'something' assert d[('EF', 'REU', 'TH*EUPBG')] == 'everything' assert d.reverse_lookup('not something') == [] assert d.reverse_lookup('something') == [('STHEUPBG', ), ('S-G', ), ('SPH-G', ), ('SPH', 'THEUPBG')] assert set( d.casereverse_lookup('something')) == {'something', 'Something'} assert d.longest_key == 3 else: assert d.reverse_lookup('something') == [('S-G', ), ('SPH-G', ), ('SPH', 'THEUPBG')] assert set( d.casereverse_lookup('something')) == {'something', 'Something'} assert d.longest_key == 2
def setup_method(self): self.t = Translator() self.s = type(self).FakeState() self.t._state = self.s self.d = StenoDictionary() self.dc = StenoDictionaryCollection([self.d]) self.t.set_dictionary(self.dc)
def test_dictionary_collection(): d1 = StenoDictionary() d1[('S', )] = 'a' d1[('T', )] = 'b' d1.path = 'd1' d2 = StenoDictionary() d2[('S', )] = 'c' d2[('W', )] = 'd' d2.path = 'd2' dc = StenoDictionaryCollection([d2, d1]) assert dc.lookup(('S', )) == 'c' assert dc.lookup(('W', )) == 'd' assert dc.lookup(('T', )) == 'b' f = lambda k, v: v == 'c' dc.add_filter(f) assert dc.lookup(('S', )) is None assert dc.raw_lookup(('S', )) == 'c' assert dc.lookup(('W', )) == 'd' assert dc.lookup(('T', )) == 'b' assert dc.reverse_lookup('c') == {('S', )} dc.remove_filter(f) assert dc.lookup(('S', )) == 'c' assert dc.lookup(('W', )) == 'd' assert dc.lookup(('T', )) == 'b' assert dc.reverse_lookup('c') == {('S', )} dc.set(('S', ), 'e') assert dc.lookup(('S', )) == 'e' assert d2[('S', )] == 'e' dc.set(('S', ), 'f', path='d1') assert dc.lookup(('S', )) == 'e' assert d1[('S', )] == 'f' assert d2[('S', )] == 'e' # Iterating on a StenoDictionaryCollection is # the same as iterating on its dictionaries' paths. assert list(dc) == ['d2', 'd1'] # Test get and []. assert dc.get('d1') == d1 assert dc['d1'] == d1 assert dc.get('invalid') is None with pytest.raises(KeyError): dc['invalid']
def setup_method(self): self.d = StenoDictionary() self.dc = StenoDictionaryCollection([self.d]) self.s = _State() self.o = self.CaptureOutput() self.tlor = Translator() self.tlor.set_dictionary(self.dc) self.tlor.add_listener(self.o) self.tlor.set_state(self.s)
def setUp(self): self.output = CaptureOutput() self.formatter = Formatter() self.formatter.set_output(self.output) self.translator = Translator() self.translator.set_min_undo_length(100) self.translator.add_listener(self.formatter.format) self.dictionary = self.translator.get_dictionary() self.dictionary.set_dicts([StenoDictionary()])
def blackbox_setup(blackbox): blackbox.output = CaptureOutput() blackbox.formatter = Formatter() blackbox.formatter.set_output(blackbox.output) blackbox.translator = Translator() blackbox.translator.set_min_undo_length(100) blackbox.translator.add_listener(blackbox.formatter.format) blackbox.dictionary = blackbox.translator.get_dictionary() blackbox.dictionary.set_dicts([StenoDictionary()])
def test_casereverse_del(self): d = StenoDictionary() d[('S-G', )] = 'something' d[('SPH-G', )] = 'something' self.assertEqual(d.casereverse_lookup('something'), ['something']) del d[('S-G', )] self.assertEqual(d.casereverse_lookup('something'), ['something']) del d[('SPH-G', )] self.assertEqual(d.casereverse_lookup('something'), [])
def test_casereverse_del(): d = StenoDictionary() d[('S-G', )] = 'something' d[('SPH-G', )] = 'something' assert d.casereverse_lookup('something') == ['something'] del d[('S-G', )] assert d.casereverse_lookup('something') == ['something'] del d[('SPH-G', )] assert d.casereverse_lookup('something') == []
def test_casereverse_lookup(): dc = StenoDictionaryCollection() d1 = StenoDictionary() d1[('PWAOUFL', )] = 'beautiful' d1[('WAOUFL', )] = 'beAuTIFul' d2 = StenoDictionary() d2[('PW-FL', )] = 'BEAUTIFUL' d3 = StenoDictionary() d3[('WAOUFL', )] = 'not beautiful' dc.set_dicts([d1, d2, d3]) assert dc.casereverse_lookup('beautiful') == { 'beautiful', 'BEAUTIFUL', 'beAuTIFul' }
def setUp(self): self.output = CaptureOutput() self.formatter = Formatter() self.formatter.set_output(self.output) self.translator = Translator() self.translator.add_listener(self.formatter.format) self.dictionary = self.translator.get_dictionary() dictionary = StenoDictionary() dictionary.save = lambda: None self.dictionary.set_dicts([dictionary])
def load_dictionary(s): """Load an RTF/CRE dictionary.""" styles = load_stylesheet(s) d = {} converter = TranslationConverter(styles) for m in DICT_ENTRY_PATTERN.finditer(s): steno = normalize_steno(m.group('steno')) translation = m.group('translation') converted = converter(translation) if converted is not None: d[steno] = converted return StenoDictionary(d)
def test_dictionary(self): notifications = [] def listener(longest_key): notifications.append(longest_key) d = StenoDictionary() self.assertEqual(d.longest_key, 0) d.add_longest_key_listener(listener) d[('S', )] = 'a' self.assertEqual(d.longest_key, 1) self.assertEqual(notifications, [1]) d[('S', 'S', 'S', 'S')] = 'b' self.assertEqual(d.longest_key, 4) self.assertEqual(notifications, [1, 4]) d[('S', 'S')] = 'c' self.assertEqual(d.longest_key, 4) self.assertEqual(d[('S', 'S')], 'c') self.assertEqual(notifications, [1, 4]) del d[('S', 'S', 'S', 'S')] self.assertEqual(d.longest_key, 2) self.assertEqual(notifications, [1, 4, 2]) del d[('S', )] self.assertEqual(d.longest_key, 2) self.assertEqual(notifications, [1, 4, 2]) d.clear() self.assertEqual(d.longest_key, 0) self.assertEqual(notifications, [1, 4, 2, 0]) d.remove_longest_key_listener(listener) d[('S', 'S')] = 'c' self.assertEqual(d.longest_key, 2) self.assertEqual(notifications, [1, 4, 2, 0]) self.assertEqual(list(StenoDictionary([('a', 'b')]).items()), [('a', 'b')]) self.assertEqual(list(StenoDictionary(a='b').items()), [('a', 'b')])
def load_dictionary(filename): """Load an RTF/CRE dictionary.""" with resource_stream(filename) as fp: s = fp.read().decode('cp1252') styles = load_stylesheet(s) d = {} converter = TranslationConverter(styles) for m in DICT_ENTRY_PATTERN.finditer(s): steno = normalize_steno(m.group('steno')) translation = m.group('translation') converted = converter(translation) if converted is not None: d[steno] = converted return StenoDictionary(d)
def test_dictionary(): notifications = [] def listener(longest_key): notifications.append(longest_key) d = StenoDictionary() assert d.longest_key == 0 assert len(d) == 0 assert not d d.add_longest_key_listener(listener) d[('S', )] = 'a' assert d.longest_key == 1 assert ('S', ) in d assert notifications == [1] assert len(d) == 1 assert d d[('S', 'S', 'S', 'S')] = 'b' assert d.longest_key == 4 assert notifications == [1, 4] assert len(d) == 2 d[('S', 'S')] = 'c' assert d.longest_key == 4 assert d[('S', 'S')] == 'c' assert notifications == [1, 4] assert len(d) == 3 del d[('S', 'S', 'S', 'S')] assert d.longest_key == 2 assert notifications == [1, 4, 2] assert len(d) == 2 del d[('S', )] assert d.longest_key == 2 assert ('S', ) not in d assert notifications == [1, 4, 2] assert len(d) == 1 assert d.reverse_lookup('c') == [('S', 'S')] assert d.casereverse_lookup('c') == ['c'] d.clear() assert d.longest_key == 0 assert notifications == [1, 4, 2, 0] assert len(d) == 0 assert not d assert d.reverse_lookup('c') == [] assert d.casereverse_lookup('c') == [] d.remove_longest_key_listener(listener) d[('S', 'S')] = 'c' assert d.longest_key == 2 assert notifications == [1, 4, 2, 0]
def load_dictionary(filename): for encoding in ('utf-8', 'latin-1'): try: with resource_stream(filename, encoding=encoding) as fp: d = json.load(fp) break except UnicodeDecodeError: continue else: raise ValueError('\'%s\' encoding could not be determined' % (filename, )) return StenoDictionary( (normalize_steno(x[0]), x[1]) for x in iteritems(dict(d)))
def test_dictionary_update(): d = StenoDictionary() d.update([(('S-G', ), 'something'), (('SPH-G', ), 'something'), (('TPHOG', ), 'nothing')]) assert d[('S-G', )] == 'something' assert d[('SPH-G', )] == 'something' assert d[('TPHOG', )] == 'nothing' assert d.reverse_lookup('something') == [('S-G', ), ('SPH-G', )] assert d.reverse_lookup('nothing') == [('TPHOG', )] d.update([(('S-G', ), 'string')]) assert d[('S-G', )] == 'string' assert d.reverse_lookup('something') == [('SPH-G', )] assert d.reverse_lookup('string') == [('S-G', )] d.clear() d.update([(('EFG', ), 'everything'), (('EFG', ), 'everything???')]) assert d[('EFG', )] == 'everything???' assert d.reverse_lookup('everything') == [] assert d.reverse_lookup('everything???') == [('EFG', )]
def test_changing_state(self): output = [] def listener(undo, do, prev): prev = list(prev) if prev else None output.append((undo, do, prev)) d = StenoDictionary() d[('S', 'P')] = 'hi' dc = StenoDictionaryCollection() dc.set_dicts([d]) t = Translator() t.set_dictionary(dc) t.translate(stroke('T')) t.translate(stroke('S')) s = copy.deepcopy(t.get_state()) t.add_listener(listener) expected = [([Translation([stroke('S')], None)], [Translation([stroke('S'), stroke('P')], 'hi')], [Translation([stroke('T')], None)])] t.translate(stroke('P')) self.assertEqual(output, expected) del output[:] t.set_state(s) t.translate(stroke('P')) self.assertEqual(output, expected) del output[:] t.clear_state() t.translate(stroke('P')) self.assertEqual(output, [([], [Translation([stroke('P')], None)], None)]) del output[:] t.set_state(s) t.translate(stroke('P')) self.assertEqual(output, [([], [Translation([stroke('P')], None)], [Translation([stroke('S'), stroke('P')], 'hi')])])
def steno_dictionaries_from_state(state_str, existing_dictionaries=None): new_dictionaries = [] for enabled, icon, path in parse_state(state_str): if icon == 'loading': continue path = expand_path(path) if existing_dictionaries is None: steno_dict = None else: steno_dict = existing_dictionaries.get(path) if steno_dict is None: if icon == 'error' or path.endswith('.bad'): steno_dict = ErroredDictionary(path, INVALID_EXCEPTION) else: steno_dict = StenoDictionary() steno_dict.path = path steno_dict.readonly = (icon == 'readonly' or path.endswith('.ro') or path.startswith('asset:')) steno_dict.enabled = enabled new_dictionaries.append(steno_dict) return new_dictionaries
def test_dictionary(): notifications = [] def listener(longest_key): notifications.append(longest_key) d = StenoDictionary() assert d.longest_key == 0 d.add_longest_key_listener(listener) d[('S', )] = 'a' assert d.longest_key == 1 assert notifications == [1] d[('S', 'S', 'S', 'S')] = 'b' assert d.longest_key == 4 assert notifications == [1, 4] d[('S', 'S')] = 'c' assert d.longest_key == 4 assert d[('S', 'S')] == 'c' assert notifications == [1, 4] del d[('S', 'S', 'S', 'S')] assert d.longest_key == 2 assert notifications == [1, 4, 2] del d[('S', )] assert d.longest_key == 2 assert notifications == [1, 4, 2] assert d.reverse_lookup('c') == {('S', 'S')} assert d.casereverse_lookup('c') == {'c'} d.clear() assert d.longest_key == 0 assert notifications == [1, 4, 2, 0] assert d.reverse_lookup('c') == set() assert d.casereverse_lookup('c') == set() d.remove_longest_key_listener(listener) d[('S', 'S')] = 'c' assert d.longest_key == 2 assert notifications == [1, 4, 2, 0]