From 643f65d17c264ead3726ef7efd4f820d78e4d5b3 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sat, 21 Oct 2023 20:22:04 +0200 Subject: add pronunciation of extra examples --- src/main.rs | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index d877fce..70a7fa3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1213,10 +1213,32 @@ fn format_batch_aux<'a>( r#"
Extra examples (reading practice)"# )?; for ex in batch.extra_examples.iter() { + let mut expl1 = Vec::new(); + let mut expl2 = Vec::new(); + for word in ex.expl.split(|c| c == ' ' || c == '~') { + let (keb, reb) = expl_clean_word(word); + let wchars = Charset::new(keb); + if !wchars.intersects(&ex.chars) { + continue; + } + if let Some(ents) = dict_idx.get(keb) { + for ent in ents.iter() { + if let Some(s) = dict_str_short(keb, reb, ent) { + if wchars.intersects(&batch.chars) { + expl1.push(s); + } else { + expl2.push(s); + } + } + } + } + } + expl1.extend(expl2.into_iter()); + let expl = expl1.join("
"); writeln!( f, - r#""#, - ex.ja, ex.en + r#""#, + ex.ja, ex.en, expl )?; } writeln!(f, r#"
  {}  
{}
  {}  
{}
{}
"#)?; @@ -1262,7 +1284,11 @@ fn expl_clean_word(w: &str) -> (&str, Option<&str>) { (ret, p) } -fn dict_str<'a>(qkeb: &str, qreb: Option<&str>, ent: &roxmltree::Node<'a, 'a>) -> Option { +fn dict_str_short<'a>( + qkeb: &str, + qreb: Option<&str>, + ent: &roxmltree::Node<'a, 'a>, +) -> Option { let r_ele = ent.children().find(|x| x.has_tag_name("r_ele")).unwrap(); let reb = r_ele.children().find(|x| x.has_tag_name("reb")).unwrap(); let reb = reb.text().unwrap().trim(); @@ -1271,7 +1297,14 @@ fn dict_str<'a>(qkeb: &str, qreb: Option<&str>, ent: &roxmltree::Node<'a, 'a>) - return None; } - let mut ret = format!(r#"{} 【{}】"#, qkeb, reb); + Some(format!( + r#"{} 【{}】"#, + qkeb, reb + )) +} + +fn dict_str<'a>(qkeb: &str, qreb: Option<&str>, ent: &roxmltree::Node<'a, 'a>) -> Option { + let mut ret = dict_str_short(qkeb, qreb, ent)?; for sense in ent.children().filter(|x| x.has_tag_name("sense")) { if let Some(s) = sense.children().find(|x| x.has_tag_name("gloss")) { -- cgit v1.2.3