Commit 3a88ccc5 authored by Thomas Dennis's avatar Thomas Dennis

Add more kana string stuff.

parent 92503272
......@@ -100,7 +100,8 @@ h1, h2, h3 { color:#800; }
th {
font-weight: bold;
font-size: 1.1em;
font-size: 12pt;
padding: 4px;
}
input {
......
......@@ -147,7 +147,7 @@ function check()
var answers = QUESTION[n].dictionary.conjugate(word, tense, flags[v][1], flags[v][2]);
var elem = document.getElementById(QUESTION[n].html_id + "_" + tense + flags[v][0]);
var guess = kana2latin(elem.value.toLowerCase()).replace(/ /g, "");
var guess = elem.value.toLowerCase().toRomaji().replace(/ /g, "");
elem.className = "wrong_answer";
for (var i = 0; i < answers.length; ++i)
......
/**
@file
@brief Defines some extra string methods for handling hiragana and katakana.
@todo Need a half-width to full-width katakana/romaji conversion function...
*/
'use strict';
/**
@brief Replace katakana characters with their hiragana equivalents.
The following characters are not handled, as they don't seem to have hiragana
equivalents:
+ ヷ (VA)
+ ヸ (VI)
+ ヹ (VE)
+ ヺ (VO)
@param in_reverse If `true`, convert hiragana to katakana instead.
@return A new string with katakana characters replaced by hiragana equivalents.
*/
function toHiragana(in_reverse=false)
{
var hiragana = {
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'',
'':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':'',
'':'','':'','':'','':'','':''
};
var text = this;
for (var k in hiragana)
{
if (in_reverse) { text = text.replace(new RegExp(hiragana[k], 'g'), k); }
else { text = text.replace(new RegExp(k, 'g'), hiragana[k]); }
}
return text;
}
/**
@brief Replace hiragana characters with their katakana equivalents.
This is really just a convenient wrapper around the `toHiragana()` function.
@param text The original text string.
@return A new string with hiragana characters replaced by katakana equivalents.
*/
function toKatakana() { return toHiragana.bind(this)(true); }
/**
@brief Convert hiragana and katakana text into latin format (Hepburn system).
The conversion process isn't particulary clever; No attempt is made to detect
cases where は or へ are being used as particles, and Kanji are simply ignored.
@todo What about "half-width" katakana or "wide" latin characters?
@param text The original text string.
@return A new string, in which kana characters have been replaced by latin text.
*/
function kana2latin(text)
function toRomaji()
{
var text = toHiragana.bind(this)();
// Handle basic characters first.
var charmap = {
'': 'a', '': 'i', '': 'u', '': 'e', '': 'o',
'': 'ka', '': 'ki', '': 'ku', '': 'ke', '': 'ko',
'': 'ga', '': 'gi', '': 'gu', '': 'ge', '': 'go',
'': 'sa', '': 'shi', '': 'su', '': 'se', '': 'so',
'': 'za', '': 'ji', '': 'zu', '': 'ze', '': 'zo',
'': 'ta', '': 'chi', '': 'tsu', '': 'te', '': 'to',
'': 'da', '': 'ji', '': 'zu', '': 'de', '': 'do',
'': 'na', '': 'ni', '': 'nu', '': 'ne', '': 'no',
'': 'ha', '': 'hi', '': 'fu', '': 'he', '': 'ho',
'': 'ba', '': 'bi', '': 'bu', '': 'be', '': 'bo',
'': 'pa', '': 'pi', '': 'pu', '': 'pe', '': 'po',
'': 'ma', '': 'mi', '': 'mu', '': 'me', '': 'mo',
'': 'ya', '': 'yu', '': 'yo', '': 'n', '': 'vu',
'': 'ra', '': 'ri', '': 'ru', '': 're', '': 'ro',
'': 'wa', '': 'wi', '': 'we', '': 'wo',
'': 'a', '': 'i', '': 'u', '': 'e', '': 'o',
'': 'ka', '': 'ki', '': 'ku', '': 'ke', '': 'ko',
'': 'ga', '': 'gi', '': 'gu', '': 'ge', '': 'go',
'': 'sa', '': 'shi', '': 'su', '': 'se', '': 'so',
'': 'za', '': 'ji', '': 'zu', '': 'ze', '': 'zo',
'': 'ta', '': 'chi', '': 'tsu', '': 'te', '': 'to',
'': 'da', '': 'ji', '': 'zu', '': 'de', '': 'do',
'': 'na', '': 'ni', '': 'nu', '': 'ne', '': 'no',
'': 'ha', '': 'hi', '': 'fu', '': 'he', '': 'ho',
'': 'ba', '': 'bi', '': 'bu', '': 'be', '': 'bo',
'': 'pa', '': 'pi', '': 'pu', '': 'pe', '': 'po',
'': 'ma', '': 'mi', '': 'mu', '': 'me', '': 'mo',
'': 'ya', '': 'yu', '': 'yo', '': 'n', '': 'vu',
'': 'ra', '': 'ri', '': 'ru', '': 're', '': 'ro',
'': 'wa', '': 'wi', '': 'we', '': 'wo',
'': '1', '': '2', '': '3', '': '4', '': '5',
'': '6', '': '7', '': '8', '': '9', '': '0',
'': '(', '': ')', '': ',', '': '.'
'':'a', '': 'i', '': 'u', '': 'e', '': 'o',
'':'ka', '': 'ki', '': 'ku', '': 'ke', '': 'ko',
'':'ga', '': 'gi', '': 'gu', '': 'ge', '': 'go',
'':'sa', '': 'shi', '': 'su', '': 'se', '': 'so',
'':'za', '': 'ji', '': 'zu', '': 'ze', '': 'zo',
'':'ta', '': 'chi', '': 'tsu', '': 'te', '': 'to',
'':'da', '': 'ji', '': 'zu', '': 'de', '': 'do',
'':'na', '': 'ni', '': 'nu', '': 'ne', '': 'no',
'':'ha', '': 'hi', '': 'fu', '': 'he', '': 'ho',
'':'ba', '': 'bi', '': 'bu', '': 'be', '': 'bo',
'':'pa', '': 'pi', '': 'pu', '': 'pe', '': 'po',
'':'ma', '': 'mi', '': 'mu', '': 'me', '': 'mo',
'':'ya', '': 'yu', '': 'yo',
'':'ra', '': 'ri', '': 'ru', '': 're', '': 'ro',
'':'wa', '': 'wi', '': 'we', '': 'wo', '': 'n',
'':'vu'
};
for (var c in charmap) { text = text.replace(new RegExp(c, 'g'), charmap[c]); }
// Next, handle compound characters.
var small_yx = {'':'ya','':'yu','':'yo','':'ya','':'yu','':'ye'};
var small_yx = {'':'ya','':'yu','':'yo'};
for (var c in small_yx)
{
......@@ -69,19 +113,27 @@ function kana2latin(text)
}
// Also handle "ヴォーカリスト" and friends.
var small_vx = {
'':'a','':'i','':'u','':'e','':'o',
'':'a','':'i','':'u','':'e','':'o'
};
var small_vx = {'':'a','':'i','':'u','':'e','':'o'};
for (var c in small_vx)
{ text = text.replace(new RegExp('vu'+c, 'g'), 'v'+small_vx[c].slice(-1)); }
{
text = text.replace(new RegExp('vu'+c, 'g'), 'v'+small_vx[c].slice(-1));
}
// Miscellaneous bits that need to be handled specially...
text = text.replace(new RegExp('', 'g'), '');
//TODO: Need to handle っ and ー here.
// Return the result.
return text;
}
/*
Make the functions available to all string instances as methods.
Comment these lines out if you don't want to pollute the String prototype with
extra methods; you can still call the functions using `toKatakana.bind("あ")()`
*/
String.prototype.toHiragana = toHiragana;
String.prototype.toKatakana = toKatakana;
String.prototype.toRomaji = toRomaji;
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