Ejemplo n.º 1
0
def test_names():
    auto = Automaton()
    auto.add_all(NAMES)
    auto.update_automaton()
    auto_matches = [(m.start, m.end) for m in auto.get_matches(TEXT)]

    with TemporaryDirectory() as tmpdir:
        #tmpdir = ''
        fnm = os.path.join(tmpdir, 'test.aca')
        auto.save_to_file(fnm)
        auto2 = Automaton()
        auto2.load_from_file(fnm)

    auto2_matches = [(m.start, m.end) for m in auto2.get_matches(TEXT)]
    assert list(auto.items()) == list(auto2.items())
    assert list(auto.prefixes()) == list(auto2.prefixes())
    assert auto_matches == auto2_matches

    auto3 = Automaton()
    auto3.load_from_string(auto2.save_to_string())
    auto3_matches = [(m.start, m.end) for m in auto3.get_matches(TEXT)]

    assert list(auto.items()) == list(auto2.items())
    assert list(auto.prefixes()) == list(auto2.prefixes())
    assert auto_matches == auto3_matches
Ejemplo n.º 2
0
def test_items():
    auto = Automaton()
    auto.add_all(names)
    ens, evs = zip(*sorted(names))
    ns, vs = zip(*list(auto.items()))
    ns = [''.join(n) for n in ns]
    assert list(ens) == list(ns)
    assert list(evs) == list(vs)
Ejemplo n.º 3
0
# access it like a Python dictionary
print(map['acid'])

# using an invalid key raises a KeyError
#print (map['invalid key'])

# you can use get to provide a default value when key is missing
print(map.get('invalid key', 'default value'))

# NB! Implementation specific special case: empty strings
# denote "missing" values, so you can't use these
map['special'] = ''
#print (map['special'])

# you can delete items
del map['electrify']

# trying to delete a non-existent item raises KeyError
#del map['invalid key']

# iterate items like a dict
print('items:')
for key, value in map.items():
    print('{}: {}'.format(key, value))

# you can also iterate prefixes
print('prefixes:')
for prefix, value in map.prefixes():
    print('{}: {}'.format(prefix, value))