azure_speech/recognizer/
event.rs

1use crate::recognizer::Language;
2use crate::RequestId;
3
4/// The raw text of message.
5///
6/// The raw message is the message received from the speech recognition service.
7pub type RawMessage = String;
8
9/// Recognizer events.
10///
11/// The events are used to notify the user of the progress of the speech recognition.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum Event {
14    /// The session started.
15    SessionStarted(RequestId),
16
17    /// The session ended.
18    SessionEnded(RequestId),
19
20    /// The speech recognition started.
21    StartDetected(RequestId, Offset),
22    /// The speech recognition ended.
23    EndDetected(RequestId, Offset),
24
25    /// Recognizing event.
26    Recognizing(RequestId, Recognized, Offset, Duration, RawMessage),
27
28    /// Recognized event.
29    Recognized(RequestId, Recognized, Offset, Duration, RawMessage),
30
31    /// UnMatch event.
32    /// This event is triggered when the speech recognition does not match any text.
33    UnMatch(RequestId, Offset, Duration, RawMessage),
34    //Cancelled(RequestId, Offset, crate::Error),
35}
36
37/// The offset of the speech recognition.
38///
39/// The offset is the time in milliseconds from the start of the conversation.
40pub type Offset = u64;
41
42/// The duration of the speech recognition.
43///
44/// The duration is the time in milliseconds of the speech recognition.
45pub type Duration = u64;
46
47/// The recognized text.
48///
49/// Contains the recognized text, the primary language and the speaker id.
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct Recognized {
52    /// The recognized text.
53    pub text: String,
54    /// The primary language of the recognized text.
55    pub primary_language: Option<PrimaryLanguage>,
56
57    // todo: Remove from here and add to a diarization module.
58    /// The speaker id of the recognized text.
59    /// This will be None if the detection of the speaker is not activated.
60    pub speaker_id: Option<String>,
61}
62
63/// The confidence of the speech recognition.
64///
65/// The confidence is the confidence of the speech recognition.
66#[derive(Debug, Clone, PartialEq, Eq, Default)]
67pub enum Confidence {
68    Low,
69    Normal,
70    High,
71    #[default]
72    Unknown,
73}
74
75impl From<&str> for Confidence {
76    fn from(value: &str) -> Self {
77        value.to_string().into()
78    }
79}
80
81impl From<String> for Confidence {
82    fn from(value: String) -> Self {
83        match value.to_lowercase().as_str() {
84            "low" => Confidence::Low,
85            "normal" => Confidence::Normal,
86            "high" => Confidence::High,
87            _ => Confidence::Unknown,
88        }
89    }
90}
91
92/// Primary language
93#[derive(Debug, Clone, PartialEq, Eq)]
94pub struct PrimaryLanguage {
95    /// The language code
96    pub language: Language,
97    /// The confidence of the language detection
98    pub confidence: Confidence,
99}
100
101impl PrimaryLanguage {
102    pub(crate) fn new(language: Language, confidence: Confidence) -> Self {
103        Self {
104            language,
105            confidence,
106        }
107    }
108}