Added tests.

This commit is contained in:
doncr 2016-12-10 01:10:03 +00:00
parent f83acda5ab
commit 4a3f71ec93

View File

@ -134,7 +134,47 @@ var words = [
"word": "買い換える", "word": "買い換える",
"furigana": "買[か]い換[か]える", "furigana": "買[か]い換[か]える",
"kana": "かいかえる" "kana": "かいかえる"
} },
{
"pos": "verb",
"english": "to buy back",
"word": "買い戻す",
"furigana": "買[か]い戻[もど]す",
"kana": "かいもどす",
},
{
"pos": "verb",
"english": "to think",
"word": "思う",
"furigana": "思[おも]う",
"kana": "おもう"
},
{
"pos": "verb",
"english": "to go",
"word": "行く",
"furigana": "行[い]く",
"kana": "いく"
},
{
"pos": "verb",
"english": "to read",
"word": "読む",
"furigana": "読[よ]む",
"kana": "よむ"
},
{
"pos": "verb",
"english": "to wake up",
"word": "起きる",
"furigana": "起[お]きる",
"kana": "おきる"
},
] ]
var log = { "history": [] }; var log = { "history": [] };
@ -143,7 +183,9 @@ Array.prototype.randomElement = function () {
return this[Math.floor(Math.random() * this.length)] return this[Math.floor(Math.random() * this.length)]
} }
function getVerbForms(dictionary_form) { function getVerbForms(word) {
function aux(dictionary_form, dictionary_kana) {
var forms = new Object(); var forms = new Object();
@ -159,6 +201,22 @@ function getVerbForms(dictionary_form) {
forms["formal"] = "きます"; forms["formal"] = "きます";
forms["te-form"] = "きて"; forms["te-form"] = "きて";
// forms["dictionary"] = "来る";
// forms["formal"] = "来ます";
// forms["past"] = "来た";
// forms["formal past"] = "来ました";
// forms["negative"] = "来ない";
// forms["formal negative"] = "来ません";
// forms["past negative"] = "来なかった";
// forms["formal past negative"] = "来ませんでした";
// forms["te-form"] = "来て";
// forms["conditional"] = "来れば";
// forms["volitional"] = "来よう";
// forms["passive"] = "来られる";
// forms["causative"] = "来させる";
// forms["potential"] = "来られる";
// forms["imperative"] = "来い";
return forms; return forms;
} else if (dictionary_form == "する") { } else if (dictionary_form == "する") {
@ -174,9 +232,13 @@ function getVerbForms(dictionary_form) {
var group; var group;
if ((dictionary_form.slice(-1) == "る") && ( if (dictionary_form.slice(-2) == "する") {
(vowelPart[dictionary_form.slice(-2)[0]] == "い") || group = "suru";
(vowelPart[dictionary_form.slice(-2)[0]] == "え"))) { } else if (dictionary_form.slice(-2) == "くる") {
group = "kuru";
} else if ((dictionary_kana.slice(-1) == "る") && (
(vowelPart[dictionary_kana.slice(-2)[0]] == "い") ||
(vowelPart[dictionary_kana.slice(-2)[0]] == "え"))) {
group = "ichidan"; group = "ichidan";
@ -187,17 +249,24 @@ function getVerbForms(dictionary_form) {
// masu form (verbs) // masu form (verbs)
if (dictionary_form.slice(-1) == "る") { var masu_root;
forms["formal"] = dictionary_form.slice(0, -1) + "ます";
} else { if (group == "ichidan") {
forms["formal"] = dictionary_form.slice(0, -1) + masu_root = dictionary_form.slice(0, -1);
combineParts[consonantPart[dictionary_form.slice(-1)] + "い"] + "ます"; } if (group == "godan") {
masu_root = dictionary_form.slice(0, -1) + combineParts[consonantPart[dictionary_form.slice(-1)] + "い"];
} }
forms["formal"] = masu_root + "ます";
forms["formal past"] = masu_root + "ました";
forms["formal negative"] = masu_root + "ません";
forms["formal past negative"] = masu_root + "ませんでした";
forms["volitional"] = masu_root + "ましょう";
// te form // te form
if (dictionary_form == "いく") { if (dictionary_form == "く") {
forms["te-form"] = "いって"; forms["te-form"] = "って";
} else if (group == "godan") { } else if (group == "godan") {
switch (dictionary_form.slice(-1)) { switch (dictionary_form.slice(-1)) {
@ -248,8 +317,8 @@ function getVerbForms(dictionary_form) {
// past informal // past informal
if (dictionary_form == "く") { if (dictionary_form == "く") {
forms["past"] = "った"; forms["past"] = "った";
} else if (group == "godan") { } else if (group == "godan") {
switch (dictionary_form.slice(-1)) { switch (dictionary_form.slice(-1)) {
@ -297,6 +366,12 @@ function getVerbForms(dictionary_form) {
return forms; return forms;
} }
return {
"word" : aux(word["word"], word["kana"]),
"kana" : aux(word["kana"], word["kana"])
};
}
function getAdjectiveForms(dictionary_form) { function getAdjectiveForms(dictionary_form) {
var result = new Object(); var result = new Object();
@ -349,7 +424,6 @@ var verb_relative_form = {
"negative": "affirmative", "negative": "affirmative",
"past": "present", "past": "present",
// "past negative" : "present affirmative",
"formal": "informal", "formal": "informal",
"te-form": "informal", "te-form": "informal",
}, },
@ -357,51 +431,73 @@ var verb_relative_form = {
"negative": { "negative": {
"dictionary": "negative", "dictionary": "negative",
// "past" : "present negative",
"past negative": "present", "past negative": "present",
// "formal" : "informal negative", "formal negative": "informal",
"te-form": "negative", "te-form": "negative",
}, },
"past" : { "past" : {
"dictionary": "past", "dictionary": "past",
// "negative" : "past affirmative",
"past negative" : "affirmative", "past negative" : "affirmative",
// "formal" : "past informal", "formal past": "informal",
"te-form": "past", "te-form": "past",
}, },
"past negative" : { "past negative" : {
// "dictionary" : "past negative",
"negative": "past", "negative": "past",
"past": "negative", "past": "negative",
// "formal" : "informal past negative", "formal past negative": "informal",
"te-form": "past negative", "te-form": "past negative",
}, },
"formal": { "formal": {
"dictionary": "formal", "dictionary": "formal",
// "negative" : "formal affirmative", "formal past": "present",
// "past" : "present formal", "formal negative": "affirmative",
// "past negative" : "present formal affirmative",
"te-form": "formal", "te-form": "formal",
}, },
"formal past": {
"past": "formal",
"formal": "past",
"formal past negative": "affirmative",
"te-form": "formal past",
},
"formal negative": {
"negative": "formal",
"formal": "negative",
"formal past negative": "present",
"te-form": "formal negative",
},
"formal past negative": {
"past negative": "formal",
"formal past": "negative",
"formal negative": "present",
"te-form": "formal past negative",
},
}; };
function generateVerbQuestion() { function generateVerbQuestion() {
var entry = words.randomElement(); var entry = words.randomElement();
var kanjiForms = getVerbForms(entry["word"]); var forms = getVerbForms(entry);
var kanaForms = getVerbForms(entry["kana"]);
var kanjiForms = forms["word"];
var kanaForms = forms["kana"];
var to_form = Object.keys(verb_relative_form).randomElement(); var to_form = Object.keys(verb_relative_form).randomElement();
var from_form = Object.keys(verb_relative_form[to_form]).randomElement(); var from_form = Object.keys(verb_relative_form[to_form]).randomElement();
var question = "What is the " + verb_relative_form[to_form][from_form] + " form of " + kanjiForms[from_form] + "?"; var question = "What is the " + verb_relative_form[to_form][from_form] + " form of <span tooltip='" + kanaForms[from_form] + "'>" + kanjiForms[from_form] + "</span>?";
var answer = kanjiForms[to_form]; var answer = kanjiForms[to_form];
var answer2 = kanaForms[to_form]; var answer2 = kanaForms[to_form];
@ -410,7 +506,7 @@ function generateVerbQuestion() {
console.log("Question = " + question); console.log("Question = " + question);
console.log("Answer = " + answer); console.log("Answer = " + answer);
$('#question').text(question); $('#question').html(question);
window.question = question; window.question = question;
window.answer = answer; window.answer = answer;
@ -431,7 +527,11 @@ function generateVerbQuestion() {
function processAnswer() { function processAnswer() {
var response = $('#answer').val(); var response = $('#answer').val().trim();
if (response == "")
return;
var correct = ((response == window.answer) || (response == window.answer2)); var correct = ((response == window.answer) || (response == window.answer2));
var klass = correct ? "correct" : "incorrect"; var klass = correct ? "correct" : "incorrect";
@ -439,6 +539,7 @@ function processAnswer() {
"question" : window.question, "question" : window.question,
"response" : response, "response" : response,
"answer" : window.answer, "answer" : window.answer,
"kana" : window.answer2,
"correct" : correct "correct" : correct
}); });
@ -464,11 +565,20 @@ function processAnswer() {
} }
function updateHistoryView(log) { function updateHistoryView(log) {
var review = $('<table>'); var review = $('<table>');
var total = 0; var total = 0;
var correct = 0; var correct = 0;
var header_tr = $('<tr>');
header_tr.append($('<th>Question</th>'));
header_tr.append($('<th>Answer</th>'));
header_tr.append($('<th>Response</th>'));
review.append(header_tr);
log.history.forEach(function (entry) { log.history.forEach(function (entry) {
total++; total++;
@ -482,18 +592,20 @@ function updateHistoryView(log) {
var td1 = $('<td>'); var td1 = $('<td>');
var td2 = $('<td>'); var td2 = $('<td>');
var td3 = $('<td>'); var td3 = $('<td>');
var td4 = $('<td>');
td1.text(entry.question); td1.html(entry.question);
td2.text(entry.answer); td2.html("<span tooltip='" + entry.kana + "'>" + entry.answer + "</span>");
td3.text(entry.response); td3.text(entry.response);
td4.text(entry.correct);
tr.append(td1); tr.append(td1);
tr.append(td2); tr.append(td2);
tr.append(td3); tr.append(td3);
tr.append(td4);
if (entry.correct) {
td3.append("<span class='answer-correct'> ○</span>");
} else {
td3.append("<span class='answer-wrong'> ×</span>");
}
review.append(tr); review.append(tr);
}); });
@ -505,12 +617,170 @@ function updateHistoryView(log) {
function proceed() { function proceed() {
if (log.history.length == 10) { if (log.history.length == 10) {
console.log("Finished."); console.log("終わり。");
} else { } else {
generateVerbQuestion(); generateVerbQuestion();
} }
} }
function tests() {
function assertEqual(expected, actual) {
if (expected != actual) {
throw "Expected: " + expected + ", actual " + actual;
}
}
// Group 1 tests::::
//
// "飲む"
// "作る"
// "買い戻す"
//
// Group 2 tests
//
// "見る"
// "食べる"
// "起きる"
// "買い換える"
//
// // Special case tests
//
// "行く"
var ikuForms = getVerbForms( { "word" : "行く", "kana" : "いく" } );
var nomuForms = getVerbForms( { "word" : "飲む", "kana" : "のむ" } );
var tsukuruForms = getVerbForms( { "word" : "作る", "kana" : "つくる" } );
var kaimodosuForms = getVerbForms( { "word" : "買い戻す", "kana" : "かいもどす" } );
var miruForms = getVerbForms( { "word" : "見る", "kana" : "みる" } );
var taberuForms = getVerbForms( { "word" : "食べる", "kana" : "たべる" } );
var okiruForms = getVerbForms( { "word" : "起きる", "kana" : "おきる" } );
var kaikaeruForms = getVerbForms( { "word" : "買い換える", "kana" : "かいかえる" } );
assertEqual("行く", ikuForms["word"]["dictionary"]);
assertEqual("行きます", ikuForms["word"]["formal"]);
assertEqual("行かない", ikuForms["word"]["negative"]);
assertEqual("行きません", ikuForms["word"]["formal negative"]);
assertEqual("行った", ikuForms["word"]["past"]);
assertEqual("行って", ikuForms["word"]["te-form"]);
assertEqual("行きました", ikuForms["word"]["formal past"]);
assertEqual("行かなかった", ikuForms["word"]["past negative"]);
assertEqual("行きませんでした", ikuForms["word"]["formal past negative"]);
// assertEqual("行ける", ikuForms["word"]["Potential, -ru"]);
// assertEqual("行かれる", ikuForms["word"]["Passive, -reru"]);
// assertEqual("行かせる", ikuForms["word"]["Causative, -seru"]);
// assertEqual("行かせられる", ikuForms["word"]["Passive causative, -serareru"]);
// assertEqual("行け", ikuForms["word"]["Imperative"]);
assertEqual("飲む", nomuForms["word"]["dictionary"]);
assertEqual("飲みます", nomuForms["word"]["formal"]);
assertEqual("飲まない", nomuForms["word"]["negative"]);
assertEqual("飲みません", nomuForms["word"]["formal negative"]);
assertEqual("飲んだ", nomuForms["word"]["past"]);
assertEqual("飲んで", nomuForms["word"]["te-form"]);
assertEqual("飲みました", nomuForms["word"]["formal past"]);
assertEqual("飲まなかった", nomuForms["word"]["past negative"]);
assertEqual("飲みませんでした", nomuForms["word"]["formal past negative"]);
// assertEqual("飲める", nomuForms["word"]["Potential, -ru"]);
// assertEqual("飲まれる", nomuForms["word"]["Passive, -reru"]);
// assertEqual("飲ませる", nomuForms["word"]["Causative, -seru"]);
// assertEqual("飲ませられる", nomuForms["word"]["Passive causative, -serareru"]);
// assertEqual("飲め", nomuForms["word"]["Imperative"]);
assertEqual("作る", tsukuruForms["word"]["dictionary"]);
assertEqual("作ります", tsukuruForms["word"]["formal"]);
assertEqual("作らない", tsukuruForms["word"]["negative"]);
assertEqual("作りません", tsukuruForms["word"]["formal negative"]);
assertEqual("作った", tsukuruForms["word"]["past"]);
assertEqual("作って", tsukuruForms["word"]["te-form"]);
assertEqual("作りました", tsukuruForms["word"]["formal past"]);
assertEqual("作らなかった", tsukuruForms["word"]["past negative"]);
assertEqual("作りませんでした", tsukuruForms["word"]["formal past negative"]);
// assertEqual("作れる", tsukuruForms["word"]["Potential, -ru"]);
// assertEqual("作られる", tsukuruForms["word"]["Passive, -reru"]);
// assertEqual("作らせる", tsukuruForms["word"]["Causative, -seru"]);
// assertEqual("作らせられる", tsukuruForms["word"]["Passive causative, -serareru"]);
// assertEqual("作れ", tsukuruForms["word"]["Imperative"]);
assertEqual("買い戻す", kaimodosuForms["word"]["dictionary"]);
assertEqual("買い戻します", kaimodosuForms["word"]["formal"]);
assertEqual("買い戻さない", kaimodosuForms["word"]["negative"]);
assertEqual("買い戻しません", kaimodosuForms["word"]["formal negative"]);
assertEqual("買い戻した", kaimodosuForms["word"]["past"]);
assertEqual("買い戻して", kaimodosuForms["word"]["te-form"]);
assertEqual("買い戻しました", kaimodosuForms["word"]["formal past"]);
assertEqual("買い戻さなかった", kaimodosuForms["word"]["past negative"]);
assertEqual("買い戻しませんでした", kaimodosuForms["word"]["formal past negative"]);
// assertEqual("買い戻せる", kaimodosuForms["word"]["Potential, -ru"]);
// assertEqual("買い戻される", kaimodosuForms["word"]["Passive, -reru"]);
// assertEqual("買い戻させる", kaimodosuForms["word"]["Causative, -seru"]);
// assertEqual("買い戻させられる", kaimodosuForms["word"]["Passive causative, -serareru"]);
// assertEqual("買い戻せ", kaimodosuForms["word"]["Imperative"]);
assertEqual("見る", miruForms["word"]["dictionary"]);
assertEqual("見ます", miruForms["word"]["formal"]);
assertEqual("見ない", miruForms["word"]["negative"]);
assertEqual("見ません", miruForms["word"]["formal negative"]);
assertEqual("見た", miruForms["word"]["past"]);
assertEqual("見て", miruForms["word"]["te-form"]);
assertEqual("見ました", miruForms["word"]["formal past"]);
assertEqual("見なかった", miruForms["word"]["past negative"]);
assertEqual("見ませんでした", miruForms["word"]["formal past negative"]);
// assertEqual("見られる", miruForms["word"]["Potential, -rareru"]);
// assertEqual("見られる", miruForms["word"]["Passive, -rareru"]);
// assertEqual("見させる", miruForms["word"]["Causative, -saseru"]);
// assertEqual("見させられる", miruForms["word"]["Passive causative, -saserareru"]);
// assertEqual("見ろ", miruForms["word"]["Imperative, -ro"]);
assertEqual("食べる", taberuForms["word"]["dictionary"]);
assertEqual("食べます", taberuForms["word"]["formal"]);
assertEqual("食べない", taberuForms["word"]["negative"]);
assertEqual("食べません", taberuForms["word"]["formal negative"]);
assertEqual("食べた", taberuForms["word"]["past"]);
assertEqual("食べて", taberuForms["word"]["te-form"]);
assertEqual("食べました", taberuForms["word"]["formal past"]);
assertEqual("食べなかった", taberuForms["word"]["past negative"]);
assertEqual("食べませんでした", taberuForms["word"]["formal past negative"]);
// assertEqual("食べられる", taberuForms["word"]["Potential, -rareru"]);
// assertEqual("食べられる", taberuForms["word"]["Passive, -rareru"]);
// assertEqual("食べさせる", taberuForms["word"]["Causative, -saseru"]);
// assertEqual("食べさせられる", taberuForms["word"]["Passive causative, -saserareru"]);
// assertEqual("食べろ", taberuForms["word"]["Imperative, -ro"]);
assertEqual("起きる", okiruForms["word"]["dictionary"]);
assertEqual("起きます", okiruForms["word"]["formal"]);
assertEqual("起きない", okiruForms["word"]["negative"]);
assertEqual("起きません", okiruForms["word"]["formal negative"]);
assertEqual("起きた", okiruForms["word"]["past"]);
assertEqual("起きて", okiruForms["word"]["te-form"]);
assertEqual("起きました", okiruForms["word"]["formal past"]);
assertEqual("起きなかった", okiruForms["word"]["past negative"]);
assertEqual("起きませんでした", okiruForms["word"]["formal past negative"]);
// assertEqual("起きられる", okiruForms["word"]["Potential, -rareru"]);
// assertEqual("起きられる", okiruForms["word"]["Passive, -rareru"]);
// assertEqual("起きさせる", okiruForms["word"]["Causative, -saseru"]);
// assertEqual("起きさせられる", okiruForms["word"]["Passive causative, -saserareru"]);
// assertEqual("起きろ", okiruForms["word"]["Imperative, -ro"]);
assertEqual("買い換える", kaikaeruForms["word"]["dictionary"]);
assertEqual("買い換えます", kaikaeruForms["word"]["formal"]);
assertEqual("買い換えない", kaikaeruForms["word"]["negative"]);
assertEqual("買い換えません", kaikaeruForms["word"]["formal negative"]);
assertEqual("買い換えた", kaikaeruForms["word"]["past"]);
assertEqual("買い換えて", kaikaeruForms["word"]["te-form"]);
assertEqual("買い換えました", kaikaeruForms["word"]["formal past"]);
assertEqual("買い換えなかった", kaikaeruForms["word"]["past negative"]);
assertEqual("買い換えませんでした", kaikaeruForms["word"]["formal past negative"]);
// assertEqual("買い換えられる", kaikaeruForms["word"]["Potential, -rareru"]);
// assertEqual("買い換えられる", kaikaeruForms["word"]["Passive, -rareru"]);
// assertEqual("買い換えさせる", kaikaeruForms["word"]["Causative, -saseru"]);
// assertEqual("買い換えさせられる", kaikaeruForms["word"]["Passive causative, -saserareru"]);
// assertEqual("買い換えろ", kaikaeruForms["word"]["Imperative, -ro"]);
console.log("Complete.");
}
$('window').ready(function() { $('window').ready(function() {
generateVerbQuestion(); generateVerbQuestion();
}); });