mirror of
https://github.com/mii443/vrclipboard-ime-gui.git
synced 2025-08-22 16:15:32 +00:00
refactor tsf conversion
This commit is contained in:
@ -28,23 +28,14 @@ impl TsfConversion {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn convert(&mut self, text: &str) -> Result<String> {
|
||||
println!();
|
||||
println!("History: {:?}, {:?}", self.conversion_history, self.clipboard_history);
|
||||
println!("{} == {}", text, self.conversion_history.last().unwrap_or(&("".to_string())).clone());
|
||||
let same_as_last_conversion = text.to_string() == self.conversion_history.last().unwrap_or(&("".to_string())).clone();
|
||||
|
||||
self.target_text = text.to_string();
|
||||
|
||||
if !same_as_last_conversion && self.now_reconvertion {
|
||||
fn reset_conversion_state(&mut self) {
|
||||
self.now_reconvertion = false;
|
||||
self.reconversion_prefix = None;
|
||||
self.reconversion_index = None;
|
||||
self.reconversion_candidates = None;
|
||||
}
|
||||
|
||||
if !self.now_reconvertion && !same_as_last_conversion {
|
||||
println!("Convert using roman_to_kanji");
|
||||
fn convert_roman_to_kanji(&mut self, text: &str) -> Result<String> {
|
||||
let o_minus_1 = self.conversion_history.get(if self.conversion_history.len() > 0 { self.conversion_history.len() - 1 } else { 0 }).unwrap_or(&("".to_string())).clone();
|
||||
let mut first_diff_position = o_minus_1.chars().zip(text.chars()).position(|(a, b)| a != b);
|
||||
|
||||
@ -60,8 +51,7 @@ impl TsfConversion {
|
||||
return Ok(self.conversion_history.last().unwrap().clone());
|
||||
}
|
||||
|
||||
if same_as_last_conversion || self.now_reconvertion {
|
||||
println!("Convert using TSF");
|
||||
fn convert_tsf(&mut self, text: &str) -> Result<String> {
|
||||
self.now_reconvertion = true;
|
||||
let mut diff_hiragana = String::new();
|
||||
if self.reconversion_prefix.is_none() {
|
||||
@ -81,16 +71,18 @@ impl TsfConversion {
|
||||
}
|
||||
println!("diff_hiragana: {}", diff_hiragana);
|
||||
|
||||
if self.reconversion_index.is_none() {
|
||||
self.reconversion_candidates = Some(self.search_candidate_provider.get_candidates(&diff_hiragana, 10)?);
|
||||
self.reconversion_index = Some(0);
|
||||
if self.reconversion_prefix.clone().unwrap() + &self.reconversion_candidates.as_ref().unwrap()[self.reconversion_index.unwrap()].clone() == text {
|
||||
self.reconversion_index = Some(self.reconversion_index.unwrap() + 1);
|
||||
}
|
||||
} else if self.reconversion_index.unwrap() + 1 < self.reconversion_candidates.as_ref().unwrap().len() {
|
||||
self.reconversion_index = Some(self.reconversion_index.unwrap() + 1);
|
||||
let candidates = self.reconversion_candidates.get_or_insert_with(|| {
|
||||
self.search_candidate_provider.get_candidates(&diff_hiragana, 10).unwrap_or_default()
|
||||
});
|
||||
|
||||
let index = self.reconversion_index.get_or_insert(0);
|
||||
|
||||
if *index == 0 && self.reconversion_prefix.as_ref().unwrap().to_owned() + &candidates[*index] == text {
|
||||
*index += 1;
|
||||
} else if *index + 1 < candidates.len() {
|
||||
*index += 1;
|
||||
} else {
|
||||
self.reconversion_index = Some(0);
|
||||
*index = 0;
|
||||
}
|
||||
|
||||
if self.reconversion_candidates.is_some() {
|
||||
@ -110,6 +102,28 @@ impl TsfConversion {
|
||||
return Ok(self.conversion_history.last().unwrap().clone());
|
||||
}
|
||||
|
||||
pub fn convert(&mut self, text: &str) -> Result<String> {
|
||||
println!();
|
||||
println!("History: {:?}, {:?}", self.conversion_history, self.clipboard_history);
|
||||
println!("{} == {}", text, self.conversion_history.last().unwrap_or(&("".to_string())).clone());
|
||||
let same_as_last_conversion = text.to_string() == self.conversion_history.last().unwrap_or(&("".to_string())).clone();
|
||||
|
||||
self.target_text = text.to_string();
|
||||
|
||||
if !same_as_last_conversion && self.now_reconvertion {
|
||||
self.reset_conversion_state();
|
||||
}
|
||||
|
||||
if !self.now_reconvertion && !same_as_last_conversion {
|
||||
println!("Convert using roman_to_kanji");
|
||||
return self.convert_roman_to_kanji(text);
|
||||
}
|
||||
|
||||
if same_as_last_conversion || self.now_reconvertion {
|
||||
println!("Convert using TSF");
|
||||
return self.convert_tsf(text);
|
||||
}
|
||||
|
||||
Err(anyhow::anyhow!("Failed to convert"))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user