#prediction #ime #autocomplete #predication

afrim-translator

Manage the predication system of the afrim input method

6 releases

0.1.4 Mar 5, 2024
0.1.3 Jan 31, 2024
0.1.2 Nov 11, 2023
0.1.0 Oct 30, 2023
0.0.2 Oct 24, 2023

#991 in Text processing

Download history 14/week @ 2024-01-29 16/week @ 2024-02-19 15/week @ 2024-02-26 155/week @ 2024-03-04 49/week @ 2024-03-11 2/week @ 2024-03-25 22/week @ 2024-04-01

80 downloads per month
Used in 2 crates (via afrim)

MIT license

18KB
170 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::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![
        (
            "jump".to_owned(),
            "".to_owned(),
            vec!["sauter".to_owned()],
            true
        ),
        // Auto-completion.
        (
            "jumper".to_owned(),
            "er".to_owned(),
            vec!["sauteur".to_owned()],
            false
        )
    ]
);

Example with the strsim feature

use afrim_translator::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![(
        "jump".to_owned(),
        "".to_owned(),
        vec!["sauter".to_owned()],
        false
    )]
);

Example with the rhai feature

#[cfg(feature = "rhai")]
use afrim_translator::Engine;
use afrim_translator::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()]);

// 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![
        (
            "jump".to_owned(),
            "".to_owned(),
            vec!["sauter".to_owned()],
            true
        ),
        #[cfg(feature = "rhai")]
        // Programmable translation.
        (
            "jump".to_owned(),
            "".to_owned(),
            vec!["\n".to_owned()],
            false
        ),
        // Auto-completion.
        (
            "jumper".to_owned(),
            "er".to_owned(),
            vec!["sauteur".to_owned()],
            false
        )
    ]
);

Dependencies

~4.5–6.5MB
~121K SLoC