-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
71 lines (61 loc) · 1.49 KB
/
Copy pathtest.js
File metadata and controls
71 lines (61 loc) · 1.49 KB
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
var fs = require('fs');
var pinyin = require("pinyin");
/*
var tarA = "w";
var tarB = "o3"
var audioA = document.createDocument("audioA");
audioA.src = "audio/" + tarA;
var audioB = document.createDocument("audioB");
audioB.src = "audio/" + tarB;
audioA.play();
audioB.play();
*/
function rand(top) {
var tmp = Math.floor(Math.random() * 100000);
tmp = tmp % top;
return tmp;
}
function genTone(target) {
var Tinitial = pinyin(target, {
style: pinyin.STYLE_INITIALS
})[0][0];
var py = pinyin(target, {
style: pinyin.STYLE_NORMAL
})[0][0];
var Tfinal = py.replace(Tinitial, "");
var tone = pinyin(target, {
style: pinyin.STYLE_TONE2
})[0][0].replace(py, "")
return {"normal": py, "initial": Tinitial, "final": Tfinal, "tone": tone};
}
function check(tar) {
if (!fs.existsSync("./audio/" + tar["initial"] + ".mp3")) {
return false;
}
if (!fs.existsSync("./audio/" + tar["final"] + tar["tone"] + ".mp3")) {
return false
}
return true;
}
function getNext() {
var data = {};
var text = fs.readFileSync('./source.in', "utf8");
do {
data["problem"] = text[rand(text.length - 1) + 1];
} while(! check(genTone(data["problem"])));
data["answer"] = rand(4) + 1;
for (var i = 1; i <= 4; i++) {
if (i === data["answer"]) {
data[i] = genTone(data["problem"]);
} else {
var tmp = genTone(text[rand(text.length - 1) + 1]);
while (! check(tmp)) {
tmp = genTone(text[rand(text.length - 1) + 1]);
}
data[i] = tmp;
}
}
return data;
}
var data = getNext();
console.log(data);