static Collection<List<String>> extractCorrectSentenceList( List<SpeechNBestList> speechNBestLists) { Collection<List<String>> correctSentences = new ArrayList<List<String>>(); for (SpeechNBestList speechNBestList : speechNBestLists) { correctSentences.add(speechNBestList.getCorrectSentence()); } return correctSentences; }
static double calculateWordErrorRateUpperBound(List<SpeechNBestList> speechNBestLists) { double totalDistance = 0.0; double totalWords = 0.0; EditDistance editDistance = new EditDistance(); for (SpeechNBestList speechNBestList : speechNBestLists) { List<String> correctSentence = speechNBestList.getCorrectSentence(); double worstDistance = Double.NEGATIVE_INFINITY; for (List<String> guess : speechNBestList.getNBestSentences()) { double distance = editDistance.getDistance(correctSentence, guess); if (distance > worstDistance) worstDistance = distance; } totalDistance += worstDistance; totalWords += correctSentence.size(); } return totalDistance / totalWords; }
static double calculateWordErrorRateRandomChoice(List<SpeechNBestList> speechNBestLists) { double totalDistance = 0.0; double totalWords = 0.0; EditDistance editDistance = new EditDistance(); for (SpeechNBestList speechNBestList : speechNBestLists) { List<String> correctSentence = speechNBestList.getCorrectSentence(); double sumDistance = 0.0; double numGuesses = 0.0; for (List<String> guess : speechNBestList.getNBestSentences()) { double distance = editDistance.getDistance(correctSentence, guess); sumDistance += distance; numGuesses += 1.0; } totalDistance += sumDistance / numGuesses; totalWords += correctSentence.size(); } return totalDistance / totalWords; }
static double calculateWordErrorRate( LanguageModel languageModel, List<SpeechNBestList> speechNBestLists, boolean verbose) { double totalDistance = 0.0; double totalWords = 0.0; EditDistance editDistance = new EditDistance(); for (SpeechNBestList speechNBestList : speechNBestLists) { List<String> correctSentence = speechNBestList.getCorrectSentence(); List<String> bestGuess = null; double bestScore = Double.NEGATIVE_INFINITY; double numWithBestScores = 0.0; double distanceForBestScores = 0.0; for (List<String> guess : speechNBestList.getNBestSentences()) { double score = Math.log(languageModel.getSentenceProbability(guess)) + (speechNBestList.getAcousticScore(guess) / 16.0); double distance = editDistance.getDistance(correctSentence, guess); if (score == bestScore) { numWithBestScores += 1.0; distanceForBestScores += distance; } if (score > bestScore || bestGuess == null) { bestScore = score; bestGuess = guess; distanceForBestScores = distance; numWithBestScores = 1.0; } } // double distance = editDistance.getDistance(correctSentence, // bestGuess); totalDistance += distanceForBestScores / numWithBestScores; totalWords += correctSentence.size(); if (verbose) { if (distanceForBestScores > 0.0) { System.out.println(); displayHypothesis("GUESS:", bestGuess, speechNBestList, languageModel); displayHypothesis("GOLD: ", correctSentence, speechNBestList, languageModel); // System.out.println("GOLD: "+correctSentence); } } } return totalDistance / totalWords; }
private static void displayHypothesis( String prefix, List<String> guess, SpeechNBestList speechNBestList, LanguageModel languageModel) { double acoustic = speechNBestList.getAcousticScore(guess) / 16.0; double language = Math.log(languageModel.getSentenceProbability(guess)); System.out.println( prefix + " AM: " + nf.format(acoustic) + " LM: " + nf.format(language) + " Total: " + nf.format(acoustic + language) + " " + guess); }