Skip to content

Commit 6360e3d

Browse files
committed
work on getting example work; still broken but progress
1 parent b5c1af0 commit 6360e3d

File tree

6 files changed

+86
-24
lines changed

6 files changed

+86
-24
lines changed

ssip-client-async/examples/automatic_language_detection.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@ use ssip_client_async::{fifo, ClientResult};
33

44
#[cfg(all(unix, not(feature = "async-mio")))]
55
fn main() -> ClientResult<()> {
6-
// spawn the speech-dispatcher daemon before creating the client
7-
// and trying to connect to the speech-dispatcher socket
8-
let languages = vec![lingua::IsoCode639_3::ENG, lingua::IsoCode639_3::SPA];
6+
use ssip::ClientName;
7+
8+
let languages = vec![lingua::IsoCode639_1::EN, lingua::IsoCode639_1::RU];
99
let mut client = fifo::Builder::new()
10-
.with_language_detection(&languages)
10+
// initialize the language detection model with a list of languages to distinguish between
11+
.with_automatic_detection_languages(&languages)
12+
// spawn the speech-dispatcher daemon before creating the client
13+
// and trying to connect to the speech-dispatcher socket
1114
.with_spawn()?
1215
.build()?;
1316

1417
client
18+
.set_client_name(ClientName::new("sasha", "example_app"))?
19+
.check_client_name_set()?
1520
.speak()?
1621
.check_receiving_data()?
17-
.send_line("test message for the spawn example")?
18-
.receive_message_id()?;
22+
.send_lines_multilingual(
23+
&"Hello, my name is Joe. меня зовут джо".to_string(),
24+
)?;
1925

2026
client.quit()?.receive()?;
2127
Ok(())

ssip-client-async/src/client.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::types::*;
2020
#[cfg(all(not(feature = "async-mio"), unix))]
2121
pub use std::os::unix::io::AsRawFd as Source;
2222

23+
use lingua::LanguageDetector;
2324
#[cfg(feature = "async-mio")]
2425
pub use mio::event::Source;
2526

@@ -69,13 +70,22 @@ macro_rules! send_range {
6970
pub struct Client<S: Read + Write + Source> {
7071
input: io::BufReader<S>,
7172
output: io::BufWriter<S>,
73+
pub language_detector: Option<LanguageDetector>,
7274
}
7375

7476
impl<S: Read + Write + Source> Client<S> {
7577
/// Create a SSIP client on the reader and writer.
76-
pub(crate) fn new(input: io::BufReader<S>, output: io::BufWriter<S>) -> Self {
78+
pub(crate) fn new(
79+
input: io::BufReader<S>,
80+
output: io::BufWriter<S>,
81+
language_detector: Option<LanguageDetector>,
82+
) -> Self {
7783
// https://stackoverflow.com/questions/58467659/how-to-store-tcpstream-with-bufreader-and-bufwriter-in-a-data-structure
78-
Self { input, output }
84+
Self {
85+
input,
86+
output,
87+
language_detector,
88+
}
7989
}
8090

8191
#[cfg(all(not(feature = "async-mio"), unix))]

ssip-client-async/src/fifo.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ mod synchronous {
6666
pub struct Builder {
6767
path: FifoPath,
6868
mode: StreamMode,
69-
pub language_detector_model: Option<lingua::LanguageDetector>,
69+
pub languages_to_detect: Option<Vec<lingua::IsoCode639_1>>,
7070
}
7171

7272
impl Builder {
7373
pub fn new() -> Self {
7474
Self {
7575
path: FifoPath::new(),
7676
mode: StreamMode::Blocking,
77-
language_detector_model: None,
77+
languages_to_detect: None,
7878
}
7979
}
8080

@@ -105,6 +105,17 @@ mod synchronous {
105105
Ok(self)
106106
}
107107

108+
fn init_language_detector(
109+
languages: &Vec<lingua::IsoCode639_1>,
110+
) -> Option<lingua::LanguageDetector> {
111+
Some(
112+
lingua::LanguageDetectorBuilder::from_iso_codes_639_1(languages)
113+
// preload all language models into memory for faster client detection
114+
.with_preloaded_language_models()
115+
.build(),
116+
)
117+
}
118+
108119
pub fn build(&self) -> io::Result<Client<UnixStream>> {
109120
let input = UnixStream::connect(self.path.get()?)?;
110121
match self.mode {
@@ -114,7 +125,21 @@ mod synchronous {
114125
}
115126

116127
let output = input.try_clone()?;
117-
Ok(Client::new(BufReader::new(input), BufWriter::new(output)))
128+
match &self.languages_to_detect {
129+
Some(languages) => {
130+
let language_detector = Self::init_language_detector(&languages).unwrap();
131+
Ok(Client::new(
132+
BufReader::new(input),
133+
BufWriter::new(output),
134+
Some(language_detector),
135+
))
136+
}
137+
None => Ok(Client::new(
138+
BufReader::new(input),
139+
BufWriter::new(output),
140+
None,
141+
)),
142+
}
118143
}
119144
}
120145
}

ssip-client-async/src/language_detection.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,42 @@ use std::io::{Read, Write};
33
#[cfg(all(not(feature = "async-mio"), unix))]
44
pub use std::os::unix::io::AsRawFd as Source;
55

6-
use crate::{fifo::Builder, Client};
7-
use lingua::IsoCode639_3;
8-
use ssip::ClientResult;
6+
use crate::{fifo::Builder, Client, OK_LANGUAGE_SET};
7+
use lingua::IsoCode639_1;
8+
use ssip::{ClientError, ClientResult};
99

1010
impl Builder {
1111
/// Initialize the language detection model with a list of languages to distinguish between
1212
/// Use the ISO 639-3 language codes to distinguish between languages
13-
pub fn with_language_detection(&mut self, languages: &Vec<IsoCode639_3>) -> &mut Self {
14-
self.language_detector_model = Some(
15-
lingua::LanguageDetectorBuilder::from_iso_codes_639_3(languages)
16-
// preload all language models into memory for faster client detection
17-
.with_preloaded_language_models()
18-
.build(),
19-
);
13+
pub fn with_automatic_detection_languages(
14+
&mut self,
15+
languages: &Vec<IsoCode639_1>,
16+
) -> &mut Self {
17+
self.languages_to_detect = Some(languages.clone());
2018
self
2119
}
2220
}
2321

2422
impl<S: Read + Write + Source> Client<S> {
2523
/// A wrapper over the `send_lines` method to send lines in multiple languages
26-
pub fn send_lines_multilingual(&mut self, lines: &[String]) -> ClientResult<()> {
27-
Ok(())
24+
pub fn send_lines_multilingual(&mut self, lines: &String) -> ClientResult<&mut Self> {
25+
let detector =
26+
self.language_detector
27+
.as_ref()
28+
.ok_or(ClientError::LanguageDetectionError(
29+
"Language detection not initialized".to_string(),
30+
))?;
31+
32+
let detection_results = detector.detect_multiple_languages_of(lines);
33+
34+
for result in detection_results {
35+
let language_code = result.language().iso_code_639_1().to_string();
36+
// the status check stalls for some reason and never returns
37+
self.set_language(ssip::ClientScope::Current, &language_code)?.check_status(OK_LANGUAGE_SET)?;
38+
let subsection = lines[result.start_index()..result.end_index()].to_string();
39+
self.send_lines(&[subsection])?.receive()?;
40+
}
41+
42+
Ok(self)
2843
}
2944
}

ssip-client-async/src/tcp.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ mod synchronous {
5858
StreamMode::TimeOut(timeout) => input.set_read_timeout(Some(timeout))?,
5959
}
6060
let output = input.try_clone()?;
61-
Ok(Client::new(BufReader::new(input), BufWriter::new(output)))
61+
Ok(Client::new(
62+
BufReader::new(input),
63+
BufWriter::new(output),
64+
None,
65+
))
6266
}
6367
}
6468
}

ssip/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ pub enum ClientError {
417417
TooManyLines,
418418
#[error("Unexpected status: {0}")]
419419
UnexpectedStatus(ReturnCode),
420+
#[error("Failure automatically detecting language: {0}")]
421+
LanguageDetectionError(String),
420422
}
421423

422424
impl ClientError {

0 commit comments

Comments
 (0)