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 overriden by a higher precedence dictionary.
        dc.set_dicts([d3, d2, d1])
        self.assertCountEqual(dc.reverse_lookup('beautiful'),
                              [('PWAOUFL',), ('PW-FL',)])
Exemplo n.º 2
0
    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')
Exemplo n.º 3
0
    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.save = lambda: None
        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([d1, d2])
        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)
Exemplo n.º 4
0
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
Exemplo n.º 5
0
    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])
        assertCountEqual(self, dc.reverse_lookup('beautiful'), [('PWAOUFL', ),
                                                                ('WAOUFL', )])

        # No duplicates.
        dc.set_dicts([d2, StenoDictionary(d2)])
        assertCountEqual(self, dc.reverse_lookup('beautiful'), [('PW-FL', )])

        # Don't stop at the first dictionary with matches.
        dc.set_dicts([d1, d2])
        assertCountEqual(self, dc.reverse_lookup('beautiful'), [('PWAOUFL', ),
                                                                ('WAOUFL', ),
                                                                ('PW-FL', )])

        # Ignore keys overriden by a higher precedence dictionary.
        dc.set_dicts([d1, d2, d3])
        assertCountEqual(self, dc.reverse_lookup('beautiful'), [('PWAOUFL', ),
                                                                ('PW-FL', )])
Exemplo n.º 6
0
 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'), [])
Exemplo n.º 7
0
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_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'), [])
Exemplo n.º 9
0
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 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])
Exemplo n.º 11
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')
Exemplo n.º 12
0
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'
Exemplo n.º 13
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')
Exemplo n.º 14
0
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'
Exemplo n.º 15
0
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'
Exemplo n.º 16
0
 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'), [])
Exemplo n.º 17
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()
Exemplo n.º 18
0
    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']
Exemplo n.º 19
0
 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)
Exemplo n.º 20
0
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']
Exemplo n.º 21
0
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', )}
Exemplo n.º 22
0
    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.save = lambda: None
        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([d1, d2])
        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)
Exemplo n.º 23
0
 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)
Exemplo n.º 24
0
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()])
Exemplo n.º 25
0
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'
    }
Exemplo n.º 26
0
 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()])
Exemplo n.º 27
0
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)
Exemplo n.º 28
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') is None
    assert dc.reverse_lookup('Testing') == []
Exemplo n.º 29
0
 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'])
     assertCountEqual(self, 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'])
     assertCountEqual(self, 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)
     assertCountEqual(self, dc.reverse_lookup('Testing'), [])
Exemplo n.º 30
0
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
Exemplo n.º 31
0
    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')])
Exemplo n.º 32
0
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)
Exemplo n.º 33
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)))
Exemplo n.º 34
0
    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')])])
Exemplo n.º 35
0
    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']
Exemplo n.º 36
0
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']
Exemplo n.º 37
0
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
Exemplo n.º 38
0
 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(StenoDictionary([('a', 'b')]).items(), [('a', 'b')])
     self.assertEqual(StenoDictionary(a='b').items(), [('a', 'b')])
    def test_dictionary_collection(self):
        dc = StenoDictionaryCollection()
        d1 = StenoDictionary()
        d1[('S',)] = 'a'
        d1[('T',)] = 'b'
        d1.set_path('d1')
        d2 = StenoDictionary()
        d2[('S',)] = 'c'
        d2[('W',)] = 'd'
        d2.set_path('d2')
        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')

        dc.set(('S',), 'f', dictionary='d1')
        self.assertEqual(dc.lookup(('S',)), 'e')
        self.assertEqual(d1[('S',)], 'f')
        self.assertEqual(d2[('S',)], 'e')
Exemplo n.º 40
0
def test_translator():

    # It's not clear that this test is needed anymore. There are separate
    # tests for _translate_stroke and test_translate_calls_translate_stroke
    # makes sure that translate calls it properly. But since I already wrote
    # this test I'm going to keep it.

    class Output:
        def __init__(self):
            self._output = []

        def write(self, undo, do, prev):
            for t in undo:
                self._output.pop()
            for t in do:
                if t.english:
                    self._output.append(t.english)
                else:
                    self._output.append('/'.join(t.rtfcre))

        def get(self):
            return ' '.join(self._output)

        def clear(self):
            del self._output[:]

    d = StenoDictionary()
    out = Output()
    t = Translator()
    dc = StenoDictionaryCollection([d])
    t.set_dictionary(dc)
    t.add_listener(out.write)

    t.translate(stroke('S'))
    assert out.get() == 'S'
    t.translate(stroke('T'))
    assert out.get() == 'S T'
    t.translate(stroke('*'))
    assert out.get() == 'S'
    t.translate(stroke('*'))
    # Undo buffer ran out
    assert out.get() == 'S ' + BACK_STRING

    t.set_min_undo_length(3)
    out.clear()
    t.translate(stroke('S'))
    assert out.get() == 'S'
    t.translate(stroke('T'))
    assert out.get() == 'S T'
    t.translate(stroke('*'))
    assert out.get() == 'S'
    t.translate(stroke('*'))
    assert out.get() == ''

    out.clear()
    d[('S',)] = 't1'
    d[('T',)] = 't2'
    d[('S', 'T')] = 't3'

    t.translate(stroke('S'))
    assert out.get() == 't1'
    t.translate(stroke('T'))
    assert out.get() == 't3'
    t.translate(stroke('T'))
    assert out.get() == 't3 t2'
    t.translate(stroke('S'))
    assert out.get() == 't3 t2 t1'
    t.translate(stroke('*'))
    assert out.get() == 't3 t2'
    t.translate(stroke('*'))
    assert out.get() == 't3'
    t.translate(stroke('*'))
    assert out.get() == 't1'
    t.translate(stroke('*'))
    assert out.get() == ''

    t.translate(stroke('S'))
    assert out.get() == 't1'
    t.translate(stroke('T'))
    assert out.get() == 't3'
    t.translate(stroke('T'))
    assert out.get() == 't3 t2'

    d[('S', 'T', 'T')] = 't4'
    d[('S', 'T', 'T', 'S')] = 't5'

    t.translate(stroke('S'))
    assert out.get() == 't5'
    t.translate(stroke('*'))
    assert out.get() == 't3 t2'
    t.translate(stroke('*'))
    assert out.get() == 't3'
    t.translate(stroke('T'))
    assert out.get() == 't4'
    t.translate(stroke('S'))
    assert out.get() == 't5'
    t.translate(stroke('S'))
    assert out.get() == 't5 t1'
    t.translate(stroke('*'))
    assert out.get() == 't5'
    t.translate(stroke('*'))
    assert out.get() == 't4'
    t.translate(stroke('*'))
    assert out.get() == 't3'
    t.translate(stroke('*'))
    assert out.get() == 't1'
    t.translate(stroke('*'))
    assert out.get() == ''

    d.clear()

    s = stroke('S')
    t.translate(s)
    t.translate(s)
    t.translate(s)
    t.translate(s)
    s = stroke('*')
    t.translate(s)
    t.translate(s)
    t.translate(s)
    t.translate(s)
    # Not enough undo to clear output.
    assert out.get() == 'S ' + BACK_STRING

    out.clear()
    t.remove_listener(out.write)
    t.translate(stroke('S'))
    assert out.get() == ''
Exemplo n.º 41
0
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') == []
    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]
Exemplo n.º 42
0
        # Deleting the file will fail on Windows
        # if we don't restore write permission.
        os.chmod(tf.name, stat.S_IWRITE)
        os.unlink(tf.name)
    # Assets are always readonly.
    d = FakeDictionary.load('asset:plover:assets/main.json')
    assert d.readonly


TEST_DICTIONARY_UPDATE_DICT = {
    ('S-G',): 'something',
    ('SPH-G',): 'something',
    ('SPH*G',): 'Something',
    ('SPH', 'THEUPBG'): 'something',
}
TEST_DICTIONARY_UPDATE_STENODICT = StenoDictionary()
TEST_DICTIONARY_UPDATE_STENODICT.update(TEST_DICTIONARY_UPDATE_DICT)

@pytest.mark.parametrize('update_from, start_empty', (
    (dict(TEST_DICTIONARY_UPDATE_DICT), True),
    (dict(TEST_DICTIONARY_UPDATE_DICT), False),
    (list(TEST_DICTIONARY_UPDATE_DICT.items()), True),
    (list(TEST_DICTIONARY_UPDATE_DICT.items()), False),
    (iter(TEST_DICTIONARY_UPDATE_DICT.items()), True),
    (iter(TEST_DICTIONARY_UPDATE_DICT.items()), False),
    (TEST_DICTIONARY_UPDATE_STENODICT, True),
    (TEST_DICTIONARY_UPDATE_STENODICT, False),
))
def test_dictionary_update(update_from, start_empty):
    d = StenoDictionary()
    if not start_empty:
Exemplo n.º 43
0
def test_translator():

    # It's not clear that this test is needed anymore. There are separate
    # tests for _translate_stroke and test_translate_calls_translate_stroke
    # makes sure that translate calls it properly. But since I already wrote
    # this test I'm going to keep it.

    class Output:
        def __init__(self):
            self._output = []

        def write(self, undo, do, prev):
            for t in undo:
                self._output.pop()
            for t in do:
                if t.english:
                    self._output.append(t.english)
                else:
                    self._output.append('/'.join(t.rtfcre))

        def get(self):
            return ' '.join(self._output)

        def clear(self):
            del self._output[:]

    d = StenoDictionary()
    out = Output()
    t = Translator()
    dc = StenoDictionaryCollection([d])
    t.set_dictionary(dc)
    t.add_listener(out.write)

    t.translate(stroke('S'))
    assert out.get() == 'S'
    t.translate(stroke('T'))
    assert out.get() == 'S T'
    t.translate(stroke('*'))
    assert out.get() == 'S'
    t.translate(stroke('*'))
    # Undo buffer ran out
    assert out.get() == 'S ' + BACK_STRING

    t.set_min_undo_length(3)
    out.clear()
    t.translate(stroke('S'))
    assert out.get() == 'S'
    t.translate(stroke('T'))
    assert out.get() == 'S T'
    t.translate(stroke('*'))
    assert out.get() == 'S'
    t.translate(stroke('*'))
    assert out.get() == ''

    out.clear()
    d[('S', )] = 't1'
    d[('T', )] = 't2'
    d[('S', 'T')] = 't3'

    t.translate(stroke('S'))
    assert out.get() == 't1'
    t.translate(stroke('T'))
    assert out.get() == 't3'
    t.translate(stroke('T'))
    assert out.get() == 't3 t2'
    t.translate(stroke('S'))
    assert out.get() == 't3 t2 t1'
    t.translate(stroke('*'))
    assert out.get() == 't3 t2'
    t.translate(stroke('*'))
    assert out.get() == 't3'
    t.translate(stroke('*'))
    assert out.get() == 't1'
    t.translate(stroke('*'))
    assert out.get() == ''

    t.translate(stroke('S'))
    assert out.get() == 't1'
    t.translate(stroke('T'))
    assert out.get() == 't3'
    t.translate(stroke('T'))
    assert out.get() == 't3 t2'

    d[('S', 'T', 'T')] = 't4'
    d[('S', 'T', 'T', 'S')] = 't5'

    t.translate(stroke('S'))
    assert out.get() == 't5'
    t.translate(stroke('*'))
    assert out.get() == 't3 t2'
    t.translate(stroke('*'))
    assert out.get() == 't3'
    t.translate(stroke('T'))
    assert out.get() == 't4'
    t.translate(stroke('S'))
    assert out.get() == 't5'
    t.translate(stroke('S'))
    assert out.get() == 't5 t1'
    t.translate(stroke('*'))
    assert out.get() == 't5'
    t.translate(stroke('*'))
    assert out.get() == 't4'
    t.translate(stroke('*'))
    assert out.get() == 't3'
    t.translate(stroke('*'))
    assert out.get() == 't1'
    t.translate(stroke('*'))
    assert out.get() == ''

    d.clear()

    s = stroke('S')
    t.translate(s)
    t.translate(s)
    t.translate(s)
    t.translate(s)
    s = stroke('*')
    t.translate(s)
    t.translate(s)
    t.translate(s)
    t.translate(s)
    # Not enough undo to clear output.
    assert out.get() == 'S ' + BACK_STRING

    out.clear()
    t.remove_listener(out.write)
    t.translate(stroke('S'))
    assert out.get() == ''
Exemplo n.º 44
0
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 sorted(d.reverse_lookup('something')) == sorted([('STHEUPBG',), ('S-G',), ('SPH-G',), ('SPH', 'THEUPBG')])
        assert sorted(d.casereverse_lookup('something')) == sorted(['something', 'Something'])
        assert d.longest_key == 3
    else:
        assert sorted(d.reverse_lookup('something')) == sorted([('S-G',), ('SPH-G',), ('SPH', 'THEUPBG')])
        assert sorted(d.casereverse_lookup('something')) == sorted(['something', 'Something'])
        assert d.longest_key == 2
Exemplo n.º 45
0
def test_search():
    dc = StenoDictionaryCollection()

    # Similarity is based on string equality after removing case and stripping special characters from the ends.
    d1 = StenoDictionary()
    d1[('WAOUFL', )] = 'beautiful'
    d1[('PWAOUFL', )] = 'Beautiful'
    d1[('PWAOUT', '-FL')] = '{^BEAUTIFUL}  '
    d1[('ULG', )] = 'ugly'
    dc.set_dicts([d1])
    assert dc.find_similar('beautiful') == [('Beautiful', {('PWAOUFL', )}),
                                            ('beautiful', {('WAOUFL', )}),
                                            ('{^BEAUTIFUL}  ', {('PWAOUT',
                                                                 '-FL')})]

    assert dc.find_similar('{#BEAUtiful}{^}') == [
        ('Beautiful', {('PWAOUFL', )}), ('beautiful', {('WAOUFL', )}),
        ('{^BEAUTIFUL}  ', {('PWAOUT', '-FL')})
    ]

    # Translations found in multiple dicts should combine non-overlapping keys in the results.
    d2 = StenoDictionary()
    del d1[('PWAOUT', '-FL')]
    d2[('PW-FL', )] = 'beautiful'
    dc.set_dicts([d1, d2])
    assert dc.find_similar('beautiful') == [('Beautiful', {('PWAOUFL', )}),
                                            ('beautiful', {('WAOUFL', ),
                                                           ('PW-FL', )})]

    # If all possible keys for a translation are overridden, that translation should not be returned.
    d3 = StenoDictionary()
    d3[('PW-FL', )] = 'not beautiful'
    d3[('WAOUFL', )] = 'not beautiful'
    dc.set_dicts([d3, d1, d2])
    assert dc.find_similar('beautiful') == [('Beautiful', {('PWAOUFL', )})]

    # For partial word search, similar words will be returned first, but if the count is greater than that,
    # the next words in sorted order which are supersets are returned. Also stops at the end of the dictionary.
    dc.set_dicts([d1])
    d1[('PWAOU', )] = 'beau'
    d1[('PWAOUFL', 'HREU')] = 'beautifully'
    d1[('UG', 'HREU', '-PBS')] = 'ugliness'
    assert dc.find_partial('beau',
                           count=4) == [('beau', {('PWAOU', )}),
                                        ('Beautiful', {('PWAOUFL', )}),
                                        ('beautiful', {('WAOUFL', )}),
                                        ('beautifully', {('PWAOUFL', 'HREU')})]
    assert dc.find_partial('UGLY', count=2) == [('ugly', {('ULG', )})]

    # Even if a word isn't present, the search will return words going forward
    # from the index where it would be found if it was there.
    assert dc.find_partial('beaut',
                           count=3) == [('Beautiful', {('PWAOUFL', )}),
                                        ('beautiful', {('WAOUFL', )}),
                                        ('beautifully', {('PWAOUFL', 'HREU')})]

    # Regex search is straightforward; return up to count entries in order that match the given regular expression.
    # If no regex metacharacters are present, should just be a case-sensitive starts-with search.
    assert dc.find_regex('beau',
                         count=4) == [('beau', {('PWAOU', )}),
                                      ('beautiful', {('WAOUFL', )}),
                                      ('beautifully', {('PWAOUFL', 'HREU')})]
    assert dc.find_regex('beautiful.?.?',
                         count=2) == [('beautiful', {('WAOUFL', )}),
                                      ('beautifully', {('PWAOUFL', 'HREU')})]
    assert dc.find_regex(' beautiful', count=3) == []
    assert dc.find_regex('(b|u).{3}$', count=2) == [('beau', {('PWAOU', )}),
                                                    ('ugly', {('ULG', )})]
    assert dc.find_regex('.*ly',
                         count=5) == [('beautifully', {('PWAOUFL', 'HREU')}),
                                      ('ugly', {('ULG', )})]

    # Regex errors won't raise if the algorithm short circuits a pattern with no possible matches.
    assert dc.find_regex('an open group that doesn\'t raise(', count=5) == []
    with pytest.raises(re.error):
        print(dc.find_regex('beautiful...an open group(', count=1))
Exemplo n.º 46
0
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
Exemplo n.º 47
0
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', )]