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 aiueo
.
: あいうえお
k: かきくけこ
s: さしすせそ
t: たちつてと
n: なにぬねの
h: はひふへほ
m: まみむめも
y: や ゆ よ
r: らりるれろ
w: わ を
N: ん
(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 me
.
Exceptions
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:
し: shi
, not si
ち: chi
, not ti
つ: tsu
, not tu
ふ: fu
, not hu
Dakuten ゛
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:
k
→ g
s
→ z
t
→ d
h
→ b
The exceptions change too:
し゛: ji
(or zhi
), not zi
ち゛: ji
, not di
つ゛: dzu
, not du
(ふ゛ 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 b
.
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.
Small 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 ya
, yu
, and 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 kya
.
If placed after chi
or shi
(or their dakuten-ed forms), the y
is removed too. しゆ is shiyu
; しゅ is shu
.
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 kitta
.
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 ya
, yu
, and yo
not after a character with i
, small 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 r
with l
or a lone n
with m
. Output can have either one space between every syllable or no spaces at all.
This is code-golf: shortest code in bytes wins.
Test Cases
Many test cases for each individual part are given in the spec. Some additional cases:
ひらか゛な → hiragana
かたかな → katakana
た゛いき゛ゃくてんさいは゛ん → daigyakutensaiban
ふ゜ろく゛らみんく゛は゜す゛るこうと゛こ゛るふ → puroguramingupazurucoudogorufu
か゛んほ゛って → ganbatte
Notes
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.
きっった
? \$\endgroup\$っし
besshi
orshshi
? \$\endgroup\$I'm not at all sorry for the title. It's a masterpiece.
Downvoted \$\endgroup\$