word = General.assignWrodClass(word) else: tags_curr = list(e[word].keys()) V[i + 1] = {} bp[i + 1] = {} for t in tags_p: V[i + 1][t] = {} bp[i + 1][t] = {} for r in tags_curr: l = {} for tT in tags_p2: l[tT] = (V[i][tT][t]) * getScore(word, r, tT, t) V[i + 1][t][r] = max(list(l.values())) bp[i + 1][t][r] = General.argmax(l) tags_p2 = tags_p tags_p = tags_curr V.pop(0) bp.pop(0) endMatrix = map(lambda x: x.values(), V[n].values()) maxEnd = list(map(max, endMatrix)) maxV = max(maxEnd) maxTIndex = maxEnd.index(maxV) maxT = list(V[n].keys())[maxTIndex] maxR = General.argmax(V[n][maxT]) y = [0 for i in range(0, n + 1)] y[n] = maxR y[n - 1] = maxT
a = row.split("\t") b = a[0].split(" ") if b[0] not in e: e[b[0]] = {} e[b[0]][b[1]] = float(a[1]) total_count = 0 total_good = 0 taged_to_file = [] for row in input_f: tag_seq = ["SS","SS"] output = [] for i,word in enumerate(row): predict_tag = General.argmax(getScore(word[0], tag_seq[-1], tag_seq[-2])) tag_seq.append(predict_tag) output.append(word[0] + "/" + predict_tag) if toCompare: total_count += 1 if predict_tag == word[1]: total_good += 1 if toCompare: print("Rate is: " + str(total_good) + "/" + str(total_count) + " = " + str(float(total_good) / total_count)) taged_to_file.append(" ".join(output)) out_file = open(greedy_hmm_output, 'w') out_file.write('\n'.join(taged_to_file)) out_file.close()
bp[i + 1] = {} for t in tags_p: w2_t = t V[i + 1][t] = {} bp[i + 1][t] = {} l = {} for r in tags_curr: l[r] = {} for tT in tags_p2: w1_t = tT score = (V[i][tT][t]) + getScore(w1,w2,word,w4,w5,w1_t,w2_t) for r in tags_curr: l[r][tT] = score[features_m[r]] for r in tags_curr: V[i + 1][t][r] = max(list(l[r].values())) bp[i + 1][t][r] = General.argmax(l[r]) tags_p2 = tags_p tags_p = tags_curr V.pop(0) bp.pop(0) endMatrix = map(lambda x: x.values(), V[row_l-1].values()) maxEnd = list(map(max, endMatrix)) maxV = max(maxEnd) maxTIndex = maxEnd.index(maxV) maxT = list(V[row_l-1].keys())[maxTIndex] maxR = General.argmax(V[row_l-1][maxT]) y = [0 for i in range(0, row_l)] y[row_l-1] = maxR y[row_l-2] = maxT for i in reversed(range(0, row_l-2)): y[i] = bp[i + 2][y[i + 1]][y[i + 2]]