Skip to content

Commit

Permalink
Add DetectLanguage enum
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienDeepgram committed Jul 17, 2024
1 parent d5b50c9 commit caf9dbb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
1 change: 1 addition & 0 deletions examples/transcription/live/simple_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async fn main() -> Result<(), DeepgramError> {
.encoding(Encoding::Linear16)
.sample_rate(44100)
.channels(2)
// .endpointing(Endpointing::Enabled(true))
.endpointing(Endpointing::CustomValue(300))
.interim_results(true)
.utterance_end_ms(1000)
Expand Down
6 changes: 4 additions & 2 deletions examples/transcription/prerecorded/prerecorded_from_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::env;
use deepgram::{
common::{
audio_source::AudioSource,
options::{CustomIntentMode, Encoding, Extra, Language, Model, Options, Redact, Summarize},
options::{CustomIntentMode, DetectLanguage, Encoding, Extra, Language, Model, Options, Redact, Summarize},
},
Deepgram, DeepgramError,
};
Expand All @@ -24,7 +24,8 @@ async fn main() -> Result<(), DeepgramError> {
.punctuate(true)
.paragraphs(true)
.redact([Redact::Pci, Redact::Other(String::from("cvv"))])
.detect_language(true)
// .detect_language(DetectLanguage::Enabled(true))
.detect_language(DetectLanguage::Restricted(vec![Language::en, Language::es]))
.diarize(true)
.diarize_version("2021-07-14.0")
.filler_words(true)
Expand All @@ -39,6 +40,7 @@ async fn main() -> Result<(), DeepgramError> {
.topics(true)
.custom_intent_mode(CustomIntentMode::Strict)
.custom_intents(["Get support", "Complain"])
// .summarize(Summarize::Enabled(true))
.summarize(Summarize::V2)
.dictation(true)
.measurements(true)
Expand Down
48 changes: 41 additions & 7 deletions src/common/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Options {
keyword_boost_legacy: Option<bool>,
utterances: Option<Utterances>,
tags: Vec<String>,
detect_language: Option<bool>,
detect_language: Option<DetectLanguage>,
query_params: Vec<(String, String)>,
encoding: Option<Encoding>,
smart_format: Option<bool>,
Expand All @@ -48,6 +48,39 @@ pub struct Options {
callback_method: Option<CallbackMethod>,
}

/// Detect Language value
///
/// See the [Deepgram Detect Language feature docs][docs] for more info.
///
/// [docs]: https://developers.deepgram.com/docs/language-detection
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub enum DetectLanguage {
#[allow(missing_docs)]
Enabled(bool),

#[allow(missing_docs)]
Restricted(Vec<Language>)
}

/// DetectLanguage Impl
impl DetectLanguage {
pub(crate) fn to_string(&self) -> String {
match self {
DetectLanguage::Enabled(value) => {
if *value {
"true".to_string()
} else {
"false".to_string()
}
},
DetectLanguage::Restricted(languages) => {
let languages_str: Vec<String> = languages.iter().map(|lang| lang.as_ref().to_string()).collect();
languages_str.join(",")
},
}
}
}

/// Callback Method value
///
/// See the [Deepgram Callback Method feature docs][docs] for more info.
Expand Down Expand Up @@ -131,7 +164,7 @@ pub enum Endpointing {
/// Endpointing impl
impl Endpointing {
#[allow(missing_docs)]
pub fn as_str(&self) -> String {
pub fn to_string(&self) -> String {
match self {
Endpointing::Enabled(value) => {
if *value {
Expand Down Expand Up @@ -1497,10 +1530,10 @@ impl OptionsBuilder {
/// # use deepgram::common::options::Options;
/// #
/// let options = Options::builder()
/// .detect_language(true)
/// .detect_language(DetectLanguage::Enabled(true))
/// .build();
/// ```
pub fn detect_language(mut self, detect_language: bool) -> Self {
pub fn detect_language(mut self, detect_language: DetectLanguage) -> Self {
self.0.detect_language = Some(detect_language);

self
Expand Down Expand Up @@ -2098,7 +2131,7 @@ impl Serialize for SerializableOptions<'_> {
}

if let Some(detect_language) = detect_language {
seq.serialize_element(&("detect_language", detect_language))?;
seq.serialize_element(&("detect_language", detect_language.to_string()))?;
}

for (param, value) in query_params {
Expand Down Expand Up @@ -2356,6 +2389,7 @@ mod serialize_options_tests {
use super::CallbackMethod;
use super::CustomIntentMode;
use super::CustomTopicMode;
use super::DetectLanguage;
use super::Encoding;
use super::Extra;
use super::Keyword;
Expand Down Expand Up @@ -2760,12 +2794,12 @@ mod serialize_options_tests {
#[test]
fn detect_language() {
check_serialization(
&Options::builder().detect_language(false).build(),
&Options::builder().detect_language(DetectLanguage::Enabled(false)).build(),
"detect_language=false",
);

check_serialization(
&Options::builder().detect_language(true).build(),
&Options::builder().detect_language(DetectLanguage::Enabled(true)).build(),
"detect_language=true",
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/listen/live.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ where
pairs.append_pair("channels", &channels.to_string());
}
if let Some(endpointing) = self.endpointing {
pairs.append_pair("endpointing", &endpointing.as_str());
pairs.append_pair("endpointing", &endpointing.to_string());
}
if let Some(utterance_end_ms) = self.utterance_end_ms {
pairs.append_pair("utterance_end_ms", &utterance_end_ms.to_string());
Expand Down

0 comments on commit caf9dbb

Please sign in to comment.