Skip to content

Commit

Permalink
Sort results by frequency
Browse files Browse the repository at this point in the history
This kind of solves the original issue, but there is still a problem
where these frequencies are by _entry_, not by _meaning_. This leads to
problems where 青 (rather than 緑) is the leading entry for "green".

Fixes #14.
  • Loading branch information
eagleflo committed Nov 25, 2020
1 parent c9df9bd commit 053db71
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Entry {
pub kanji: String,
pub reading: String,
pub meanings: Vec<String>,
pub frequency: i32,
}

fn upsert(dictionary: &mut Dictionary, key: String, entry: &Entry) {
Expand Down Expand Up @@ -40,6 +41,14 @@ fn read_dictionary() -> (Dictionary, Dictionary, Dictionary) {
Some(e) => e.text().unwrap(),
None => continue,
};
let nf = match node
.descendants()
.find(|n| n.has_tag_name("re_pri") && n.text().unwrap().starts_with("nf"))
{
Some(e) => e.text().unwrap(),
None => "",
};

let glosses = node
.descendants()
.filter(|n| n.has_tag_name("gloss"))
Expand All @@ -50,6 +59,11 @@ fn read_dictionary() -> (Dictionary, Dictionary, Dictionary) {
kanji: keb.to_string(),
reading: reb.to_string(),
meanings: glosses,
frequency: if !nf.is_empty() {
nf[2..].parse().unwrap_or(999)
} else {
999
},
};

if !keb.is_empty() {
Expand Down Expand Up @@ -94,7 +108,8 @@ fn collect_results(dictionary: &'static Dictionary, input: &str) -> Vec<&'static
}
}
}
return results;
results.sort_by_key(|e| e.frequency);
results
}

pub fn lookup(input: &str) -> Vec<&Entry> {
Expand Down

0 comments on commit 053db71

Please sign in to comment.