1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
|
use std::collections::HashMap;
use std::fs;
use std::io::{self, BufRead, Write};
use rand::prelude::*;
use anyhow::{anyhow, Result};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use structopt::StructOpt;
mod charset;
use charset::Charset;
#[derive(Debug, StructOpt)]
#[structopt(name = "datagengo", about = "Japanese example practice maker")]
struct Opt {
#[structopt(subcommand)]
cmd: Cmd,
}
#[derive(Debug, StructOpt)]
enum Cmd {
ParseKanjidic,
New {
#[structopt(default_value = "10")]
count: usize,
#[structopt(long = "truncate")]
truncate: Option<usize>,
},
Simplify,
Rebalance {
#[structopt(default_value = "5")]
start: usize,
},
Format,
}
fn main() {
let opt = Opt::from_args();
match opt.cmd {
Cmd::ParseKanjidic => {
let levels = parse_kanjidic().expect("error");
for (level, chars) in levels.iter() {
println!("{}: {}", level, chars.to_string());
}
}
Cmd::New { truncate, count } => {
let kanji_levels = read_kanji_levels().expect("read_kanji_levels");
let all_kanji = Charset::new(
kanji_levels
.iter()
.map(|(_, x)| x.to_string())
.collect::<Vec<_>>()
.join(""),
);
let kanji_levels = kanji_levels
.into_iter()
.map(|(l, x)| (l, Charset::new(x)))
.collect::<Vec<_>>();
let mut ex = read_examples(&all_kanji).expect("read_examples");
ex.retain(|e| (5..=25).contains(&e.ja.chars().count()));
let mut batches: Vec<Batch> = fs::read("data/batches.json")
.map_err(anyhow::Error::from)
.and_then(|x| Ok(serde_json::from_slice(&x)?))
.unwrap_or_default();
if let Some(t) = truncate {
batches.truncate(t);
}
println!("---- starting after {} batches ----", batches.len());
let target_len = batches.len() + count;
gen_batches(&mut batches, target_len, &kanji_levels, &ex);
fs::write(
"data/batches.json",
serde_json::to_string_pretty(&batches)
.expect("serialize")
.as_bytes(),
)
.expect("save");
}
Cmd::Simplify => {
let mut batches: Vec<Batch> = fs::read("data/batches.json")
.map_err(anyhow::Error::from)
.and_then(|x| Ok(serde_json::from_slice(&x)?))
.unwrap_or_default();
for batch in batches.iter_mut() {
simplify_batch(batch);
}
fs::write(
"data/batches.json",
serde_json::to_string_pretty(&batches)
.expect("serialize")
.as_bytes(),
)
.expect("save");
}
Cmd::Rebalance { start } => {
let mut batches: Vec<Batch> = fs::read("data/batches.json")
.map_err(anyhow::Error::from)
.and_then(|x| Ok(serde_json::from_slice(&x)?))
.unwrap_or_default();
let kanji_levels = read_kanji_levels().expect("read_kanji_levels");
for (level, _) in kanji_levels.iter() {
rebalance_level(level, &mut batches[start..]);
}
fs::write(
"data/batches.json",
serde_json::to_string_pretty(&batches)
.expect("serialize")
.as_bytes(),
)
.expect("save");
}
Cmd::Format => {
let jmdict = fs::read_to_string("data/JMdict_e.xml").expect("read_jmdict");
let jmdict = roxmltree::Document::parse_with_options(
&jmdict,
roxmltree::ParsingOptions {
allow_dtd: true,
..Default::default()
},
)
.expect("parse_jmdict");
let jmdict_idx = index_jmdict(&jmdict);
let batches = fs::read("data/batches.json")
.map_err(anyhow::Error::from)
.and_then(|x| Ok(serde_json::from_slice::<Vec<Batch>>(&x)?))
.expect("read/parse");
fs::create_dir_all("public").expect("mkdir public");
fs::copy("static/style.css", "public/style.css").expect("copy style.css");
batches
.iter()
.enumerate()
.for_each(|x| format_batch(&jmdict_idx, batches.len(), x));
let kanji_levels = read_kanji_levels().expect("read_kanji_levels");
format_index(&batches, &kanji_levels).expect("format_index");
}
}
}
// =====================================================================
// PARSING DATA FILES
// =====================================================================
type DictIndex<'a> = HashMap<&'a str, Vec<roxmltree::Node<'a, 'a>>>;
fn index_jmdict<'a>(dict: &'a roxmltree::Document) -> DictIndex<'a> {
let dict = dict
.root()
.children()
.find(|x| x.has_tag_name("JMdict"))
.unwrap();
let mut ret: DictIndex<'a> = HashMap::new();
for x in dict.children().filter(|x| x.has_tag_name("entry")) {
for r in x.children().filter(|x| x.has_tag_name("k_ele")) {
if let Some(keb) = r.children().find(|x| x.has_tag_name("keb")) {
let txt = keb.text().unwrap().trim();
ret.entry(txt).or_default().push(x);
}
}
}
ret
}
fn parse_kanjidic() -> Result<Vec<(String, Charset)>> {
let file = fs::read_to_string("data/kanjidic2.xml")?;
let xml = roxmltree::Document::parse(&file)?;
let kanjidic = xml.root().first_child().unwrap();
assert!(kanjidic.has_tag_name("kanjidic2"));
let mut levels = HashMap::new();
for x in kanjidic.children() {
if !x.has_tag_name("character") {
continue;
}
let mut literal = None;
let mut jlpt = None;
let mut grade = None;
for y in x.children() {
if y.has_tag_name("literal") {
literal = y.text();
}
if y.has_tag_name("misc") {
for z in y.children() {
if z.has_tag_name("jlpt") {
jlpt = z.text().and_then(|x| str::parse::<i32>(x).ok());
}
if z.has_tag_name("grade") {
grade = z.text().and_then(|x| str::parse::<i32>(x).ok());
}
}
}
}
match grade {
Some(i) if i <= 6 => grade = Some(7),
_ => (),
}
if let Some(lit) = literal {
levels
.entry((jlpt, grade))
.or_insert(String::new())
.extend(lit.chars());
}
}
let mut levels = levels.into_iter().collect::<Vec<_>>();
levels.sort_by_key(|((j, g), _)| match (j, g) {
(Some(j), Some(g)) => (10 - *j) * 20 + *g,
(Some(j), None) => (10 - *j) * 20 + 15,
(None, Some(g)) => 1000 + *g,
(None, None) => 1015,
});
let mut ret = Vec::new();
let mut pc = Charset::default();
for ((j, g), chars) in levels.into_iter() {
let name = match (j, g) {
(Some(j), Some(7)) => format!("N{}a", j),
(Some(j), Some(8)) => format!("N{}b", j),
(Some(j), Some(g)) => format!("N{}-{}", j, g),
(Some(j), None) => format!("N{}+", j),
(None, Some(7)) => format!("N0a"),
(None, Some(8)) => format!("N0b"),
(None, Some(g)) => format!("N0-{}", g),
(None, None) => format!("N0+"),
};
let chars = Charset::new(chars).diff(&pc);
pc = pc.union(&chars);
ret.push((name, chars));
}
Ok(ret)
}
fn read_kanji_levels() -> Result<Vec<(String, String)>> {
Ok(fs::read_to_string("data/kanji_levels.txt")?
.lines()
.filter_map(|l| l.split_once(": "))
.map(|(l, k)| (l.to_string(), k.to_string()))
.collect::<Vec<_>>())
}
fn read_examples(all_kanji: &Charset) -> Result<Vec<Example>> {
let file = fs::File::open("data/examples.utf")?;
let mut ret = Vec::new();
let mut a = "".to_string();
for (i, line) in io::BufReader::new(file).lines().enumerate() {
let line = line?;
if line.starts_with("A:") {
a = line;
} else if line.starts_with("B:") {
let s = a.strip_prefix("A: ");
let t = line.strip_prefix("B: ");
if let (Some(a), Some(b)) = (s, t) {
if let Some((ja, eng)) = a.split_once("\t") {
if let Some((eng, id)) = eng.split_once("#") {
ret.push(Example {
ja: ja.to_string(),
en: eng.to_string(),
expl: b.to_string(),
id: Some(id.to_string()),
chars: Charset::new(ja).inter(all_kanji),
});
} else {
ret.push(Example {
ja: ja.to_string(),
en: eng.to_string(),
expl: b.to_string(),
id: None,
chars: Charset::new(ja).inter(all_kanji),
});
}
}
}
}
if i % 10000 == 0 {
eprintln!("read examples: {}/300 (x1000)", i / 1000);
}
}
Ok(ret)
}
// =====================================================================
// BATCH STRUCTURES AND GENERATION
// =====================================================================
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Example {
ja: String,
en: String,
expl: String,
id: Option<String>,
chars: Charset,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct Batch {
level: String,
chars: Charset,
chars_p1: Charset,
chars_p2: Charset,
chars_bad: Charset,
examples: Vec<Example>,
}
fn gen_batches(
batches: &mut Vec<Batch>,
target_len: usize,
kanji_levels: &[(String, Charset)],
examples: &[Example],
) {
let mut remainder = None;
while batches.len() < target_len {
let done = Charset::from_iter(
batches
.iter()
.map(|x| x.chars.chars().iter().copied())
.flatten()
.chain(remainder.as_ref().map(|x: &Batch| x.chars.chars())
.unwrap_or_default().iter().copied()),
);
let mut advanced = false;
for (i, (level, chars)) in kanji_levels.iter().enumerate() {
let diff = chars.diff(&done);
if !diff.is_empty() {
let avoid = Charset::from_iter(
kanji_levels
.iter()
.skip(i + 1)
.filter(|(l, _)| !l.ends_with("-9") && !l.ends_with("-10"))
.map(|(_, c)| c.chars().iter().copied())
.flatten(),
);
let level_examples = level_examples(&diff, &avoid, examples);
if !level_examples.is_empty() {
remainder = gen_level(batches, level, &diff, &done, &avoid, level_examples, remainder);
advanced = true;
break;
}
}
}
if !advanced {
break;
}
}
if let Some(r) = remainder {
if batches.len() < target_len {
batches.push(r);
}
}
}
fn gen_level(
batches: &mut Vec<Batch>,
level: &str,
chars: &Charset,
done: &Charset,
avoid: &Charset,
mut examples: Vec<&Example>,
mut remainder: Option<Batch>,
) -> Option<Batch> {
examples.shuffle(&mut thread_rng());
println!(
"Level {}: {} characters using {} examples",
level,
chars.len(),
examples.len()
);
let avg_len = examples.len() as f32 * 20. / chars.len() as f32;
let mut done = remainder.as_ref()
.map(|x| x.chars.clone())
.unwrap_or_default()
.union(&done);
'outer: while !examples.is_empty() {
println!("iter with {} examples", examples.len());
let mut batch = remainder.take().unwrap_or_else(|| Batch {
level: level.to_string(),
chars: Charset::default(),
chars_p1: Charset::default(),
chars_p2: Charset::default(),
chars_bad: Charset::default(),
examples: vec![],
});
let n_chars = 20 - batch.chars.len();
let mut dyn_mat: Vec<Vec<Option<(Charset, Option<(usize, usize)>)>>> = vec![];
for ex in examples.iter() {
let mut dyn_row = vec![None; n_chars + 1];
let chars_common = ex.chars.inter(&chars).diff(&done);
if chars_common.len() > 6 {
dyn_mat.push(dyn_row);
continue;
}
if chars_common.len() < dyn_row.len() {
dyn_row[chars_common.len()] = Some((chars_common.clone(), None));
}
for (i, dyn_prev) in dyn_mat.iter().enumerate() {
for (j, dpr) in dyn_prev.iter().enumerate() {
if let Some((chars_inter, _prev)) = dpr {
assert_eq!(chars_inter.len(), j);
let new_chars_common = chars_inter.union(&chars_common);
let new_chars_common_len = new_chars_common.len();
if new_chars_common_len > chars_inter.len() && new_chars_common_len <= n_chars {
dyn_row[new_chars_common_len] = Some((new_chars_common, Some((i, j))));
}
}
}
}
dyn_mat.push(dyn_row);
}
let dyn_mat_cnt = |i| {
let mut i: usize = i;
let mut cnt = 0;
let mut j: usize = n_chars;
loop {
match &dyn_mat[i][j] {
None => return 1000000,
Some((_, ij_prev)) => {
cnt += 1;
match ij_prev {
Some((iprev, jprev)) => {
i = *iprev;
j = *jprev;
}
None => return cnt,
}
}
}
}
};
let i = (0..dyn_mat.len()).min_by_key(|i| {
let x = dyn_mat_cnt(*i) as i64 - (avg_len as i64 + 2);
x * x
}).unwrap();
let (mut i, mut j) = (i, n_chars);
loop {
match &dyn_mat[i][j] {
None => break 'outer,
Some((chars, ij_prev)) => {
println!("Add {}: {}", examples[i].chars.inter(&chars).to_string(), examples[i].ja);
batch.examples.push(examples[i].clone());
examples.remove(i);
batch.chars = batch.chars.union(&chars);
match ij_prev {
Some((iprev, jprev)) => {
assert!(*iprev < i);
i = *iprev;
j = *jprev;
}
None => break,
}
}
}
}
assert_eq!(batch.chars.len(), 20);
batch.chars_p1 = batches
.iter()
.rev()
.next()
.map(|b| b.chars.clone())
.unwrap_or(Charset::default())
.inter(&batch.chars);
batch.chars_p2 = batches
.iter()
.rev()
.skip(1)
.next()
.map(|b| b.chars.clone())
.unwrap_or(Charset::default())
.inter(&batch.chars);
batch.chars_bad = batch.chars.inter(&avoid);
done = done.union(&batch.chars);
println!("-> batch {:03}: {} with {} examples", batches.len(), batch.chars.to_string(), batch.examples.len());
batches.push(batch);
}
if !examples.is_empty() {
let n_remaining = chars.diff(&done).len();
let ex_chars = Charset::from_iter(
examples.iter()
.map(|x| x.chars.chars().iter().copied())
.flatten()).diff(&done);
println!("still {} examples, {} chars in ex, {} total", examples.len(), ex_chars.len(), n_remaining);
assert!(ex_chars.len() <= n_remaining);
if ex_chars.len() > 20 {
return None;
}
let remainder = Batch {
level: level.to_string(),
chars: ex_chars,
chars_p1: Charset::default(),
chars_p2: Charset::default(),
chars_bad: Charset::default(),
examples: examples.iter().map(|x| (**x).clone()).collect(),
};
println!("remainer batch: {:?}", remainder);
Some(remainder)
} else {
None
}
}
fn level_examples<'a>(
chars: &Charset,
avoid: &Charset,
all_examples: &'a [Example],
) -> Vec<&'a Example> {
println!("Calculating examples for {}", chars.to_string());
let mut todo = chars.clone();
let mut bad = Charset::default();
let mut examples = vec![];
let cost = |ex: &Example, ex_todo_inter: usize, ex_chars_inter: usize| {
(
-(ex.chars.inter_len(&avoid) as i32),
ex_todo_inter,
ex_chars_inter,
-(ex.ja.chars().count() as i32),
(thread_rng().gen::<usize>())
)
};
let mut all_with_inter = all_examples
.par_iter()
.map(|ex| (ex, ex.chars.inter_len(&chars)))
.map(|(ex, ex_chars_inter)| (ex, ex_chars_inter, ex_chars_inter))
.collect::<Vec<_>>();
while !todo.is_empty() {
let best = all_with_inter.par_iter().enumerate()
.filter(|(_, (_, ex_todo_inter, ex_tgt_inter))| (1..=6).contains(ex_todo_inter))
.max_by_key(
|(_, (ex, ex_todo_inter, ex_chars_inter))| cost(*ex, *ex_todo_inter, *ex_chars_inter),
);
if let Some((i, (ex, ex_todo_inter, _))) = best {
let ex = *ex;
assert_eq!(*ex_todo_inter, ex.chars.inter(&todo).len());
examples.push(ex);
all_with_inter.remove(i);
todo = todo.diff(&ex.chars);
bad = bad.union(&ex.chars.inter(&avoid));
all_with_inter
.par_iter_mut()
.for_each(|(ex2, ex_todo_inter, _)| {
if ex2.chars.inter_len(&ex.chars) > 0 {
*ex_todo_inter = ex2.chars.inter_len(&todo);
}
});
} else {
break;
}
}
if !todo.is_empty() {
println!("MISSING: NO SENTENCES FOR {}", todo.to_string());
}
if !bad.is_empty() {
println!("USED BAD CHARS: {}", bad.to_string());
}
examples
}
/*
let (mut target_i, target_level, mut target_chars) = kanji_levels
.iter()
.enumerate()
.map(|(i, (l, c))| (i, l, c.diff(&prev_chars)))
.find(|(_, _, c)| !c.is_empty())
.ok_or(anyhow!("no more batches to make!"))?;
let chars_p1 = previous
.iter()
.rev()
.next()
.map(|b| b.chars.clone())
.unwrap_or(Charset::default());
let chars_p2 = previous
.iter()
.rev()
.skip(1)
.next()
.map(|b| b.chars.clone())
.unwrap_or(Charset::default());
let mut chars_late = Charset::default();
let mut chars_bad = Charset::from_iter(
kanji_levels
.iter()
.skip(target_i + 1)
.map(|(_, c)| c.chars().iter().copied())
.flatten(),
);
let mut chars_bad_avoid = Charset::from_iter(
kanji_levels
.iter()
.skip(target_i + 1)
.filter(|(l, _)| !l.ends_with("-9") && !l.ends_with("-10"))
.map(|(_, c)| c.chars().iter().copied())
.flatten(),
);
let mut batch = Batch {
level: target_level.to_string(),
chars: Charset::default(),
chars_p1: Charset::default(),
chars_p2: Charset::default(),
chars_bad: Charset::default(),
examples: Vec::new(),
};
let mut batch_chars = Charset::default();
eprintln!("----");
eprintln!("Level : {}", batch.level);
eprintln!("Target : {}", target_chars.to_string());
eprintln!("Prev1 : {}", chars_p1.to_string());
eprintln!("Prev2 : {}", chars_p2.to_string());
eprintln!("Bad : {} characters", chars_bad.len());
let batch_len = 20;
let mut stalled = false;
while batch.chars.len() < batch_len && !target_chars.is_empty() {
let need = batch_len - batch.chars.len();
let should_add = need > target_chars.len() && target_chars.len() <= 3;
if target_i + 1 < kanji_levels.len() && (should_add || stalled) {
// upgrade to next level
target_i += 1;
chars_late = chars_late.union(&target_chars);
target_chars = target_chars.union(&kanji_levels[target_i].1.diff(&prev_chars));
chars_bad = chars_bad.diff(&target_chars);
chars_bad_avoid = chars_bad_avoid.diff(&target_chars);
if batch.examples.is_empty() {
batch.level = kanji_levels[target_i].0.to_string();
} else {
batch.level = format!("{} + {}", batch.level, kanji_levels[target_i].0);
}
eprintln!("Level : {}", batch.level);
eprintln!("Target: {}", target_chars.to_string());
eprintln!("Late : {}", chars_late.to_string());
eprintln!("Bad : {} characters", chars_bad.len());
stalled = false;
}
/* this one works well enough
let cost = |ex: &Example, ex_tgt_inter: usize| {
20i32 * ex_tgt_inter as i32
+ 30i32 * ex.chars.inter_len(&chars_late) as i32
+ 6i32 * ex.chars.inter_len(&batch.chars) as i32
+ 4i32 * ex.chars.inter_len(&chars_p1) as i32
+ 3i32 * ex.chars.inter_len(&chars_p2) as i32
- 40i32 * ex.chars.inter_len(&chars_bad) as i32
};
*/
let cost = |ex: &Example, ex_tgt_inter: usize| {
(
-(ex.chars.inter_len(&chars_bad_avoid) as i32),
ex_tgt_inter,
ex.chars.inter_len(&chars_late),
2 * ex.chars.inter_len(&chars_p1) + ex.chars.inter_len(&chars_p2),
-(ex.ja.chars().count() as i32),
)
};
let cand_1 = examples
.par_iter()
.map(|ex| (ex, ex.chars.inter_len(&target_chars)))
.filter(|(_, ex_tgt_inter)| {
(1..=4).contains(ex_tgt_inter) && *ex_tgt_inter + batch.chars.len() <= batch_len
})
.max_by_key(|(ex, ex_tgt_inter)| cost(ex, *ex_tgt_inter));
let cand = cand_1.or_else(|| {
examples
.par_iter()
.map(|ex| (ex, ex.chars.inter_len(&target_chars)))
.filter(|(_, ex_tgt_inter)| *ex_tgt_inter > 0)
.max_by_key(|(ex, ex_tgt_inter)| cost(ex, *ex_tgt_inter))
});
if let Some((ex, _)) = cand {
eprintln!(
"* add {} (rep: {}, p1: {}, p2: {}, bad: {}) {}",
ex.chars.inter(&target_chars).to_string(),
ex.chars.inter(&batch.chars).to_string(),
ex.chars.inter(&chars_p1).to_string(),
ex.chars.inter(&chars_p2).to_string(),
ex.chars.inter(&chars_bad).to_string(),
ex.ja
);
batch.chars = batch.chars.union(&ex.chars.inter(&target_chars));
target_chars = target_chars.diff(&ex.chars);
chars_late = chars_late.diff(&ex.chars);
batch.examples.push(ex.clone());
batch_chars = batch_chars.union(&ex.chars);
stalled = false;
} else {
if stalled {
eprintln!(
"could not find suitable sentence, stopping batch now (need {})",
need
);
break;
}
stalled = true;
}
}
batch.chars_p1 = chars_p1.inter(&batch_chars);
batch.chars_p2 = chars_p2.inter(&batch_chars);
batch.chars_bad = chars_bad.inter(&batch_chars);
Ok(batch)
*/
fn simplify_batch(batch: &mut Batch) {
let mut char_cnt = HashMap::<char, usize>::new();
for ex in batch.examples.iter() {
for ch in batch.chars.inter(&ex.chars).chars() {
*char_cnt.entry(*ch).or_default() += 1;
}
}
loop {
let i_opt = batch.examples.iter().position(|ex| {
batch
.chars
.inter(&ex.chars)
.chars()
.iter()
.all(|x| char_cnt[x] >= 2)
});
if let Some(i) = i_opt {
println!(
"Removing {} [{}]",
batch.examples[i].ja,
batch.examples[i].chars.to_string()
);
batch.examples.remove(i);
} else {
break;
}
}
}
fn rebalance_level(level: &str, batches: &mut [Batch]) {
let mut i_batch = vec![];
let mut n_ex = 0;
for (i, b) in batches.iter().enumerate() {
if b.level == level {
i_batch.push(i);
n_ex += b.examples.len();
}
}
if i_batch.len() < 2 {
return;
}
println!(
"Level {}: {} batches, {} examples, avg {:.2}",
level,
i_batch.len(),
n_ex,
n_ex as f32 / i_batch.len() as f32
);
//todo!()
}
// =====================================================================
// FORMATTING TO HTML
// =====================================================================
fn format_batch<'a>(dict_idx: &DictIndex<'a>, count: usize, (i, batch): (usize, &Batch)) {
format_batch_aux(dict_idx, count, i, batch).expect("format batch");
}
fn format_batch_aux<'a>(
dict_idx: &DictIndex<'a>,
count: usize,
i: usize,
batch: &Batch,
) -> Result<()> {
let mut f = io::BufWriter::new(fs::File::create(format!("public/{:03}.html", i))?);
write!(
f,
r#"<!DOCTYPE html>
<html>
<head>
<meta charset=\"UTF-8\" />
<title>Batch #{:03}</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>"#,
i
)?;
writeln!(f, r#"<p><a href="index.html">index</a>"#)?;
for j in 0..count {
if j != i {
writeln!(f, r#" <a href="{:03}.html">{:03}</a>"#, j, j)?;
} else {
writeln!(f, " {:03}", j)?;
}
}
writeln!(f, r#"</p>"#)?;
writeln!(f, "<p>Level: {}</p>", batch.level)?;
writeln!(
f,
r#"<p class="ja chars">【{}】</p>"#,
batch.chars.to_string()
)?;
for ex in batch.examples.iter() {
writeln!(f, "<hr />")?;
write!(f, r#"<p class="ja">"#)?;
for c in ex.ja.chars() {
if batch.chars.contains(c) {
write!(f, r#"<span class="char_cur">{}</span>"#, c)?;
} else if batch.chars_p1.contains(c) {
write!(f, r#"<span class="char_p1">{}</span>"#, c)?;
} else if batch.chars_p2.contains(c) {
write!(f, r#"<span class="char_p2">{}</span>"#, c)?;
} else if batch.chars_bad.contains(c) {
write!(f, r#"<span class="char_bad">{}</span>"#, c)?;
} else {
write!(f, "{}", c)?;
}
}
writeln!(f, "</p>")?;
writeln!(f, r#"<p class="en">{}</p>"#, ex.en)?;
writeln!(f, r#"<details><summary>Explanation</summary>"#)?;
let mut expl_batch = Vec::new();
let mut expl_all = 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(keb, reb, ent) {
if wchars.intersects(&batch.chars) {
expl_batch.push(s);
} else {
expl_all.push(s);
}
}
}
}
}
for be in expl_batch {
writeln!(f, r#"<p>{}</p>"#, be)?;
}
writeln!(f, r#"<p class="chars">"#)?;
for c in ex.chars.inter(&batch.chars).chars().iter() {
writeln!(
f,
r#"<a href="https://jisho.org/search/{}%20%23kanji">{}</a>"#,
c, c
)?;
}
writeln!(f, r#"</p>"#)?;
for be in expl_all {
writeln!(f, r#"<p>{}</p>"#, be)?;
}
writeln!(f, r#"</details>"#)?;
}
write!(f, "</body></html>")?;
f.flush()?;
Ok(())
}
fn expl_clean_word(w: &str) -> (&str, Option<&str>) {
let mut ret = w;
for delim in ['(', '{', '['] {
if let Some((s, _)) = ret.split_once(delim) {
ret = s;
}
}
let p = w
.split_once('(')
.and_then(|(_, r)| r.split_once(')'))
.map(|(p, _)| p);
(ret, p)
}
fn dict_str<'a>(qkeb: &str, qreb: Option<&str>, ent: &roxmltree::Node<'a, 'a>) -> Option<String> {
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();
if qreb.map(|x| x != reb).unwrap_or(false) {
return None;
}
let mut ret = format!("{} [{}]", qkeb, reb);
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")) {
ret.extend(format!(" {};", s.text().unwrap().trim()).chars());
}
}
if ret.chars().rev().next() == Some(';') {
ret.pop();
}
Some(ret)
}
fn format_index(batches: &[Batch], kanji_levels: &[(String, String)]) -> Result<()> {
let mut f = io::BufWriter::new(fs::File::create("public/index.html")?);
write!(
f,
r#"<!DOCTYPE html>
<html>
<head>
<meta charset=\"UTF-8\" />
<title>List of batches</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>"#
)?;
writeln!(f, "<table>")?;
writeln!(f, "<tr><th>Num</th><th>Level</th><th>Chars</th><th>Examples</th><th>Review</th><th>Ignore</th></tr>")?;
for (i, batch) in batches.iter().enumerate() {
writeln!(
f,
r#"<tr><td><a href="{:03}.html">{:03}</a></td><td>{}</td><td>{}</td><td> {}</td><td>{}</td><td>{}</td></tr>"#,
i,
i,
batch.level,
batch.chars.to_string(),
batch.examples.len(),
batch.chars_p1.union(&batch.chars_p2).to_string(),
batch.chars_bad.to_string()
)?;
}
writeln!(f, r#"</table>"#)?;
writeln!(f, "<hr />")?;
let all_chars = Charset::from_iter(
batches
.iter()
.map(|x| x.chars.chars().iter().copied())
.flatten(),
);
writeln!(f, "<table>")?;
writeln!(
f,
"<tr><th>Level</th><th>Count</th><th>Chars</th><th>Missing chars</th></tr>"
)?;
for (lvl, chars) in kanji_levels.iter() {
if lvl == "N0+" || lvl == "N0-9" || lvl.ends_with("-10") {
continue;
}
let chars = Charset::new(chars);
let missing = chars.diff(&all_chars);
writeln!(
f,
"<tr><td>{}</td><td>{}</td><td>{}</td><td>{} ({})</td></tr>",
lvl,
chars.len(),
chars.to_string(),
missing.to_string(),
missing.len()
)?;
}
writeln!(f, "</table>")?;
write!(f, "</body></html>")?;
f.flush()?;
Ok(())
}
|