Skip to content

Commit

Permalink
Implement Include English word feature OpenBangla/OpenBangla-Keyboa…
Browse files Browse the repository at this point in the history
  • Loading branch information
mominul committed Dec 16, 2019
1 parent 4fbb71c commit 880a18b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/phonetic/suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::cmp::Ordering;

use super::database::Database;
use crate::utility::{Utility, split_string};
use crate::settings;

pub(crate) struct PhoneticSuggestion {
pub(crate) buffer: String,
Expand Down Expand Up @@ -37,7 +38,7 @@ impl PhoneticSuggestion {
if middle.len() > 2 {
for i in 1..middle.len() {
let suffix_key = &middle[i..];

if let Some(suffix) = self.database.find_suffix(suffix_key) {
let key = &middle[0..(middle.len() - suffix_key.len())];
if let Some(cache) = self.cache.get(key) {
Expand Down Expand Up @@ -152,6 +153,11 @@ impl PhoneticSuggestion {
}
}

// Include written English word if the feature is enabled.
if settings::get_settings_phonetic_include_english() && !self.suggestions.iter().any(|i| i == term) {
self.suggestions.push(term.to_string());
}

self.suggestions.clone()
}

Expand Down Expand Up @@ -229,7 +235,26 @@ mod tests {
use rustc_hash::FxHashMap;

use super::PhoneticSuggestion;
use crate::settings::tests::set_default_phonetic;
use crate::settings::{tests::set_default_phonetic, ENV_PHONETIC_INCLUDE_ENGLISH};

//#[test] TODO: Enable this test after the environ variable data race issue is mitigated.
fn test_suggestion_with_english() {
set_default_phonetic();
std::env::set_var(ENV_PHONETIC_INCLUDE_ENGLISH, "true");

let mut suggestion = PhoneticSuggestion::default();

assert_eq!(suggestion.suggestion_with_dict(":)"), vec![":)", "ঃ)"]);
assert_eq!(suggestion.suggestion_with_dict("{a}"), vec![
"{আ}",
"{আঃ}",
"{া}",
"{এ}",
"{অ্যা}",
"{অ্যাঁ}",
"{a}"
]);
}

#[test]
fn test_suggestion_only_phonetic() {
Expand Down
7 changes: 7 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub(crate) const ENV_LAYOUT_FILE: &str = "RITI_LAYOUT_FILE";
pub(crate) const ENV_ENTER_CLOSES_PREVIEW_WIN: &str = "RITI_ENTER_CLOSES_PREVIEW_WIN";
pub(crate) const ENV_PREVIEW_WIN_HORIZONTAL: &str = "RITI_PREVIEW_WIN_HORIZONTAL";
pub(crate) const ENV_PHONETIC_DATABASE_ON: &str = "RITI_PHONETIC_DATABASE_ON";
pub(crate) const ENV_PHONETIC_INCLUDE_ENGLISH: &str = "RITI_PHONETIC_INCLUDE_ENGLISH";
pub(crate) const ENV_DATABASE_DIR: &str = "RITI_DATABASE_DIR";
pub(crate) const ENV_LAYOUT_FIXED_DATABASE_ON: &str = "RITI_LAYOUT_FIXED_DATABASE_ON";
pub(crate) const ENV_LAYOUT_FIXED_VOWEL: &str = "RITI_LAYOUT_FIXED_VOWEL";
Expand Down Expand Up @@ -33,6 +34,11 @@ pub(crate) fn get_settings_phonetic_database_on() -> bool {
var(ENV_PHONETIC_DATABASE_ON).unwrap().parse().unwrap()
}

/// Check if the including English in suggestion is on for Phonetic method.
pub(crate) fn get_settings_phonetic_include_english() -> bool {
var(ENV_PHONETIC_INCLUDE_ENGLISH).unwrap().parse().unwrap()
}

/// Get the base file path of database directory.
pub(crate) fn get_settings_database_dir() -> PathBuf {
var(ENV_DATABASE_DIR).unwrap().into()
Expand Down Expand Up @@ -105,6 +111,7 @@ pub(crate) mod tests {
set_var(ENV_ENTER_CLOSES_PREVIEW_WIN, "true");
set_var(ENV_PREVIEW_WIN_HORIZONTAL, "true");
set_var(ENV_PHONETIC_DATABASE_ON, "true");
set_var(ENV_PHONETIC_INCLUDE_ENGLISH, "false");
}

/// Sets default settings for testing Fixed Method.
Expand Down

0 comments on commit 880a18b

Please sign in to comment.