Crate afrim_translator
source ·Expand description
This crate provides a range of language-related functionalities, including translation, auto-suggestions, auto-correction and more. It’s designed to enhance the language processing tasks within in input method engine.
Note: We use IndexMap
instead of HashMap
for better performance
when dealing with big datasets.
§Feature flags
To reduce the amount of compiled code in the crate, you can enable feature manually. This is
done by adding default-features = false
to your dependency specification. Below is a list of
the features available in this crate.
rhai
: Enables the usage of rhai script files.rhai-wasm
: Like rhai, but wasm compatible.strsim
: Enables the text similarity algorithm for better predictions.serde
: Enables serde feature.
§Example
use afrim_translator::{Predicate, Translator};
use indexmap::IndexMap;
// Prepares the dictionary.
let mut dictionary = IndexMap::new();
dictionary.insert("jump".to_string(), vec!["sauter".to_string()]);
dictionary.insert("jumper".to_string(), vec!["sauteur".to_string()]);
dictionary.insert("nihao".to_string(), vec!["hello".to_string()]);
// Builds the translator.
let mut translator = Translator::new(dictionary, true);
assert_eq!(
translator.translate("jump"),
vec![
Predicate {
code: "jump".to_owned(),
remaining_code: "".to_owned(),
texts: vec!["sauter".to_owned()],
can_commit: true
},
// Auto-completion.
Predicate {
code: "jumper".to_owned(),
remaining_code: "er".to_owned(),
texts: vec!["sauteur".to_owned()],
can_commit: false
}
]
);
§Example with the strsim feature
use afrim_translator::{Predicate, Translator};
use indexmap::IndexMap;
// Prepares the dictionary.
let mut dictionary = IndexMap::new();
dictionary.insert("jump".to_string(), vec!["sauter".to_string()]);
dictionary.insert("jumper".to_string(), vec!["sauteur".to_string()]);
// Builds the translator.
let mut translator = Translator::new(dictionary, true);
// Auto-suggestion / Auto-correction.
#[cfg(feature = "strsim")]
assert_eq!(
translator.translate("junp"),
vec![Predicate {
code: "jump".to_owned(),
remaining_code: "".to_owned(),
texts: vec!["sauter".to_owned()],
can_commit: false
}]
);
§Example with the rhai feature
#[cfg(feature = "rhai")]
use afrim_translator::Engine;
use afrim_translator::{Translator, Predicate};
use indexmap::IndexMap;
// Prepares the dictionary.
let mut dictionary = IndexMap::new();
dictionary.insert("jump".to_string(), vec!["sauter".to_string()]);
dictionary.insert("jumper".to_string(), vec!["sauteur".to_string()]);
// Prepares the script.
#[cfg(feature = "rhai")]
let engine = Engine::new();
#[cfg(feature = "rhai")]
let jump_translator = engine.compile(r#"
// The main script function.
fn translate(input) {
if input == "jump" {
[input, "", "\n", false]
}
}
"#).unwrap();
// Builds the translator.
let mut translator = Translator::new(dictionary, true);
// Registers the jump translator.
#[cfg(feature = "rhai")]
translator.register("jump".to_string(), jump_translator);
assert_eq!(
translator.translate("jump"),
vec![
Predicate {
code: "jump".to_owned(),
remaining_code: "".to_owned(),
texts: vec!["sauter".to_owned()],
can_commit: true
},
#[cfg(feature = "rhai")]
// Programmable translation.
Predicate {
code: "jump".to_owned(),
remaining_code: "".to_owned(),
texts: vec!["\n".to_owned()],
can_commit: false
},
// Auto-completion.
Predicate {
code: "jumper".to_owned(),
remaining_code: "er".to_owned(),
texts: vec!["sauteur".to_owned()],
can_commit: false
}
]
);
Structs§
- Rhai main scripting engine.
- Struct representing the predicate.
- Core structure of the translator.