/*
 * Copyright 1999-2013 Carnegie Mellon University.
 * Portions Copyright 2004 Sun Microsystems, Inc.
 * Portions Copyright 2004 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 org.alienlabs.amazon;

import java.io.InputStream;

import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.StreamSpeechRecognizer;
import edu.cmu.sphinx.decoder.adaptation.Stats;
import edu.cmu.sphinx.decoder.adaptation.Transform;
import edu.cmu.sphinx.result.WordResult;
import edu.cmu.sphinx.util.TimeFrame;

/**
 * A simple example that shows how to transcribe a continuous audio file that
 * has multiple utterances in it.
 */
public class TranscriberDemo {

    public static String transcribe() throws Exception {
        System.out.println("Loading models...");

        Configuration configuration = new Configuration();

        // Load model from the jar
        configuration
			.setAcousticModelPath("file:/home/nostromo/english/cmusphinx-en-us-5.2");
				//.setAcousticModelPath("resource:/org/alienlabs/amazon/cmusphinx-en-us-5.2");
                //.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");

        // You can also load model from folder
        // configuration.setAcousticModelPath("file:en-us");

        configuration
			.setDictionaryPath("file:/home/nostromo/english/cmudict.0.7a_SPHINX_40.dict");
			//.setDictionaryPath("resource:/org/alienlabs/amazon/cmudict.0.7a_SPHINX_40");
                //.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
        configuration
			.setLanguageModelPath("file:/home/nostromo/english/cmusphinx-5.0-en-us.lm.bin");
			//.setLanguageModelPath("resource:/org/alienlabs/amazon/cmusphinx.lm.bin");
                //.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");

        StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(
                configuration);
        InputStream stream = TranscriberDemo.class.getResourceAsStream("test2.wav");
        //.getResourceAsStream("/edu/cmu/sphinx/demo/aligner/10001-90210-01803.wav");
        //stream.skip(44);

        // Simple recognition with generic model
        recognizer.startRecognition(stream);
        SpeechResult result;
        StringBuilder output = new StringBuilder();

        while ((result = recognizer.getResult()) != null) {

            System.out.format("Hypothesis: %s\n", result.getHypothesis());
			
			if (!result.getWords().isEmpty() && !"".equals(result.getHypothesis().trim())) {
				output.append
				(result.getHypothesis()).append
				(" from: ").append
				(result.getWords().get(0).getTimeFrame().getStart()).append
				(", to: ").append
				(result.getWords().get(0).getTimeFrame().getEnd()).append(" ");
			}
			
            System.out.println("List of recognized words and their times:");
            for (WordResult r : result.getWords()) {
                System.out.println(r);
            }

            System.out.println("Best 3 hypothesis:");
            for (String s : result.getNbest(3))
                System.out.println(s);

        }
        recognizer.stopRecognition();
		System.out.println("### " + output.toString());
        return output.toString();
    }
}

@@ -1110,28 +1110,99 @@
 onfiguration
+.setAcousticModelPath(%22resource:/edu/cmu/sphinx/models/en-us/en-us%22);
 %0A%09%09%09
+//
 .setAcoustic
@@ -1362,83 +1362,8 @@
     
-    //.setAcousticModelPath(%22resource:/edu/cmu/sphinx/models/en-us/en-us%22);
 %0A%0A  
@@ -1486,28 +1486,109 @@
 onfiguration
+.setDictionaryPath(%22resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict%22);
 %0A%09%09%09
+//
 .setDictiona
@@ -1752,32 +1752,55 @@
         
-//.setDictionary
+%0A        configuration.setLanguageModel
 Path(%22re
@@ -1839,55 +1839,29 @@
 -us/
-cmudict-en-us.dict%22);%0A        configuration
+en-us.lm.bin%22);
 %0A%09%09%09
+//
 .set
@@ -2036,86 +2036,8 @@
     
-//.setLanguageModelPath(%22resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin%22);
 %0A%0A  

put resources in custom sphinx4 jar