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', )])
Beispiel #2
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
    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',)])
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
Beispiel #5
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'}
Beispiel #6
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', )]
Beispiel #7
0
        # 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:
        # 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:
        d.update({