@Override
public Alignment viterbiAlign(PhrasePair sentence,
PerplexityReporter reporter) {
this.buildHMMTables(sentence);
Alignment res = new Alignment(sentence.getF().size(), sentence.getE().size());
int J = sentence.getF().size() + 1;
int numStates = getNumStates();
viterbi.resize(J, getNumStates());
backtrace.resize(J, getNumStates());
viterbi.fill(Float.NEGATIVE_INFINITY);
viterbi.set(0, 0, 0.0f);
int lene = sentence.getE().getWords().length;
Alignment m1a = null;
if (m1_post != null)
m1a = m1_post.alignPosteriorThreshold(THRESH);
//System.out.println(emission);
for (int j = 1; j < J; j++) {
//System.out.println("J="+j);
boolean valid = false;
for (int s = 1; s < numStates; s++) {
float best = Float.NEGATIVE_INFINITY;
int best_s = -1;
double emitLogProb = Math.log(emission.get(j, s));
if (emitLogProb == Float.NEGATIVE_INFINITY) {
//System.out.println("BAD STATE: " + j + " " + s);
continue;
}
//System.out.println("j="+j + " s="+s+ " ep"+emitLogProb);
for (int s_prev = 0; s_prev < numStates; s_prev++) {
float m1boost = 1.0f;
float m1penalty = 0.0f;
boolean use_m1 = false;
if (m1a != null && m1a.isFAligned(j-1)) {
float m1post = 0.0f;
use_m1 = true;
for (int i=0; i<lene; i++) {
if (m1a.aligned(j-1, i))
m1post = m1_post.getAlignmentPointPosterior(j-1, i+1);
}
m1boost = (float)Math.sqrt(m1post);
m1penalty = 1.0f - m1boost;
}
float trans = getTransitionProb(s_prev, s);
if (use_m1) {
if (s <= l && s > 0 && m1a.aligned(j-1, s-1))
trans = m1boost;
else
trans *= m1penalty;
}
float cur = (float)(viterbi.get(j - 1, s_prev) +