示例#1
0
        public void TestFilteredBreakIteratorBuilder()
        {
            FilteredBreakIteratorBuilder builder;
            BreakIterator baseBI;
            BreakIterator filteredBI;

            String text      = "In the meantime Mr. Weston arrived with his small ship, which he had now recovered. Capt. Gorges, who informed the Sgt. here that one purpose of his going east was to meet with Mr. Weston, took this opportunity to call him to account for some abuses he had to lay to his charge."; // (William Bradford, public domain. http://catalog.hathitrust.org/Record/008651224 ) - edited.
            String ABBR_MR   = "Mr.";
            String ABBR_CAPT = "Capt.";

            {
                Logln("Constructing empty builder\n");
                builder = FilteredBreakIteratorBuilder.GetEmptyInstance();

                Logln("Constructing base BI\n");
                baseBI = BreakIterator.GetSentenceInstance(new CultureInfo("en"));

                Logln("Building new BI\n");
                filteredBI = builder.WrapIteratorWithFilter(baseBI);

                assertDefaultBreakBehavior(filteredBI, text);
            }

            {
                Logln("Constructing empty builder\n");
                builder = FilteredBreakIteratorBuilder.GetEmptyInstance();

                Logln("Adding Mr. as an exception\n");

                assertEquals("2.1 suppressBreakAfter", true, builder.SuppressBreakAfter(ABBR_MR));
                assertEquals("2.2 suppressBreakAfter", false, builder.SuppressBreakAfter(ABBR_MR));
                assertEquals("2.3 unsuppressBreakAfter", true, builder.UnsuppressBreakAfter(ABBR_MR));
                assertEquals("2.4 unsuppressBreakAfter", false, builder.UnsuppressBreakAfter(ABBR_MR));
                assertEquals("2.5 suppressBreakAfter", true, builder.SuppressBreakAfter(ABBR_MR));

                Logln("Constructing base BI\n");
                baseBI = BreakIterator.GetSentenceInstance(new CultureInfo("en"));

                Logln("Building new BI\n");
                filteredBI = builder.WrapIteratorWithFilter(baseBI);

                Logln("Testing:");
                filteredBI.SetText(text);
                assertEquals("2nd next", 84, filteredBI.Next());
                assertEquals("2nd next", 90, filteredBI.Next());
                assertEquals("2nd next", 278, filteredBI.Next());
                filteredBI.First();
            }


            {
                Logln("Constructing empty builder\n");
                builder = FilteredBreakIteratorBuilder.GetEmptyInstance();

                Logln("Adding Mr. and Capt as an exception\n");
                assertEquals("3.1 suppressBreakAfter", true, builder.SuppressBreakAfter(ABBR_MR));
                assertEquals("3.2 suppressBreakAfter", true, builder.SuppressBreakAfter(ABBR_CAPT));

                Logln("Constructing base BI\n");
                baseBI = BreakIterator.GetSentenceInstance(new CultureInfo("en"));

                Logln("Building new BI\n");
                filteredBI = builder.WrapIteratorWithFilter(baseBI);

                Logln("Testing:");
                filteredBI.SetText(text);
                assertEquals("3rd next", 84, filteredBI.Next());
                assertEquals("3rd next", 278, filteredBI.Next());
                filteredBI.First();
            }

            {
                Logln("Constructing English builder\n");
                builder = FilteredBreakIteratorBuilder.GetInstance(ULocale.ENGLISH);

                Logln("Constructing base BI\n");
                baseBI = BreakIterator.GetSentenceInstance(new CultureInfo("en"));

                Logln("unsuppressing 'Capt'");
                assertEquals("1st suppressBreakAfter", true, builder.UnsuppressBreakAfter(ABBR_CAPT));

                Logln("Building new BI\n");
                filteredBI = builder.WrapIteratorWithFilter(baseBI);

                if (filteredBI != null)
                {
                    Logln("Testing:");
                    filteredBI.SetText(text);
                    assertEquals("4th next", 84, filteredBI.Next());
                    assertEquals("4th next", 90, filteredBI.Next());
                    assertEquals("4th next", 278, filteredBI.Next());
                    filteredBI.First();
                }
            }

            {
                Logln("Constructing English builder\n");
                builder = FilteredBreakIteratorBuilder.GetInstance(ULocale.ENGLISH);

                Logln("Constructing base BI\n");
                baseBI = BreakIterator.GetSentenceInstance(new CultureInfo("en"));

                Logln("Building new BI\n");
                filteredBI = builder.WrapIteratorWithFilter(baseBI);

                if (filteredBI != null)
                {
                    assertEnglishBreakBehavior(filteredBI, text);
                }
            }

            {
                Logln("Constructing English @ss=standard\n");
                filteredBI = BreakIterator.GetSentenceInstance(ULocale.ForLanguageTag("en-US-u-ss-standard"));

                if (filteredBI != null)
                {
                    assertEnglishBreakBehavior(filteredBI, text);
                }
            }

            {
                Logln("Constructing Afrikaans @ss=standard - should be == default\n");
                filteredBI = BreakIterator.GetSentenceInstance(ULocale.ForLanguageTag("af-u-ss-standard"));

                assertDefaultBreakBehavior(filteredBI, text);
            }

            {
                Logln("Constructing Japanese @ss=standard - should be == default\n");
                filteredBI = BreakIterator.GetSentenceInstance(ULocale.ForLanguageTag("ja-u-ss-standard"));

                assertDefaultBreakBehavior(filteredBI, text);
            }
            {
                Logln("Constructing tfg @ss=standard - should be == default\n");
                filteredBI = BreakIterator.GetSentenceInstance(ULocale.ForLanguageTag("tfg-u-ss-standard"));

                assertDefaultBreakBehavior(filteredBI, text);
            }

            {
                Logln("Constructing French builder");
                builder = FilteredBreakIteratorBuilder.GetInstance(ULocale.FRENCH);

                Logln("Constructing base BI\n");
                baseBI = BreakIterator.GetSentenceInstance(new CultureInfo("fr"));

                Logln("Building new BI\n");
                filteredBI = builder.WrapIteratorWithFilter(baseBI);

                if (filteredBI != null)
                {
                    assertFrenchBreakBehavior(filteredBI, text);
                }
            }
        }