diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 294a72a..b2ae445 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1904,7 +1904,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e76a40b972b250590567018fd753f44adedc270994fb7be37e4d8c20764bab49" dependencies = [ - "itertools", + "itertools 0.10.5", ] [[package]] @@ -1916,6 +1916,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "0.4.8" @@ -2050,7 +2059,7 @@ dependencies = [ "diff", "ena", "is-terminal", - "itertools", + "itertools 0.10.5", "lalrpop-util", "petgraph", "regex", @@ -4960,6 +4969,7 @@ dependencies = [ "clipboard", "clipboard-master", "ipc-channel", + "itertools 0.14.0", "once_cell", "platform-dirs", "regex", @@ -5019,7 +5029,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a74666202acfcb4f9b995be2e3e9f7f530deb65e05a1407b8d0b30c9c451238a" dependencies = [ "fnv", - "itertools", + "itertools 0.10.5", "lazy_static", ] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 05ea768..4f28d27 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -35,6 +35,7 @@ tokio = { version = "1.44.2", features = ["full"] } ipc-channel = "0.19.0" zip = "2.6.1" wana_kana = "4.0.0" +itertools = "0.14.0" [dependencies.tracing-subscriber] version = "0.3.16" diff --git a/src-tauri/src/azookey/server.rs b/src-tauri/src/azookey/server.rs index ec3f1cb..0a5effc 100644 --- a/src-tauri/src/azookey/server.rs +++ b/src-tauri/src/azookey/server.rs @@ -2,6 +2,7 @@ use std::{collections::HashMap, sync::LazyLock}; use azookey_binding::{Candidate, ComposingText, KanaKanjiConverter}; use ipc_channel::ipc::IpcOneShotServer; +use itertools::Itertools; use platform_dirs::AppDirs; use super::IpcMessage; @@ -61,6 +62,56 @@ impl AzookeyConversionServer { instance } + fn pre_process_text(text: &str) -> String { + let mut result = String::new(); + + // replace all characters in the text with their corresponding replacements + for c in text.chars() { + if let Some(&replacement) = SIGNMAP.get(c.to_string().as_str()) { + result.push_str(replacement); + } else { + result.push(c); + } + } + + // push 'n' if the last and second last characters are 'n' + if result.ends_with('n') { + let mut chars = result.chars().collect::>(); + if chars.len() > 1 && chars[chars.len() - 2] != 'n' { + chars.push('n'); + } + result = chars.into_iter().collect::(); + } + + // push '§' at the end of the string to avoid unnecessary prediction + result.push_str("§"); + + result + } + + fn post_process_text(text: &str) -> String { + let mut result = text.to_string(); + + if result.ends_with('§') { + result.pop(); + } + + result + } + + fn post_process_candidates(candidates: Vec) -> Vec { + candidates + .iter() + .take(8) + .map(|c| { + let mut candidate = c.clone(); + candidate.text = Self::post_process_text(&candidate.text); + candidate + }) + .unique_by(|c| c.text.clone()) + .collect() + } + pub fn server_loop(mut self) { let (a, _) = self.server.accept().unwrap(); let mut sender = None; @@ -74,16 +125,7 @@ impl AzookeyConversionServer { self.composing_text = ComposingText::new(); } Ok(IpcMessage::InsertAtCursorPosition(text)) => { - let text: String = text - .chars() - .map(|c| { - if let Some(&replacement) = SIGNMAP.get(c.to_string().as_str()) { - return replacement.to_string(); - } else { - c.to_string() - } - }) - .collect(); + let text = Self::pre_process_text(&text); self.composing_text.insert_at_cursor_position(&text); } Ok(IpcMessage::RequestCandidates(context, weight_path)) => { @@ -99,11 +141,7 @@ impl AzookeyConversionServer { &weight_path, ); if let Some(s) = sender.as_ref() { - let candidates = candidates - .iter() - .take(8) - .map(|c| c.clone()) - .collect::>(); + let candidates = Self::post_process_candidates(candidates); s.send(IpcMessage::Candidates(candidates)).unwrap(); } }