Skip to content

Instantly share code, notes, and snippets.

@Joooohee
Last active September 28, 2024 03:26
Show Gist options
  • Save Joooohee/1c1e200f4400d56cb91a2c282fa4e7bf to your computer and use it in GitHub Desktop.
Save Joooohee/1c1e200f4400d56cb91a2c282fa4e7bf to your computer and use it in GitHub Desktop.
한글 키보드 / 한글 조합, 분리 / 중성,초성,종성
Dictionary<string, string> combinations = new Dictionary<string, string>
{
{ "ㅗㅏ", "ㅘ" },
{ "ㅗㅐ", "ㅙ" },
{ "ㅜㅓ", "ㅝ" },
{ "ㅜㅐ", "ㅞ" },
{ "ㅡㅣ", "ㅢ" },
{ "ㅜㅣ", "ㅟ" },
{ "ㅗㅣ", "ㅚ" }
};
string[] chosungs = { "ㄱ", "ㄲ", "ㄴ", "ㄷ", "ㄸ", "ㄹ", "ㅁ", "ㅂ", "ㅃ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅉ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ" };
string[] jungsungs = { "ㅏ", "ㅐ", "ㅑ", "ㅒ", "ㅓ", "ㅔ", "ㅕ", "ㅖ", "ㅗ", "ㅘ", "ㅙ", "ㅚ", "ㅛ", "ㅜ", "ㅝ", "ㅞ", "ㅟ", "ㅠ", "ㅡ", "ㅢ", "ㅣ" };
string[] jongsungs = { "", "ㄱ", "ㄲ", "ㄳ", "ㄴ", "ㄵ", "ㄶ", "ㄷ", "ㄹ", "ㄺ", "ㄻ", "ㄼ", "ㄽ", "ㄾ", "ㄿ", "ㅀ", "ㅁ", "ㅂ", "ㅄ", "ㅅ", "ㅆ", "ㅇ", "ㅈ", "ㅊ", "ㅋ", "ㅌ", "ㅍ", "ㅎ" };
private int getChosungIndex(string chosung)
{
return Array.IndexOf(chosungs, chosung);
}
private int getJungsungIndex(string jungsung)
{
return Array.IndexOf(jungsungs, jungsung);
}
private int getJongsungIndex(string jongsung)
{
return Array.IndexOf(jongsungs, jongsung);
}
private bool isContained(string[] array, int index, string input)
{
if (idnex < 0 || index + 1 > input.Length)
{
return false;
}
else
{
return array.Contains(input[index].ToString());
}
}
private string mergeSyllable(string currentChosung, string currentJungsung, string currentJongsung)
{
int chosungIndex = getChosungIndex(currentChosung);
int jungsungIndex = getJungsungIndex(currentJungsung);
int jongsungIndex = getJongsungIndex(currentJongsung);
int syllableCode = 0xAC00 + (chosungIndex * 21 + jungsungIndex) * 28 + jongsungIndex;
var currentSyllable = ((char)syllableCode).ToString();
return currentSyllable;
}
private (string, string, string) deviceSyllable(char syllable)
{
ushort UnicodeHangeulBase = 0xAC00;
ushort check = Convert.ToUInt16(syllable);
int Code = check - UnicodeHangeulBase;
int JongsungCode = Code % 28;
Code = (Code - JongsungCode) / 28;
int JungsungCode = Code % 21;
Code = (Code - JungsungCode) / 21;
int ChosungCode = Code;
string chosung = chosungs[ChosungCode];
string jungsung = jungsungs[JungsungCode];
string jongsung = jongsungs[JongsungCode];
return (chosung, jungsung, jongsung);
}
private string getCompletedText(string input)
{
string chosung = string.Empty;
string jungsung = string.Empty;
string jongsung = string.Empty;
StringBuilder result = new StringBuilder();
if (input.Length == 1)
{
return input;
}
for (int i = 0; i < input.Length; i++)
{
if (chosungs.Contains(input[i].ToString()))
{
if (isContained(jungsungs, i + 1, input))
{
chosung = input[i].ToString();
}
else if (!string.IsNullOrEmpty(chosung) && !string.IsNullOrEmpty(jungsung))
{
jongsung = input[i].ToString();
result.Remove(result.Length - 1, 1);
var syllable = mergeSyllable(chosung, jungsung, jongsung);
result.Append(syllable);
chosung = string.Empty;
jungsung = string.Empty;
jongsung = string.Empty;
}
else
{
result.Append(input[i]);
continue;
}
}
else if (jungsungs.Contains(input[i].ToString()))
{
if (isContained(chosungs, i - 1, input))
{
jungsung = input[i].ToString();
var syllable = mergeSyllable(chosung, jungsung, jongsung);
result.Append(syllable);
}
else if (isContained(jungsungs, i - 1, input) && combinations.ContainsKey(input.Substring(i - 1, 2)))
{
jungsung = combinations[input.Substring(i - 1, 2)];
if(string.IsNullOrEmpty(chosung))
{
rresult.Remove(result.Length - 1, 1);
result.Append(jungsung);
continue;
}
else
{
result.Remove(result.Length - 1, 1);
var syllable = mergeSyllable(chosung, jungsung, jongsung);
result.Append(syllable);
}
}
else
{
result.Append(input[i]);
continue;
}
}
else
{
result.Append(input[i]);
}
}
return result.ToString();
}
private void KeyPad_Click(object sender, EventArgs e)
{
var key = getKeyPadValue((Panel)sender);
textBox.Text += key;
StringBuilder result = new StringBuilder();
foreach (char ch in txtUserName.Text)
{
if (chosungs.Contains(ch.ToString()) || jungsungs.Contains(ch.ToString()))
{
result.Append(ch);
continue;
}
var (initial, medial, final) = deviceSyllable(ch);
result.Append(initial);
result.Append(medial);
result.Append(final);
}
var combinedText = getCompletedText(result.ToString());
textBox.Text = combinedText;
textBox.SelectionStart = textBox.Text.Length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment