View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.transcription.microsoft.azure.model;
23  
24  import org.apache.commons.lang3.StringUtils;
25  
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Optional;
30  
31  public class MicrosoftAzureSpeechTranscriptionJson {
32  
33    // CHECKSTYLE:OFF checkstyle:LineLength
34  
35    // Documentation:
36    // https://eastus.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-1/operations/Transcriptions_ListFiles
37  
38    // CHECKSTYLE:ON checkstyle:LineLength
39    // CHECKSTYLE:OFF checkstyle:VisibilityModifier
40  
41    public String source;
42    public String timestamp;
43    public long durationInTicks;
44    public String duration;
45    public List<Map<String, Object>> combinedRecognizedPhrases;
46    public List<MicrosoftAzureSpeechTranscriptionJsonRecognizedPhrases> recognizedPhrases;
47  
48    // CHECKSTYLE:ON checkstyle:VisibilityModifier
49  
50    public MicrosoftAzureSpeechTranscriptionJson() { }
51  
52    public String toSrt(float minConfidence, int maxCueLength) {
53      StringBuilder sb = new StringBuilder();
54      long segmentIndex = 1;
55      for (MicrosoftAzureSpeechTranscriptionJsonRecognizedPhrases phrase : recognizedPhrases) {
56        String[] cues = phrase.toSrt(minConfidence, maxCueLength);
57        if (cues != null) {
58          for (String cue : cues) {
59            sb.append(String.format("\n%d\n", segmentIndex++)).append(cue);
60          }
61        }
62      }
63      return sb.toString();
64    }
65  
66    public String toWebVtt(float minConfidence, int maxCueLength) {
67      StringBuilder sb = new StringBuilder();
68      sb.append("WEBVTT\n");
69      for (MicrosoftAzureSpeechTranscriptionJsonRecognizedPhrases phrase : recognizedPhrases) {
70        String[] cues = phrase.toWebVtt(minConfidence, maxCueLength);
71        if (cues != null) {
72          for (String cue : cues) {
73            if (StringUtils.isNotBlank(cue)) {
74              sb.append("\n");
75              sb.append(cue);
76            }
77          }
78        }
79      }
80      return sb.toString();
81    }
82  
83    public Map<String, Float> getRecognizedLocales() {
84      Map<String, Long> localeDurations = new HashMap<>();
85      for (MicrosoftAzureSpeechTranscriptionJsonRecognizedPhrases recognizedPhrase : recognizedPhrases) {
86        if (StringUtils.isNotBlank(recognizedPhrase.locale)) {
87          localeDurations.put(recognizedPhrase.locale,
88              localeDurations.getOrDefault(recognizedPhrase.locale, 0L) + recognizedPhrase.durationInTicks);
89        }
90      }
91      Map<String, Float> relativeLocaleDurations = new HashMap<>();
92      for (Map.Entry<String, Long> localeDuration : localeDurations.entrySet()) {
93        relativeLocaleDurations.put(localeDuration.getKey(),
94            localeDuration.getValue().floatValue() / Long.valueOf(durationInTicks).floatValue());
95      }
96      return relativeLocaleDurations;
97    }
98  
99    public String getRecognizedLocale() {
100     Optional<Map.Entry<String, Float>> localeOpt = getRecognizedLocales().entrySet().stream()
101         .max(Map.Entry.comparingByValue());
102     return localeOpt.isPresent() ? localeOpt.get().getKey() : "";
103   }
104 }