7 releases
0.2.1 | Apr 28, 2024 |
---|---|
0.2.0 |
|
0.1.4 | Mar 5, 2024 |
0.1.3 | Jan 31, 2024 |
0.0.2 | Oct 24, 2023 |
#1267 in Text processing
Used in 2 crates
(via afrim)
20KB
193 lines
Afrim Translator
Enhance the language processing tasks within in input method engine.
Features
- rhai: Enables the usage of the rhai scripting language.
- rhai-wasm: Like rhai, but wasm compatible.
- strsim: Enables the text similarity algorithm for better predictions.
- serde: Enables the serialization/deseroalization.
lib.rs
:
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
}
]
);
Dependencies
~5–8MB
~139K SLoC