] return G def vis(s1, s2, markovLik1, markovLik2, cyklik1, cyklik2): figsize = (30, 8) fig = plt.figure(figsize=figsize) ax = fig.add_subplot(2, 1, 1) ax.plot(s1, "--o") ax.set_title("Markov = %.4f \n CYK = %.4f" % (markovLik1, cyklik1)) plt.grid() bx = fig.add_subplot(2, 1, 2) bx.plot(s2, "--o") bx.set_title("Markov = %.4f \n CYK = %.4f" % (markovLik2, cyklik2)) plt.grid() plt.show() if __name__ == "__main__": _, s1 = sample() _, s2 = sample2() g = grammar() m = markov() markovLik1, _ = m.decode(s1) markovLik2, _ = m.decode(s2) cyklik1 = cyk(s1, g) cyklik2 = cyk(s2, g) vis(s1, s2, markovLik1, markovLik2, cyklik1, cyklik2) # markovLearning(s1) # markovLearning(s2)
def build(bp, root): a, b, c = root if c: (i,j, g, nta) = a (i, s1, bb, ntb) = b (s2, j, cc, ntc) = c print "%s[%s:%s] -> %s[%s:%s] %s[%s:%s]"%(nta, i, j, ntb, i, s1, ntc, s2, j) build(bp, bp[i, s1, bb]) build(bp, bp[s2, j, cc]) else: (i,j, g, nta) = a (i, s1, bb, ntb) = b print "%s[%s] -> %s[%s]"%(nta, i, ntb, i) if __name__ == '__main__': _, S = sample() A = Dist(np.array([0.0]), np.array([0.1])) B = Dist(np.array([5.0]), np.array([0.1])) C = Dist(np.array([-5.0]), np.array([0.1])) G = [ ('NT_1', A, None, 1.0), ('NT_2', B, None, 1.0), ("NT_3", C, None, 1.0), ("NT_4", "NT_1", "NT_1", 0.2), ("NT_4", "NT_4", "NT_1", 0.8), ("NT_5", "NT_2", "NT_2", 0.2), ("NT_5", "NT_5", "NT_2", 0.8), ("NT_6", "NT_3", "NT_3", 0.2), ("NT_6", "NT_6", "NT_3", 0.8), ("NT_7", "NT_5", "NT_4", 1.0), ("NT_S", "NT_7", "NT_6", 1.0) ] print cyk(S, G)