coqui_stt/
candidate_transcript.rs

1use crate::{OwnedTokenMetadata, TokenMetadata};
2use std::fmt::{Display, Formatter, Write};
3
4/// A single transcript computed by the model,
5/// including a confidence value and the metadata for its constituent tokens.
6#[repr(transparent)]
7pub struct CandidateTranscript(coqui_stt_sys::CandidateTranscript);
8
9unsafe impl Send for CandidateTranscript {}
10unsafe impl Sync for CandidateTranscript {}
11
12impl CandidateTranscript {
13    /// Return an array of tokens in this transcript.
14    #[inline]
15    #[must_use]
16    pub fn tokens(&self) -> &[TokenMetadata] {
17        let data = self.0.tokens.cast();
18        let len = self.num_tokens() as usize;
19
20        // SAFETY: the inner objects will always be of type TokenMetadata,
21        // and the length will always be proper
22        unsafe { std::slice::from_raw_parts(data, len) }
23    }
24
25    /// Approximated confidence value for this transcript.
26    /// This is roughly the sum of the acoustic model logit values for
27    /// each timestep/character that contributed to the creation of this transcript.
28    #[inline]
29    #[must_use]
30    pub const fn confidence(&self) -> f64 {
31        self.0.confidence
32    }
33
34    /// Total number of tokens in this transcript.
35    #[inline]
36    #[must_use]
37    pub const fn num_tokens(&self) -> u32 {
38        self.0.num_tokens
39    }
40
41    /// Convert this into an [`OwnedCandidateTranscript`](OwnedCandidateTranscript) struct.
42    ///
43    /// **Warning**: this can be very expensive depending on the total number of tokens in this object.
44    #[inline]
45    #[must_use]
46    pub fn to_owned(&self) -> OwnedCandidateTranscript {
47        let tokens = self.tokens().iter().map(TokenMetadata::to_owned).collect();
48        OwnedCandidateTranscript {
49            tokens,
50            confidence: self.confidence(),
51        }
52    }
53}
54
55/// An owned variant of [`CandidateTranscript`](CandidateTranscript).
56#[derive(Clone, Debug)]
57pub struct OwnedCandidateTranscript {
58    tokens: Vec<OwnedTokenMetadata>,
59    confidence: f64,
60}
61
62impl OwnedCandidateTranscript {
63    /// Return an array of tokens in this transcript.
64    #[inline]
65    #[must_use]
66    pub fn tokens(&self) -> &[OwnedTokenMetadata] {
67        &self.tokens[..]
68    }
69
70    /// Approximated confidence value for this transcript.
71    /// This is roughly the sum of the acoustic model logit values for
72    /// each timestep/character that contributed to the creation of this transcript.
73    #[inline]
74    #[must_use]
75    pub const fn confidence(&self) -> f64 {
76        self.confidence
77    }
78
79    /// Total number of tokens in this transcript.
80    #[inline]
81    #[must_use]
82    pub fn num_tokens(&self) -> usize {
83        self.tokens.len()
84    }
85}
86
87impl Display for OwnedCandidateTranscript {
88    #[inline]
89    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
90        for token in &self.tokens {
91            f.write_str(&token.text)?;
92            f.write_char(' ')?;
93        }
94        Ok(())
95    }
96}