private void Test1Sentence(BreakIterator bi, String text)
        {
            int start = bi.Text.BeginIndex;

            assertEquals(start, bi.First());
            int current = bi.Current;

            assertEquals(bi.Text.EndIndex, bi.Next());
            int end = bi.Current - start;

            assertEquals(text, text.Substring(current - start, end - start));

            assertEquals(text.Length, bi.Last() - start);
            end = bi.Current;
            bi.Previous();
            assertEquals(BreakIterator.Done, bi.Previous());
            int previous = bi.Current;

            assertEquals(text, text.Substring(previous - start, end - start));
            assertEquals(start, bi.Current);

            assertEquals(BreakIterator.Done, bi.Following(bi.Last() / 2 + start));

            assertEquals(BreakIterator.Done, bi.Preceding(bi.Last() / 2 + start));

            assertEquals(start, bi.First());
            assertEquals(BreakIterator.Done, bi.Next(13));
            assertEquals(BreakIterator.Done, bi.Next(-8));
        }
示例#2
0
        /** Asserts that two breakiterators break the text the same way */
        public void assertSameBreaks(CharacterIterator one, CharacterIterator two, BreakIterator expected, BreakIterator actual)
        {
            expected.SetText(one);
            actual.SetText(two);

            assertEquals(expected.Current, actual.Current);

            // next()
            int v = expected.Current;

            while (v != BreakIterator.DONE)
            {
                assertEquals(v = expected.Next(), actual.Next());
                assertEquals(expected.Current, actual.Current);
            }

            // first()
            assertEquals(expected.First(), actual.First());
            assertEquals(expected.Current, actual.Current);
            // last()
            assertEquals(expected.Last(), actual.Last());
            assertEquals(expected.Current, actual.Current);

            // previous()
            v = expected.Current;
            while (v != BreakIterator.DONE)
            {
                assertEquals(v = expected.Previous(), actual.Previous());
                assertEquals(expected.Current, actual.Current);
            }

            // following()
            for (int i = one.BeginIndex; i <= one.EndIndex; i++)
            {
                expected.First();
                actual.First();
                assertEquals(expected.Following(i), actual.Following(i));
                assertEquals(expected.Current, actual.Current);
            }

            // preceding()
            for (int i = one.BeginIndex; i <= one.EndIndex; i++)
            {
                expected.Last();
                actual.Last();
                assertEquals(expected.Preceding(i), actual.Preceding(i));
                assertEquals(expected.Current, actual.Current);
            }
        }
示例#3
0
        private List <String> _testLastAndPrevious(BreakIterator bi, String text)
        {
            int           p      = bi.Last();
            int           lastP  = p;
            List <String> result = new List <String>();

            if (p != text.Length)
            {
                Errln("last() returned " + p + " instead of " + text.Length);
            }
            while (p != BreakIterator.Done)
            {
                p = bi.Previous();
                if (p != BreakIterator.Done)
                {
                    if (p >= lastP)
                    {
                        Errln("previous() failed to move backward: previous() on position "
                              + lastP + " yielded " + p);
                    }

                    result.Insert(0, text.Substring(p, lastP - p)); // ICU4N: Corrected 2nd substring parameter
                }
                else
                {
                    if (lastP != 0)
                    {
                        Errln("previous() returned DONE prematurely: offset was "
                              + lastP + " instead of 0");
                    }
                }
                lastP = p;
            }
            return(result);
        }
 public virtual int FindStartOffset(StringBuilder buffer, int start)
 {
     // avoid illegal start offset
     if (start > buffer.Length || start < 1) return start;
     bi.SetText(buffer.ToString(0, start - 0));
     bi.Last();
     return bi.Previous();
 }
 private void Test0Sentences(BreakIterator bi)
 {
     assertEquals(0, bi.Current);
     assertEquals(0, bi.First());
     assertEquals(BreakIterator.Done, bi.Next());
     assertEquals(0, bi.Last());
     assertEquals(BreakIterator.Done, bi.Previous());
     assertEquals(BreakIterator.Done, bi.Following(0));
     assertEquals(BreakIterator.Done, bi.Preceding(0));
     assertEquals(0, bi.First());
     assertEquals(BreakIterator.Done, bi.Next(13));
     assertEquals(BreakIterator.Done, bi.Next(-8));
 }
        private void Do3SentenceTest(BreakIterator bi) // LUCENENET NOTE: Refactored a bit because Substring in .NET requires some light math to match Java
        {
            assertEquals(0, bi.Current);
            assertEquals(0, bi.First());
            int current = bi.Current;

            assertEquals(SENTENCES[0], TEXT.Substring(current, bi.Next() - current)); // LUCNENENET: Corrected 2nd parameter
            current = bi.Current;
            assertEquals(SENTENCES[1], TEXT.Substring(current, bi.Next() - current)); // LUCNENENET: Corrected 2nd parameter
            current = bi.Current;
            assertEquals(bi.Text.EndIndex, bi.Next());
            int next = bi.Current;

            assertEquals(SENTENCES[2], TEXT.Substring(current, next - current)); // LUCNENENET: Corrected 2nd parameter
            assertEquals(BreakIterator.Done, bi.Next());

            assertEquals(TEXT.Length, bi.Last());
            int end  = bi.Current;
            int prev = bi.Previous();

            assertEquals(SENTENCES[2], TEXT.Substring(prev, end - prev)); // LUCNENENET: Corrected 2nd parameter
            end  = bi.Current;
            prev = bi.Previous();
            assertEquals(SENTENCES[1], TEXT.Substring(prev, end - prev)); // LUCNENENET: Corrected 2nd parameter
            end  = bi.Current;
            prev = bi.Previous();
            assertEquals(SENTENCES[0], TEXT.Substring(prev, end - prev)); // LUCNENENET: Corrected 2nd parameter
            assertEquals(BreakIterator.Done, bi.Previous());
            assertEquals(0, bi.Current);

            assertEquals(59, bi.Following(39));
            assertEquals(59, bi.Following(31));
            assertEquals(31, bi.Following(30));

            assertEquals(0, bi.Preceding(57));
            assertEquals(0, bi.Preceding(58));
            assertEquals(31, bi.Preceding(59));

            assertEquals(0, bi.First());
            assertEquals(59, bi.Next(2));
            assertEquals(0, bi.Next(-2));
        }
        public void TestLineIteration()
        {
            BreakIterator bi = GetLineInstance(System.Globalization.CultureInfo.InvariantCulture);

            // Test empty
            Assert.AreEqual(0, bi.Current);
            Assert.AreEqual(BreakIterator.Done, bi.Next());
            Assert.AreEqual(0, bi.Current);

            bi.SetText(LINE_TEXT);

            // Ensure position starts at 0 when initialized
            Assert.AreEqual(0, bi.Current);

            // Check first boundary (Apache\t^Lucene) - Ensure we break on \t
            Assert.AreEqual(7, bi.Next());

            // Ensure Current returns the most recent boundary
            Assert.AreEqual(7, bi.Current);

            // Check next boundary (Lucene^(TM))
            Assert.AreEqual(13, bi.Next());

            // Ensure Current returns the most recent boundary
            Assert.AreEqual(13, bi.Current);

            // Check next boundary (Lucene(TM) ^is a)
            Assert.AreEqual(18, bi.Next());

            // Ensure Current returns the most recent boundary
            Assert.AreEqual(18, bi.Current);

            // Move to start of high-performance
            bi.Next();
            bi.Next();

            // Check next boundary (high-\n^performance)
            Assert.AreEqual(29, bi.Next());


            // Check last boundary (in Java.^)
            Assert.AreEqual(108, bi.Last());


            // Check move past last boundary
            Assert.AreEqual(BreakIterator.Done, bi.Next());

            // Ensure we are still at last boundary
            Assert.AreEqual(108, bi.Current);


            // Check MovePrevious
            Assert.AreEqual(103, bi.Previous());

            // Ensure we get the same value for Current as the last move
            Assert.AreEqual(103, bi.Current);


            // Check MoveFirst
            Assert.AreEqual(0, bi.First());

            // Ensure we get the same value for Current as the last move
            Assert.AreEqual(0, bi.Current);


            // Check moving beyond first boundary
            Assert.AreEqual(BreakIterator.Done, bi.Previous());

            // Ensure we are still at first boundary
            Assert.AreEqual(0, bi.Current);


            // Check MoveLast()
            Assert.AreEqual(108, bi.Last());
        }
        public void TestWordIteration()
        {
            BreakIterator bi = GetWordInstance(System.Globalization.CultureInfo.InvariantCulture);

            bi.SetText("");

            // Test empty
            Assert.AreEqual(0, bi.Current);
            Assert.AreEqual(BreakIterator.Done, bi.Next());
            Assert.AreEqual(0, bi.Current);

            bi.SetText(TEXT);

            // Ensure position starts at 0 when initialized
            Assert.AreEqual(0, bi.Current);

            // Check first boundary (Apache^)
            Assert.AreEqual(6, bi.Next());

            // Ensure Current returns the last boundary iterated to
            Assert.AreEqual(6, bi.Current);

            // Check second boundary (^Lucene)
            Assert.AreEqual(7, bi.Next());

            // Ensure Current returns the last boundary iterated to
            Assert.AreEqual(7, bi.Current);

            // Check third boundary (Lucene^)
            Assert.AreEqual(13, bi.Next());

            // Ensure Current returns the last boundary iterated to
            Assert.AreEqual(13, bi.Current);

            // Check fourth boundary (^TM)
            Assert.AreEqual(14, bi.Next());

            // Check fifth boundary (TM^)
            Assert.AreEqual(16, bi.Next());

            // Check sixth boundary (TM)^
            Assert.AreEqual(17, bi.Next());

            // Check seventh boundary (^is)
            Assert.AreEqual(18, bi.Next());

            // Move to (^high-performance)
            bi.Next();
            bi.Next();
            bi.Next();

            // Check next boundary (^high-performance)
            Assert.AreEqual(23, bi.Next());

            // Ensure we don't break on hyphen (high-performance^)
            Assert.AreEqual(39, bi.Next());


            // Check MoveLast()
            Assert.AreEqual(107, bi.Last());

            // Check going past last boundary
            Assert.AreEqual(BreakIterator.Done, bi.Next());

            // Check we are still at last boundary
            Assert.AreEqual(107, bi.Current);


            // Check MoveFirst()
            Assert.AreEqual(0, bi.First());

            // Check going past first boundary
            Assert.AreEqual(BreakIterator.Done, bi.Previous());

            // Check we are still at first boundary
            Assert.AreEqual(0, bi.Current);
        }
        public void TestSentenceIteration()
        {
            BreakIterator bi = GetSentenceInstance(System.Globalization.CultureInfo.InvariantCulture);

            bi.SetText("");

            // Test empty
            Assert.AreEqual(0, bi.Current);
            Assert.AreEqual(BreakIterator.Done, bi.Next());
            Assert.AreEqual(0, bi.Current);

            bi.SetText(SENTENCE_TEXT);

            // Ensure position starts at 0 when initialized
            Assert.AreEqual(0, bi.Current);

            // Check first boundary (in Java.^) - Ensure we don't break on \n
            Assert.AreEqual(108, bi.Next());

            // Ensure Current returns the most recent boundary
            Assert.AreEqual(108, bi.Current);

            // Check next boundary (especially cross-platform.^)
            Assert.AreEqual(221, bi.Next());

            // Check next boundary (free download.^)
            Assert.AreEqual(290, bi.Next());

            // Check next boundary (things easy.^)
            Assert.AreEqual(324, bi.Next());

            // Check next boundary (is powerful.^)
            Assert.AreEqual(344, bi.Next());

            // Check next boundary (is exciting.^)
            Assert.AreEqual(364, bi.Next());

            // Check next boundary (is cool.^)
            Assert.AreEqual(380, bi.Next());

            // Check last boundary (Lucene now?^)
            Assert.AreEqual(400, bi.Next());

            // Check move past last boundary
            Assert.AreEqual(BreakIterator.Done, bi.Next());

            // Ensure we are still at last boundary
            Assert.AreEqual(400, bi.Current);


            // Check MovePrevious
            Assert.AreEqual(380, bi.Previous());

            // Ensure we get the same value for Current as the last move
            Assert.AreEqual(380, bi.Current);


            // Check MoveFirst
            Assert.AreEqual(0, bi.First());

            // Ensure we get the same value for Current as the last move
            Assert.AreEqual(0, bi.Current);

            // Check moving beyond first boundary
            Assert.AreEqual(BreakIterator.Done, bi.Previous());

            // Ensure we are still at first boundary
            Assert.AreEqual(0, bi.Current);


            // Check MoveLast()
            Assert.AreEqual(400, bi.Last());
        }
示例#10
0
 public override int Last()
 {
     // Don't suppress a break opportunity at the end of text.
     return(@delegate.Last());
 }
示例#11
0
        /// <summary>
        /// Returns the position at the end of the next layout.  Does NOT
        /// update the current position of this <code>LineBreakMeasurer</code>.
        /// </summary>
        /// <param name="wrappingWidth"> the maximum visible advance permitted for
        ///    the text in the next layout </param>
        /// <param name="offsetLimit"> the first character that can not be included
        ///    in the next layout, even if the text after the limit would fit
        ///    within the wrapping width; <code>offsetLimit</code> must be
        ///    greater than the current position </param>
        /// <param name="requireNextWord"> if <code>true</code>, the current position
        ///    that is returned if the entire next word does not fit within
        ///    <code>wrappingWidth</code>; if <code>false</code>, the offset
        ///    returned is at least one greater than the current position </param>
        /// <returns> an offset in the text representing the limit of the
        ///    next <code>TextLayout</code> </returns>
        public int NextOffset(float wrappingWidth, int offsetLimit, bool requireNextWord)
        {
            int nextOffset = Pos;

            if (Pos < Limit)
            {
                if (offsetLimit <= Pos)
                {
                    throw new IllegalArgumentException("offsetLimit must be after current position");
                }

                int charAtMaxAdvance = Measurer.GetLineBreakIndex(Pos, wrappingWidth);

                if (charAtMaxAdvance == Limit)
                {
                    nextOffset = Limit;
                }
                else if (char.IsWhiteSpace(Measurer.Chars[charAtMaxAdvance - Start]))
                {
                    nextOffset = BreakIter.Following(charAtMaxAdvance);
                }
                else
                {
                    // Break is in a word;  back up to previous break.

                    // NOTE:  I think that breakIter.preceding(limit) should be
                    // equivalent to breakIter.last(), breakIter.previous() but
                    // the authors of BreakIterator thought otherwise...
                    // If they were equivalent then the first branch would be
                    // unnecessary.
                    int testPos = charAtMaxAdvance + 1;
                    if (testPos == Limit)
                    {
                        BreakIter.Last();
                        nextOffset = BreakIter.Previous();
                    }
                    else
                    {
                        nextOffset = BreakIter.Preceding(testPos);
                    }

                    if (nextOffset <= Pos)
                    {
                        // first word doesn't fit on line
                        if (requireNextWord)
                        {
                            nextOffset = Pos;
                        }
                        else
                        {
                            nextOffset = System.Math.Max(Pos + 1, charAtMaxAdvance);
                        }
                    }
                }
            }

            if (nextOffset > offsetLimit)
            {
                nextOffset = offsetLimit;
            }

            return(nextOffset);
        }