-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #608 from chewing/refactor-input-mode
refactor: Merge fuzzy_search_mode and conversion_engine options
- Loading branch information
Showing
17 changed files
with
424 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use crate::dictionary::LookupStrategy; | ||
|
||
use super::{ChewingEngine, ConversionEngine}; | ||
|
||
/// TODO: doc | ||
#[derive(Debug, Default)] | ||
pub struct FuzzyChewingEngine { | ||
inner: ChewingEngine, | ||
} | ||
|
||
impl FuzzyChewingEngine { | ||
pub fn new() -> FuzzyChewingEngine { | ||
FuzzyChewingEngine { | ||
inner: ChewingEngine { | ||
lookup_strategy: LookupStrategy::FuzzyPartialPrefix, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl ConversionEngine for FuzzyChewingEngine { | ||
fn convert<'a>( | ||
&'a self, | ||
dict: &'a dyn crate::dictionary::Dictionary, | ||
comp: &'a super::Composition, | ||
) -> Box<dyn Iterator<Item = Vec<super::Interval>> + 'a> { | ||
Box::new(ChewingEngine::convert(&self.inner, dict, comp)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::iter; | ||
|
||
use crate::dictionary::Dictionary; | ||
|
||
use super::{Composition, ConversionEngine, Interval}; | ||
|
||
/// Simple engine does not perform any intelligent conversion. | ||
#[derive(Debug, Default)] | ||
pub struct SimpleEngine; | ||
|
||
impl SimpleEngine { | ||
pub fn new() -> SimpleEngine { | ||
SimpleEngine | ||
} | ||
pub fn convert<'a>( | ||
&'a self, | ||
dict: &'a dyn Dictionary, | ||
comp: &'a Composition, | ||
) -> impl Iterator<Item = Vec<Interval>> + Clone + 'a { | ||
let mut intervals = vec![]; | ||
|
||
for (i, sym) in comp.symbols().iter().enumerate() { | ||
if comp | ||
.selections | ||
.iter() | ||
.any(|selection| selection.intersect_range(i, i + 1)) | ||
{ | ||
continue; | ||
} | ||
if sym.is_char() { | ||
intervals.push(Interval { | ||
start: i, | ||
end: i + 1, | ||
is_phrase: false, | ||
str: sym.to_char().unwrap().to_string().into_boxed_str(), | ||
}); | ||
} else { | ||
let phrase = dict.lookup_first_phrase( | ||
&[sym.to_syllable().unwrap()], | ||
crate::dictionary::LookupStrategy::Standard, | ||
); | ||
let phrase_str = phrase.map_or_else( | ||
|| sym.to_syllable().unwrap().to_string(), | ||
|phrase| phrase.to_string(), | ||
); | ||
intervals.push(Interval { | ||
start: i, | ||
end: i + 1, | ||
is_phrase: true, | ||
str: phrase_str.into_boxed_str(), | ||
}) | ||
} | ||
} | ||
intervals.extend_from_slice(comp.selections()); | ||
intervals.sort_by_key(|int| int.start); | ||
iter::once(intervals) | ||
} | ||
} | ||
|
||
impl ConversionEngine for SimpleEngine { | ||
fn convert<'a>( | ||
&'a self, | ||
dict: &'a dyn Dictionary, | ||
comp: &'a Composition, | ||
) -> Box<dyn Iterator<Item = Vec<Interval>> + 'a> { | ||
Box::new(SimpleEngine::convert(self, dict, comp)) | ||
} | ||
} |
Oops, something went wrong.