Here's a function that does the reverse conversion from Hangul to QWERTY keys:
def hangul_to_latin(text):
# Reverse mappings from Korean to QWERTY
consonants_to_latin = {
'ㄱ': 'r', 'ㄲ': 'R', 'ㄴ': 's', 'ㄷ': 'e', 'ㄸ': 'E',
'ㄹ': 'f', 'ㅁ': 'a', 'ㅂ': 'q', 'ㅃ': 'Q', 'ㅅ': 't',
'ㅆ': 'T', 'ㅇ': 'd', 'ㅈ': 'w', 'ㅉ': 'W', 'ㅊ': 'c',
'ㅋ': 'z', 'ㅌ': 'x', 'ㅍ': 'v', 'ㅎ': 'g'
}
vowels_to_latin = {
'ㅏ': 'k', 'ㅐ': 'o', 'ㅑ': 'i', 'ㅒ': 'O', 'ㅓ': 'j',
'ㅔ': 'p', 'ㅕ': 'u', 'ㅖ': 'P', 'ㅗ': 'h', 'ㅘ': 'hk',
'ㅙ': 'ho', 'ㅚ': 'hl', 'ㅛ': 'y', 'ㅜ': 'n', 'ㅝ': 'nj',
'ㅞ': 'np', 'ㅟ': 'nl', 'ㅠ': 'b', 'ㅡ': 'm', 'ㅢ': 'ml',
'ㅣ': 'l'
}
# Final consonants (받침) to Latin
final_consonants_to_latin = {
'ㄱ': 'r', 'ㄲ': 'R', 'ㄳ': 'rt', 'ㄴ': 's', 'ㄵ': 'sw',
'ㄶ': 'sg', 'ㄷ': 'e', 'ㄹ': 'f', 'ㄺ': 'fr', 'ㄻ': 'fa',
'ㄼ': 'fq', 'ㄽ': 'ft', 'ㄾ': 'fx', 'ㄿ': 'fv', 'ㅀ': 'fg',
'ㅁ': 'a', 'ㅂ': 'q', 'ㅄ': 'qt', 'ㅅ': 't', 'ㅆ': 'T',
'ㅇ': 'd', 'ㅈ': 'w', 'ㅊ': 'c', 'ㅋ': 'z', 'ㅌ': 'x',
'ㅍ': 'v', 'ㅎ': 'g'
}
# Lists of Jamo for checking/indexing
initial_consonants = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
medial_vowels = ['ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ']
final_consonants = [None, 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
result = []
for char in text:
char_code = ord(char)
# Check if it's a Hangul syllable
if 0xAC00 <= char_code <= 0xD7A3:
# Decompose the syllable
char_code_offset = char_code - 0xAC00
jongseong_index = char_code_offset % 28
jungseong_index = ((char_code_offset - jongseong_index) // 28) % 21
choseong_index = ((char_code_offset - jongseong_index) // 28) // 21
# Get the corresponding Jamo
choseong = initial_consonants[choseong_index]
jungseong = medial_vowels[jungseong_index]
jongseong = final_consonants[jongseong_index] if jongseong_index > 0 else None
# Convert to Latin keys
latin_cho = consonants_to_latin[choseong]
latin_jung = vowels_to_latin[jungseong]
latin_jong = final_consonants_to_latin[jongseong] if jongseong else ""
result.append(latin_cho + latin_jung + latin_jong)
# For single Jamo characters (not part of a syllable)
elif 0x1100 <= char_code <= 0x11FF:
# Check what kind of Jamo it is
if char in consonants_to_latin:
result.append(consonants_to_latin[char])
elif char in vowels_to_latin:
result.append(vowels_to_latin[char])
else:
# Unknown Jamo, pass through
result.append(char)
# Handle individual Jamo in Compatibility Jamo block
elif 0x3131 <= char_code <= 0x318E:
if char in consonants_to_latin:
result.append(consonants_to_latin[char])
elif char in vowels_to_latin:
result.append(vowels_to_latin[char])
else:
# Unknown compatibility Jamo, pass through
result.append(char)
# Non-Hangul character, pass through unchanged
else:
result.append(char)
return ''.join(result)
# Example usage
if __name__ == "__main__":
# Test cases
print(hangul_to_latin("안녕")) # dkssud
print(hangul_to_latin("안녕하세요")) # dkssudgktpdy
print(hangul_to_latin("한글")) # gksrmf
print(hangul_to_latin("Hello 안녕")) # Hello dkssud
This function does the reverse of the previous one:
- It breaks down each Hangul syllable into its component parts (initial consonant, medial vowel, final consonant)
- It maps each component back to the corresponding QWERTY key sequence
- It handles standalone Jamo characters from both the modern Hangul Jamo block and the Hangul Compatibility Jamo block
- It preserves non-Hangul characters like spaces, punctuation, and English text
This function will be useful for showing keyboard suggestions or hints to users who are trying to type Korean but are unfamiliar with the keyboard layout. It can show them which QWERTY keys to press to produce the desired Hangul characters.
Would you like me to make any modifications to this function?