Package edu.umd.hooka

Examples of edu.umd.hooka.Alignment$AIterator


    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());
  }
View Full Code Here


    Phrase e = new Phrase(ew, 1);
    PhrasePair pp = new PhrasePair(f, e);
    hmm = new HMM(tt, at);
    hmm.buildHMMTables(pp);
    PerplexityReporter cr = new PerplexityReporter();
    Alignment a = hmm.viterbiAlign(pp, cr);
    assertEquals(a.getELength(), e.size());
    assertEquals(a.getFLength(), f.size());
    System.out.println(a.toStringVisual() + "\n"+cr);
  }
View Full Code Here

    PhrasePair pp = new PhrasePair(f, e);
    tt.normalize();
    hmm = new HMM(tt, at);
    hmm.buildHMMTables(pp);
    PerplexityReporter cr = new PerplexityReporter();
    Alignment a = hmm.viterbiAlign(pp, cr);
    assertEquals(a.getELength(), e.size());
    assertEquals(a.getFLength(), f.size());
    System.err.println(hmm.emission);
    System.err.println(hmm.transition);
    System.out.println(tt);
    System.err.println(hmm.viterbi);
    System.err.println(hmm.backtrace);
View Full Code Here

    System.out.println("COUNTS:\n" + tc);
    tc.normalize();
    ac.normalize();
   
    PerplexityReporter cr = new PerplexityReporter();
    Alignment a = hmm.viterbiAlign(pp, cr);
    assertEquals(a.getELength(), e.size());
    assertEquals(a.getFLength(), f.size());
    AlignmentPosteriorGrid pg = hmm.computeAlignmentPosteriors(pp);
    System.out.println(a.toStringVisual() + "\nPG:\n"+pg + "\n"+cr + "\n");
    System.out.println(hmm.emission);
    System.out.println(hmm.transition);
    System.out.println("Done NULL");
  }
View Full Code Here

        Path pathTTable = new Path(ttable);
        TTable tt = loadTTable(pathTTable);
        m1 = new Model1(tt, true);
      }
      cr.reset();
      Alignment a = m1.viterbiAlign(value, cr);
      out.set(a.toString());
      output.collect(key, out);
      reporter.incrCounter(CrossEntropyCounters.LOGPROB, (long)(cr.getTotalLogProb()));
      reporter.incrCounter(CrossEntropyCounters.WORDCOUNT, cr.getTotalWordCount());
    }
View Full Code Here

  @Override
  public Alignment viterbiAlign(PhrasePair sentence, PerplexityReporter viterbiPerp) {
    int[] es = sentence.getE().getWords();
    int[] fs = sentence.getF().getWords();
    float threshold = 0.27f;
    Alignment res = new Alignment(fs.length, es.length);
    AlignmentPosteriorGrid g = computeAlignmentPosteriors(sentence);
    for (int j=0; j<fs.length; j++) {
      for (int i=1; i<es.length; i++) {
        float post = g.getAlignmentPointPosterior(j, i);
        if (post > threshold) { res.align(j, i-1); }
      }
    }
    return res;
  }
View Full Code Here

  }

  public Alignment realViterbiAlign(PhrasePair sentence, PerplexityReporter viterbiPerp) {
    int[] es = sentence.getE().getWords();
    int[] fs = sentence.getF().getWords();
    Alignment res = new Alignment(fs.length, es.length);
    float viterbiScore = 0.0f;
    for (int j=0; j<fs.length; j++) {
      float bestProb = -1.0f;
      int besti = -1;
      int starti = 0; // TODO - should use NULL?
      if (_includeEnglishNullWord) starti = -1;
      for (int i=starti; i<es.length; i++) {
        float curProb = 0.0f;
        if (i == -1)
          curProb = tmodel.get(0, fs[j]);
        else
          curProb = tmodel.get(es[i], fs[j]);
        if (curProb > bestProb) { bestProb = curProb; besti = i; }
      }
      if (besti < 0) {
        ; //throw new RuntimeException("Implement or ignore!");
      } else {
        res.align(j, besti);
      }
      viterbiScore += Math.log(bestProb);
    }
    viterbiPerp.addFactor(viterbiScore - (fs.length * Math.log(es.length + 1.0)), fs.length);
    return res;
View Full Code Here

  public static float computeAER(Iterator<ReferenceAlignment> refi, Iterator<Alignment> testi) {
    float num = 0.0f;
    float den = 0.0f;
    while (refi.hasNext()) {
      ReferenceAlignment ref = refi.next();
      Alignment test = testi.next();
      num += (ref.countProbableHits(test) + ref.countSureHits(test));
      den += (test.countAlignmentPoints() + ref.countAlignmentPoints());
    }
    if (testi.hasNext())
      throw new RuntimeException("Mismatch in lengths");
    return num / den;
  }
View Full Code Here

  @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) +
View Full Code Here

  static class GrowDiagFinalAndRefiner extends Refiner {
    static Koehn koehn = new Koehn();
    static KoehnFinal koehnFinal = new KoehnFinal();
    public Alignment refine(Alignment a2, Alignment a1) {
      Alignment au = Alignment.union(a1, a2);
      Alignment a = Alignment.intersect(a1, a2);
     
      grow(a, koehn, false, au);
      //System.out.println(a1.toString());
      grow(a, koehnFinal, true, a1);
      grow(a, koehnFinal, true, a2);
View Full Code Here

TOP

Related Classes of edu.umd.hooka.Alignment$AIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.