📄 batchmoderecognizer.java
字号:
/* * Copyright 1999-2002 Carnegie Mellon University. * Portions Copyright 2002 Sun Microsystems, Inc. * Portions Copyright 2002 Mitsubishi Electric 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.tools.batch;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.Iterator;import java.util.List;import java.util.logging.Logger;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.UnsupportedAudioFileException;import edu.cmu.sphinx.frontend.DataProcessor;import edu.cmu.sphinx.frontend.util.StreamCepstrumSource;import edu.cmu.sphinx.frontend.util.StreamDataSource;import edu.cmu.sphinx.recognizer.Recognizer;import edu.cmu.sphinx.recognizer.RecognizerState;import edu.cmu.sphinx.result.Result;import edu.cmu.sphinx.util.BatchItem;import edu.cmu.sphinx.util.BatchManager;import edu.cmu.sphinx.util.PooledBatchManager;import edu.cmu.sphinx.util.SimpleBatchManager;import edu.cmu.sphinx.util.Utilities;import edu.cmu.sphinx.util.props.Configurable;import edu.cmu.sphinx.util.props.ConfigurationManager;import edu.cmu.sphinx.util.props.PropertyException;import edu.cmu.sphinx.util.props.PropertySheet;import edu.cmu.sphinx.util.props.PropertyType;import edu.cmu.sphinx.util.props.Registry;import edu.cmu.sphinx.util.CommandInterpreter;import edu.cmu.sphinx.util.CommandInterface;/** * Decodes a batch file containing a list of files to decode. The files can be * either audio files or cepstral files, but defaults to audio files. * The audio data should be 16-bit, 16kHz, PCM-linear data. * Since this classes makes use of Java Sound, it supports all the audio file * formats that are supported by Java Sound. If the audio file does not * correspond to a format supported by Java Sound, it is treated as a raw * audio file (i.e., one without a header). Audio file formats differ * in the endian order of the audio data. Therefore, it is important to * specify it correctly in the configuration of the * <a href="../../frontend/util/StreamDataSource.html">StreamDataSource</a>. * Note that in the ideal situation, the audio format of the data should * be passed into the StreamDataSource, so that no extra configuration is * needed. This will be fixed in future releases. * <p> * To run this BatchModeRecognizer: * <pre> * java BatchModeRecognizer <xmlConfigFile> <batchFile> * </pre> * where <code>xmlConfigFile</code> is an XML-based configuration file and * <code>batchFile</code> is a file listing all the files to decode and * transcript of those files. For information about the configuration * file, refer to the document * <a href="../../util/props/doc-files/ConfigurationManagement.html"> * Sphinx-4 Configuration Management</a>. For information about the * batch file, refer to the <a href="../../../../../../index.html#batch_files"> * batch file description</a>. * * This class will send recognition results to the logger if the log level is * set to INFO. * */public class BatchModeRecognizer implements Configurable { /** * The SphinxProperty name for how many files to skip for every decode. */ public final static String PROP_SKIP = "skip"; /** * The default value for the property PROP_SKIP. */ public final static int PROP_SKIP_DEFAULT = 0; /** * The SphinxProperty name for how many utterances to process */ public final static String PROP_COUNT = "count"; /** * The default value for the property PROP_COUNT. */ public final static int PROP_COUNT_DEFAULT = 1000000; /** * The SphinxProperty that specified which batch job is to be run. * */ public final static String PROP_WHICH_BATCH = "whichBatch"; /** * The default value for the property PROP_WHICH_BATCH. */ public final static int PROP_WHICH_BATCH_DEFAULT = 0; /** * The SphinxProperty for the total number of batch jobs the decoding run * is being divided into. * * The BatchDecoder supports running a subset of a batch. This allows a * test to be distributed among several machines. * */ public final static String PROP_TOTAL_BATCHES = "totalBatches"; /** * The default value for the property PROP_TOTAL_BATCHES. */ public final static int PROP_TOTAL_BATCHES_DEFAULT = 1; /** * The SphinxProperty that defines whether or not the decoder should use * the pooled batch manager */ public final static String PROP_USE_POOLED_BATCH_MANAGER = "usePooledBatchManager"; /** * The default value for the property PROP_USE_POOLED_BATCH_MANAGER. */ public final static boolean PROP_USE_POOLED_BATCH_MANAGER_DEFAULT = false; /** * The Sphinx property that specifies the recognizer to use */ public final static String PROP_RECOGNIZER = "recognizer"; /** * The sphinx property that specifies the input source */ public final static String PROP_INPUT_DATA_PROCESSORS = "inputDataProcessors"; // ------------------------------- // Configuration data // -------------------------------- private String name; private List inputDataProcessors; private int skip; private int totalCount; private int whichBatch; private int totalBatches; private boolean usePooledBatchManager; private BatchManager batchManager; private Recognizer recognizer; private Logger logger; private BatchItem curBatchItem; private ConfigurationManager cm; /* * (non-Javadoc) * * @see edu.cmu.sphinx.util.props.Configurable#register(java.lang.String, * edu.cmu.sphinx.util.props.Registry) */ public void register(String name, Registry registry) throws PropertyException { this.name = name; registry.register(PROP_SKIP, PropertyType.INT); registry.register(PROP_COUNT, PropertyType.INT); registry.register(PROP_WHICH_BATCH, PropertyType.INT); registry.register(PROP_TOTAL_BATCHES, PropertyType.INT); registry.register(PROP_USE_POOLED_BATCH_MANAGER, PropertyType.BOOLEAN); registry.register(PROP_RECOGNIZER, PropertyType.COMPONENT); registry.register(PROP_INPUT_DATA_PROCESSORS, PropertyType.COMPONENT_LIST); } /* * (non-Javadoc) * * @see edu.cmu.sphinx.util.props.Configurable#newProperties(edu.cmu.sphinx.util.props.PropertySheet) */ public void newProperties(PropertySheet ps) throws PropertyException { logger = ps.getLogger(); cm = ps.getPropertyManager(); skip = ps.getInt(PROP_SKIP, PROP_SKIP_DEFAULT); totalCount = ps.getInt(PROP_COUNT, PROP_COUNT_DEFAULT); if (totalCount <= 0) { totalCount = Integer.MAX_VALUE; } whichBatch = ps.getInt(PROP_WHICH_BATCH, PROP_WHICH_BATCH_DEFAULT); totalBatches = ps .getInt(PROP_TOTAL_BATCHES, PROP_TOTAL_BATCHES_DEFAULT); usePooledBatchManager = ps.getBoolean(PROP_USE_POOLED_BATCH_MANAGER, PROP_USE_POOLED_BATCH_MANAGER_DEFAULT); recognizer = (Recognizer) ps.getComponent(PROP_RECOGNIZER, Recognizer.class); inputDataProcessors = (List) ps.getComponentList (PROP_INPUT_DATA_PROCESSORS, DataProcessor.class); } /* * (non-Javadoc) * * @see edu.cmu.sphinx.util.props.Configurable#getName() */ public String getName() { return name; } /** * Sets the batch file to use for this recogition * * @param batchFile * the name of the batch file * @throws IOException * if the file could not be opened or read. */ public void setBatchFile(String batchFile) throws IOException { if (usePooledBatchManager) { batchManager = new PooledBatchManager(batchFile, skip); } else { batchManager = new SimpleBatchManager(batchFile, skip, whichBatch, totalBatches); } } /** * Decodes the batch of audio files * * @throws IOException * if there is an I/O error processing the batch file */ public void decode(String batchFile) { BatchItem batchItem; int count = 0; try { recognizer.allocate(); setBatchFile(batchFile); batchManager.start(); logger.info("BatchDecoder: decoding files in " + batchManager.getFilename()); while (count < totalCount && (batchItem = batchManager.getNextItem()) != null) { setInputStream(batchItem.getFilename()); Result result = recognizer.recognize(batchItem.getTranscript()); logger.info("File : " + batchItem.getFilename()); logger.info("Result: " + result); count++; } batchManager.stop(); recognizer.deallocate(); } catch (IOException io) { logger.severe("I/O error during decoding: " + io.getMessage()); } logger.info("BatchDecoder: " + count + " files decoded"); } /** * Sets the input stream to the given filename * @param filename the filename to set the input stream to * @return the InputStream representing the filename * @throws IOException if an error occurs */ private void setInputStream(String filename) throws IOException { for (Iterator i = inputDataProcessors.iterator(); i.hasNext(); ) { DataProcessor dataSource = (DataProcessor) i.next(); InputStream is = null; try { File file = new File(filename); logger.info (AudioSystem.getAudioFileFormat(file).toString()); is = AudioSystem.getAudioInputStream(file); } catch (UnsupportedAudioFileException uafe) { logger.info ("Reading " + filename + " as raw audio file."); is = new FileInputStream(filename); } if (dataSource instanceof StreamDataSource) { ((StreamDataSource) dataSource).setInputStream(is, filename); } else if (dataSource instanceof StreamCepstrumSource) { boolean isBigEndian = Utilities .isCepstraFileBigEndian(filename); StreamCepstrumSource cepstrumSource = (StreamCepstrumSource) dataSource; cepstrumSource.setInputStream(is, isBigEndian); } } } /** * Add commands to the given interpreter to support shell mode * * * @param ci the interpreter */ private void addCommands(CommandInterpreter ci) { ci.add("ls", new CommandInterface() { public String execute(CommandInterpreter ci, String[] args) { if (args.length != 1) { ci.putResponse("Usage: ls"); } else { String[] names = cm.getInstanceNames(Configurable.class); for (int i = 0; i < names.length; i++) { ci.putResponse(names[i]); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -