📄 sphinx3loader.java
字号:
String length = (String) properties.get(PROP_VECTOR_LENGTH); if (length != null) { return Integer.parseInt(length); } else { return PROP_VECTOR_LENGTH_DEFAULT; } } /** * Returns the default model definition file. * * @return the default model definition file */ private String getModelDefault() { loadProperties(); String mdef = (String) properties.get(PROP_MODEL); if (mdef != null) { return mdef; } else { return PROP_MODEL_DEFAULT; } } /** * Returns the default data location. * * @return the default data location */ private String getDataLocationDefault() { loadProperties(); String location = (String) properties.get(PROP_DATA_LOCATION); if (location != null) { return location; } else { return PROP_DATA_LOCATION_DEFAULT; } } /* * (non-Javadoc) * * @see edu.cmu.sphinx.util.props.Configurable#getName() */ public String getName() { return name; } public void load() throws IOException { // TODO: what is this all about? hmmManager = new HMMManager(); contextIndependentUnits = new LinkedHashMap(); // dummy pools for these elements meanTransformationMatrixPool = createDummyMatrixPool("meanTransformationMatrix"); meanTransformationVectorPool = createDummyVectorPool("meanTransformationMatrix"); varianceTransformationMatrixPool = createDummyMatrixPool("varianceTransformationMatrix"); varianceTransformationVectorPool = createDummyVectorPool("varianceTransformationMatrix"); // do the actual acoustic model loading loadModelFiles(model); } /** * Return the HmmManager. * * @return the hmmManager */ protected HMMManager getHmmManager() { return hmmManager; } /** * Return the MatrixPool. * * @return the matrixPool */ protected Pool getMatrixPool() { return matrixPool; } /** * Return the MixtureWeightsPool. * * @return the mixtureWeightsPool */ protected Pool getMixtureWeightsPool() { return mixtureWeightsPool; } /** * Return the acoustic model properties. * * @return the acoustic model properties */ protected Properties getProperties() { if (properties == null) { loadProperties(); } return properties; } /** * Return the location. * * @return the location */ protected String getLocation() { return location; } /** * Loads the AcousticModel from a directory in the file system. * * @param modelName * the name of the acoustic model; if null we just load from * the default location */ private void loadModelFiles(String modelName) throws FileNotFoundException, IOException, ZipException { logger.config("Loading Sphinx3 acoustic model: " + modelName); logger.config(" Path : " + location); logger.config(" modellName: " + model); logger.config(" dataDir : " + dataDir); if (binary) { meansPool = loadDensityFileBinary (dataDir + "means", -Float.MAX_VALUE); variancePool = loadDensityFileBinary (dataDir + "variances", varianceFloor); mixtureWeightsPool = loadMixtureWeightsBinary (dataDir + "mixture_weights", mixtureWeightFloor); matrixPool = loadTransitionMatricesBinary (dataDir + "transition_matrices"); } else { meansPool = loadDensityFileAscii (dataDir + "means.ascii", -Float.MAX_VALUE); variancePool = loadDensityFileAscii (dataDir + "variances.ascii", varianceFloor); mixtureWeightsPool = loadMixtureWeightsAscii (dataDir + "mixture_weights.ascii", mixtureWeightFloor); matrixPool = loadTransitionMatricesAscii (dataDir + "transition_matrices.ascii"); } senonePool = createSenonePool(distFloor, varianceFloor); // load the HMM model file InputStream modelStream = getClass().getResourceAsStream(model); if (modelStream == null) { throw new IOException("can't find model " + model); } loadHMMPool(useCDUnits, modelStream, location + File.separator + model); } /** * Returns the map of context indepent units. The map can be accessed by * unit name. * * @return the map of context independent units. */ public Map getContextIndependentUnits() { return contextIndependentUnits; } /** * Creates the senone pool from the rest of the pools. * * @param distFloor * the lowest allowed score * @param varianceFloor * the lowest allowed variance * * @return the senone pool */ private Pool createSenonePool(float distFloor, float varianceFloor) { Pool pool = new Pool("senones"); int numMixtureWeights = mixtureWeightsPool.size(); int numMeans = meansPool.size(); int numVariances = variancePool.size(); int numGaussiansPerSenone = mixtureWeightsPool.getFeature(NUM_GAUSSIANS_PER_STATE, 0); int numSenones = mixtureWeightsPool.getFeature(NUM_SENONES, 0); int whichGaussian = 0; logger.fine("NG " + numGaussiansPerSenone); logger.fine("NS " + numSenones); logger.fine("NMIX " + numMixtureWeights); logger.fine("NMNS " + numMeans); logger.fine("NMNS " + numVariances); assert numGaussiansPerSenone > 0; assert numMixtureWeights == numSenones; assert numVariances == numSenones * numGaussiansPerSenone; assert numMeans == numSenones * numGaussiansPerSenone; for (int i = 0; i < numSenones; i++) { MixtureComponent[] mixtureComponents = new MixtureComponent[numGaussiansPerSenone]; for (int j = 0; j < numGaussiansPerSenone; j++) { mixtureComponents[j] = new MixtureComponent( logMath, (float[]) meansPool.get(whichGaussian), (float[][]) meanTransformationMatrixPool.get(0), (float[]) meanTransformationVectorPool.get(0), (float[]) variancePool.get(whichGaussian), (float[][]) varianceTransformationMatrixPool.get(0), (float[]) varianceTransformationVectorPool.get(0), distFloor, varianceFloor); whichGaussian++; } Senone senone = new GaussianMixture( logMath, (float[]) mixtureWeightsPool.get(i), mixtureComponents, i); pool.put(i, senone); } return pool; } /** * Loads the Sphinx 3 acoustic model properties file, which is * basically a normal system properties file. * * @param url * the path to the acoustic properties file * * @return a SphinxProperty object containing the acoustic properties, * or null if there are no acoustic model properties * * @throws FileNotFoundException * if the file cannot be found * @throws IOException * if an error occurs while loading the data */ private SphinxProperties loadAcousticPropertiesFile(URL url) throws FileNotFoundException, IOException { //TODO what to do for prefix here // Ultimately we will be getting rid of this embedded // sphinx properties sheet. In the mean time String context = "acoustic." + getName() + "." + url; SphinxProperties.initContext(context, url); return (SphinxProperties.getSphinxProperties(context)); } /** * Loads the sphinx3 densityfile, a set of density arrays are created and * placed in the given pool. * * @param path * the name of the data * @param floor * the minimum density allowed * * @return a pool of loaded densities * * @throws FileNotFoundException * if a file cannot be found * @throws IOException * if an error occurs while loading the data */ private Pool loadDensityFileAscii(String path, float floor) throws FileNotFoundException, IOException { int token_type; int numStates; int numStreams; int numGaussiansPerState; InputStream inputStream = getClass().getResourceAsStream(path); if (inputStream == null) { throw new FileNotFoundException("Error trying to read file " + location + path); } // 'false' argument refers to EOL is insignificant ExtendedStreamTokenizer est = new ExtendedStreamTokenizer(inputStream, '#', false); Pool pool = new Pool(path); logger.fine("Loading density file from: " + path); est.expectString("param"); numStates = est.getInt("numStates"); numStreams = est.getInt("numStreams"); 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("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; } // System.out.println(" " + i + " " + j + " " + k + // " " + density[k]); } int id = i * numGaussiansPerState + j; pool.put(id, density); } } est.close(); return pool; } /** * Loads the sphinx3 densityfile, a set of density arrays are created and * placed in the given pool. * * @param path * the name of the data * @param floor * the minimum density allowed * * @return a pool of loaded densities * * @throws FileNotFoundException * if a file cannot be found * @throws IOException * if an error occurs while loading the data */ private Pool loadDensityFileBinary(String path, float floor) throws FileNotFoundException, IOException { int token_type; int numStates; int numStreams; int numGaussiansPerState; Properties props = new Properties(); int blockSize = 0; DataInputStream dis = readS3BinaryHeader(location, path, props); String version = props.getProperty("version"); boolean doCheckSum; if (version == null || !version.equals(DENSITY_FILE_VERSION)) { throw new IOException("Unsupported version in " + path); } String checksum = props.getProperty("chksum0"); doCheckSum = (checksum != null && checksum.equals("yes")); numStates = readInt(dis); numStreams = readInt(dis); numGaussiansPerState = readInt(dis); int[] vectorLength = new int[numStreams]; for (int i = 0; i < numStreams; i++) { vectorLength[i] = readInt(dis); } int rawLength = readInt(dis); //System.out.println("Nstates " + numStates); //System.out.println("Nstreams " + numStreams); //System.out.println("NgaussiansPerState " + numGaussiansPerState); //System.out.println("vectorLength " + vectorLength.length); //System.out.println("rawLength " + rawLength); for (int i = 0; i < numStreams; i++) { blockSize += vectorLength[i]; } assert rawLength == numGaussiansPerState * blockSize * numStates; assert numStreams == 1; Pool pool = new Pool(path); pool.setFeature(NUM_SENONES, numStates); pool.setFeature(NUM_STREAMS, numStreams); pool.setFeature(NUM_GAUSSIANS_PER_STATE, numGaussiansPerState); int r = 0; for (int i = 0; i < numStates; i++) { for (int j = 0; j < numStreams; j++) { for (int k = 0; k < numGaussiansPerState; k++) { float[] density = readFloatArray(dis, vectorLength[j]); floorData(density, floor); pool.put(i * numGaussiansPerState + k, density); } } } int checkSum = readInt(dis); // BUG: not checking the check sum yet. dis.close(); return pool; } /** * Reads the S3 binary hearder from the given location+path. Adds * header information to the given set of properties. * * @param location * the location of the file * @param path * the name of the file * @param props * the properties * * @return the input stream positioned after the header * * @throws IOException * on error */ protected DataInputStream readS3BinaryHeader(String location, String path, Properties props) throws IOException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -