at.normalize();
// System.out.println(at);
int[] fw = {2, 1, 2, 3};
//int[] fw = {2, 1, 2};
int[] ew = {1, 2, 1};
Phrase f = new Phrase(fw, 1);
Phrase e = new Phrase(ew, 1);
PhrasePair pp = new PhrasePair(f, e);
hmm = new HMM(tt, at);
hmm.buildHMMTables(pp);
hmm.baumWelch(pp, null);
TTable_monolithic tc = (TTable_monolithic)tt.clone(); tc.clear();
ATable ac = (ATable)at.clone(); ac.clear();
hmm.addPartialTranslationCountsToTTable(tc);
hmm.addPartialJumpCountsToATable(ac);
System.out.println("COUNTS:\n" + tc);
tc.normalize();
ac.normalize();
System.out.println("OLD:\n" + at);
System.out.println("NEW:\n" + ac);
System.out.println("\nnew:\n"+tc);
//if (true) return;
PerplexityReporter cr = new PerplexityReporter();
Alignment a = hmm.viterbiAlign(pp, cr);
double ce1 = cr.getCrossEntropy();
System.out.println(a.toStringVisual() + "\n"+cr);
assertTrue(a.aligned(0, 0));
System.out.println(hmm.backtrace);
hmm = new HMM(tc, ac);
hmm.buildHMMTables(pp);
cr.reset();
a = hmm.viterbiAlign(pp, cr);
double ce2 = cr.getCrossEntropy();
// perplexity should decrease!
assertTrue(ce1 > ce2);
assert(tc.get(1,1) > 0.0f);
System.out.println(a.toStringVisual() + "\n" + cr + "\nPG::\n" + hmm.computeAlignmentPosteriors(pp));
assertEquals(a.getELength(), e.size());
assertEquals(a.getFLength(), f.size());
}