-
Notifications
You must be signed in to change notification settings - Fork 1
Begin automatic language detection support #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
C-Loftus
wants to merge
6
commits into
odilia-app:main
Choose a base branch
from
C-Loftus:lingua
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9aedc9c
Begin automatic language detection support
C-Loftus b5c1af0
fix formatting
C-Loftus 6360e3d
work on getting example work; still broken but progress
C-Loftus a7b7f0b
fix fmt
C-Loftus 8f613a3
example working
C-Loftus c668df1
backup
C-Loftus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
ssip-client-async/examples/automatic_language_detection.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| use lingua::DetectionResult; | ||
| use lingua::Language::{English, Spanish, Chinese}; | ||
| use lingua::LanguageDetectorBuilder; | ||
|
|
||
| fn main() { | ||
| let languages = vec![English, Chinese]; | ||
| let detector = LanguageDetectorBuilder::from_languages(&languages).build(); | ||
| let sentence = "Hello my name is Joe. 你好世界"; | ||
|
|
||
| let results: Vec<DetectionResult> = detector.detect_multiple_languages_of(sentence); | ||
|
|
||
| println!("{:?}", results); | ||
|
|
||
| if let [first, second] = &results[..] { | ||
| assert_eq!(first.language(), English); | ||
| assert_eq!( | ||
| &sentence[first.start_index()..first.end_index()], | ||
| "Hello my name is Joe." | ||
| ); | ||
|
|
||
| assert_eq!(second.language(), Spanish); | ||
| assert_eq!( | ||
| &sentence[second.start_index()..second.end_index()], | ||
| "你好世界" | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| use std::io::{Read, Write}; | ||
| // Trick to have common implementation for std and mio streams.. | ||
| #[cfg(all(not(feature = "async-mio"), unix))] | ||
| pub use std::os::unix::io::AsRawFd as Source; | ||
|
|
||
| use crate::{fifo::Builder, Client, OK_LANGUAGE_SET}; | ||
| use lingua::IsoCode639_1; | ||
| use ssip::{ClientError, ClientResult}; | ||
|
|
||
| impl Builder { | ||
| /// Initialize the language detection model with a list of languages to distinguish between | ||
| /// Use the ISO 639-3 language codes to distinguish between languages | ||
| pub fn with_automatic_detection_languages( | ||
| &mut self, | ||
| languages: &Vec<IsoCode639_1>, | ||
| ) -> &mut Self { | ||
| self.languages_to_detect = Some(languages.clone()); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl<S: Read + Write + Source> Client<S> { | ||
| /// A wrapper over the `send_lines` method to send lines in multiple languages. Uses whatever languages were set when the client was built | ||
| pub fn send_lines_multilingual(&mut self, lines: &String) -> ClientResult<&mut Self> { | ||
| let detector = | ||
| self.language_detector | ||
| .as_ref() | ||
| .ok_or(ClientError::LanguageDetectionError( | ||
| "Language detection not initialized".to_string(), | ||
| ))?; | ||
|
|
||
| let detection_results = detector.detect_multiple_languages_of(lines); | ||
|
|
||
| for result in detection_results { | ||
| let language_code = result.language().iso_code_639_1().to_string(); | ||
| // the status check stalls for some reason and never returns | ||
| self.set_language(ssip::ClientScope::Current, &language_code)? | ||
| .check_status(OK_LANGUAGE_SET)?; | ||
| let subsection = lines[result.start_index()..result.end_index()].to_string(); | ||
| self.speak()? | ||
| .check_receiving_data()? | ||
| .send_lines(&[subsection])? | ||
| .receive()?; | ||
| } | ||
|
|
||
| Ok(self) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.