Gave names to a bunch of HG/SS areas.
[zzz-pokedex.git] / pokedex / roomaji.py
1 # encoding: utf8
2 """Provides `romanize()` for romanizing simple Japanese text."""
3
4 _roomaji_kana = {
5 u'ア': 'a', u'イ': 'i', u'ウ': 'u', u'エ': 'e', u'オ': 'o',
6 u'カ': 'ka', u'キ': 'ki', u'ク': 'ku', u'ケ': 'ke', u'コ': 'ko',
7 u'サ': 'sa', u'シ': 'shi', u'ス': 'su', u'セ': 'se', u'ソ': 'so',
8 u'タ': 'ta', u'チ': 'chi', u'ツ': 'tsu', u'テ': 'te', u'ト': 'to',
9 u'ナ': 'na', u'ニ': 'ni', u'ヌ': 'nu', u'ネ': 'ne', u'ノ': 'no',
10 u'ハ': 'ha', u'ヒ': 'hi', u'フ': 'fu', u'ヘ': 'he', u'ホ': 'ho',
11 u'マ': 'ma', u'ミ': 'mi', u'ム': 'mu', u'メ': 'me', u'モ': 'mo',
12 u'ヤ': 'ya', u'ユ': 'yu', u'ヨ': 'yo',
13 u'ラ': 'ra', u'リ': 'ri', u'ル': 'ru', u'レ': 're', u'ロ': 'ro',
14 u'ワ': 'wa', u'ヰ': 'wi', u'ヱ': 'we', u'ヲ': 'wo',
15 u'ン': 'n',
16 u'ガ': 'ga', u'ギ': 'gi', u'グ': 'gu', u'ゲ': 'ge', u'ゴ': 'go',
17 u'ザ': 'za', u'ジ': 'ji', u'ズ': 'zu', u'ゼ': 'ze', u'ゾ': 'zo',
18 u'ダ': 'da', u'ヂ': 'ji', u'ヅ': 'dzu', u'デ': 'de', u'ド': 'do',
19 u'バ': 'ba', u'ビ': 'bi', u'ブ': 'bu', u'ベ': 'be', u'ボ': 'bo',
20 u'パ': 'pa', u'ピ': 'pi', u'プ': 'pu', u'ペ': 'pe', u'ポ': 'po',
21 }
22
23 _roomaji_youon = {
24 u'ャ': 'ya', u'ュ': 'yu', u'ョ': 'yo',
25 #u'ゃ': 'ya', u'ゅ': 'yu', u'ょ': 'yo',
26 }
27
28 # XXX If romanize() ever handles hiragana, it will need to make sure that the
29 # preceding character was a katakana
30 # This does not include every small kana combination, but should include every
31 # one used in a Pokémon name. An exhaustive list would be.. very long
32 _roomaji_small_kana = {
33 u'ァ': 'a', u'ィ': 'i', u'ゥ': 'u', u'ェ': 'e', u'ォ': 'o',
34 }
35 _roomaji_small_kana_combos = {
36 u'チェ': 'che',
37 u'シェ': 'she',
38 u'テァ': 'tha', u'ティ': 'ti', u'テゥ': 'thu', u'テェ': 'tye', u'テォ': 'tho',
39 u'デァ': 'dha', u'ディ': 'di', u'デゥ': 'dhu', u'デェ': 'dye', u'デォ': 'dho',
40 u'ファ': 'fa', u'フィ': 'fi', u'ホゥ': 'hu', u'フェ': 'fe', u'フォ': 'fo',
41 }
42
43 def romanize(string):
44 """Converts a string of kana to roomaji."""
45
46 vowels = ['a', 'e', 'i', 'o', 'u', 'y']
47
48 characters = []
49 last_kana = None # Used for ー; っ or ッ; ん or ン
50 last_char = None # Used for small kana combos
51 for char in string:
52 # Full-width Latin
53 if ord(char) >= 0xff11 and ord(char) <= 0xff5e:
54 if last_kana == 'sokuon':
55 raise ValueError("Sokuon cannot precede Latin characters.")
56
57 char = chr(ord(char) - 0xff11 + 0x31)
58 characters.append(char)
59
60 last_kana = None
61
62 # Small vowel kana
63 elif char in _roomaji_small_kana:
64 combo = last_char + char
65 if combo in _roomaji_small_kana_combos:
66 characters[-1] = _roomaji_small_kana_combos[combo]
67
68 else:
69 # If we don't know what it is... act dumb and treat it as a
70 # full-size vowel. Better than bailing, and seems to occur a
71 # lot, e.g. ピィ is "pii"
72 characters.append(_roomaji_small_kana[char])
73
74 # Youon
75 elif char in _roomaji_youon:
76 if not last_kana or last_kana[-1] != 'i' or last_kana == 'i':
77 raise ValueError("Youon must follow an -i sound.")
78
79 # Drop the -i and append the ya/yu/yo sound
80 new_sound = _roomaji_youon[char]
81 if last_kana in ['chi', 'shi', 'ji']:
82 # Strip the y-
83 new_char = last_kana[:-1] + new_sound[1:]
84 else:
85 new_char = last_kana[:-1] + new_sound
86
87 characters[-1] = new_char
88 last_kana = new_char
89
90 # Sokuon
91 #elif char in (u'っ', u'ッ'):
92 elif char in (u'ッ',):
93 # Remember it and double the consonant next time around
94 last_kana = 'sokuon'
95
96 # Extended vowel or n
97 elif char == u'ー':
98 if last_kana[-1] not in vowels:
99 raise ValueError(u"'ー' must follow by a vowel.")
100 characters.append(last_kana[-1])
101
102 last_kana = None
103
104 # Regular ol' kana
105 elif char in _roomaji_kana:
106 kana = _roomaji_kana[char]
107
108 if last_kana == 'sokuon':
109 if kana[0] in vowels:
110 raise ValueError("Sokuon cannot precede a vowel.")
111
112 characters.append(kana[0])
113 elif last_kana == 'n' and kana[0] in vowels:
114 characters.append("'")
115
116 characters.append(kana)
117
118 last_kana = kana
119
120 # Not Japanese?
121 else:
122 if last_kana == 'sokuon':
123 raise ValueError("Sokuon must be followed by another kana.")
124
125 characters.append(char)
126
127 last_kana = None
128
129 last_char = char
130
131
132 if last_kana == 'sokuon':
133 raise ValueError("Sokuon cannot be the last character.")
134
135 return unicode(''.join(characters))