threadedacousticscorer.java

来自「It is the Speech recognition software. 」· Java 代码 · 共 547 行 · 第 1/2 页

JAVA
547
字号
                    } else {                        Scoreable myBest = scoreScoreables(job);                        semaphore.post(myBest);                    }                }                best = semaphore.pend();            } else {                ScoreableJob job = new ScoreableJob(scoreableList, 0,                        scoreableList.size());                best = scoreScoreables(job);            }        } catch (DataProcessingException dpe) {            dpe.printStackTrace();            return best;        }        return best;    }    /**     * Performs post-recognition cleanup.     */    public void stopRecognition() {    }    /**     * Scores all of the Scoreables in the ScoreableJob     *      * @param job     *                the scoreable job     * @return the best scoring scoreable in the job     */    private Scoreable scoreScoreables(ScoreableJob job) {        Scoreable best = job.getFirst();        int listSize = job.getScoreables().size();        int end = job.getStart() + job.getSize();        if (end > listSize) {            end = listSize;        }        ListIterator iterator = job.getListIterator();        for (int i = job.getStart(); i < end; i++) {            Scoreable scoreable = (Scoreable) iterator.next();            // since we are potentially doing somethigns such as frame            // skipping and grow skipping, this check can become            // troublesome. Thus it is currently disabled.            /*             * if (false && scoreable.getFrameNumber() != currentData.getID()) {             * throw new Error ("Frame number mismatch: Token: " +             * scoreable.getFrameNumber() + " Data: " + currentData.getID()); }             */            if (scoreable.calculateScore(currentData, keepData,                        acousticGain) > best                    .getScore()) {                best = scoreable;            }        }        return best;    }    /**     * A scoring thread waits for a new scoreable to arrive at the mailbox,     * scores it, and notifies when its done by posting to the semaphore     */    class ScoringThread extends Thread {        /**         * Creates a new ScoringThread.         */        ScoringThread() {            setDaemon(true);        }        /**         * Waits for a scoreable job and scores the scoreable in the job,         * signally back when done         */        public void run() {            while (true) {                ScoreableJob scoreableJob = mailbox.pend();                Scoreable best = scoreScoreables(scoreableJob);                semaphore.post(best);            }        }    }}/** * Mailbox class allows a set of threads to communicate a single scoreable job */class Mailbox {    private ScoreableJob curScoreableJob;    /**     * Posts a scoreable to the mail box. The caller will block until the     * mailbox is empty and will then notify any waiters     */    synchronized void post(ScoreableJob scoreableJob) {        while (curScoreableJob != null) {            try {                wait();            } catch (InterruptedException ioe) {            }        }        curScoreableJob = scoreableJob;        notifyAll();    }    /**     * Waits for a scoreable to arrive in the mailbox and returns it. This will     * block the caller until a scoreable arrives     *      * @return the next scoreable     */    synchronized ScoreableJob pend() {        ScoreableJob returnScoreableJob;        while (curScoreableJob == null) {            try {                wait();            } catch (InterruptedException ioe) {            }        }        returnScoreableJob = curScoreableJob;        curScoreableJob = null;        notifyAll();        return returnScoreableJob;    }}/** * A counting semaphore */class Semaphore {    int count;    Scoreable bestScoreable;    /**     * Sets the count for this counting semaphore     *      * @param count     *                the count for the semaphore     */    synchronized void reset(int count) {        this.count = count;        bestScoreable = null;    }    /**     * Pends the caller until the count reaches zero     *      * @return the best scoreable encounted     */    synchronized Scoreable pend() {        while (count > 0) {            try {                wait();            } catch (InterruptedException ioe) {            }        }        return bestScoreable;    }    /**     * Posts to the semaphore, decrementing the counter by one. should the     * counter arrive at zero, wake up any penders.     *      * @param postedBest     *                the best scoreable encounted for this batch.     */    synchronized void post(Scoreable postedBest) {        if (bestScoreable == null                || postedBest.getScore() > bestScoreable.getScore()) {            bestScoreable = postedBest;        }        count--;        if (count <= 0) {            notifyAll();        }    }}/** * Represent a set of scoreables to be scored */class ScoreableJob {    private List scoreables;    private int start;    private int size;    /**     * Creates a scoreable job     *      * @param scoreables     *                the list of scoreables     * @param start     *                the starting point for this job     * @param size     *                the number of scoreables in this job     */    ScoreableJob(List scoreables, int start, int size) {        this.scoreables = scoreables;        this.start = start;        this.size = size;    }    /**     * Gets the starting index for this job     *      * @return the starting index     */    int getStart() {        return start;    }    /**     * Gets the number of scoreables in this job     *      * @return the number of scoreables in this job     */    int getSize() {        return size;    }    /**     * Returns the first scoreable in this job.     *      * @return the first scoreable in this job     */    Scoreable getFirst() {        return (Scoreable) scoreables.get(start);    }    /**     * Gets the entire list of scoreables     *      * @return the list of scoreables     */    List getScoreables() {        return scoreables;    }    /**     * Returns a ListIterator for this job.     *      * @return a ListIterator for this job.     */    ListIterator getListIterator() {        return scoreables.listIterator(start);    }    /**     * Returns a string representation of this object     *      * @return the string representation     */    public String toString() {        return "List size " + scoreables.size() + " start " + start + " size "                + size;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?