Guest User

TranscribeFile.java

a guest
Mar 1st, 2017
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.PrintStream;
  7.  
  8. import edu.cmu.sphinx.api.Configuration;
  9. import edu.cmu.sphinx.api.SpeechResult;
  10. import edu.cmu.sphinx.api.StreamSpeechRecognizer;
  11. import edu.cmu.sphinx.decoder.adaptation.Stats;
  12. import edu.cmu.sphinx.decoder.adaptation.Transform;
  13. import edu.cmu.sphinx.result.Result;
  14. import edu.cmu.sphinx.decoder.search.Token;
  15.  
  16. public class TranscribeFile {
  17.    public static void showUsage() {
  18.       System.out.println("Usage: java com.example.TranscribeFile <input file> [<output file>]");
  19.    }
  20.  
  21.    //returns a string representing <time> in the format
  22.    //"hours:minutes:seconds.milliseconds"
  23.    //
  24.    //argument: <time> is the time in milliseconds
  25.    public static String millisecondsToHMS(long time) {
  26.       long h,m,s,mill;
  27.       mill = time % 1000;
  28.       time = (time - mill)/1000;
  29.       s = time % 60;
  30.       time = (time - s)/60;
  31.       m = time % 60;
  32.       time = (time - m)/60;
  33.       h = time;
  34.  
  35.       return String.format("%d:%02d:%02d.%04d", h, m, s, mill);
  36.    }
  37.  
  38.    public static void main(String[] args) throws Exception {
  39.       boolean verbose = false;
  40.       String in_file = null;
  41.       String out_file = null;
  42.       PrintStream out = null;
  43.  
  44.       //Parse options
  45.       try {
  46.          if (args.length < 1)
  47.             throw new Exception("Too few arguments.");
  48.          if (args.length > 2)
  49.             throw new Exception("Too many arguments.");
  50.  
  51.          in_file = args[0];
  52.          if (args.length >= 2)
  53.             out_file = args[1];
  54.       } catch (Exception e) {
  55.          System.out.format("Error: %s\n", e.getMessage());
  56.          showUsage();
  57.          System.exit(1);
  58.       }
  59.  
  60.       if (out_file != null)
  61.          out = new PrintStream(new FileOutputStream(out_file));
  62.       else
  63.          out = System.out;
  64.       Configuration configuration = new Configuration();
  65.  
  66.       //configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
  67.       configuration.setAcousticModelPath("file:/Users/johnny/Downloads/cmusphinx-en-us-ptm-5.2");
  68.       configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
  69.       //configuration.setDictionaryPath("file:/Users/johnny/Downloads/cmudict-0.7b.dict");
  70.       //configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin");
  71.       configuration.setLanguageModelPath("file:/Users/johnny/Downloads/en-70k-0.2-pruned.lm");
  72.  
  73.       StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
  74.       InputStream stream;
  75.  
  76.       //SpeechResult result;
  77.       stream = new FileInputStream(new File(in_file));
  78.       recognizer.startRecognition(stream);
  79.       SpeechResult result;
  80.       Token token;
  81.       long time;
  82.       while ((result = recognizer.getResult()) != null) {
  83.          token = result.getResult().getBestToken();
  84.          if (token != null) {
  85.             time = token.getCollectTime();
  86.             out.format("%s: %s\n", millisecondsToHMS(time), token.getWordPathNoFiller()); //print result together with timecode
  87.          } else {
  88.             out.println("");
  89.          }
  90.       }
  91.       recognizer.stopRecognition();
  92.       stream.close();
  93.       out.close();
  94.    }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment