<commit_before>/*
 * Copyright 1999-2002 Carnegie Mellon University.  
 * Portions Copyright 2002 Sun Microsystems, Inc.  
 * Portions Copyright 2002 Mitsubishi Electronic Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 *
 */

package edu.cmu.sphinx.decoder.linguist.util;


import edu.cmu.sphinx.knowledge.acoustic.HMM;
import edu.cmu.sphinx.knowledge.acoustic.HMMState;
import edu.cmu.sphinx.knowledge.acoustic.HMMStateArc;
import edu.cmu.sphinx.knowledge.acoustic.HMMPosition;
import edu.cmu.sphinx.knowledge.acoustic.Unit;
import edu.cmu.sphinx.knowledge.acoustic.AcousticModel;
import edu.cmu.sphinx.knowledge.acoustic.Context;
import edu.cmu.sphinx.knowledge.acoustic.LeftRightContext;
import edu.cmu.sphinx.util.Timer;

import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;


/**
 * The HMMPool provides the ability to manage units via small integer
 * IDs.  Context Independent units and context dependent units can
 * be converted to an ID. IDs can be used to quickly retrieve a unit
 * or an hmm associated with the unit.  This class operates under the
 * constraint that context sizes are exactly one, which is generally
 * only valid for large vocabulary tasks.
 */
public class HMMPool {
    private AcousticModel model;
    private Unit[] unitTable;
    private HMM hmmTable[][];
    private int numCIUnits;

    public HMMPool(AcousticModel model) {
        int maxCIUnits = 0;
        this.model = model;
        Timer.start("buildHmmPool");

        if (model.getLeftContextSize() != 1) {
            throw new Error("LexTreeLinguist: Unsupported left context size");
        }

        if (model.getRightContextSize() != 1) {
            throw new Error("LexTreeLinguist: Unsupported right context size");
        }

        // count CI units:

        for (Iterator i = model.getContextIndependentUnitIterator();
                i.hasNext();) {
            Unit unit = (Unit) i.next();
            if (unit.getBaseID() > maxCIUnits) {
                maxCIUnits = unit.getBaseID();
            }
        }

        numCIUnits = maxCIUnits + 1;

        unitTable = new Unit[numCIUnits * numCIUnits * numCIUnits];

        for (Iterator i = model.getHMMIterator(); i.hasNext(); ) {
            HMM hmm = (HMM) i.next();
            Unit unit = hmm.getUnit();
            int id = getID(unit);
            unitTable[id] = unit;
            // System.out.println("Unit " + unit + " id " + id);
        }


        // build up the hmm table to allow quick access to the hmms
        hmmTable = new
            HMM[HMMPosition.MAX_POSITIONS][unitTable.length];

        for (Iterator i = HMMPosition.iterator(); i.hasNext(); ) {
            HMMPosition position = (HMMPosition) i.next();
            int index = position.getIndex();
            for (int j = 1 ; j < unitTable.length; j++) {
                Unit unit = unitTable[j];
                if (unit != null) {
                    hmmTable[index][j] = 
                        model.lookupNearestHMM(unit, position);
                }
            }
        }
        Timer.stop("buildHmmPool");
    }

    /**
     * Returns the number of CI units
     * 
     * @return the number of CI Units
     */
    public int getNumCIUnits() {
        return numCIUnits;<commit_msg>HMMPool now substitutes CI hmms when a CD hmm is missing.<commit_after>/*
 * Copyright 1999-2002 <NAME> University.  
 * Portions Copyright 2002 Sun Microsystems, Inc.  
 * Portions Copyright 2002 Mitsubishi Electronic Research Laboratories.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * See the file "license.terms" for information on usage and
 * redistribution of this file, and for a DISCLAIMER OF ALL 
 * WARRANTIES.
 *
 */

package edu.cmu.sphinx.decoder.linguist.util;


import edu.cmu.sphinx.knowledge.acoustic.HMM;
import edu.cmu.sphinx.knowledge.acoustic.HMMState;
import edu.cmu.sphinx.knowledge.acoustic.HMMStateArc;
import edu.cmu.sphinx.knowledge.acoustic.HMMPosition;
import edu.cmu.sphinx.knowledge.acoustic.Unit;
import edu.cmu.sphinx.knowledge.acoustic.AcousticModel;
import edu.cmu.sphinx.knowledge.acoustic.Context;
import edu.cmu.sphinx.knowledge.acoustic.LeftRightContext;
import edu.cmu.sphinx.util.Timer;

import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;


/**
 * The HMMPool provides the ability to manage units via small integer
 * IDs.  Context Independent units and context dependent units can
 * be converted to an ID. IDs can be used to quickly retrieve a unit
 * or an hmm associated with the unit.  This class operates under the
 * constraint that context sizes are exactly one, which is generally
 * only valid for large vocabulary tasks.
 */
public class HMMPool {
    private AcousticModel model;
    private Unit[] unitTable;
    private HMM hmmTable[][];
    private int numCIUnits;

    public HMMPool(AcousticModel model) {
        int maxCIUnits = 0;
        this.model = model;
        Timer.start("buildHmmPool");

        if (model.getLeftContextSize() != 1) {
            throw new Error("LexTreeLinguist: Unsupported left context size");
        }

        if (model.getRightContextSize() != 1) {
            throw new Error("LexTreeLinguist: Unsupported right context size");
        }

        // count CI units:

        for (Iterator i = model.getContextIndependentUnitIterator();
                i.hasNext();) {
            Unit unit = (Unit) i.next();
            if (unit.getBaseID() > maxCIUnits) {
                maxCIUnits = unit.getBaseID();
            }
        }

        numCIUnits = maxCIUnits + 1;

        unitTable = new Unit[numCIUnits * numCIUnits * numCIUnits];

        for (Iterator i = model.getHMMIterator(); i.hasNext(); ) {
            HMM hmm = (HMM) i.next();
            Unit unit = hmm.getUnit();
            int id = getID(unit);
            unitTable[id] = unit;
            // System.out.println("Unit " + unit + " id " + id);
        }


        // build up the hmm table to allow quick access to the hmms
        hmmTable = new
            HMM[HMMPosition.MAX_POSITIONS][unitTable.length];

        for (Iterator i = HMMPosition.iterator(); i.hasNext(); ) {
            HMMPosition position = (HMMPosition) i.next();
            int index = position.getIndex();
            for (int j = 1 ; j < unitTable.length; j++) {
                Unit unit = unitTable[j];
                if (unit == null) {
                    unit = unitTable[getCentralUnitID(j)];
                }
                if (unit != null) {
                    hmmTable[index][j] = 
                        model.lookupNearestHMM(unit, position);
                    assert hmmTable[index][j] != null;
                }
            }
        }
        Timer.stop("buildHmmPool");
    }

    /**
     * Returns the number of CI units
     * 
     * @return the number of CI Units
     */
    public int getNumCIUnits() {
        return numCIUnits;
{'fraction_non_alphanumeric': 0.10049400141143261, 'fraction_numerical': 0.0059280169371912494, 'mean_word_length': 2.2775208140610546, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1209552', 'n_tokens_mistral': 2230, 'n_tokens_neox': 2049, 'n_words': 590}