Skip to content

Commit f57969f

Browse files
committed
fix(dict): use max freq from all dictionaries as phrase freq
1 parent 702d374 commit f57969f

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ What's New in libchewing (unreleased)
1414
* Bug Fixes
1515
- dict: fixed parsing trie dictionary file with extension fields.
1616
- dict: fixed trie_buf tombstone is not cleared after adding phrase again.
17+
- dict: use max freq of all dictionaries as phrase frequency to avoid repeatedly
18+
adding static dictionary freq to user dictionary freq.
1719

1820
* Changes
1921
- rust: breaking! renamed SystemDictionaryLoader to AssetLoader.

src/dictionary/layered.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::{
3333
/// assert_eq!(
3434
/// [
3535
/// ("側", 1, 0).into(),
36-
/// ("冊", 101, 0).into(),
36+
/// ("冊", 100, 0).into(),
3737
/// ("測", 1, 0).into(),
3838
/// ("策", 100, 0).into(),
3939
/// ]
@@ -105,7 +105,7 @@ impl Dictionary for Layered {
105105
/// dictionaries.
106106
///
107107
/// When a phrase appears in multiple dictionaries, the final
108-
/// frequency is the sum of all frequency in all dictionaries.
108+
/// frequency is the max of all frequency in all dictionaries.
109109
///
110110
/// Pseudo code
111111
///
@@ -114,7 +114,7 @@ impl Dictionary for Layered {
114114
/// Foreach d in d_layers
115115
/// Foreach phrase, freq in d.lookup_syllables()
116116
/// If phrase in phrases
117-
/// Set phrases[phrase].freq += freq
117+
/// Set phrases[phrase].freq = max(phrases[phrase].freq, freq)
118118
/// Else
119119
/// Add phrases <- (phrase, freq)
120120
/// ```
@@ -128,7 +128,7 @@ impl Dictionary for Layered {
128128
match sort_map.entry(phrase.to_string()) {
129129
Entry::Occupied(entry) => {
130130
let index = *entry.get();
131-
phrases[index].freq += phrase.freq;
131+
phrases[index].freq = phrase.freq.max(phrases[index].freq);
132132
phrases[index].last_used =
133133
match (phrases[index].last_used, phrase.last_used) {
134134
(Some(orig), Some(new)) => Some(u64::max(orig, new)),
@@ -326,7 +326,7 @@ mod tests {
326326
assert_eq!(
327327
[
328328
("側", 1, 0).into(),
329-
("冊", 101, 0).into(),
329+
("冊", 100, 0).into(),
330330
("測", 1, 0).into(),
331331
("策", 100, 0).into(),
332332
]
@@ -375,7 +375,7 @@ mod tests {
375375
assert_eq!(
376376
[
377377
("側", 1, 0).into(),
378-
("冊", 101, 0).into(),
378+
("冊", 100, 0).into(),
379379
("測", 1, 0).into(),
380380
("策", 100, 0).into(),
381381
]

0 commit comments

Comments
 (0)