Package edu.cmu.sphinx.util

Examples of edu.cmu.sphinx.util.LogMath


        for (Map.Entry<WordSequence, Integer> e : unigrams.entrySet()) {
            uniprobs.put(e.getKey(),
                         (float) e.getValue() * deflate / wordCount);
        }

        LogMath lmath = LogMath.getLogMath();
        float logUnigramWeight = lmath.linearToLog(unigramWeight);
        float invLogUnigramWeight = lmath.linearToLog(1 - unigramWeight);
        float logUniformProb = -lmath.linearToLog(uniprobs.size());

        Set<WordSequence> sorted1grams = new TreeSet<WordSequence>(unigrams.keySet());
        Iterator<WordSequence> iter =
                new TreeSet<WordSequence>(bigrams.keySet()).iterator();
        WordSequence ws = iter.hasNext() ? iter.next() : null;
        for (WordSequence unigram : sorted1grams) {
            float p = lmath.linearToLog(uniprobs.get(unigram));
            p += logUnigramWeight;
            p = lmath.addAsLinear(p, logUniformProb + invLogUnigramWeight);
            logProbs.put(unigram, p);

            float sum = 0.f;
            while (ws != null) {
                int cmp = ws.getOldest().compareTo(unigram);
                if (cmp > 0) {
                    break;
                }
                if (cmp == 0) {
                    sum += uniprobs.get(ws.getNewest());
                }
                ws = iter.hasNext() ? iter.next() : null;
            }

            logBackoffs.put(unigram, lmath.linearToLog(discount / (1 - sum)));
        }

        Map<WordSequence, Float> biprobs = new HashMap<WordSequence, Float>();
        for (Map.Entry<WordSequence, Integer> entry : bigrams.entrySet()) {
            int unigramCount = unigrams.get(entry.getKey().getOldest());
            biprobs.put(entry.getKey(),
                        entry.getValue() * deflate / unigramCount);
        }

        Set<WordSequence> sorted2grams = new TreeSet<WordSequence>(bigrams.keySet());
        iter = new TreeSet<WordSequence>(trigrams.keySet()).iterator();
        ws = iter.hasNext() ? iter.next() : null;
        for (WordSequence biword : sorted2grams) {
            logProbs.put(biword, lmath.linearToLog(biprobs.get(biword)));

            float sum = 0.f;
            while (ws != null) {
                int cmp = ws.getOldest().compareTo(biword);
                if (cmp > 0) {
                    break;
                }
                if (cmp == 0) {
                    sum += biprobs.get(ws.getNewest());
                }
                ws = iter.hasNext() ? iter.next() : null;
            }
            logBackoffs.put(biword, lmath.linearToLog(discount / (1 - sum)));
        }

        for (Map.Entry<WordSequence, Integer> e : trigrams.entrySet()) {
            float p = e.getValue() * deflate;
            p /= bigrams.get(e.getKey().getOldest());
            logProbs.put(e.getKey(), lmath.linearToLog(p));
        }
    }
View Full Code Here


public class PosteriorTest {

  @Test
  public void testPosterior() {

      LogMath logMath = LogMath.getLogMath();
     
    Lattice lattice = new Lattice();

    Node a = lattice.addNode("A", "A", 0, 0);
    Node b = lattice.addNode("B", "B", 0, 0);
    Node c = lattice.addNode("C", "C", 0, 0);
    Node d = lattice.addNode("D", "D", 0, 0);

    double acousticAB = 4;
    double acousticAC = 6;
    double acousticCB = 1;
    double acousticBD = 5;
    double acousticCD = 2;

    lattice.setInitialNode(a);
    lattice.setTerminalNode(d);

    lattice.addEdge(a, b, logMath.linearToLog(acousticAB), 0);
    lattice.addEdge(a, c, logMath.linearToLog(acousticAC), 0);
    lattice.addEdge(c, b, logMath.linearToLog(acousticCB), 0);
    lattice.addEdge(b, d, logMath.linearToLog(acousticBD), 0);
    lattice.addEdge(c, d, logMath.linearToLog(acousticCD), 0);

    lattice.computeNodePosteriors(1.0f);
    double pathABD = acousticAB * acousticBD;
    double pathACBD = acousticAC * acousticCB * acousticBD;
    double pathACD = acousticAC * acousticCD;
    double allPaths = pathABD + pathACBD + pathACD;

    double bPosterior = (pathABD + pathACBD) / allPaths;
    double cPosterior = (pathACBD + pathACD) / allPaths;

    double delta = 1e-4;
    Assert.assertEquals (logMath.logToLinear((float) a.getPosterior()), 1.0, delta);
    Assert.assertEquals (logMath.logToLinear((float) b.getPosterior()), bPosterior, delta);
    Assert.assertEquals (logMath.logToLinear((float) c.getPosterior()), cPosterior, delta);
    Assert.assertEquals (logMath.logToLinear((float) d.getPosterior()), 1.0, delta);
  }
View Full Code Here

TOP

Related Classes of edu.cmu.sphinx.util.LogMath

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.