コード例 #1
0
 public void Init()
 {
     characterBreak = BreakIterator.GetCharacterInstance();
     wordBreak      = BreakIterator.GetWordInstance();
     lineBreak      = BreakIterator.GetLineInstance();
     //Logln("Creating sentence iterator...");
     sentenceBreak = BreakIterator.GetSentenceInstance();
     //Logln("Finished creating sentence iterator...");
     titleBreak = BreakIterator.GetTitleInstance();
 }
コード例 #2
0
ファイル: RBBITest.cs プロジェクト: bongohrtech/ICU4N
            public void doTest()
            {
                BreakIterator brkIter;

                switch (type)
                {
                case BreakIterator.KIND_CHARACTER: brkIter = BreakIterator.GetCharacterInstance(locale); break;

                case BreakIterator.KIND_WORD: brkIter = BreakIterator.GetWordInstance(locale); break;

                case BreakIterator.KIND_LINE: brkIter = BreakIterator.GetLineInstance(locale); break;

                case BreakIterator.KIND_SENTENCE: brkIter = BreakIterator.GetSentenceInstance(locale); break;

                default: Errln("Unsupported break iterator type " + type); return;
                }
                brkIter.SetText(text);
                int[] foundOffsets = new int[maxOffsetCount];
                int   offset, foundOffsetsCount = 0;

                // do forwards iteration test
                while (foundOffsetsCount < maxOffsetCount && (offset = brkIter.Next()) != BreakIterator.Done)
                {
                    foundOffsets[foundOffsetsCount++] = offset;
                }
                if (!offsetsMatchExpected(foundOffsets, foundOffsetsCount))
                {
                    // log error for forwards test
                    String textToDisplay = (text.Length <= 16) ? text : text.Substring(0, 16 - 0); // ICU4N: Checked 2nd parameter
                    Errln("For type " + type + " " + locale + ", text \"" + textToDisplay + "...\"" +
                          "; expect " + expectOffsets.Length + " offsets:" + formatOffsets(expectOffsets, expectOffsets.Length) +
                          "; found " + foundOffsetsCount + " offsets fwd:" + formatOffsets(foundOffsets, foundOffsetsCount));
                }
                else
                {
                    // do backwards iteration test
                    --foundOffsetsCount; // back off one from the end offset
                    while (foundOffsetsCount > 0)
                    {
                        offset = brkIter.Previous();
                        if (offset != foundOffsets[--foundOffsetsCount])
                        {
                            // log error for backwards test
                            String textToDisplay = (text.Length <= 16) ? text : text.Substring(0, 16 - 0); // ICU4N: Checked 2nd parameter
                            Errln("For type " + type + " " + locale + ", text \"" + textToDisplay + "...\"" +
                                  "; expect " + expectOffsets.Length + " offsets:" + formatOffsets(expectOffsets, expectOffsets.Length) +
                                  "; found rev offset " + offset + " where expect " + foundOffsets[foundOffsetsCount]);
                            break;
                        }
                    }
                }
            }
コード例 #3
0
ファイル: RBBITest.cs プロジェクト: bongohrtech/ICU4N
        public void TestBug12873()
        {
            // Bug with RuleBasedBreakIterator's internal structure for recording potential look-ahead
            // matches not being cloned when a break iterator is cloned. This resulted in usage
            // collisions if the original break iterator and its clone were used concurrently.

            // The Line Break rules for Regional Indicators make use of look-ahead rules, and
            // show the bug. 1F1E6 = \uD83C\uDDE6 = REGIONAL INDICATOR SYMBOL LETTER A
            // Regional indicators group into pairs, expect breaks after two code points, which
            // is after four 16 bit code units.

            string dataToBreak        = "\uD83C\uDDE6\uD83C\uDDE6\uD83C\uDDE6\uD83C\uDDE6\uD83C\uDDE6\uD83C\uDDE6";
            RuleBasedBreakIterator bi = (RuleBasedBreakIterator)BreakIterator.GetLineInstance();

            AssertionException[] assertErr = new AssertionException[1];  // saves an error found from within a thread



            List <ThreadJob> threads = new List <ThreadJob>();

            for (int n = 0; n < 4; ++n)
            {
                threads.Add(new WorkerThread(dataToBreak, bi, assertErr));
            }
            foreach (var thread in threads)
            {
                thread.Start();
            }
            foreach (var thread in threads)
            {
#if FEATURE_THREADINTERRUPT
                try
                {
#endif
                thread.Join();
#if FEATURE_THREADINTERRUPT
            }
            catch (ThreadInterruptedException e) {
                fail(e.ToString());
            }
#endif
            }

            // JUnit wont see failures from within the worker threads, so
            // check again if one occurred.
            if (assertErr[0] != null)
            {
                throw assertErr[0];
            }
        }
コード例 #4
0
        public void TestLineBoundary()
        {
            StringBuilder text = new StringBuilder(TEXT);
            // we test this with default locale, its randomized by LuceneTestCase
            BreakIterator    bi      = BreakIterator.GetLineInstance(CultureInfo.CurrentCulture);
            IBoundaryScanner scanner = new BreakIteratorBoundaryScanner(bi);

            int start    = TEXT.IndexOf("any application");
            int expected = TEXT.IndexOf("nearly");

            TestFindStartOffset(text, start, expected, scanner);

            expected = TEXT.IndexOf("application that requires");
            TestFindEndOffset(text, start, expected, scanner);
        }
コード例 #5
0
        public void TestGetSetText()
        {
            Logln("Testing getText setText ");
            String str1 = "first string.";
            String str2 = "Second string.";
            //RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator) BreakIterator.getCharacterInstance(Locale.getDefault());
            RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator)BreakIterator.GetWordInstance(CultureInfo.CurrentCulture);
            CharacterIterator      text1     = new StringCharacterIterator(str1);

            //CharacterIterator text1Clone = (CharacterIterator) text1.Clone();
            //CharacterIterator text2 = new StringCharacterIterator(str2);
            wordIter1.SetText(str1);
            if (!wordIter1.Text.Equals(text1))
            {
                Errln("ERROR:1 error in setText or getText ");
            }
            if (wordIter1.Current != 0)
            {
                Errln("ERROR:1 setText did not set the iteration position to the beginning of the text, it is"
                      + wordIter1.Current + "\n");
            }
            wordIter1.Next(2);
            wordIter1.SetText(str2);
            if (wordIter1.Current != 0)
            {
                Errln("ERROR:2 setText did not reset the iteration position to the beginning of the text, it is"
                      + wordIter1.Current + "\n");
            }

            // Test the CharSequence overload of setText() for a simple case.
            BreakIterator lineIter = BreakIterator.GetLineInstance(new CultureInfo("en"));
            ICharSequence csText   = "Hello, World. ".ToCharSequence();
            // Expected Line Brks  ^      ^      ^
            //                     0123456789012345
            List <int> expected = new List <int>();

            expected.Add(0); expected.Add(7); expected.Add(14);
            lineIter.SetText(csText);
            for (int pos = lineIter.First(); pos != BreakIterator.Done; pos = lineIter.Next())
            {
                assertTrue("", expected.Contains(pos));
            }
            assertEquals("", csText.Length, lineIter.Current);
        }
コード例 #6
0
 private BreakIterator GetLineInstance(System.Globalization.CultureInfo locale)
 {
     return(BreakIterator.GetLineInstance(locale));
 }
コード例 #7
0
ファイル: RBBITest.cs プロジェクト: bongohrtech/ICU4N
        public void TestThaiDictionaryBreakIterator()
        {
            int position;
            int index;

            int[]  result = { 1, 2, 5, 10, 11, 12, 11, 10, 5, 2, 1, 0 };
            char[] ctext  =
            {
                (char)0x0041, (char)0x0020,
                (char)0x0E01, (char)0x0E32,(char)0x0E23,  (char)0x0E17, (char)0x0E14, (char)0x0E25, (char)0x0E2D, (char)0x0E07,
                (char)0x0020, (char)0x0041
            };
            String text = new String(ctext);

            ULocale       locale = ULocale.CreateCanonical("th");
            BreakIterator b      = BreakIterator.GetWordInstance(locale);

            b.SetText(text);

            index = 0;
            // Test forward iteration
            while ((position = b.Next()) != BreakIterator.Done)
            {
                if (position != result[index++])
                {
                    Errln("Error with ThaiDictionaryBreakIterator forward iteration test at " + position + ".\nShould have been " + result[index - 1]);
                }
            }

            // Test backward iteration
            while ((position = b.Previous()) != BreakIterator.Done)
            {
                if (position != result[index++])
                {
                    Errln("Error with ThaiDictionaryBreakIterator backward iteration test at " + position + ".\nShould have been " + result[index - 1]);
                }
            }

            //Test invalid sequence and spaces
            char[] text2 =
            {
                (char)0x0E01, (char)0x0E39, (char)0x0020, (char)0x0E01, (char)0x0E34, (char)0x0E19, (char)0x0E01, (char)0x0E38, (char)0x0E49, (char)0x0E07, (char)0x0020, (char)0x0E1B,
                (char)0x0E34, (char)0x0E49, (char)0x0E48, (char)0x0E07, (char)0x0E2D, (char)0x0E22, (char)0x0E39, (char)0x0E48, (char)0x0E43, (char)0x0E19,
                (char)0x0E16, (char)0x0E49, (char)0x0E33
            };
            int[] expectedWordResult =
            {
                2, 3, 6, 10, 11, 15, 17, 20, 22
            };
            int[] expectedLineResult =
            {
                3, 6, 11, 15, 17, 20, 22
            };
            BreakIterator brk = BreakIterator.GetWordInstance(new ULocale("th"));

            brk.SetText(new String(text2));
            position = index = 0;
            while ((position = brk.Next()) != BreakIterator.Done && position < text2.Length)
            {
                if (position != expectedWordResult[index++])
                {
                    Errln("Incorrect break given by thai word break iterator. Expected: " + expectedWordResult[index - 1] + " Got: " + position);
                }
            }

            brk = BreakIterator.GetLineInstance(new ULocale("th"));
            brk.SetText(new String(text2));
            position = index = 0;
            while ((position = brk.Next()) != BreakIterator.Done && position < text2.Length)
            {
                if (position != expectedLineResult[index++])
                {
                    Errln("Incorrect break given by thai line break iterator. Expected: " + expectedLineResult[index - 1] + " Got: " + position);
                }
            }
            // Improve code coverage
            if (brk.Preceding(expectedLineResult[1]) != expectedLineResult[0])
            {
                Errln("Incorrect preceding position.");
            }
            if (brk.Following(expectedLineResult[1]) != expectedLineResult[2])
            {
                Errln("Incorrect following position.");
            }
            int[] fillInArray = new int[2];
            if (((RuleBasedBreakIterator)brk).GetRuleStatusVec(fillInArray) != 1 || fillInArray[0] != 0)
            {
                Errln("Error: Since getRuleStatusVec is not supported in DictionaryBasedBreakIterator, it should return 1 and fillInArray[0] == 0.");
            }
        }
コード例 #8
0
        public void TestExtended()
        {
            TestParams tp = new TestParams();


            //
            //  Open and read the test data file.
            //
            StringBuilder testFileBuf = new StringBuilder();
            Stream        @is         = null;

            try
            {
                @is = typeof(RBBITestExtended).GetTypeInfo().Assembly.GetManifestResourceStream("ICU4N.Dev.Test.Rbbi.rbbitst.txt");
                if (@is == null)
                {
                    Errln("Could not open test data file rbbitst.txt");
                    return;
                }
                StreamReader isr = new StreamReader(@is, Encoding.UTF8);
                try
                {
                    int c;
                    int count = 0;
                    for (; ;)
                    {
                        c = isr.Read();
                        if (c < 0)
                        {
                            break;
                        }
                        count++;
                        if (c == 0xFEFF && count == 1)
                        {
                            // BOM in the test data file. Discard it.
                            continue;
                        }

                        testFileBuf.AppendCodePoint(c);
                    }
                }
                finally
                {
                    isr.Dispose();
                }
            }
            catch (IOException e)
            {
                Errln(e.ToString());
                try
                {
                    @is.Dispose();
                }
                catch (IOException ignored)
                {
                }
                return;
            }

            String testString = testFileBuf.ToString();


            const int PARSE_COMMENT = 1;
            const int PARSE_TAG     = 2;
            const int PARSE_DATA    = 3;
            const int PARSE_NUM     = 4;
            const int PARSE_RULES   = 5;

            int parseState = PARSE_TAG;

            int savedState = PARSE_TAG;

            int lineNum  = 1;
            int colStart = 0;
            int column   = 0;
            int charIdx  = 0;
            int i;

            int tagValue = 0;                                   // The numeric value of a <nnn> tag.

            StringBuilder rules          = new StringBuilder(); // Holds rules from a <rules> ... </rules> block
            int           rulesFirstLine = 0;                   // Line number of the start of current <rules> block

            int len = testString.Length;

            for (charIdx = 0; charIdx < len;)
            {
                int c = testString.CodePointAt(charIdx);
                charIdx++;
                if (c == '\r' && charIdx < len && testString[charIdx] == '\n')
                {
                    // treat CRLF as a unit
                    c = '\n';
                    charIdx++;
                }
                if (c == '\n' || c == '\r')
                {
                    lineNum++;
                    colStart = charIdx;
                }
                column = charIdx - colStart + 1;

                switch (parseState)
                {
                case PARSE_COMMENT:
                    if (c == 0x0a || c == 0x0d)
                    {
                        parseState = savedState;
                    }
                    break;

                case PARSE_TAG:
                {
                    if (c == '#')
                    {
                        parseState = PARSE_COMMENT;
                        savedState = PARSE_TAG;
                        break;
                    }
                    if (UCharacter.IsWhitespace(c))
                    {
                        break;
                    }
                    if (testString.StartsWith("<word>", charIdx - 1))
                    {
                        tp.bi    = BreakIterator.GetWordInstance(tp.currentLocale);
                        charIdx += 5;
                        break;
                    }
                    if (testString.StartsWith("<char>", charIdx - 1))
                    {
                        tp.bi    = BreakIterator.GetCharacterInstance(tp.currentLocale);
                        charIdx += 5;
                        break;
                    }
                    if (testString.StartsWith("<line>", charIdx - 1))
                    {
                        tp.bi    = BreakIterator.GetLineInstance(tp.currentLocale);
                        charIdx += 5;
                        break;
                    }
                    if (testString.StartsWith("<sent>", charIdx - 1))
                    {
                        tp.bi    = BreakIterator.GetSentenceInstance(tp.currentLocale);
                        charIdx += 5;
                        break;
                    }
                    if (testString.StartsWith("<title>", charIdx - 1))
                    {
                        tp.bi    = BreakIterator.GetTitleInstance(tp.currentLocale);
                        charIdx += 6;
                        break;
                    }
                    if (testString.StartsWith("<rules>", charIdx - 1) ||
                        testString.StartsWith("<badrules>", charIdx - 1))
                    {
                        charIdx        = testString.IndexOf('>', charIdx) + 1;
                        parseState     = PARSE_RULES;
                        rules.Length   = (0);
                        rulesFirstLine = lineNum;
                        break;
                    }

                    if (testString.StartsWith("<locale ", charIdx - 1))
                    {
                        int closeIndex = testString.IndexOf(">", charIdx);
                        if (closeIndex < 0)
                        {
                            Errln("line" + lineNum + ": missing close on <locale  tag.");
                            break;
                        }
                        String localeName = testString.Substring(charIdx + 6, closeIndex - (charIdx + 6));         // ICU4N: Corrected 2nd parameter
                        localeName       = localeName.Trim();
                        tp.currentLocale = new ULocale(localeName);
                        charIdx          = closeIndex + 1;
                        break;
                    }
                    if (testString.StartsWith("<data>", charIdx - 1))
                    {
                        parseState            = PARSE_DATA;
                        charIdx              += 5;
                        tp.dataToBreak.Length = (0);
                        Arrays.Fill(tp.expectedBreaks, 0);
                        Arrays.Fill(tp.srcCol, 0);
                        Arrays.Fill(tp.srcLine, 0);
                        break;
                    }

                    Errln("line" + lineNum + ": Tag expected in test file.");
                    return;
                    //parseState = PARSE_COMMENT;
                    //savedState = PARSE_DATA;
                }

                case PARSE_RULES:
                    if (testString.StartsWith("</rules>", charIdx - 1))
                    {
                        charIdx   += 7;
                        parseState = PARSE_TAG;
                        try
                        {
                            tp.bi = new RuleBasedBreakIterator(rules.ToString());
                        }
                        catch (ArgumentException e)
                        {
                            Errln(String.Format("rbbitst.txt:{0}  Error creating break iterator from rules.  {1}", lineNum, e));
                        }
                    }
                    else if (testString.StartsWith("</badrules>", charIdx - 1))
                    {
                        charIdx   += 10;
                        parseState = PARSE_TAG;
                        bool goodRules = true;
                        try
                        {
                            new RuleBasedBreakIterator(rules.ToString());
                        }
                        catch (ArgumentException e)
                        {
                            goodRules = false;
                        }
                        if (goodRules)
                        {
                            Errln(String.Format(
                                      "rbbitst.txt:{0}  Expected, but did not get, a failure creating break iterator from rules.",
                                      lineNum));
                        }
                    }
                    else
                    {
                        rules.AppendCodePoint(c);
                    }
                    break;

                case PARSE_DATA:
                    if (c == '•')
                    {
                        int breakIdx = tp.dataToBreak.Length;
                        tp.expectedBreaks[breakIdx] = -1;
                        tp.srcLine[breakIdx]        = lineNum;
                        tp.srcCol[breakIdx]         = column;
                        break;
                    }

                    if (testString.StartsWith("</data>", charIdx - 1))
                    {
                        // Add final entry to mappings from break location to source file position.
                        //  Need one extra because last break position returned is after the
                        //    last char in the data, not at the last char.
                        int idx = tp.dataToBreak.Length;
                        tp.srcLine[idx] = lineNum;
                        tp.srcCol[idx]  = column;

                        parseState = PARSE_TAG;
                        charIdx   += 6;

                        // RUN THE TEST!
                        executeTest(tp);
                        break;
                    }

                    if (testString.StartsWith("\\N{", charIdx - 1))
                    {
                        int nameEndIdx = testString.IndexOf('}', charIdx);
                        if (nameEndIdx == -1)
                        {
                            Errln("Error in named character in test file at line " + lineNum +
                                  ", col " + column);
                        }
                        // Named character, e.g. \N{COMBINING GRAVE ACCENT}
                        // Get the code point from the name and insert it into the test data.
                        String charName = testString.Substring(charIdx + 2, nameEndIdx - (charIdx + 2));     // ICU4N: Corrected 2nd parameter
                        c = UCharacter.GetCharFromName(charName);
                        if (c == -1)
                        {
                            Errln("Error in named character in test file at line " + lineNum +
                                  ", col " + column);
                        }
                        else
                        {
                            // Named code point was recognized.  Insert it
                            //   into the test data.
                            tp.dataToBreak.AppendCodePoint(c);
                            for (i = tp.dataToBreak.Length - 1; i >= 0 && tp.srcLine[i] == 0; i--)
                            {
                                tp.srcLine[i] = lineNum;
                                tp.srcCol[i]  = column;
                            }
                        }
                        if (nameEndIdx > charIdx)
                        {
                            charIdx = nameEndIdx + 1;
                        }
                        break;
                    }

                    if (testString.StartsWith("<>", charIdx - 1))
                    {
                        charIdx++;
                        int breakIdx = tp.dataToBreak.Length;
                        tp.expectedBreaks[breakIdx] = -1;
                        tp.srcLine[breakIdx]        = lineNum;
                        tp.srcCol[breakIdx]         = column;
                        break;
                    }

                    if (c == '<')
                    {
                        tagValue   = 0;
                        parseState = PARSE_NUM;
                        break;
                    }

                    if (c == '#' && column == 3)
                    {       // TODO:  why is column off so far?
                        parseState = PARSE_COMMENT;
                        savedState = PARSE_DATA;
                        break;
                    }

                    if (c == '\\')
                    {
                        // Check for \ at end of line, a line continuation.
                        //     Advance over (discard) the newline
                        int cp = testString.CodePointAt(charIdx);
                        if (cp == '\r' && charIdx < len && testString.CodePointAt(charIdx + 1) == '\n')
                        {
                            // We have a CR LF
                            //  Need an extra increment of the input ptr to move over both of them
                            charIdx++;
                        }
                        if (cp == '\n' || cp == '\r')
                        {
                            lineNum++;
                            column = 0;
                            charIdx++;
                            colStart = charIdx;
                            break;
                        }

                        // Let unescape handle the back slash.
                        int[] charIdxAr = new int[1];
                        charIdxAr[0] = charIdx;
                        cp           = Utility.UnescapeAt(testString, charIdxAr);
                        if (cp != -1)
                        {
                            // Escape sequence was recognized.  Insert the char
                            //   into the test data.
                            charIdx = charIdxAr[0];
                            tp.dataToBreak.AppendCodePoint(cp);
                            for (i = tp.dataToBreak.Length - 1; i >= 0 && tp.srcLine[i] == 0; i--)
                            {
                                tp.srcLine[i] = lineNum;
                                tp.srcCol[i]  = column;
                            }

                            break;
                        }


                        // Not a recognized backslash escape sequence.
                        // Take the next char as a literal.
                        //  TODO:  Should this be an error?
                        c       = testString.CodePointAt(charIdx);
                        charIdx = testString.OffsetByCodePoints(charIdx, 1);
                    }

                    // Normal, non-escaped data char.
                    tp.dataToBreak.AppendCodePoint(c);

                    // Save the mapping from offset in the data to line/column numbers in
                    //   the original input file.  Will be used for better error messages only.
                    //   If there's an expected break before this char, the slot in the mapping
                    //     vector will already be set for this char; don't overwrite it.
                    for (i = tp.dataToBreak.Length - 1; i >= 0 && tp.srcLine[i] == 0; i--)
                    {
                        tp.srcLine[i] = lineNum;
                        tp.srcCol[i]  = column;
                    }
                    break;


                case PARSE_NUM:
                    // We are parsing an expected numeric tag value, like <1234>,
                    //   within a chunk of data.
                    if (UCharacter.IsWhitespace(c))
                    {
                        break;
                    }

                    if (c == '>')
                    {
                        // Finished the number.  Add the info to the expected break data,
                        //   and switch parse state back to doing plain data.
                        parseState = PARSE_DATA;
                        if (tagValue == 0)
                        {
                            tagValue = -1;
                        }
                        int breakIdx = tp.dataToBreak.Length;
                        tp.expectedBreaks[breakIdx] = tagValue;
                        tp.srcLine[breakIdx]        = lineNum;
                        tp.srcCol[breakIdx]         = column;
                        break;
                    }

                    if (UCharacter.IsDigit(c))
                    {
                        tagValue = tagValue * 10 + UCharacter.Digit(c);
                        break;
                    }

                    Errln(String.Format("Syntax Error in rbbitst.txt at line {0}, col {1}", lineNum, column));
                    return;
                }
            }

            // Reached end of test file. Raise an error if parseState indicates that we are
            //   within a block that should have been terminated.
            if (parseState == PARSE_RULES)
            {
                Errln(String.Format("rbbitst.txt:{0} <rules> block beginning at line {1} is not closed.",
                                    lineNum, rulesFirstLine));
            }
            if (parseState == PARSE_DATA)
            {
                Errln(String.Format("rbbitst.txt:{0} <data> block not closed.", lineNum));
            }
        }
コード例 #9
0
        public void TestLastPreviousPreceding()
        {
            int    p, q;
            String testString = "This is a word break. Isn't it? 2.25 dollars";

            Logln("Testing last(),previous(), preceding() with custom rules");
            Logln("testing word iteration for string \"" + testString + "\"");
            RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator)BreakIterator.GetWordInstance(new CultureInfo("en"));

            wordIter1.SetText(testString);
            p = wordIter1.Last();
            if (p != testString.Length)
            {
                Errln("ERROR: last() returned" + p + "instead of" + testString.Length);
            }
            q = wordIter1.Previous();
            doTest(testString, p, q, 37, "dollars");
            p = q;
            q = wordIter1.Previous();
            doTest(testString, p, q, 36, " ");
            q = wordIter1.Preceding(25);
            doTest(testString, 25, q, 22, "Isn");
            p = q;
            q = wordIter1.Previous();
            doTest(testString, p, q, 21, " ");
            q = wordIter1.Preceding(20);
            doTest(testString, 20, q, 15, "break");
            p = wordIter1.Preceding(wordIter1.First());
            if (p != BreakIterator.Done)
            {
                Errln("ERROR: preceding()  at starting position returned #" + p + " instead of 0");
            }
            testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This  costs $20,00,000.";
            Logln("testing sentence iter - String:- \"" + testString + "\"");
            RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator)BreakIterator.GetSentenceInstance(CultureInfo.CurrentCulture);

            sentIter1.SetText(testString);
            p = sentIter1.Last();
            if (p != testString.Length)
            {
                Errln("ERROR: last() returned" + p + "instead of " + testString.Length);
            }
            q = sentIter1.Previous();
            doTest(testString, p, q, 60, "This  costs $20,00,000.");
            p = q;
            q = sentIter1.Previous();
            doTest(testString, p, q, 41, "How are you doing? ");
            q = sentIter1.Preceding(40);
            doTest(testString, 40, q, 31, "Thankyou.");
            q = sentIter1.Preceding(25);
            doTest(testString, 25, q, 20, "I'am ");
            sentIter1.First();
            p = sentIter1.Previous();
            q = sentIter1.Preceding(sentIter1.First());
            if (p != BreakIterator.Done || q != BreakIterator.Done)
            {
                Errln("ERROR: previous()/preceding() at starting position returned #"
                      + p + " and " + q + " instead of 0\n");
            }
            testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This\n costs $20,00,000.";
            Logln("testing line iter - String:- \"" + testString + "\"");
            RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator)BreakIterator.GetLineInstance(CultureInfo.CurrentCulture);

            lineIter1.SetText(testString);
            p = lineIter1.Last();
            if (p != testString.Length)
            {
                Errln("ERROR: last() returned" + p + "instead of " + testString.Length);
            }
            q = lineIter1.Previous();
            doTest(testString, p, q, 72, "$20,00,000.");
            p = q;
            q = lineIter1.Previous();
            doTest(testString, p, q, 66, "costs ");
            q = lineIter1.Preceding(40);
            doTest(testString, 40, q, 31, "Thankyou.");
            q = lineIter1.Preceding(25);
            doTest(testString, 25, q, 20, "I'am ");
            lineIter1.First();
            p = lineIter1.Previous();
            q = lineIter1.Preceding(sentIter1.First());
            if (p != BreakIterator.Done || q != BreakIterator.Done)
            {
                Errln("ERROR: previous()/preceding() at starting position returned #"
                      + p + " and " + q + " instead of 0\n");
            }
        }
コード例 #10
0
        public void TestFirstNextFollowing()
        {
            int    p, q;
            String testString = "This is a word break. Isn't it? 2.25";

            Logln("Testing first() and next(), following() with custom rules");
            Logln("testing word iterator - string :- \"" + testString + "\"\n");
            RuleBasedBreakIterator wordIter1 = (RuleBasedBreakIterator)BreakIterator.GetWordInstance(CultureInfo.CurrentCulture);

            wordIter1.SetText(testString);
            p = wordIter1.First();
            if (p != 0)
            {
                Errln("ERROR: first() returned" + p + "instead of 0");
            }
            q = wordIter1.Next(9);
            doTest(testString, p, q, 20, "This is a word break");
            p = q;
            q = wordIter1.Next();
            doTest(testString, p, q, 21, ".");
            p = q;
            q = wordIter1.Next(3);
            doTest(testString, p, q, 28, " Isn't ");
            p = q;
            q = wordIter1.Next(2);
            doTest(testString, p, q, 31, "it?");
            q = wordIter1.Following(2);
            doTest(testString, 2, q, 4, "is");
            q = wordIter1.Following(22);
            doTest(testString, 22, q, 27, "Isn't");
            wordIter1.Last();
            p = wordIter1.Next();
            q = wordIter1.Following(wordIter1.Last());
            if (p != BreakIterator.Done || q != BreakIterator.Done)
            {
                Errln("ERROR: next()/following() at last position returned #"
                      + p + " and " + q + " instead of" + testString.Length + "\n");
            }
            RuleBasedBreakIterator charIter1 = (RuleBasedBreakIterator)BreakIterator.GetCharacterInstance(CultureInfo.CurrentCulture);

            testString = "Write hindi here. ";
            Logln("testing char iter - string:- \"" + testString + "\"");
            charIter1.SetText(testString);
            p = charIter1.First();
            if (p != 0)
            {
                Errln("ERROR: first() returned" + p + "instead of 0");
            }
            q = charIter1.Next();
            doTest(testString, p, q, 1, "W");
            p = q;
            q = charIter1.Next(4);
            doTest(testString, p, q, 5, "rite");
            p = q;
            q = charIter1.Next(12);
            doTest(testString, p, q, 17, " hindi here.");
            p = q;
            q = charIter1.Next(-6);
            doTest(testString, p, q, 11, " here.");
            p = q;
            q = charIter1.Next(6);
            doTest(testString, p, q, 17, " here.");
            p = charIter1.Following(charIter1.Last());
            q = charIter1.Next(charIter1.Last());
            if (p != BreakIterator.Done || q != BreakIterator.Done)
            {
                Errln("ERROR: following()/next() at last position returned #"
                      + p + " and " + q + " instead of" + testString.Length);
            }
            testString = "Hello! how are you? I'am fine. Thankyou. How are you doing? This  costs $20,00,000.";
            RuleBasedBreakIterator sentIter1 = (RuleBasedBreakIterator)BreakIterator.GetSentenceInstance(CultureInfo.CurrentCulture);

            Logln("testing sentence iter - String:- \"" + testString + "\"");
            sentIter1.SetText(testString);
            p = sentIter1.First();
            if (p != 0)
            {
                Errln("ERROR: first() returned" + p + "instead of 0");
            }
            q = sentIter1.Next();
            doTest(testString, p, q, 7, "Hello! ");
            p = q;
            q = sentIter1.Next(2);
            doTest(testString, p, q, 31, "how are you? I'am fine. ");
            p = q;
            q = sentIter1.Next(-2);
            doTest(testString, p, q, 7, "how are you? I'am fine. ");
            p = q;
            q = sentIter1.Next(4);
            doTest(testString, p, q, 60, "how are you? I'am fine. Thankyou. How are you doing? ");
            p = q;
            q = sentIter1.Next();
            doTest(testString, p, q, 83, "This  costs $20,00,000.");
            q = sentIter1.Following(1);
            doTest(testString, 1, q, 7, "ello! ");
            q = sentIter1.Following(10);
            doTest(testString, 10, q, 20, " are you? ");
            q = sentIter1.Following(20);
            doTest(testString, 20, q, 31, "I'am fine. ");
            p = sentIter1.Following(sentIter1.Last());
            q = sentIter1.Next(sentIter1.Last());
            if (p != BreakIterator.Done || q != BreakIterator.Done)
            {
                Errln("ERROR: following()/next() at last position returned #"
                      + p + " and " + q + " instead of" + testString.Length);
            }
            testString = "Hello! how\r\n (are)\r you? I'am fine- Thankyou. foo\u00a0bar How, are, you? This, costs $20,00,000.";
            Logln("(UnicodeString)testing line iter - String:- \"" + testString + "\"");
            RuleBasedBreakIterator lineIter1 = (RuleBasedBreakIterator)BreakIterator.GetLineInstance(CultureInfo.CurrentCulture);

            lineIter1.SetText(testString);
            p = lineIter1.First();
            if (p != 0)
            {
                Errln("ERROR: first() returned" + p + "instead of 0");
            }
            q = lineIter1.Next();
            doTest(testString, p, q, 7, "Hello! ");
            p = q;
            p = q;
            q = lineIter1.Next(4);
            doTest(testString, p, q, 20, "how\r\n (are)\r ");
            p = q;
            q = lineIter1.Next(-4);
            doTest(testString, p, q, 7, "how\r\n (are)\r ");
            p = q;
            q = lineIter1.Next(6);
            doTest(testString, p, q, 30, "how\r\n (are)\r you? I'am ");
            p = q;
            q = lineIter1.Next();
            doTest(testString, p, q, 36, "fine- ");
            p = q;
            q = lineIter1.Next(2);
            doTest(testString, p, q, 54, "Thankyou. foo\u00a0bar ");
            q = lineIter1.Following(60);
            doTest(testString, 60, q, 64, "re, ");
            q = lineIter1.Following(1);
            doTest(testString, 1, q, 7, "ello! ");
            q = lineIter1.Following(10);
            doTest(testString, 10, q, 12, "\r\n");
            q = lineIter1.Following(20);
            doTest(testString, 20, q, 25, "you? ");
            p = lineIter1.Following(lineIter1.Last());
            q = lineIter1.Next(lineIter1.Last());
            if (p != BreakIterator.Done || q != BreakIterator.Done)
            {
                Errln("ERROR: following()/next() at last position returned #"
                      + p + " and " + q + " instead of" + testString.Length);
            }
        }
コード例 #11
0
        public void TestNullLocale()
        {
            CultureInfo loc  = null;
            ULocale     uloc = null;

            BreakIterator brk;

            // Character
            try
            {
                brk = BreakIterator.GetCharacterInstance(loc);
                Errln("getCharacterInstance((Locale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }
            try
            {
                brk = BreakIterator.GetCharacterInstance(uloc);
                Errln("getCharacterInstance((ULocale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }

            // Line
            try
            {
                brk = BreakIterator.GetLineInstance(loc);
                Errln("getLineInstance((Locale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }
            try
            {
                brk = BreakIterator.GetLineInstance(uloc);
                Errln("getLineInstance((ULocale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }

            // Sentence
            try
            {
                brk = BreakIterator.GetSentenceInstance(loc);
                Errln("getSentenceInstance((Locale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }
            try
            {
                brk = BreakIterator.GetSentenceInstance(uloc);
                Errln("getSentenceInstance((ULocale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }

            // Title
            try
            {
                brk = BreakIterator.GetTitleInstance(loc);
                Errln("getTitleInstance((Locale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }
            try
            {
                brk = BreakIterator.GetTitleInstance(uloc);
                Errln("getTitleInstance((ULocale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }

            // Word
            try
            {
                brk = BreakIterator.GetWordInstance(loc);
                Errln("getWordInstance((Locale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }
            try
            {
                brk = BreakIterator.GetWordInstance(uloc);
                Errln("getWordInstance((ULocale)null) did not throw NPE.");
            }
            catch (ArgumentNullException e) { /* OK */ }
        }