You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Gili Nachum <gi...@gmail.com> on 2013/03/10 10:19:11 UTC

Re: Lightweight detection of whether a keyword is CJK or not (language detection)

Answering myself for next generations' sake.
Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS does the job.

Example:

import junit.framework.Assert;
import org.junit.Test;

public class DetectCJK {

    @Test
    public void test1() {
        Assert.assertEquals(Character.UnicodeBlock.BASIC_LATIN,
Character.UnicodeBlock.of('a'));
        Assert.assertEquals(Character.UnicodeBlock.HEBREW,
Character.UnicodeBlock.of('א'));
        Assert.assertEquals("Traditional Chinese: Electricity",
                Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS,
Character.UnicodeBlock.of('電'));
        Assert.assertEquals("Simplified Chinese: Electricity",
                Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS,
Character.UnicodeBlock.of('电'));
        Assert.assertEquals("Simplified Chinese: Japanese",
                Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS,
Character.UnicodeBlock.of('電'));

        String chineseWritingStr = "漢字/汉字";
        int length = chineseWritingStr.codePointCount(0,
chineseWritingStr.length()-1);
        for (int i=0; i<length; i++) {
            int codePoint = chineseWritingStr.codePointAt(0);
            Assert.assertEquals("Chinese: Chinese writing",
                    Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS,
Character.UnicodeBlock.of(codePoint));
        }
    }
}


On Fri, Feb 22, 2013 at 12:51 AM, Gili Nachum <gi...@gmail.com> wrote:

> Hello, Is there anything in the Lucene core/contrib that could help detect
> if a keyword is CJK or not?
> I was thinking that an okay heuristic might be to inspect if the keyword's
> characters unicode value is within CJK ranges. Anything that does that?
>
> I'm seeing really bad performance when users query for keywords with a
> wildcard (say: "abc*") . Therefore, as a defensive measure, I plan to
> restrict wildcard queries to have a minimum of 4 characters (e.g., reject
> "abc*" allow "abcd*").
> However, for CJK keywords, I would like to make an exception, since in CJK
> just one or two letters stand for a distinct word (I'm okay that some CJK
> characters are not words, but are phonetic in nature).
>
> Thanks.
> Gili.
>

Re: Lightweight detection of whether a keyword is CJK or not (language detection)

Posted by Gili Nachum <gi...@gmail.com>.
This character lies in the CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A block.
Added extensions detection, I assume (not really knowing) that all of these
characters are not phonetic as well.

import java.lang.Character.UnicodeBlock;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import junit.framework.Assert;
import org.junit.Test;

public class DetectCJK {

    Set<UnicodeBlock> cjkUnicodeBlocks = new HashSet<UnicodeBlock>(
            Arrays.asList(new Character.UnicodeBlock[] {
            Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS,
            Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A,
            Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
            // C and D were added in Java7 - I'm using Java6
            //,Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C,
            //Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D,
    }));

    @Test
    public void test1() {
        Assert.assertEquals(Character.UnicodeBlock.BASIC_LATIN,
Character.UnicodeBlock.of('a'));
        Assert.assertEquals(Character.UnicodeBlock.HEBREW,
Character.UnicodeBlock.of('א'));
        assertCJK('電', "Traditional Chinese: Electricity");
        assertCJK('电', "Simplified Chinese: Electricity");
        assertCJK('電', "Simplified Chinese: Japanese");
        assertCJK('㒨', "in CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A");
    }

    private void assertCJK(Character character, String message) {
        UnicodeBlock unicodeBlock = Character.UnicodeBlock.of(character);
        Assert.assertTrue(message, cjkUnicodeBlocks.contains(unicodeBlock));
    }
}


On Mon, Mar 11, 2013 at 12:10 AM, Trejkaz <tr...@trypticon.org> wrote:

> On Sun, Mar 10, 2013 at 8:19 PM, Gili Nachum <gi...@gmail.com> wrote:
> > Answering myself for next generations' sake.
> > Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS does the job.
>
> How about 㒨?
>
> TX
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>
>

Re: Lightweight detection of whether a keyword is CJK or not (language detection)

Posted by Trejkaz <tr...@trypticon.org>.
On Sun, Mar 10, 2013 at 8:19 PM, Gili Nachum <gi...@gmail.com> wrote:
> Answering myself for next generations' sake.
> Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS does the job.

How about 㒨?

TX

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org