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
|
use std::collections::HashMap;
use std::fs;
use std::cmp::Ordering;
use std::io::{self, BufRead, Write};
use anyhow::{anyhow, Result};
use rayon::prelude::*;
use serde::{Serialize, Deserialize};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "datagengo", about = "Japanese example practice maker")]
struct Opt {
#[structopt(subcommand)]
cmd: Cmd,
}
#[derive(Debug, StructOpt)]
enum Cmd {
ParseKanjidic,
New,
Format,
}
fn main() {
let opt = Opt::from_args();
match opt.cmd {
Cmd::ParseKanjidic => {
let levels = parse_kanjidic().expect("error");
for (jlpt, grade, chars) in levels.iter() {
println!("{}.{}: {}", jlpt, grade, chars);
}
}
Cmd::New => {
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();
println!("---- starting after {} batches ----", batches.len());
for _ in 0..10 {
let batch = gen_batch(&batches, &kanji_levels, &ex).expect("gen_batch");
batches.push(batch);
}
fs::write("data/batches.json", serde_json::to_string_pretty(&batches).expect("serialize").as_bytes()).expect("save");
}
Cmd::Format => {
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");
batches.par_iter()
.enumerate()
.for_each(|x| format_batch(batches.len(), x));
}
}
}
fn parse_kanjidic() -> Result<Vec<(i32, i32, String)>> {
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("grade") {
grade = z.text().and_then(|x| str::parse::<i32>(x).ok());
}
if z.has_tag_name("jlpt") {
jlpt = z.text().and_then(|x| str::parse::<i32>(x).ok());
}
}
}
}
if jlpt.is_none() && grade.is_none() {
continue;
}
let level = (jlpt.unwrap_or(0), grade.unwrap_or(0));
if let Some(lit) = literal {
levels.entry(level).or_insert(String::new()).extend(lit.chars());
}
}
let mut levels = levels.into_iter().map(|((j, g), c)| (j, g, c)).collect::<Vec<_>>();
levels.sort_by_key(|(j, g, _)| (-*j, *g));
Ok(levels)
}
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", i/1000);
}
}
Ok(ret)
}
fn gen_batch(previous: &[Batch], kanji_levels: &[(String, Charset)], examples: &[Example]) -> Result<Batch> {
let prev_chars = Charset::from_iter(previous.iter()
.map(|x| x.chars.chars().iter().copied())
.flatten());
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_missing = 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 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;
while batch.chars.len() < batch_len && !target_chars.is_empty() {
let need = batch_len - batch.chars.len();
if need >= 2 && target_chars.len() <= 1 && target_i + 1 < kanji_levels.len() {
// upgrade to next level
target_i += 1;
chars_missing = chars_missing.union(&target_chars);
target_chars = target_chars.union(&kanji_levels[target_i].1);
chars_bad = chars_bad.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!("Missing: {}", chars_missing.to_string());
eprintln!("Bad : {} characters", chars_bad.len());
}
if let Some((ex, _)) = 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)|
20i32 * *ex_tgt_inter as i32
+ 30i32 * ex.chars.inter_len(&chars_missing) 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) {
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_missing = chars_missing.diff(&ex.chars);
batch.examples.push(ex.clone());
batch_chars = batch_chars.union(&ex.chars);
} else {
eprintln!("could not find suitable sentence, stopping batch now (missing {})", need);
break;
}
}
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 format_batch(count: usize, (i, batch): (usize, &Batch)) {
format_batch_aux(count, i, batch).expect("format batch");
}
fn format_batch_aux(count: usize, i: usize, batch: &Batch) -> Result<()> {
let mut f = io::BufWriter::new(fs::File::create(format!("html/{: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, "<p>")?;
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, "<p>Characters: {}</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 style="text-align: center; font-size: 1.2em">{}</p>"#, ex.en)?;
}
write!(f, "</body></html>")?;
f.flush()?;
Ok(())
}
#[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>,
}
#[derive(Debug, Eq, PartialEq, Hash, Clone, Serialize, Deserialize, Default)]
struct Charset(Vec<char>);
impl Charset {
fn new<S: AsRef<str>>(s: S) -> Self {
let mut chars = s.as_ref().chars().collect::<Vec<_>>();
chars.sort();
chars.dedup();
Self(chars)
}
fn from_iter<S: IntoIterator<Item=char>>(s: S) -> Self {
let mut chars = s.into_iter().collect::<Vec<_>>();
chars.sort();
chars.dedup();
Self(chars)
}
fn intersects(&self, other: &Self) -> bool {
let mut it1 = self.0.iter().peekable();
let mut it2 = other.0.iter().peekable();
while let (Some(c1), Some(c2)) = (it1.peek(), it2.peek()) {
match c1.cmp(c2) {
Ordering::Equal => return true,
Ordering::Less => it1.next(),
Ordering::Greater => it2.next(),
};
}
false
}
fn inter_len(&self, other: &Self) -> usize {
let mut it1 = self.0.iter().peekable();
let mut it2 = other.0.iter().peekable();
let mut ret = 0;
while let (Some(c1), Some(c2)) = (it1.peek(), it2.peek()) {
match c1.cmp(c2) {
Ordering::Equal => {
ret += 1;
it1.next();
it2.next();
}
Ordering::Less => {
it1.next();
}
Ordering::Greater => {
it2.next();
}
};
}
ret
}
fn inter(&self, other: &Self) -> Charset {
let mut it1 = self.0.iter().peekable();
let mut it2 = other.0.iter().peekable();
let mut ret = Vec::new();
while let (Some(c1), Some(c2)) = (it1.peek(), it2.peek()) {
match c1.cmp(c2) {
Ordering::Equal => {
ret.push(**c1);
it1.next();
it2.next();
}
Ordering::Less => {
it1.next();
}
Ordering::Greater => {
it2.next();
}
};
}
Self(ret)
}
fn union(&self, other: &Self) -> Charset {
let mut it1 = self.0.iter().peekable();
let mut it2 = other.0.iter().peekable();
let mut ret = Vec::new();
while let (Some(c1), Some(c2)) = (it1.peek(), it2.peek()) {
match c1.cmp(c2) {
Ordering::Equal => {
ret.push(**c1);
it1.next();
it2.next();
}
Ordering::Less => {
ret.push(**c1);
it1.next();
}
Ordering::Greater => {
ret.push(**c2);
it2.next();
}
};
}
while let Some(c) = it1.peek() {
ret.push(**c);
it1.next();
}
while let Some(c) = it2.peek() {
ret.push(**c);
it2.next();
}
Self(ret)
}
fn diff(&self, other: &Self) -> Charset {
let mut it1 = self.0.iter().peekable();
let mut it2 = other.0.iter().peekable();
let mut ret = Vec::new();
while let (Some(c1), Some(c2)) = (it1.peek(), it2.peek()) {
match c1.cmp(c2) {
Ordering::Equal => {
it1.next();
it2.next();
}
Ordering::Less => {
ret.push(**c1);
it1.next();
}
Ordering::Greater => {
it2.next();
}
};
}
while let Some(c) = it1.peek() {
ret.push(**c);
it1.next();
}
Self(ret)
}
fn len(&self) -> usize {
self.0.len()
}
fn is_empty(&self) -> bool {
self.0.is_empty()
}
fn chars(&self) -> &[char] {
&self.0
}
fn contains(&self, c: char) -> bool {
self.0.binary_search(&c).is_ok()
}
fn to_string(&self) -> String {
self.0.iter().collect::<String>()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_charset() {
let c1 = Charset::new("azerty");
let c2 = Charset::new("uiopqsqdf");
let c3 = Charset::new("hello, world");
assert!(!c1.intersects(&c2));
assert!(c1.intersects(&c3));
assert!(c2.intersects(&c3));
assert_eq!(c1.inter_len(&c2), 0);
assert_eq!(c1.inter_len(&c3), 2);
assert_eq!(c2.inter_len(&c3), 2);
assert_eq!(c1.inter(&c2), Charset::new(""));
assert_eq!(c1.inter(&c3), Charset::new("er"));
assert_eq!(c2.inter(&c3), Charset::new("od"));
assert_eq!(c1.union(&c2), Charset::new("azertyuiopqsdf"));
assert_eq!(c1.union(&c3), Charset::new("azertyhello, world"));
assert_eq!(c2.union(&c3), Charset::new("uiopqsdfhello, world"));
assert_eq!(c1.diff(&c2), Charset::new("azerty"));
assert_eq!(c1.diff(&c3), Charset::new("azty"));
assert_eq!(c2.diff(&c3), Charset::new("uipqsf"));
assert_eq!(c2.diff(&c1), Charset::new("uiopqsdf"));
assert_eq!(c3.diff(&c1), Charset::new("hllo, wold"));
assert_eq!(c3.diff(&c2), Charset::new("hell, wrl"));
}
}
|