Esempio n. 1
0
def test_subgradient():
    for h in hypergraphs():
        w = utils.random_log_viterbi_potentials(h)
        constraints, edge = random_have_constraint(h)
        path = ph.best_path(h, w)
        match = constraints.check(path)
        if edge not in path:
            nt.assert_equal(match[0], "have")

        cpath = opt.best_constrained_path(h, w,
                                          constraints)
        assert edge in cpath
Esempio n. 2
0
def test_best_path():
    """
    Test viterbi path finding.
    """
    for h in hypergraphs():
        w = utils.random_log_viterbi_potentials(h)
        path = ph.best_path(h, w)
        nt.assert_not_equal(w.dot(path), 0.0)
        valid_path(h, path)
        same = False
        for other_path in utils.all_paths(h):
            assert w.dot(path) >= w.dot(other_path)
            if path == other_path: same = True
        assert same
Esempio n. 3
0
def test_lp():
    import pydecode.lp as lp
    for h in hypergraphs():
        w = utils.random_log_viterbi_potentials(h)

        g = lp.HypergraphLP.make_lp(h, w)
        g.solve()
        path = g.path
        opath = ph.best_path(h, w)

        nt.assert_almost_equal(w.dot(path), w.dot(opath))
        for edge in path.edges:
            assert edge in opath


        # Constraint.
        constraints, edge = random_have_constraint(h)
        g = lp.HypergraphLP.make_lp(h, w)
        g.add_constraints(constraints)
        g.solve()
        assert edge in g.path