Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ What's New in libchewing (unreleased)

* Bug Fixes
- dict: fixed parsing trie dictionary file with extension fields.
- dict: fixed trie_buf tombstone is not cleared after adding phrase again.

* Changes
- rust: breaking! renamed SystemDictionaryLoader to AssetLoader.
Expand Down
65 changes: 64 additions & 1 deletion src/dictionary/trie_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ impl TrieBuf {
warn!("phrase {} {syllables:?} already exist", phrase.text);
return Ok(());
}

self.graveyard.remove(&(
Cow::from(syllables.to_vec()),
Cow::from(phrase.text.to_string()),
));
debug!("added phrase {} {syllables:?}", phrase.text);
self.btree.insert(
(
Expand All @@ -193,6 +196,10 @@ impl TrieBuf {
user_freq: u32,
time: u64,
) -> Result<(), UpdateDictionaryError> {
self.graveyard.remove(&(
Cow::from(syllables.to_vec()),
Cow::from(phrase.text.to_string()),
));
debug!("updated phrase {} {syllables:?}", phrase.text);
self.btree.insert(
(
Expand Down Expand Up @@ -455,6 +462,62 @@ mod tests {
Ok(())
}

#[test]
fn remove_then_add_phrase() -> Result<(), Box<dyn Error>> {
let tmp_dir = tempfile::tempdir()?;
let file_path = tmp_dir.path().join("user.dat");
let mut dict = TrieBuf::open(file_path)?;
dict.add_phrase(
&[syl![Z, TONE4], syl![D, I, AN, TONE3]],
("dict", 1, 2).into(),
)?;
dict.remove_phrase(&[syl![Z, TONE4], syl![D, I, AN, TONE3]], "dict")?;
dict.add_phrase(
&[syl![Z, TONE4], syl![D, I, AN, TONE3]],
("dict", 1, 2).into(),
)?;
assert_eq!(
Some(("dict", 1, 2).into()),
dict.lookup(
&[syl![Z, TONE4], syl![D, I, AN, TONE3]],
LookupStrategy::Standard
)
.first()
.cloned(),
"remove and add phrase again should cancel out"
);
Ok(())
}

#[test]
fn remove_then_update_phrase() -> Result<(), Box<dyn Error>> {
let tmp_dir = tempfile::tempdir()?;
let file_path = tmp_dir.path().join("user.dat");
let mut dict = TrieBuf::open(file_path)?;
dict.add_phrase(
&[syl![Z, TONE4], syl![D, I, AN, TONE3]],
("dict", 1, 2).into(),
)?;
dict.remove_phrase(&[syl![Z, TONE4], syl![D, I, AN, TONE3]], "dict")?;
dict.update_phrase(
&[syl![Z, TONE4], syl![D, I, AN, TONE3]],
("dict", 1, 2).into(),
0,
0,
)?;
assert_eq!(
Some(("dict", 0, 0).into()),
dict.lookup(
&[syl![Z, TONE4], syl![D, I, AN, TONE3]],
LookupStrategy::Standard
)
.first()
.cloned(),
"remove and update phrase again should cancel out"
);
Ok(())
}

#[test]
fn create_new_dictionary_and_query() -> Result<(), Box<dyn Error>> {
let tmp_dir = tempfile::tempdir()?;
Expand Down
Loading