Romanization of Japanese is converting Japanese text into Latin characters. In this challenge, you will be given a string of Japanese characters as input and expected to convert them to the correct ASCII string.
What You'll Need To Know
The Japanese language has three writing systems: hiragana (the curvy one used for short words), katakana (the angle-y one used for sounds and words borrowed from other langauges), and kanji (the dense characters originally from Chinese). In this challenge we will only worry about hiragana.
There are 46 characters in the hiragana syllabary. Each character represents a syllable. The characters are organized by first sound (consonant) and second sound (vowel). The columns in order are
y: や ゆ よ
w: わ を
(if you copy and paste this table note that I have used ideographic spaces U+3000 to space out y and w)
So, for instance, あとめ should produce an output of
atome. The first character is
a, the second is
to, and the third is
Like any good language, Japanese has exceptions to its rules, and the hiragana table has several. These characters are pronounced slightly differently than their location in the table would imply:
The word 'dakuten' means 'muddy mark': the dakuten turns sounds into their voiced equivalents (usually); for example, か
ka turns into か゛
ga. A full list of the changes:
The exceptions change too:
(ふ゛ acts as you would expect; it is not an exception)
The handakuten is an additional character ゜ that applies to the
h row. If placed after a character, it changes the character's sound to
p rather than
Both the dakuten and handakuten are going to be given as individual characters. You will not need to deal with the precomposed forms or the combining characters.
Finally, there are small versions of some of the characters. They modify characters that come before or after them.
These are the small forms of
yo. They are only placed after sounds in the
i-column; they remove the
i and add their sound. So, きや turns into
kiya; きゃ turns into
If placed after
shi (or their dakuten-ed forms), the
y is removed too. しゆ is
shiyu; しゅ is
The last thing you'll have to deal with is the small
tsu. っ doubles the consonant that comes after it, no matter what; it does nothing else. For instance, きた is
kita; きった is
Summary, Input, and Output
Your program must be able to transliterate: the 46 basic hiragana, their dakuten and handakuten forms, and their combinations with small characters.
Undefined behavior includes: small
yo not after a character with
tsu at the end of a string, dakuten on an unaffected character, handakuten on a non-
p character, and anything else not mentioned in the above spec/introduction.
You may assume all inputs are valid and contain only the Japanese characters mentioned above.
Case does not matter in output; you may also replace
l or a lone
m. Output can have either one space between every syllable or no spaces at all.
This is code-golf: shortest code in bytes wins.
Many test cases for each individual part are given in the spec. Some additional cases:
I do not know much Japanese besides what I've written here. Please let me know if I've made any mistakes.
I was originally planning to include katakana too (so my English transliteration test case could be slightly more accurate), but that would be too much for a code golf challenge.
The Unicode names include the transliteration of each character individually, but without the exceptions. This may or may not be helpful to you.
Thanks to squeamishossifrage for correcting two typos!
I'm sorry if this is too long; I attempted to fit most of the quirks of hiragana into the challenge but some things (like small vowel-only hiragana, changing n to m in front of some consonants, and the repetition mark) had to be cut to keep the challenge manageable.
I'm not at all sorry for the title. It's a masterpiece.