azookey conversion accuracy improvement

This commit is contained in:
mii443
2025-05-06 22:33:51 +09:00
parent b05d54f853
commit 993ebc0254
3 changed files with 67 additions and 18 deletions

16
src-tauri/Cargo.lock generated
View File

@ -1904,7 +1904,7 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e76a40b972b250590567018fd753f44adedc270994fb7be37e4d8c20764bab49" checksum = "e76a40b972b250590567018fd753f44adedc270994fb7be37e4d8c20764bab49"
dependencies = [ dependencies = [
"itertools", "itertools 0.10.5",
] ]
[[package]] [[package]]
@ -1916,6 +1916,15 @@ dependencies = [
"either", "either",
] ]
[[package]]
name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.8" version = "0.4.8"
@ -2050,7 +2059,7 @@ dependencies = [
"diff", "diff",
"ena", "ena",
"is-terminal", "is-terminal",
"itertools", "itertools 0.10.5",
"lalrpop-util", "lalrpop-util",
"petgraph", "petgraph",
"regex", "regex",
@ -4960,6 +4969,7 @@ dependencies = [
"clipboard", "clipboard",
"clipboard-master", "clipboard-master",
"ipc-channel", "ipc-channel",
"itertools 0.14.0",
"once_cell", "once_cell",
"platform-dirs", "platform-dirs",
"regex", "regex",
@ -5019,7 +5029,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a74666202acfcb4f9b995be2e3e9f7f530deb65e05a1407b8d0b30c9c451238a" checksum = "a74666202acfcb4f9b995be2e3e9f7f530deb65e05a1407b8d0b30c9c451238a"
dependencies = [ dependencies = [
"fnv", "fnv",
"itertools", "itertools 0.10.5",
"lazy_static", "lazy_static",
] ]

View File

@ -35,6 +35,7 @@ tokio = { version = "1.44.2", features = ["full"] }
ipc-channel = "0.19.0" ipc-channel = "0.19.0"
zip = "2.6.1" zip = "2.6.1"
wana_kana = "4.0.0" wana_kana = "4.0.0"
itertools = "0.14.0"
[dependencies.tracing-subscriber] [dependencies.tracing-subscriber]
version = "0.3.16" version = "0.3.16"

View File

@ -2,6 +2,7 @@ use std::{collections::HashMap, sync::LazyLock};
use azookey_binding::{Candidate, ComposingText, KanaKanjiConverter}; use azookey_binding::{Candidate, ComposingText, KanaKanjiConverter};
use ipc_channel::ipc::IpcOneShotServer; use ipc_channel::ipc::IpcOneShotServer;
use itertools::Itertools;
use platform_dirs::AppDirs; use platform_dirs::AppDirs;
use super::IpcMessage; use super::IpcMessage;
@ -61,6 +62,56 @@ impl AzookeyConversionServer {
instance 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::<Vec<_>>();
if chars.len() > 1 && chars[chars.len() - 2] != 'n' {
chars.push('n');
}
result = chars.into_iter().collect::<String>();
}
// 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<Candidate>) -> Vec<Candidate> {
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) { pub fn server_loop(mut self) {
let (a, _) = self.server.accept().unwrap(); let (a, _) = self.server.accept().unwrap();
let mut sender = None; let mut sender = None;
@ -74,16 +125,7 @@ impl AzookeyConversionServer {
self.composing_text = ComposingText::new(); self.composing_text = ComposingText::new();
} }
Ok(IpcMessage::InsertAtCursorPosition(text)) => { Ok(IpcMessage::InsertAtCursorPosition(text)) => {
let text: String = text let text = Self::pre_process_text(&text);
.chars()
.map(|c| {
if let Some(&replacement) = SIGNMAP.get(c.to_string().as_str()) {
return replacement.to_string();
} else {
c.to_string()
}
})
.collect();
self.composing_text.insert_at_cursor_position(&text); self.composing_text.insert_at_cursor_position(&text);
} }
Ok(IpcMessage::RequestCandidates(context, weight_path)) => { Ok(IpcMessage::RequestCandidates(context, weight_path)) => {
@ -99,11 +141,7 @@ impl AzookeyConversionServer {
&weight_path, &weight_path,
); );
if let Some(s) = sender.as_ref() { if let Some(s) = sender.as_ref() {
let candidates = candidates let candidates = Self::post_process_candidates(candidates);
.iter()
.take(8)
.map(|c| c.clone())
.collect::<Vec<Candidate>>();
s.send(IpcMessage::Candidates(candidates)).unwrap(); s.send(IpcMessage::Candidates(candidates)).unwrap();
} }
} }