1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35
36
37
38
39
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
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 }