Commit ed1de39d authored by Thomas Dennis's avatar Thomas Dennis

Allow tenses other than past/present for verbs.

Also update the CSS a bit and output a score when the results are shown.
parent 94da24ec
......@@ -2,38 +2,39 @@
var ADJECTIVE_I = {
about: "~i adjective",
tense_list: ["present", "past"],
word_list:
[
{ "romaji": "takai", "kanji": "高い", "description": "high/expensive" }
],
conjugate: function(word, kwargs)
conjugate: function(word, tense, affirmative, formal)
{
if (kwargs.formal)
if (formal)
{
if (kwargs.negative)
if (!affirmative)
{
// TODO: Also allow "~ku nai desu" and "~ku nakatta desu" here.
if (kwargs.past_tense) { return word.slice(0, -1) + "ku arimasen deshita"; }
if (tense == "past") { return word.slice(0, -1) + "ku arimasen deshita"; }
else { return word.slice(0, -1) + "ku arimasen"; }
}
else
{
if (kwargs.past_tense) { return word.slice(0, -1) + "katta desu"; }
if (tense == "past") { return word.slice(0, -1) + "katta desu"; }
else { return word + " desu"; }
}
}
else
{
if (kwargs.negative)
if (!affirmative)
{
if (kwargs.past_tense) { return word.slice(0, -1) + "ku nakatta"; }
if (tense == "past") { return word.slice(0, -1) + "ku nakatta"; }
else { return word.slice(0, -1) + "ku nai"; }
}
else
{
if (kwargs.past_tense) { return word.slice(0, -1) + "katta"; }
if (tense == "past") { return word.slice(0, -1) + "katta"; }
else { return word; }
}
}
......
......@@ -2,6 +2,7 @@
var ADJECTIVE_NA = {
about: "~na adjective",
tense_list: ["present", "past"],
word_list:
[
......@@ -25,32 +26,32 @@ var ADJECTIVE_NA = {
{ "romaji": "hadena", "kanji": "派手な", "description": "showy" }
],
conjugate: function(word, kwargs)
conjugate: function(word, tense, affirmative, formal)
{
word = word.slice(0, -2); // Remove the "na" from the end.
if (kwargs.formal)
if (formal)
{
if (kwargs.negative)
if (!affirmative)
{
if (kwargs.past_tense) { return word + " dewa arimasen deshita"; }
if (tense == "past") { return word + " dewa arimasen deshita"; }
else { return word + " dewa arimasen"; }
}
else
{
if (kwargs.past_tense) { return word + " deshita"; }
if (tense == "past") { return word + " deshita"; }
else { return word + " desu"; }
}
}
else
{
if (kwargs.negative)
if (!affirmative)
{
if (kwargs.past_tense) { return word + " dewa nakatta"; }
if (tense == "past") { return word + " dewa nakatta"; }
else { return word + " dewa nai"; }
}
else
{
if (kwargs.past_tense) { return word + " datta"; }
if (tense == "past") { return word + " datta"; }
else { return word + " da"; }
}
}
......
......@@ -13,8 +13,9 @@
<body onload="setup()">
<h1>Japanese Conjugations Quiz</h1>
<p>
Based on <a href="http://japanese.about.com/library/weekly/aa031101b.htm">this</a>
and <a href="http://japanese.about.com/library/weekly/aa040101b.htm">this</a>.
Based on <a href="http://japanese.about.com/library/weekly/aa031101b.htm">this</a>,
<a href="http://japanese.about.com/library/weekly/aa040101b.htm">this</a>, and
<a href="http://www.csse.monash.edu.au/~jwb/cgi-bin/wwwjdic.cgi">this</a>.
</p>
<hr>
<div id="quizarea">
......@@ -24,6 +25,7 @@
<p>
<a class="jslink" href="#" onclick="check()"><b>Check Results</b></a>
<a class="jslink" href="#" onclick="reset()">Reset</a>
<span id="score"></span>
</p>
<hr>
<small>
......
......@@ -12,10 +12,8 @@ var QUESTION = [
function setup()
{
var LABEL = ["present", "present_negative", "past", "past_negative"];
var parent = document.getElementById("quizarea");
// Generate some HTML elements for each question.
var parent = document.getElementById("quizarea");
for (var n = 0; n < QUESTION.length; ++n)
{
......@@ -33,27 +31,35 @@ function setup()
// This is the actual "answer grid" where text can be entered.
var table = root.appendChild(document.createElement('table'));
for (var y = 0; y < 5; ++y)
for (var y = 0; y < (QUESTION[n].dictionary.tense_list.length * 2) + 1; ++y)
{
var row = table.appendChild(document.createElement('tr'));
for (var x = 0; x < 3; ++x)
{
var td = row.appendChild(document.createElement(x && y ? "td" : "th"));
if (y == 0)
if (y > 0)
{
// This is the top row of the table.
td.appendChild(document.createTextNode(["*", "Informal", "Formal"][x]));
var label = QUESTION[n].dictionary.tense_list[Math.floor((y-1)/2)];
if (y % 2 == 0) { label += "_negative"; }
if (x > 0)
{
// Any other cells are for answers.
var input = td.appendChild(document.createElement("input"));
input.id = QUESTION[n].html_id + "_" + label + (x < 2 ? "" : "_formal");
}
else if (x == 0)
else
{
// This is the left-hand column.
td.appendChild(document.createTextNode(LABEL[y-1].replace(/_/, " ")));
td.appendChild(document.createTextNode(label.replace(/_/g, " ")));
}
}
else
{
// Any other cells are for answers.
var input = td.appendChild(document.createElement("input"));
input.id = QUESTION[n].html_id + "_" + LABEL[y-1] + (x < 2 ? "" : "_formal");
// This is the top row of the table.
td.appendChild(document.createTextNode(["*", "Informal", "Formal"][x]));
}
}
}
......@@ -73,6 +79,9 @@ function setup()
function reset()
{
// Reset the score.
document.getElementById("score").innerHTML = "";
// Reset the answer boxes so that they're all blank again.
var input = document.getElementsByTagName("input");
for (var i = 0; i < input.length; ++i)
......@@ -110,45 +119,54 @@ function reset()
function check()
{
// This maps the HTML element IDs to the correct validation flags.
var flag_set = {
"_present": { "past_tense": false, "negative" : false, "formal": false },
"_present_negative": { "past_tense": false, "negative" : true, "formal": false },
"_past": { "past_tense": true, "negative" : false, "formal": false },
"_past_negative": { "past_tense": true, "negative" : true, "formal": false },
"_present_formal": { "past_tense": false, "negative" : false, "formal": true },
"_present_negative_formal": { "past_tense": false, "negative" : true, "formal": true },
"_past_formal": { "past_tense": true, "negative" : false, "formal": true },
"_past_negative_formal": { "past_tense": true, "negative" : true, "formal": true }
}
var flags = [
["", true, false],
["_formal", true, true],
["_negative", false, false],
["_negative_formal", false, true]
];
// Loop through all answers for each question and validate each one.
var score = 0;
var total = 0;
for (var n = 0; n < QUESTION.length; ++n)
{
var errors = [];
for (var a in flag_set)
// Loop through each tense, such as "present", "past", etc.
for (var t = 0; t < QUESTION[n].dictionary.tense_list.length; ++t)
{
for (var v = 0; v < flags.length; ++v)
{
var elem = document.getElementById(QUESTION[n].html_id + a);
var word = QUESTION[n]["current_word"];
var expected = QUESTION[n].dictionary.conjugate(word, flag_set[a]);
var tense = QUESTION[n].dictionary.tense_list[t];
var expected = QUESTION[n].dictionary.conjugate(word, tense, flags[v][1], flags[v][2]);
var elem = document.getElementById(QUESTION[n].html_id + "_" + tense + flags[v][0]);
if (elem.value.toLowerCase().replace(/ /g, "") != expected.toLowerCase().replace(/ /g, ""))
{
elem.className = "wrong_answer";
// Explain WHY the answer was considered to be incorrect.
var message = "The "
message += flag_set[a].formal ? "formal, " : "informal, ";
if (flag_set[a].negative) { message += "negative-"; }
message += flag_set[a].past_tense ? "past-tense" : "present-tense";
message += " conjugation of " + word + " is <b>" + expected + "</b>."
var message = "The <b>" + (flags[v][2] ? "formal, " : "informal, ");
if (!flags[v][1]) { message += "negative-"; }
message += tense + "</b> conjugation should be <b>" + expected + "</b>."
errors.push(message)
} else { elem.className = "right_answer"; }
}
else
{
elem.className = "right_answer";
++score;
}
++total;
}
}
......@@ -163,4 +181,7 @@ function check()
}
// Show the score.
document.getElementById("score").innerHTML = "Score: " + String(score) + "/" + String(total);
}
body { background: #EEF; font-family: sans; font-size:small; }
h1 { margin: 0; text-align: center; }
h1 { margin: 0; text-align: center; color:#800; }
h2 { color:#800; }
table
{
......@@ -23,3 +24,11 @@ table
.blank_answer { background: #FFF; color: #000; }
.right_answer { background: #AFA; color: #000; }
.wrong_answer { background: #FAA; color: #000; }
#score
{
padding: 8px;
color: #008;
font-size: large;
font-weight: bold;
}
......@@ -2,6 +2,7 @@
var VERB_ERU = {
about: "Group 2 (Ichidan) ~iru/eru verb",
tense_list: ["present", "past", "potential"],
word_list:
[
......@@ -17,33 +18,50 @@ var VERB_ERU = {
{ "romaji": "taberu", "kanji": "食べる", "description": "to eat" }
],
conjugate: function(word, kwargs)
conjugate: function(word, tense, affirmative, formal)
{
if (kwargs.formal)
switch (tense)
{
if (kwargs.negative)
case "present":
if (formal)
{
if (kwargs.past_tense) { return word.slice(0, -2) + "masen deshita"; }
if (affirmative) { return word.slice(0, -2) + "masu"; }
else { return word.slice(0, -2) + "masen"; }
}
else
{
if (kwargs.past_tense) { return word.slice(0, -2) + "mashita"; }
else { return word.slice(0, -2) + "masu"; }
if (affirmative) { return word; }
else { return word.slice(0, -2) + "nai"; }
}
break;
case "past":
if (formal)
{
if (affirmative) { return word.slice(0, -2) + "mashita"; }
else { return word.slice(0, -2) + "masen deshita"; }
}
else
{
if (kwargs.negative)
if (affirmative) { return word.slice(0, -2) + "ta"; }
else { return word.slice(0, -2) + "nakatta"; }
}
break;
case "potential":
if (formal)
{
if (kwargs.past_tense) { return word.slice(0, -2) + "nakatta"; }
else { return word.slice(0, -2) + "nai"; }
if (affirmative) { return word.slice(0, -2) + "raremasu"; }
else { return word.slice(0, -2) + "raremasen"; }
}
else
{
if (kwargs.past_tense) { return word.slice(0, -2) + "ta"; }
else { return word; }
if (affirmative) { return word.slice(0, -2) + "rareru"; }
else { return word.slice(0, -2) + "rarenai"; }
}
break;
}
}
......
......@@ -2,6 +2,7 @@
var VERB_U = {
about: "Group 1 (Godan) ~u verb",
tense_list: ["present", "past", "potential"],
word_list:
[
......@@ -21,31 +22,36 @@ var VERB_U = {
{ "romaji": "yomu", "kanji": "読む", "description": "to read" }
],
conjugate: function(word, kwargs)
conjugate: function(word, tense, affirmative, formal)
{
if (kwargs.formal)
// As a special case, words ending with "[vowel]u" should use WA not A.
var negative_stem = word.slice(0, -1);
if (negative_stem.slice(-1).match(/[AEIOUaeiou]/)) { negative_stem += "w"; }
switch (tense)
{
if (kwargs.negative)
case "present":
if (formal)
{
if (kwargs.past_tense) { return word.slice(0, -1) + "imasen deshita"; }
if (affirmative) { return word.slice(0, -1) + "imasu"; }
else { return word.slice(0, -1) + "imasen"; }
}
else
{
if (kwargs.past_tense) { return word.slice(0, -1) + "imashita"; }
else { return word.slice(0, -1) + "imasu"; }
}
if (affirmative) { return word; }
else { return negative_stem + "anai"; }
}
else
{
if (kwargs.negative)
break;
case "past":
if (formal)
{
// As a special case, words ending with "[vowel]u" should use WA not A.
var verb_stem = word.slice(0, -1);
if (verb_stem.slice(-1).match(/[AEIOUaeiou]/)) { verb_stem += "w"; }
return verb_stem + (kwargs.past_tense ? "anakatta" : "anai");
if (affirmative) { return word.slice(0, -1) + "imashita"; }
else { return word.slice(0, -1) + "imasen deshita"; }
}
else if (kwargs.past_tense)
else if (affirmative)
{
// Handle "tsu" first - "su" is handled below.
......@@ -64,7 +70,23 @@ var VERB_U = {
// If all else, fails, assume it ends with "u".
return word.slice(0, -1) + "tta";
} else { return word; }
}
else { return negative_stem + "anakatta"; }
break;
case "potential":
if (formal)
{
if (affirmative) { return word.slice(0, -1) + "emasu"; }
else { return word.slice(0, -1) + "emasen"; }
}
else
{
if (affirmative) { return word.slice(0, -1) + "eru"; }
else { return word.slice(0, -1) + "enai"; }
}
break;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment