@Test public void testImportCaptionPush() throws Exception { inputStream = IBMWatsonCaptionConverterTest.class.getResourceAsStream("/pulled_transcription.json"); importCaption(); }
@Override public List<Caption> importCaption(InputStream inputStream, String language) throws CaptionConverterException { List<Caption> captionList = new ArrayList<Caption>(); JSONParser jsonParser = new JSONParser(); try { JSONObject resultsObj = (JSONObject) jsonParser.parse(new InputStreamReader(inputStream)); String jobId = "Unknown"; if (resultsObj.get("id") != null) jobId = (String) resultsObj.get("id"); if (resultsObj.get("warnings") != null) { JSONArray warningsArray = (JSONArray) resultsObj.get("warnings"); if (warningsArray != null) { for (Object w : warningsArray) logger.warn("Warning from Speech-To-Text service: {}" + w); } } JSONArray outerResultsArray = (JSONArray) resultsObj.get("results"); JSONObject obj = (JSONObject) outerResultsArray.get(0); JSONArray resultsArray = (JSONArray) obj.get("results"); resultsLoop: for (int i = 0; i < resultsArray.size(); i++) { JSONObject resultElement = (JSONObject) resultsArray.get(i); if (!(Boolean) resultElement.get("final")) continue; JSONArray alternativesArray = (JSONArray) resultElement.get("alternatives"); if (alternativesArray != null && alternativesArray.size() > 0) { JSONObject alternativeElement = (JSONObject) alternativesArray.get(0); String transcript = (String) alternativeElement.get("transcript"); if (transcript != null) { JSONArray timestampsArray = (JSONArray) alternativeElement.get("timestamps"); if (timestampsArray == null || timestampsArray.size() == 0) { logger.warn("Could not build caption object for job {}, result index {}: timestamp data not found", jobId, i); continue; } String[] words = transcript.split("\\s+"); StringBuffer line = new StringBuffer(); int indexFirst = -1; int indexLast = -1; for (int j = 0; j < words.length; j++) { if (indexFirst == -1) indexFirst = j; line.append(words[j]); line.append(" "); if (line.length() >= LINE_SIZE || j == words.length - 1) { indexLast = j; double start = -1; double end = -1; if (indexLast < timestampsArray.size()) { JSONArray wordTsArray = (JSONArray) timestampsArray.get(indexFirst); if (wordTsArray.size() == 3) start = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(1).toString()), "s")).doubleValue(); wordTsArray = (JSONArray) timestampsArray.get(indexLast); if (wordTsArray.size() == 3) end = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(2).toString()), "s")).doubleValue(); } if (start == -1 || end == -1) { logger.warn("Could not build caption object for job {}, result index {}: start/end times not found", jobId, i); continue resultsLoop; } String[] captionLines = new String[1]; captionLines[0] = line.toString().replace("%HESITATION", "..."); captionList.add(new CaptionImpl(buildTime((long) (start * 1000)), buildTime((long) (end * 1000)), captionLines)); indexFirst = -1; indexLast = -1; line.setLength(0); } } } } } } catch (Exception e) { logger.warn("Error when parsing IBM Watson transcriptions result: {}" + e.getMessage()); throw new CaptionConverterException(e); } return captionList; }
IBMWatsonCaptionConverter implements CaptionConverter { @Override public List<Caption> importCaption(InputStream inputStream, String language) throws CaptionConverterException { List<Caption> captionList = new ArrayList<Caption>(); JSONParser jsonParser = new JSONParser(); try { JSONObject resultsObj = (JSONObject) jsonParser.parse(new InputStreamReader(inputStream)); String jobId = "Unknown"; if (resultsObj.get("id") != null) jobId = (String) resultsObj.get("id"); if (resultsObj.get("warnings") != null) { JSONArray warningsArray = (JSONArray) resultsObj.get("warnings"); if (warningsArray != null) { for (Object w : warningsArray) logger.warn("Warning from Speech-To-Text service: {}" + w); } } JSONArray outerResultsArray = (JSONArray) resultsObj.get("results"); JSONObject obj = (JSONObject) outerResultsArray.get(0); JSONArray resultsArray = (JSONArray) obj.get("results"); resultsLoop: for (int i = 0; i < resultsArray.size(); i++) { JSONObject resultElement = (JSONObject) resultsArray.get(i); if (!(Boolean) resultElement.get("final")) continue; JSONArray alternativesArray = (JSONArray) resultElement.get("alternatives"); if (alternativesArray != null && alternativesArray.size() > 0) { JSONObject alternativeElement = (JSONObject) alternativesArray.get(0); String transcript = (String) alternativeElement.get("transcript"); if (transcript != null) { JSONArray timestampsArray = (JSONArray) alternativeElement.get("timestamps"); if (timestampsArray == null || timestampsArray.size() == 0) { logger.warn("Could not build caption object for job {}, result index {}: timestamp data not found", jobId, i); continue; } String[] words = transcript.split("\\s+"); StringBuffer line = new StringBuffer(); int indexFirst = -1; int indexLast = -1; for (int j = 0; j < words.length; j++) { if (indexFirst == -1) indexFirst = j; line.append(words[j]); line.append(" "); if (line.length() >= LINE_SIZE || j == words.length - 1) { indexLast = j; double start = -1; double end = -1; if (indexLast < timestampsArray.size()) { JSONArray wordTsArray = (JSONArray) timestampsArray.get(indexFirst); if (wordTsArray.size() == 3) start = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(1).toString()), "s")).doubleValue(); wordTsArray = (JSONArray) timestampsArray.get(indexLast); if (wordTsArray.size() == 3) end = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(2).toString()), "s")).doubleValue(); } if (start == -1 || end == -1) { logger.warn("Could not build caption object for job {}, result index {}: start/end times not found", jobId, i); continue resultsLoop; } String[] captionLines = new String[1]; captionLines[0] = line.toString().replace("%HESITATION", "..."); captionList.add(new CaptionImpl(buildTime((long) (start * 1000)), buildTime((long) (end * 1000)), captionLines)); indexFirst = -1; indexLast = -1; line.setLength(0); } } } } } } catch (Exception e) { logger.warn("Error when parsing IBM Watson transcriptions result: {}" + e.getMessage()); throw new CaptionConverterException(e); } return captionList; } }
IBMWatsonCaptionConverter implements CaptionConverter { @Override public List<Caption> importCaption(InputStream inputStream, String language) throws CaptionConverterException { List<Caption> captionList = new ArrayList<Caption>(); JSONParser jsonParser = new JSONParser(); try { JSONObject resultsObj = (JSONObject) jsonParser.parse(new InputStreamReader(inputStream)); String jobId = "Unknown"; if (resultsObj.get("id") != null) jobId = (String) resultsObj.get("id"); if (resultsObj.get("warnings") != null) { JSONArray warningsArray = (JSONArray) resultsObj.get("warnings"); if (warningsArray != null) { for (Object w : warningsArray) logger.warn("Warning from Speech-To-Text service: {}" + w); } } JSONArray outerResultsArray = (JSONArray) resultsObj.get("results"); JSONObject obj = (JSONObject) outerResultsArray.get(0); JSONArray resultsArray = (JSONArray) obj.get("results"); resultsLoop: for (int i = 0; i < resultsArray.size(); i++) { JSONObject resultElement = (JSONObject) resultsArray.get(i); if (!(Boolean) resultElement.get("final")) continue; JSONArray alternativesArray = (JSONArray) resultElement.get("alternatives"); if (alternativesArray != null && alternativesArray.size() > 0) { JSONObject alternativeElement = (JSONObject) alternativesArray.get(0); String transcript = (String) alternativeElement.get("transcript"); if (transcript != null) { JSONArray timestampsArray = (JSONArray) alternativeElement.get("timestamps"); if (timestampsArray == null || timestampsArray.size() == 0) { logger.warn("Could not build caption object for job {}, result index {}: timestamp data not found", jobId, i); continue; } String[] words = transcript.split("\\s+"); StringBuffer line = new StringBuffer(); int indexFirst = -1; int indexLast = -1; for (int j = 0; j < words.length; j++) { if (indexFirst == -1) indexFirst = j; line.append(words[j]); line.append(" "); if (line.length() >= LINE_SIZE || j == words.length - 1) { indexLast = j; double start = -1; double end = -1; if (indexLast < timestampsArray.size()) { JSONArray wordTsArray = (JSONArray) timestampsArray.get(indexFirst); if (wordTsArray.size() == 3) start = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(1).toString()), "s")).doubleValue(); wordTsArray = (JSONArray) timestampsArray.get(indexLast); if (wordTsArray.size() == 3) end = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(2).toString()), "s")).doubleValue(); } if (start == -1 || end == -1) { logger.warn("Could not build caption object for job {}, result index {}: start/end times not found", jobId, i); continue resultsLoop; } String[] captionLines = new String[1]; captionLines[0] = line.toString().replace("%HESITATION", "..."); captionList.add(new CaptionImpl(buildTime((long) (start * 1000)), buildTime((long) (end * 1000)), captionLines)); indexFirst = -1; indexLast = -1; line.setLength(0); } } } } } } catch (Exception e) { logger.warn("Error when parsing IBM Watson transcriptions result: {}" + e.getMessage()); throw new CaptionConverterException(e); } return captionList; }  }
IBMWatsonCaptionConverter implements CaptionConverter { @Override public List<Caption> importCaption(InputStream inputStream, String language) throws CaptionConverterException { List<Caption> captionList = new ArrayList<Caption>(); JSONParser jsonParser = new JSONParser(); try { JSONObject resultsObj = (JSONObject) jsonParser.parse(new InputStreamReader(inputStream)); String jobId = "Unknown"; if (resultsObj.get("id") != null) jobId = (String) resultsObj.get("id"); if (resultsObj.get("warnings") != null) { JSONArray warningsArray = (JSONArray) resultsObj.get("warnings"); if (warningsArray != null) { for (Object w : warningsArray) logger.warn("Warning from Speech-To-Text service: {}" + w); } } JSONArray outerResultsArray = (JSONArray) resultsObj.get("results"); JSONObject obj = (JSONObject) outerResultsArray.get(0); JSONArray resultsArray = (JSONArray) obj.get("results"); resultsLoop: for (int i = 0; i < resultsArray.size(); i++) { JSONObject resultElement = (JSONObject) resultsArray.get(i); if (!(Boolean) resultElement.get("final")) continue; JSONArray alternativesArray = (JSONArray) resultElement.get("alternatives"); if (alternativesArray != null && alternativesArray.size() > 0) { JSONObject alternativeElement = (JSONObject) alternativesArray.get(0); String transcript = (String) alternativeElement.get("transcript"); if (transcript != null) { JSONArray timestampsArray = (JSONArray) alternativeElement.get("timestamps"); if (timestampsArray == null || timestampsArray.size() == 0) { logger.warn("Could not build caption object for job {}, result index {}: timestamp data not found", jobId, i); continue; } String[] words = transcript.split("\\s+"); StringBuffer line = new StringBuffer(); int indexFirst = -1; int indexLast = -1; for (int j = 0; j < words.length; j++) { if (indexFirst == -1) indexFirst = j; line.append(words[j]); line.append(" "); if (line.length() >= LINE_SIZE || j == words.length - 1) { indexLast = j; double start = -1; double end = -1; if (indexLast < timestampsArray.size()) { JSONArray wordTsArray = (JSONArray) timestampsArray.get(indexFirst); if (wordTsArray.size() == 3) start = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(1).toString()), "s")).doubleValue(); wordTsArray = (JSONArray) timestampsArray.get(indexLast); if (wordTsArray.size() == 3) end = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(2).toString()), "s")).doubleValue(); } if (start == -1 || end == -1) { logger.warn("Could not build caption object for job {}, result index {}: start/end times not found", jobId, i); continue resultsLoop; } String[] captionLines = new String[1]; captionLines[0] = line.toString().replace("%HESITATION", "..."); captionList.add(new CaptionImpl(buildTime((long) (start * 1000)), buildTime((long) (end * 1000)), captionLines)); indexFirst = -1; indexLast = -1; line.setLength(0); } } } } } } catch (Exception e) { logger.warn("Error when parsing IBM Watson transcriptions result: {}" + e.getMessage()); throw new CaptionConverterException(e); } return captionList; }  @Override List<Caption> importCaption(InputStream inputStream, String language); @Override void exportCaption(OutputStream outputStream, List<Caption> captions, String language); @Override String[] getLanguageList(InputStream inputStream); @Override String getExtension(); @Override Type getElementType(); }
IBMWatsonCaptionConverter implements CaptionConverter { @Override public List<Caption> importCaption(InputStream inputStream, String language) throws CaptionConverterException { List<Caption> captionList = new ArrayList<Caption>(); JSONParser jsonParser = new JSONParser(); try { JSONObject resultsObj = (JSONObject) jsonParser.parse(new InputStreamReader(inputStream)); String jobId = "Unknown"; if (resultsObj.get("id") != null) jobId = (String) resultsObj.get("id"); if (resultsObj.get("warnings") != null) { JSONArray warningsArray = (JSONArray) resultsObj.get("warnings"); if (warningsArray != null) { for (Object w : warningsArray) logger.warn("Warning from Speech-To-Text service: {}" + w); } } JSONArray outerResultsArray = (JSONArray) resultsObj.get("results"); JSONObject obj = (JSONObject) outerResultsArray.get(0); JSONArray resultsArray = (JSONArray) obj.get("results"); resultsLoop: for (int i = 0; i < resultsArray.size(); i++) { JSONObject resultElement = (JSONObject) resultsArray.get(i); if (!(Boolean) resultElement.get("final")) continue; JSONArray alternativesArray = (JSONArray) resultElement.get("alternatives"); if (alternativesArray != null && alternativesArray.size() > 0) { JSONObject alternativeElement = (JSONObject) alternativesArray.get(0); String transcript = (String) alternativeElement.get("transcript"); if (transcript != null) { JSONArray timestampsArray = (JSONArray) alternativeElement.get("timestamps"); if (timestampsArray == null || timestampsArray.size() == 0) { logger.warn("Could not build caption object for job {}, result index {}: timestamp data not found", jobId, i); continue; } String[] words = transcript.split("\\s+"); StringBuffer line = new StringBuffer(); int indexFirst = -1; int indexLast = -1; for (int j = 0; j < words.length; j++) { if (indexFirst == -1) indexFirst = j; line.append(words[j]); line.append(" "); if (line.length() >= LINE_SIZE || j == words.length - 1) { indexLast = j; double start = -1; double end = -1; if (indexLast < timestampsArray.size()) { JSONArray wordTsArray = (JSONArray) timestampsArray.get(indexFirst); if (wordTsArray.size() == 3) start = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(1).toString()), "s")).doubleValue(); wordTsArray = (JSONArray) timestampsArray.get(indexLast); if (wordTsArray.size() == 3) end = NumberFormat.getInstance(Locale.US).parse(removeEndCharacter((wordTsArray.get(2).toString()), "s")).doubleValue(); } if (start == -1 || end == -1) { logger.warn("Could not build caption object for job {}, result index {}: start/end times not found", jobId, i); continue resultsLoop; } String[] captionLines = new String[1]; captionLines[0] = line.toString().replace("%HESITATION", "..."); captionList.add(new CaptionImpl(buildTime((long) (start * 1000)), buildTime((long) (end * 1000)), captionLines)); indexFirst = -1; indexLast = -1; line.setLength(0); } } } } } } catch (Exception e) { logger.warn("Error when parsing IBM Watson transcriptions result: {}" + e.getMessage()); throw new CaptionConverterException(e); } return captionList; }  @Override List<Caption> importCaption(InputStream inputStream, String language); @Override void exportCaption(OutputStream outputStream, List<Caption> captions, String language); @Override String[] getLanguageList(InputStream inputStream); @Override String getExtension(); @Override Type getElementType();  }