Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1281x 1281x 338x 338x 1x 427x 75x 352x 1x 70x 8x 70x 4x 70x 4x 70x 4x 70x 20x 70x 2x 70x 6x 70x 1x 70x 10x 70x 4x 70x 5x 70x 770x 68x 1x 60x 55x 60x 4x 60x 21x 61x 61x 1x 60x 180x 80x 1x 427x 34x 34x 427x 70x 70x 427x 61x 427x 10x 10x 2x 427x 13x 3x 1x 427x 2135x 188x 1x 149x 427x 427x 9821x 355x 2562x 396x 427x 427x 427x 427x 427x 427x 8x 419x 75x 344x | const map = {
consonant: {
k: [..."かきくけこ"],
s: [..."さすせそ"],
sh: ["し",],
t: [..."たてと"],
ts: ["つ",],
ch: ["ち",],
n: [..."なにぬねのん"],
h: [..."はひへほ"],
m: [..."まみむめも"],
y: [..."やゆよ"],
ySupPre: [..."ゃゅょ"],
supPre: [..."ぁぃぅぇぉゎ"],
r: [..."らりるれろ"],
w: [..."わを",],
f: ["ふ"],
p: [..."ぱぴぷぺぽ"],
b: [..."ばびぶべぼ"],
j: [..."じぢ"],
z: [..."ざずぜぞ"],
d: [..."だでど"],
g: [..."がぎぐげご"],
repeatNextConsonant: ["っ"],
v: ["ゔ"],
},
vowel: {
a: [..."あかがさざただなはばぱまやらわぁゃゎ"],
i: [..."いきぎしじちぢにひびぴみりぃ"],
u: [..."うくぐすずつづぬふぶぷむゆるぅゅゔ"],
e: [..."えけげせぜてでねへべぺめれぇ"],
o: [..."おこごそぞとどのほぼぽもよろをょぉ"],
"-": ["ー"]
},
longVowel: {
a: 'ā',
i: 'ī',
u: 'ū',
e: 'ē',
o: 'ō',
} as { [key: string]: string }
};
declare interface tempObj {
consonant: string,
vowel?: string,
katakana: boolean,
through: boolean,
char: string,
}
const [lowerKatakana, upperKatakana] = [0x30A1, 0x30FB];
function isKatakana(ji: string, extended = false): boolean {
const code = ji.codePointAt(0);
return code! >= lowerKatakana && code! < (extended ? 0x30FF : upperKatakana);
}
function isHiragana(ji: string): boolean {
const code = ji.codePointAt(0);
return code! >= 0x3041 && code! <= 0x309F;
}
const noHiraganaCorrespondant = 'ヷヸヹヺ・ーヿ';
function convHiragana(ji: string): string {
if (isKatakana(ji) && !noHiraganaCorrespondant.includes(ji)) {
return String.fromCharCode(ji.codePointAt(0)! - 0x60);
}
return ji;
}
interface Strategy {
matches: (previous: tempObj, vowel: string) => boolean;
doWork: (previous: tempObj, curr: tempObj) => void;
}
interface GlobalStrategy {
matches: (prev: tempObj, curr: tempObj, next: tempObj) => boolean;
doWork: (prev: tempObj, curr: tempObj, next: tempObj) => void;
}
class SmallAiueoStrategies {
static strategies: Strategy[] = [
// JE, CHE, SHE
{
matches: (previous, vowel) => ['j', 'ch', 'sh'].includes(previous.consonant) && previous.vowel === 'i' && vowel === 'e',
doWork: (previous) => delete previous.vowel,
},
// DU, TU
{
matches: (previous, vowel) => [...'td'].includes(previous.consonant) && previous.vowel === 'o' && vowel === 'u',
doWork: (previous) => delete previous.vowel,
},
// DI, TI
{
matches: (previous, vowel) => [...'td'].includes(previous.consonant) && previous.vowel === 'e' && vowel === 'i',
doWork: (previous) => delete previous.vowel,
},
// SI
{
matches: (previous, vowel) => ['s', 'z'].includes(previous.consonant) && previous.vowel === 'u' && vowel === 'i',
doWork: (previous) => delete previous.vowel,
},
// Fa, Fi, Fu, Fe, Fo, Va, Vi, Vu, Ve, Vo
{
matches: (previous, vowel) => [..."fv"].includes(previous.consonant),
doWork: (previous) => delete previous.vowel,
},
// YE
{
matches: (previous, vowel) => previous.consonant === '' && previous.vowel === 'i' && vowel === 'e',
doWork: (previous) => previous.vowel = 'y',
},
// WO, WI, WE
{
matches: (previous, vowel) => previous.consonant === '' && previous.vowel === 'u' && [...'oei'].includes(vowel),
doWork: (previous) => previous.vowel = 'w',
},
// TWA
{
matches: (previous, vowel) => previous.consonant === 't' && previous.vowel === 'o' && vowel !== 'u',
doWork: (previous) => previous.vowel = 'w',
},
// GWA, KWA
{
matches: (previous, vowel) => ['k','g'].includes(previous.consonant) && previous.vowel === 'u' && vowel !== 'u',
doWork: (previous) => previous.vowel = 'w',
},
// HYE, BYE, PYE, KYE
{
matches: (previous, vowel) => [...'bhkp'].includes(previous.consonant) && previous.vowel === 'i' && vowel === 'e',
doWork: (previous) => previous.vowel = 'y',
},
// TSA, TSI, TSE, TSO
{
matches: (previous, vowel) => previous.consonant === 'ts' && previous.vowel === 'u',
doWork: (previous) => delete previous.vowel,
},
];
static doWork(previous: tempObj, curr: tempObj): void {
SmallAiueoStrategies.strategies
.filter(strategy => strategy.matches(previous, curr.vowel!))
.forEach(strategy => strategy.doWork(previous, curr));
}
}
class SmallYayuyoStrategies {
static strategies: Strategy[] = [
{
matches: (previous, vowel) => ["i", "e"].includes(previous.vowel!),
doWork: (previous) => delete previous.vowel,
},
{
matches: (previous, vowel) => [..."fv"].includes(previous.consonant) && previous.vowel === "u",
doWork: (previous) => delete previous.vowel,
},
{
matches: (previous, vowel) => ["sh", "ch", "j"].includes(previous.consonant),
doWork: (previous, curr) => curr.consonant = ""
},
];
static doWork(previous: tempObj, curr: tempObj): void {
curr.consonant = "y";
if (!previous) {
return;
}
SmallYayuyoStrategies.strategies
.filter(strategy => strategy.matches(previous, curr.vowel!))
.forEach(strategy => strategy.doWork(previous, curr));
}
}
class GlobalStrategies {
static strategies: GlobalStrategy[] = [
{
matches: (prev, curr, next) => (curr.vowel === '-' && prev && !!prev.vowel)
|| (curr.vowel === prev?.vowel && !curr.consonant)
|| (curr.vowel === 'u' && !curr.consonant && prev?.vowel === 'o'),
doWork: (prev, curr, next) => {
prev.vowel = map.longVowel[prev.vowel || ''];
curr.vowel = undefined;
}
},
{
matches: (prev, curr, next) => curr.consonant === "supPre",
doWork: (prev, curr, next) => {
SmallAiueoStrategies.doWork(prev, curr);
curr.consonant = "";
}
},
{
matches: (prev, curr, next) => curr.consonant === "ySupPre",
doWork: (prev, curr, next) => SmallYayuyoStrategies.doWork(prev, curr),
},
{
matches: (prev, curr, next) => curr.consonant === "repeatNextConsonant",
doWork: (prev, curr, next) => {
curr.consonant = next?.consonant.charAt(0) || "";
if (next?.consonant === "ch") {
curr.consonant = "t";
}
}
},
{
matches: (prev, curr, next) => curr.consonant === "n" && !curr.vowel,
doWork: (prev, curr, next) => {
if (next && (!next.consonant || next.consonant === 'y')) {
curr.consonant = "n’";
}
}
}
];
static doWork = (prev: tempObj, curr: tempObj, next: tempObj): void =>
GlobalStrategies.strategies
.filter(strategy => strategy.matches(prev, curr, next))
.forEach(strategy => strategy.doWork(prev, curr, next))
}
/**
* Hiragana is transformed into lower case output.
* Katakana is transformed into UPPER case output.
* ```js
* toRomaji('ローマじ') // === 'RŌMAji';
* ```
* The sequences of same vowels and 'ou' output with prolonged vowel of the first.
* The sequence of 'ei' is not represented with long 'e'.
* ex)
* - 'ちちゅうかい', 'chichūkai'
* - 'ふりょう', 'furyō'
* - 'しょうがくせい', 'shōgakusei'
*
* Caveat: The rule to not consider as prolonged vowel sound
* when the consecutive vowels are from separate Kanjis is not respected.
*
* ex) 'きいはんとうのばあい'
* - OK: 'kiihantōnobaai'
* - Our _wrong_ output: 'kīhantōnobāi'
*
* 'おかあさん'
* - should be and is 'okāsan'
*
* 'とおり'
* - should be and is 'tōri'
* @param kana The input with Hiragana/Katakana
* @returns Romaji representation of the input
*/
export function toRomaji(kana: string): string {
return [...kana]
.map((ji) => ({
katakana: isKatakana(ji),
ji: convHiragana(ji),
char: ji,
through: !isKatakana(ji, true) && !isHiragana(ji)
}))
.map(({ ji, katakana, through, char }) =>
({
consonant: Object.entries(map.consonant)
.filter(([, v]) => v.includes(ji))
.map(([k,]) => k).join(),
vowel: Object.entries(map.vowel)
.filter(([, v]) => v.includes(ji))
.map(([k,]) => k).join(),
katakana,
through,
char,
} as tempObj))
.reduce((target, curr, i, arr) => {
const next = arr[i + 1];
const previous = target[i - 1];
GlobalStrategies.doWork(previous, curr, next);
target.push(curr);
return target;
}, [] as tempObj[])
.map(({ consonant, vowel, katakana, through, char }) => {
if (through) {
return char;
}
if (katakana) {
return (consonant + (vowel || "")).toUpperCase();
}
return consonant + (vowel || "");
})
.join("");
} |