-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added: language report; refactored: report module for common report s…
…tuff;
- Loading branch information
1 parent
d665f3b
commit 7e050f6
Showing
5 changed files
with
186 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::structure::*; | ||
use crate::japanese::*; | ||
use crate::report::*; | ||
|
||
use std::fmt::Write; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct LangStats{ | ||
rp: ReportHeader, | ||
kanji: HashMap<String, usize>, | ||
other: HashMap<String, usize>, | ||
} | ||
|
||
pub fn lang_stats_report(mut s: LangStats, doc: &mut String){ | ||
write_header(&mut s.rp, doc); | ||
|
||
write_list(s.other, "Hiragana/Katakana frequencies:", doc); | ||
write_list(s.kanji, "Kanji frequencies:", doc); | ||
} | ||
|
||
pub fn accumulate_lang_stats(chapter: Chapter, stats: &mut LangStats, log: &mut String){ | ||
set_current_manga(&mut stats.rp.manga, chapter.manga, log); | ||
stats.rp.volumes.push(chapter.volume); | ||
stats.rp.chapters.push(chapter.chapter); | ||
for picture in chapter.pic{ | ||
stats.rp.pictures += 1; | ||
|
||
if let Some(texts) = picture.text{ | ||
for text in texts{ | ||
let replacements = if let Some(kmap) = &text.kmap{ | ||
for [kanji, mapping] in kmap{ | ||
let key = format!("{}: {}", kanji, mapping); | ||
update(&mut stats.kanji, &key, 1, |a, b| a + b); | ||
} | ||
map_kanjis(&text.lines, kmap.as_slice()) | ||
} else { | ||
text.lines.clone() | ||
}; | ||
if could_contain_kanji(&replacements){ | ||
let _ = writeln!( | ||
log, | ||
"Warning: lines {:#?} contain kanji or untranslateable characters. | ||
Every kanji is counted as one (1) mora.", | ||
replacements | ||
); | ||
} | ||
let morae = replacements.iter().flat_map(|line| line.chars()) | ||
.fold(0, |acc, c| acc + to_mora(c)); | ||
for line in text.lines{ | ||
for c in line.chars(){ | ||
if is_hiragana(c) || is_katakana(c) { | ||
update(&mut stats.other, &c.to_string(), 1, |a, b| a + b); | ||
} | ||
} | ||
} | ||
stats.rp.morae += morae; | ||
} | ||
}; | ||
} | ||
} | ||
|
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,64 @@ | ||
use std::fmt::Write; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct ReportHeader{ | ||
pub manga: String, | ||
pub volumes: Vec<usize>, | ||
pub chapters: Vec<usize>, | ||
pub pictures: usize, | ||
pub morae: usize, | ||
} | ||
|
||
pub fn write_header(h: &mut ReportHeader, doc: &mut String){ | ||
let _ = writeln!(doc, "Manga: {}", h.manga); | ||
let _ = write!(doc, "Volumes: "); | ||
|
||
h.volumes.sort(); | ||
h.volumes.dedup(); | ||
for vol in &h.volumes{ | ||
let _ = write!(doc, "{}, ", vol); | ||
} | ||
doc.pop(); | ||
doc.pop(); | ||
let _ = writeln!(doc); | ||
|
||
let _ = write!(doc, "Chapters: "); | ||
h.chapters.sort(); | ||
h.chapters.dedup(); | ||
for chap in &h.chapters{ | ||
let _ = write!(doc, "{}, ", chap); | ||
} | ||
doc.pop(); | ||
doc.pop(); | ||
let _ = writeln!(doc); | ||
|
||
let _ = writeln!(doc, "Pictures: {}", h.pictures); | ||
let _ = writeln!(doc, "Morae spoken: {}", h.morae); | ||
} | ||
|
||
|
||
pub fn write_list(hmap: HashMap<String, usize>, title: &str, doc: &mut String){ | ||
let _ = writeln!(doc, "{}", title); | ||
let mut list = hmap.into_iter().collect::<Vec<_>>(); | ||
list.sort_unstable_by(|(_, a), (_, b)| b.partial_cmp(a).unwrap()); | ||
for (name, count) in list{ | ||
let _ = writeln!(doc, "\t{}: {}", name, count); | ||
} | ||
} | ||
|
||
pub fn update<T: Copy>(map: &mut HashMap<String, T>, key: &str, val: T, fun: fn(T, T) -> T){ | ||
if let Some(x) = map.get_mut(key){ | ||
*x = fun(*x, val); | ||
} else { | ||
map.insert(key.to_string(), val); | ||
} | ||
} | ||
|
||
pub fn set_current_manga(current: &mut String, mut chapter: String, log: &mut String){ | ||
if current.is_empty(){ | ||
*current = chapter; | ||
} else if current != &mut chapter{ | ||
let _ = writeln!(log, "Different manga found: {}. Current manga is: {}.", chapter, current); | ||
} | ||
} |
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