static double calculatePerplexity( LanguageModel languageModel, Collection<List<String>> sentenceCollection) { double logProbability = 0.0; double numSymbols = 0.0; double oneProbability = 0; for (List<String> sentence : sentenceCollection) { oneProbability = languageModel.getSentenceProbability(sentence); if (!(oneProbability > 0)) { oneProbability = languageModel.getSentenceProbability(sentence); } logProbability += Math.log(oneProbability) / Math.log(2.0); numSymbols += sentence.size(); } double avgLogProbability = logProbability / numSymbols; double perplexity = Math.pow(0.5, avgLogProbability); return perplexity; }
private static void displayHypothesis( String prefix, List<String> guess, SpeechNBestList speechNBestList, LanguageModel languageModel) { final double acoustic = speechNBestList.getAcousticScore(guess) / 16.0; final double language = Math.log(languageModel.getSentenceProbability(guess)); System.out.println( prefix + "\tAM: " + nf.format(acoustic) + "\tLM: " + nf.format(language) + "\tTotal: " + nf.format(acoustic + language) + "\t" + guess); }
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; }