@@ -3,27 +3,42 @@ use std::io::{Read, Write};
33#[ cfg( all( not( feature = "async-mio" ) , unix) ) ]
44pub 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
1010impl 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
2422impl < 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}
0 commit comments