Android determines whether the character is Chinese, Korean, or Japanese

Source: Internet
Author: User

We often need to judge in the program whether a character is a character in CJK (Chinese, Japanese, Korean) language.

For example, in Contacts, the program needs to determine the language of the contact name.

Today, we will introduce a method for determining the language of a character in NameSplitter.

Take determining whether the character is Chinese as an example.

First, you can use the guessFullNameStyle function to determine the language of the character (using UnicodeBlock );

    public static int guessFullNameStyle(String name) {        if (name == null) {            return FullNameStyle.UNDEFINED;        }        int nameStyle = FullNameStyle.UNDEFINED;        int length = name.length();        int offset = 0;        while (offset < length) {            int codePoint = Character.codePointAt(name, offset);            if (Character.isLetter(codePoint)) {                UnicodeBlock unicodeBlock = UnicodeBlock.of(codePoint);                if (!isLatinUnicodeBlock(unicodeBlock)) {                    if (isCJKUnicodeBlock(unicodeBlock)) {                        // We don't know if this is Chinese, Japanese or Korean -                        // trying to figure out by looking at other characters in the name                        return guessCJKNameStyle(name, offset + Character.charCount(codePoint));                    }                    if (isJapanesePhoneticUnicodeBlock(unicodeBlock)) {                        return FullNameStyle.JAPANESE;                    }                    if (isKoreanUnicodeBlock(unicodeBlock)) {                        return FullNameStyle.KOREAN;                    }                }                nameStyle = FullNameStyle.WESTERN;            }            offset += Character.charCount(codePoint);        }        return nameStyle;    }

 

    private static int guessCJKNameStyle(String name, int offset) {        int length = name.length();        while (offset < length) {            int codePoint = Character.codePointAt(name, offset);            if (Character.isLetter(codePoint)) {                UnicodeBlock unicodeBlock = UnicodeBlock.of(codePoint);                if (isJapanesePhoneticUnicodeBlock(unicodeBlock)) {                    return FullNameStyle.JAPANESE;                }                if (isKoreanUnicodeBlock(unicodeBlock)) {                    return FullNameStyle.KOREAN;                }            }            offset += Character.charCount(codePoint);        }        return FullNameStyle.CJK;    }

 

Secondly, if the result is CJK, we need to further judge whether it is Chinese, Japanese, or Korean.

    /**     * If the supplied name style is undefined, returns a default based on the     * language, otherwise returns the supplied name style itself.     *      * @param nameStyle See {@link FullNameStyle}.     */    public static int getAdjustedFullNameStyle(int nameStyle) {        String mLanguage = Locale.getDefault().getLanguage().toLowerCase();        if (nameStyle == FullNameStyle.UNDEFINED) {            if (JAPANESE_LANGUAGE.equals(mLanguage)) {                return FullNameStyle.JAPANESE;            } else if (KOREAN_LANGUAGE.equals(mLanguage)) {                return FullNameStyle.KOREAN;            } else if (CHINESE_LANGUAGE.equals(mLanguage)) {                return FullNameStyle.CHINESE;            } else {                return FullNameStyle.WESTERN;            }        } else if (nameStyle == FullNameStyle.CJK) {            if (JAPANESE_LANGUAGE.equals(mLanguage)) {                return FullNameStyle.JAPANESE;            } else if (KOREAN_LANGUAGE.equals(mLanguage)) {                return FullNameStyle.KOREAN;            } else {                return FullNameStyle.CHINESE;            }        }        return nameStyle;    }

Well, that's probably the case. I wrote a small Demo using Eclipse, hoping to help you ~

Source code link:

Http://pan.baidu.com/share/link? Consumer id = 4025849919 & uk = 2953765628

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.