Search code examples
javaandroidcjk

How to convert to Korean initials


Hi Im working on a Korean android app.

Here I implemented a listview with alphabetic section headers to display contacts. I used substring method of String class to get the first letter as section header. For korean contacts also im taking first letter using substring method. Here I need to display Korean initials as ㄱ ㄴ ㄷ ㄹ ㅁ ㅂ ㅅ ㅇ ㅈ ㅊ ㅋ ㅌ ㅍ ㅎ with respects to first letter of the Korean contact. I'm not really know which first letter of the Korean contact list matches with these Korean initials.

So please help me how i can do this or give me reference to follow...

Thanks in Advance..


Solution

  • I don't think it is normal behaviour in a Korean app to have intellisense from just an intial character - it seems to usually be done with a full Jamo. However, I see no reason why you shouldn't do it - so lets do it.

    Firstly, you missed the double initials. These are different from the singles, as they do require a different key press (usually shift+character). Anyhow, your list of initials should be:

    ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ
    

    The first thing you need to do when an initial is typed in is to get the range of characters that start with that particular initial.

    By looking at the Windows Character Map, I one can see that the first letter, Ka (가) is at Unicode point 0xAC00 (or decimal, 44032), so to get any range, this value will have to be added to any calculation we do. So you should have a constant called FirstLetter or whatever, and its value should be 44032.

    까 is at 0xAE4C (or decimal 44620), so we now have a multiplier (i.e. the number of characters beginning with any particular initial - which is the same for all initials, so that is 44620-44032 which is 588.

    We now have enough information to implement your intellisense. If you have the initialisms in an array, we can use the index of the typed character in that array to find the range of characters that start with it.

    So if we start with ㄱ, we obviously want to return everything from 가 to 깋. All 588 of them. So ㄱ gives us a '0', so we have

    startCodePoint = index * 588 + 44032
    // = 0 * 588 + 44032 == 44032 == 가
    endCodePoint = (index + 1) * 588 + 44032
    // this will include 까
    

    And then you can just check that a particular character starts with 'ㄱ' by checking

    if(charcode >= startCodePoint && charcode < endCodePoint) { ... }
    

    where charcode is the first character of one of the items in your intellisense list.

    Use a similar methodology to find out how to check that a character starts with say '가'. Everything is in order in Unicode, so this is a very simple task.


    To get the initial letter of any character, you can use the above formula in reverse.

    i.e.

    1. Get the unicode value of the first character (e.g. 각)
    2. Subtract 44032 from this value.
    3. Divide this value by 588.
    4. Use that value as the index to retrieve the intial character from the list of initials.

    e.g.

    String initials = "ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ";
    int value = character.codePointAt(0);
    value = (value - 44032) / 588;
    String initial = initials.substring(value, 1);