Package edu.cmu.sphinx.util

Examples of edu.cmu.sphinx.util.ExtendedStreamTokenizer


     * @throws IOException
     *             if an error occurs while loading the data
     */
    protected void loadHMMPool(boolean useCDUnits, InputStream inputStream,
            String path) throws IOException {
        ExtendedStreamTokenizer est = new ExtendedStreamTokenizer(inputStream,
                '#', false);

        logger.fine("Loading HMM file from: " + path);

        est.expectString(MODEL_VERSION);

        int numBase = est.getInt("numBase");
        est.expectString("n_base");

        int numTri = est.getInt("numTri");
        est.expectString("n_tri");

        int numStateMap = est.getInt("numStateMap");
        est.expectString("n_state_map");

        int numTiedState = est.getInt("numTiedState");
        est.expectString("n_tied_state");

        int numContextIndependentTiedState = est
                .getInt("numContextIndependentTiedState");
        est.expectString("n_tied_ci_state");

        int numTiedTransitionMatrices = est.getInt("numTiedTransitionMatrices");
        est.expectString("n_tied_tmat");

        int numStatePerHMM = numStateMap / (numTri + numBase);

        assert numTiedState == mixtureWeightsPool.getFeature(NUM_SENONES, 0);
        assert numTiedTransitionMatrices == transitionsPool.size();

        // Load the base phones
        for (int i = 0; i < numBase; i++) {
            String name = est.getString();
            String left = est.getString();
            String right = est.getString();
            String position = est.getString();
            String attribute = est.getString();
            int tmat = est.getInt("tmat");

            int[] stid = new int[numStatePerHMM - 1];

            for (int j = 0; j < numStatePerHMM - 1; j++) {
                stid[j] = est.getInt("j");
                assert stid[j] >= 0 && stid[j] < numContextIndependentTiedState;
            }
            est.expectString("N");

            assert left.equals("-");
            assert right.equals("-");
            assert position.equals("-");
            assert tmat < numTiedTransitionMatrices;

            Unit unit = unitManager.getUnit(name, attribute.equals(FILLER));
            contextIndependentUnits.put(unit.getName(), unit);

            if (logger.isLoggable(Level.FINE)) {
                logger.fine("Loaded " + unit);
            }

            // The first filler
            if (unit.isFiller() && unit.getName().equals(SILENCE_CIPHONE)) {
                unit = UnitManager.SILENCE;
            }

            float[][] transitionMatrix = transitionsPool.get(tmat);
            SenoneSequence ss = getSenoneSequence(stid);

            HMM hmm = new SenoneHMM(unit, ss, transitionMatrix,
                    HMMPosition.lookup(position));
            hmmManager.put(hmm);
        }

        if (hmmManager.get(HMMPosition.UNDEFINED, UnitManager.SILENCE) == null) {
            throw new IOException("Could not find SIL unit in acoustic model");
        }

        // Load the context dependent phones. If the useCDUnits
        // property is false, the CD phones will not be created, but
        // the values still need to be read in from the file.

        String lastUnitName = "";
        Unit lastUnit = null;
        int[] lastStid = null;
        SenoneSequence lastSenoneSequence = null;

        for (int i = 0; i < numTri; i++) {
            String name = est.getString();
            String left = est.getString();
            String right = est.getString();
            String position = est.getString();
            String attribute = est.getString();
            int tmat = est.getInt("tmat");

            int[] stid = new int[numStatePerHMM - 1];

            for (int j = 0; j < numStatePerHMM - 1; j++) {
                stid[j] = est.getInt("j");
                assert stid[j] >= numContextIndependentTiedState
                        && stid[j] < numTiedState;
            }
            est.expectString("N");

            assert !left.equals("-");
            assert !right.equals("-");
            assert !position.equals("-");
            assert attribute.equals("n/a");
            assert tmat < numTiedTransitionMatrices;

            if (useCDUnits) {
                Unit unit;
                String unitName = (name + ' ' + left + ' ' + right);

                if (unitName.equals(lastUnitName)) {
                    unit = lastUnit;
                } else {
                    Unit[] leftContext = new Unit[1];
                    leftContext[0] = contextIndependentUnits.get(left);

                    Unit[] rightContext = new Unit[1];
                    rightContext[0] = contextIndependentUnits.get(right);

                    Context context = LeftRightContext.get(leftContext,
                            rightContext);
                    unit = unitManager.getUnit(name, false, context);
                }
                lastUnitName = unitName;
                lastUnit = unit;

                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Loaded " + unit);
                }

                float[][] transitionMatrix = transitionsPool.get(tmat);

                SenoneSequence ss = lastSenoneSequence;
                if (ss == null || !sameSenoneSequence(stid, lastStid)) {
                    ss = getSenoneSequence(stid);
                }
                lastSenoneSequence = ss;
                lastStid = stid;

                HMM hmm = new SenoneHMM(unit, ss, transitionMatrix,
                        HMMPosition.lookup(position));
                hmmManager.put(hmm);
            }
        }

        est.close();
    }
View Full Code Here


     * {@inheritDoc}
     */
    @Override
    protected void loadHMMPool(boolean useCDUnits, InputStream inputStream, String path)
            throws IOException {
        ExtendedStreamTokenizer est = new ExtendedStreamTokenizer(inputStream, '#', false);

        logger.info("Loading HMM file from: ");
        logger.info(path);

        est.expectString(MODEL_VERSION);

        int numBase = est.getInt("numBase");
        est.expectString("n_base");

        int numTri = est.getInt("numTri");
        est.expectString("n_tri");

        est.getInt("numStateMap");
        est.expectString("n_state_map");

        int numTiedState = est.getInt("numTiedState");
        est.expectString("n_tied_state");

        int numContextIndependentTiedState = est.getInt("numContextIndependentTiedState");
        est.expectString("n_tied_ci_state");

        int numTiedTransitionMatrices = est.getInt("numTiedTransitionMatrices");
        est.expectString("n_tied_tmat");

        int[] maxStid = new int[maxModelSize];

        HMMManager hmmManager = super.getHmmManager();
        Pool<float[][]> matrixPool = super.getMatrixPool();
        Pool<float[]> mixtureWeightsPool = super.getMixtureWeightsPool();
        Map<String, Unit> contextIndependentUnits = super.getContextIndependentUnits();

        assert numTiedState == mixtureWeightsPool.getFeature(NUM_SENONES, 0);
        assert numTiedTransitionMatrices == matrixPool.size();

        // Load the base phones
        for (int i = 0; i < numBase; i++) {
            String left = est.getString();
            String right = est.getString();
            String position = est.getString();
            int tmat = est.getInt("tmat");

            // Read all state ID in the line...
            for (int j = 0; ; j++) {
                String str = est.getString();

                // ... until we reach a "N"

                if (!str.equals("N")) {
                    int id = Integer.parseInt(str);
                    try {
                        maxStid[j] = id;
                    } catch (ArrayIndexOutOfBoundsException aie) {
                        throw new Error("Use a larger value for " +
                                "maxStatePerModel");
                    }
                    assert maxStid[j] >= 0 &&
                            maxStid[j] < numContextIndependentTiedState;
                } else {
                    break;
                }
            }

            assert left.equals("-");
            assert right.equals("-");
            assert position.equals("-");
            assert tmat < numTiedTransitionMatrices;

//          int[] stid = Arrays.copyOf(maxStid, numStates);
//          Unit unit = Unit.getUnit(name, attribute.equals(FILLER));
//          contextIndependentUnits.put(unit.getName(), unit);
//
//          if (logger.isLoggable(Level.FINE)) {
//              logger.fine("Loaded " + unit);
//          }
//           The first filler
//          if (unit.isFiller() && unit.getName().equals(SILENCE_CIPHONE)) {
//                unit = Unit.SILENCE;
//          }
//          float[][] transitionMatrix = matrixPool.get(tmat);
//          SenoneSequence ss = getSenoneSequence(stid);
//          HMM hmm = new SenoneHMM(unit, ss, transitionMatrix, HMMPosition.lookup(position));
//          hmmManager.put(hmm);
        }

        // Load the context dependent phones.  If the useCDUnits
        // property is false, the CD phones will not be created, but
        // the values still need to be read in from the file.

        String lastUnitName = "";
        Unit lastUnit = null;
        int[] lastStid = null;
        SenoneSequence lastSenoneSequence = null;

        for (int i = 0; i < numTri; i++) {
            String name = est.getString();
            String left = est.getString();
            String right = est.getString();
            String position = est.getString();
            String attribute = est.getString();
            int tmat = est.getInt("tmat");

            int numStates = 0;

            // Read all state ID in the line...
            for (int j = 0; ; j++) {
                String str = est.getString();

                // ... until we reach a "N"

                if (!str.equals("N")) {
                    int id = Integer.parseInt(str);
                    maxStid[j] = id;
                    assert maxStid[j] >= numContextIndependentTiedState &&
                            maxStid[j] < numTiedState;
                } else {
                    numStates = j;
                    break;
                }
            }
            int[] stid = Arrays.copyOf(maxStid, numStates);

            assert !left.equals("-");
            assert !right.equals("-");
            assert !position.equals("-");
            assert attribute.equals("n/a");
            assert tmat < numTiedTransitionMatrices;

            if (useCDUnits) {
                Unit unit = null;
                String unitName = (name + ' ' + left + ' ' + right);

                if (unitName.equals(lastUnitName)) {
                    unit = lastUnit;
                } else {
                    Unit[] leftContext = new Unit[1];
                    leftContext[0] = contextIndependentUnits.get(left);

                    Unit[] rightContext = new Unit[1];
                    rightContext[0] = contextIndependentUnits.get(right);

                    //Context context = LeftRightContext.get(leftContext, rightContext);
                    //unit = Unit.getUnit(name, false, context);
                }
                lastUnitName = unitName;
                lastUnit = unit;

                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Loaded " + unit);
                }

                float[][] transitionMatrix = matrixPool.get(tmat);

                SenoneSequence ss = lastSenoneSequence;
                if (ss == null || !sameSenoneSequence(stid, lastStid)) {
                    ss = getSenoneSequence(stid);
                }
                lastSenoneSequence = ss;
                lastStid = stid;

                HMM hmm = new SenoneHMM(unit, ss, transitionMatrix, HMMPosition.lookup(position));
                hmmManager.put(hmm);
            }
        }

        est.close();
    }
View Full Code Here

        LogMath logMath = LogMath.getLogMath();
        logger.info("Loading transition matrices from: ");
        logger.info(path);

        Pool<float[][]> pool = new Pool<float[][]>(path);
        ExtendedStreamTokenizer est = new ExtendedStreamTokenizer(inputStream, '#', false);

        est.expectString("tmat");
        int numMatrices = est.getInt("numMatrices");
        est.expectString("X");
        // numStates = est.getInt("numStates");

        for (int i = 0; i < numMatrices; i++) {
            est.expectString("tmat");
            est.expectString("[" + i + ']');
            est.expectString("nstate");
            // Number of emitting states + 1, final non-emitting state
            int numStates = est.getInt("numStates") + 1;
            float[][] tmat = new float[numStates][numStates];

            for (int j = 0; j < numStates; j++) {
                for (int k = 0; k < numStates; k++) {

                    // the last row is just zeros, so we just do
                    // the first (numStates - 1) rows

                    if (j < numStates - 1) {
                         if (k == j || k == j + 1) {
                             tmat[j][k] = est.getFloat("tmat value");
                         }
                    }

                    tmat[j][k] = logMath.linearToLog(tmat[j][k]);

                    if (logger.isLoggable(Level.FINE)) {
                        logger.fine("tmat j " + j + " k "
                                + k + " tm " + tmat[j][k]);
                    }
                }
            }
            pool.put(i, tmat);
        }
        est.close();
        return pool;
    }
View Full Code Here

        if (inputStream == null) {
            throw new FileNotFoundException("Error trying to read file " + path);
        }

        // 'false' argument refers to EOL is insignificant
        ExtendedStreamTokenizer est = new ExtendedStreamTokenizer(inputStream, '#', false);
        Pool<float[]> pool = new Pool<float[]>(path);
        est.expectString("param");
        int numStates = est.getInt("numStates");
        int numStreams = est.getInt("numStreams");
        int numGaussiansPerState = est.getInt("numGaussiansPerState");
        pool.setFeature(NUM_SENONES, numStates);
        pool.setFeature(NUM_STREAMS, numStreams);
        pool.setFeature(NUM_GAUSSIANS_PER_STATE, numGaussiansPerState);
        int vectorLength = 39;

        for (int i = 0; i < numStates; i++) {
            est.expectString("mgau");
            est.expectInt("mgau index", i);
            est.expectString("feat");
            est.expectInt("feat index", 0);
            for (int j = 0; j < numGaussiansPerState; j++) {
                est.expectString("density");
                est.expectInt("densityValue", j);
                float[] density = new float[vectorLength];
                for (int k = 0; k < vectorLength; k++) {
                    density[k] = est.getFloat("val");
                    if (density[k] < floor) {
                        density[k] = floor;
                    }
                }
                int id = i * numGaussiansPerState + j;
                pool.put(id, density);
            }
        }
        est.close();
        return pool;
    }
View Full Code Here

        if (inputStream == null) {
            throw new FileNotFoundException("Error trying to read file " + path);
        }

        Pool<float[]> pool = new Pool<float[]>(path);
        ExtendedStreamTokenizer est = new ExtendedStreamTokenizer(inputStream, '#', false);
        est.expectString("mixw");
        int numStates = est.getInt("numStates");
        int numStreams = est.getInt("numStreams");
        int numGaussiansPerState = est.getInt("numGaussiansPerState");
        pool.setFeature(NUM_SENONES, numStates);
        pool.setFeature(NUM_STREAMS, numStreams);
        pool.setFeature(NUM_GAUSSIANS_PER_STATE, numGaussiansPerState);
        for (int i = 0; i < numStates; i++) {
            est.expectString("mixw");
            est.expectString("[" + i);
            est.expectString("0]");
            // float total = est.getFloat("total");
            float[] logMixtureWeight = new float[numGaussiansPerState];
            for (int j = 0; j < numGaussiansPerState; j++) {
                float val = est.getFloat("mixwVal");
                if (val < floor) {
                    val = floor;
                }
                logMixtureWeight[j] = val;
            }
            LogMath.getLogMath().linearToLog(logMixtureWeight);
            pool.put(i, logMixtureWeight);
        }
        est.close();
        return pool;
    }
View Full Code Here

        if (inputStream == null) {
            throw new FileNotFoundException("Error trying to read file " + path);
        }

        Pool<float[][]> pool = new Pool<float[][]>(path);
        ExtendedStreamTokenizer est =
            new ExtendedStreamTokenizer(inputStream, '#', false);
        LogMath logMath = LogMath.getLogMath();
        est.expectString("tmat");
        int numMatrices = est.getInt("numMatrices");
        int numStates = est.getInt("numStates");
        logger.fine("with " + numMatrices + " and " + numStates + " states");

        // read in the matrices
        for (int i = 0; i < numMatrices; i++) {
            est.expectString("tmat");
            est.expectString("[" + i + ']');
            float[][] tmat = new float[numStates][numStates];
            for (int j = 0; j < numStates; j++) {
                for (int k = 0; k < numStates; k++) {
                    // the last row is just zeros, so we just do
                    // the first (numStates - 1) rows
                    if (j < numStates - 1) {
                        if (k == j || k == j + 1) {
                            tmat[j][k] = est.getFloat("tmat value");
                        }
                    }
                    tmat[j][k] = logMath.linearToLog(tmat[j][k]);
                    if (logger.isLoggable(Level.FINE)) {
                        logger.fine("tmat j " + j + " k " + k + " tm " + tmat[j][k]);
                    }
                }
            }
            pool.put(i, tmat);
        }
        est.close();
        return pool;
    }
View Full Code Here

        // TODO: this should be flexible, but we're hardwiring for now
        int numStreams = 1;
        // Since we're initializing, we start simple.
        int numGaussiansPerState = 1;

        ExtendedStreamTokenizer est = new ExtendedStreamTokenizer(inputStream, '#', false);

        // Initialize the pools we'll need.
        meansPool = new Pool<float[]>("means");
        variancePool = new Pool<float[]>("variances");
        mixtureWeightsPool = new Pool<float[]>("mixtureweights");
        matrixPool = new Pool<float[][]>("transitionmatrices");
        senonePool = new Pool<Senone>("senones");

        float distFloor = ps.getFloat(PROP_MC_FLOOR);
        float mixtureWeightFloor = ps.getFloat(PROP_MW_FLOOR);
        float transitionProbabilityFloor = 0;
        float varianceFloor = ps.getFloat(PROP_VARIANCE_FLOOR);

        logger.info("Loading phone list file from: ");
        logger.info(path);

        // At this point, we only accept version 0.1
        String version = "0.1";
        est.expectString("version");
        est.expectString(version);

        est.expectString("same_sized_models");
        boolean sameSizedModels = est.getString().equals("yes");

        if (sameSizedModels) {
            est.expectString("n_state");
            numState = est.getInt("numBase");
        }

        // for this phone list version, let's assume left-to-right
        // models, with optional state skip.
        est.expectString("tmat_skip");
        boolean tmatSkip = est.getString().equals("yes");

        // Load the phones with sizes

        // stateIndex contains the absolute state index, that is, a
        // unique index in the senone pool.
        for (int stateIndex = 0, unitCount = 0; ;) {
            String phone = est.getString();
            if (est.isEOF()) {
                break;
            }
            int size = numState;
            if (!sameSizedModels) {
                size = est.getInt("ModelSize");
            }
            phoneList.put(phone, size);
            logger.fine("Phone: " + phone + " size: " + size);
            int[] stid = new int[size];
            String position = "-";

            for (int j = 0; j < size; j++, stateIndex++) {
                stid[j] = stateIndex;
            }

            Unit unit = unitManager.getUnit(phone,  phone.equals(SILENCE_CIPHONE));

            contextIndependentUnits.put(unit.getName(), unit);

            if (logger.isLoggable(Level.FINE)) {
                logger.fine("Loaded " + unit + " with " + size + " states");
            }

            // Means
            addModelToDensityPool(meansPool, stid, numStreams, numGaussiansPerState);

            // Variances
            addModelToDensityPool(variancePool, stid, numStreams, numGaussiansPerState);

            // Mixture weights
            addModelToMixtureWeightPool(mixtureWeightsPool, stid, numStreams, numGaussiansPerState, mixtureWeightFloor);

            // Transition matrix
            addModelToTransitionMatrixPool(matrixPool, unitCount, stid.length, transitionProbabilityFloor, tmatSkip);

            // After creating all pools, we create the senone pool.
            addModelToSenonePool(senonePool, stid, distFloor, varianceFloor);

            // With the senone pool in place, we go through all units, and
            // create the HMMs.

            // Create tmat
            float[][] transitionMatrix = matrixPool.get(unitCount);
            SenoneSequence ss = getSenoneSequence(stid);

            HMM hmm = new SenoneHMM(unit, ss, transitionMatrix, HMMPosition.lookup(position));
            hmmManager.put(hmm);
            unitCount++;
        }

        // If we want to use this code to load sizes/create models for
        // CD units, we need to find another way of establishing the
        // number of CI models, instead of just reading until the end
        // of file.

        est.close();
    }
View Full Code Here

     * @throws java.io.IOException if there is an error reading the dictionary
     */
    protected Map<String, Word> loadDictionary(InputStream inputStream, boolean isFillerDict)
            throws IOException {
        Map<String, List<Pronunciation>> dictPronunciations = new HashMap<String, List<Pronunciation>>();
        ExtendedStreamTokenizer est = new ExtendedStreamTokenizer(inputStream,
                true);
        String word;
        while ((word = est.getString()) != null) {
            word = removeParensFromWord(word);
            word = word.toLowerCase();
            List<Unit> units = new ArrayList<Unit>(20);

            List<Pronunciation> pronunciations = dictPronunciations.get(word);
            if (pronunciations == null) {
                pronunciations = new LinkedList<Pronunciation>();
            }
           
            // Count units and add them
            String unitText;
            while ((unitText = est.getString()) != null) {
                units.add(getCIUnit(unitText, isFillerDict));
            }
            pronunciations.add(new Pronunciation(units));
            // if we are adding a SIL ending duplicate
            if (!isFillerDict && addSilEndingPronunciation) {
                units.add(UnitManager.SILENCE);
                pronunciations.add(new Pronunciation(units));
            }
           
            dictPronunciations.put(word, pronunciations);
        }
        inputStream.close();
        est.close();
        return createWords(dictPronunciations, isFillerDict);
    }
View Full Code Here


    /** Creates the grammar. */
    @Override
    protected GrammarNode createGrammar() throws IOException {
        ExtendedStreamTokenizer tok = new ExtendedStreamTokenizer(path, true);
        GrammarNode initialNode = createGrammarNode("<sil>");
        GrammarNode branchNode = createGrammarNode(false);
        GrammarNode finalNode = createGrammarNode("<sil>");
        finalNode.setFinalNode(true);
        List<GrammarNode> wordGrammarNodes = new LinkedList<GrammarNode>();
        while (!tok.isEOF()) {
            String word;
            while ((word = tok.getString()) != null) {
                word = word.toLowerCase();
                GrammarNode wordNode = createGrammarNode(word);
                wordGrammarNodes.add(wordNode);
            }
        }
View Full Code Here

        // replace each word node with a pair of nodes, which
        // consists of the word node and a new dummy end node, which is
        // for adding null or backoff transitions
        maxNodeId = expandWordNodes(maxNodeId);

        ExtendedStreamTokenizer tok = new ExtendedStreamTokenizer(path, true);

        // Second pass, add all of the arcs

        while (!tok.isEOF()) {
            String token;
            tok.skipwhite();
            token = tok.getString();

            // System.out.println(token);

            if (token == null) {
                break;

            } else if (token.equals("I")) {
                assert initialNode == null;
                int initialID = tok.getInt("initial ID");
                String nodeName = "G" + initialID;

                // TODO: FlatLinguist requires the initial grammar node
                // to contain a single silence. We'll do that for now,
                // but once the FlatLinguist is fixed, this should be
                // returned to its former method of creating an empty
                // initial grammar node
                //          initialNode = createGrammarNode(initialID, false);

                initialNode = createGrammarNode(initialID,
                        Dictionary.SILENCE_SPELLING);
                nodes.put(nodeName, initialNode);

                // optionally add a silence node
                if (addInitialSilenceNode) {
                    GrammarNode silenceNode =
                            createGrammarNode(++maxNodeId,
                                    Dictionary.SILENCE_SPELLING);
                    initialNode.add(silenceNode, LogMath.LOG_ONE);
                    silenceNode.add(initialNode, LogMath.LOG_ONE);
                }

            } else if (token.equals("T")) {
                int thisID = tok.getInt("this id");
                int nextID = tok.getInt("next id");

                GrammarNode thisNode = get(thisID);
                GrammarNode nextNode = get(nextID);

                // if the source node is an FSTGrammarNode, we want
                // to join the endNode to the destination node

                if (hasEndNode(thisNode)) {
                    thisNode = getEndNode(thisNode);
                }

                float lnProb = 0f;        // negative natural log
                String output = tok.getString();

                if (output == null || output.equals(",")) {

                    // these are epsilon (meaning backoff) transitions

                    if (output != null && output.equals(",")) {
                        tok.getString(); // skip the word
                        lnProb = tok.getFloat("probability");
                    }

                    // if the destination node has been expanded
                    // we actually want to add the backoff transition
                    // to the endNode

                    if (hasEndNode(nextNode)) {
                        nextNode = getEndNode(nextNode);
                    }

                } else {
                    String word = tok.getString();     // skip words
                    lnProb = tok.getFloat("probability");

                    if (ignoreUnknownTransitions && word.equals("<unknown>")) {
                        continue;
                    }
                    /*
                    * System.out.println(nextNode + ": " + output);
                    */
                    assert hasWord(nextNode);
                }

                thisNode.add(nextNode, convertProbability(lnProb));

            } else if (token.equals("F")) {
                int thisID = tok.getInt("this id");
                float lnProb = tok.getFloat("probability");

                GrammarNode thisNode = get(thisID);
                GrammarNode nextNode = finalNode;

                if (hasEndNode(thisNode)) {
                    thisNode = getEndNode(thisNode);
                }

                thisNode.add(nextNode, convertProbability(lnProb));
            }
        }
        tok.close();

        assert initialNode != null;

        return initialNode;
    }
View Full Code Here

TOP

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

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.