Ejemplo n.º 1
0
def run(sentence, tagset, hmm_prob):
    max_iterations = 200
    #step_size = 100

    n = len(sentence)

    u = {}#defaultdict() # dual decomposition parameter
    init_dd_param(u, n, tagset)
 
    k = 1 # number of iterations
    while k <= max_iterations:
       step_size = 1.0 / math.sqrt(k)
       #print "\niteration:", k
       #print "-------------------------------"
       #print "step size = ", "{0:.2f}".format(step_size)
       tags1, aug_hmm_score, hmm_score = viterbi.run(sentence, tagset, hmm_prob, u)
       #print "vit output:", ' '.join(tags1)
       if k == 1:
          best_tags = tags1
       tags2, fst_score = fst_search.run(best_tags, u, tagset)

       if agree(tags1, tags2): 
           #sys.stderr.write("hmm only = "+ str( hmm_score) + "\n")
           #sys.stderr.write("fst only = "+ str(fst_score) + "\n")
           #sys.stderr.write("big hmm  = "+ str(aug_hmm_score) + "\n")
           #sys.stderr.write("hmm fst  = "+ str(aug_hmm_score + fst_score) + "\n")
           return best_tags, k, tags1, tags2  # converges in the kth iteration
       y = compute_indicators(tags1, tagset)
       z = compute_indicators(tags2, tagset)
       update(y, z, u, step_size)

       k += 1
    return best_tags, -1, tags1, tags2 # does not converge
Ejemplo n.º 2
0
def run(sentence, tagset, hmm, k_best_list):
    max_iter = len(k_best_list)*200
    
    n = len(sentence)
    k = len(k_best_list)

    u = {} # dual decomposition parameter
    init_dd_param(u, n, tagset) 

    iteration = 1
    while iteration <= max_iter:
        #print iteration
        step_size = 1.0 / math.sqrt(iteration)
        #print "step size", step_size 
        
        seq1, score1, score2 = viterbi.run(sentence, tagset, hmm, u)
        y = compute_indicators(seq1, tagset)
        #print 0, ' '.join(seq1)

        seq2, fst_score = fst_search.run(k_best_list, u, tagset)
        z = compute_indicators(seq2, tagset)
        #print j+1, ' '.join(seq)
       
        # check for agreement
        if seq1 != seq2:
            update(y, z, u, step_size)
        else:
            return seq1, iteration

        iteration += 1
    return seq1, -1 
Ejemplo n.º 3
0
def run(sentence, tagset, hmm, k_best_list):
    max_iter = len(k_best_list) * 200

    n = len(sentence)
    k = len(k_best_list)

    u = []  # dd parameter list
    for j in range(k + 1):
        u_j = {}
        init_dd_param(u_j, n, tagset)
        u.append(u_j)
    w = {}
    init_dd_param(w, n, tagset)
    ku = {}
    init_dd_param(ku, n, tagset)

    iteration = 1
    while iteration <= max_iter:
        # print iteration
        step_size = 21.0 / math.sqrt(iteration)
        # print "step size", step_size

        seqs = []
        indicators = []
        for i in u[0].iterkeys():
            for t in u[0][i].iterkeys():
                ku[i][t] = -1 * u[0][i][t]
        seq1, score1, score2 = viterbi.run(sentence, tagset, hmm, ku)
        seqs.append(seq1)
        indicators.append(compute_indicators(seq1, tagset))
        # print 0, ' '.join(seq1)

        for j in range(k):
            seq, fst_score = fst_search.run(k_best_list[j], u[j + 1], tagset)
            # print j+1, ' '.join(seq)
            seqs.append(seq)
            indicators.append(compute_indicators(seq, tagset))

        # check for agreement
        agree = True
        for seq in seqs[1:]:
            if seq != seq1:
                agree = False
                break

        if agree == False:
            update(indicators, u, w, step_size)
        else:
            return seq1, iteration

        iteration += 1
    return seq1, -1