pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / icu / src / test / org / apache / lucene / analysis / icu / segmentation / TestLaoBreakIterator.java
1 package org.apache.lucene.analysis.icu.segmentation;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.InputStream;
21
22 import org.apache.lucene.util.LuceneTestCase;
23
24 import com.ibm.icu.lang.UCharacter;
25 import com.ibm.icu.text.BreakIterator;
26 import com.ibm.icu.text.RuleBasedBreakIterator;
27 import com.ibm.icu.text.UTF16;
28
29 /**
30  * Tests LaoBreakIterator and its RBBI rules
31  */
32 public class TestLaoBreakIterator extends LuceneTestCase {
33   private BreakIterator wordIterator;
34   
35   @Override
36   public void setUp() throws Exception {
37     super.setUp();
38     InputStream is = getClass().getResourceAsStream("Lao.brk");
39     wordIterator = new LaoBreakIterator(RuleBasedBreakIterator.getInstanceFromCompiledRules(is));
40     is.close();
41   }
42   
43   private void assertBreaksTo(BreakIterator iterator, String sourceText, String tokens[]) {
44     char text[] = sourceText.toCharArray();
45     CharArrayIterator ci = new CharArrayIterator();
46     ci.setText(text, 0, text.length);
47     iterator.setText(ci);
48     
49     for (int i = 0; i < tokens.length; i++) {
50       int start, end;
51       do {
52         start = iterator.current();
53         end = iterator.next();
54       } while (end != BreakIterator.DONE && !isWord(text, start, end));
55       assertTrue(start != BreakIterator.DONE);
56       assertTrue(end != BreakIterator.DONE);
57       assertEquals(tokens[i], new String(text, start, end - start));
58     }
59     
60     assertTrue(iterator.next() == BreakIterator.DONE);
61   }
62   
63   protected boolean isWord(char text[], int start, int end) {
64     int codepoint;
65     for (int i = start; i < end; i += UTF16.getCharCount(codepoint)) {
66       codepoint = UTF16.charAt(text, 0, end, start);
67
68       if (UCharacter.isLetterOrDigit(codepoint))
69         return true;
70       }
71
72     return false;
73   }
74   
75   public void testBasicUsage() throws Exception {
76     assertBreaksTo(wordIterator, "ກວ່າດອກ", new String[] { "ກວ່າ", "ດອກ" });
77     assertBreaksTo(wordIterator, "ຜູ້​ເຂົ້າ", new String[] { "ຜູ້", "ເຂົ້າ" });
78     assertBreaksTo(wordIterator, "", new String[] {});
79     assertBreaksTo(wordIterator, "ສະບາຍດີ", new String[] { "ສະ", "ບາຍ", "ດີ" });
80   }
81   
82   public void testNumerics() throws Exception {
83     assertBreaksTo(wordIterator, "໐໑໒໓", new String[] { "໐໑໒໓" });
84     assertBreaksTo(wordIterator, "໐໑໒໓.໕໖", new String[] { "໐໑໒໓.໕໖" });
85   }
86  
87   public void testTextAndNumerics() throws Exception {
88     assertBreaksTo(wordIterator, "ກວ່າດອກ໐໑໒໓", new String[] { "ກວ່າ", "ດອກ", "໐໑໒໓" });
89   }
90 }