Java Code Examples for com.ibm.icu.text.BreakIterator#getSentenceInstance()

The following examples show how to use com.ibm.icu.text.BreakIterator#getSentenceInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocaleDisplayNamesImpl.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
private String adjustForUsageAndContext(CapitalizationContextUsage usage, String name) {
    if (name != null && name.length() > 0 && UCharacter.isLowerCase(name.codePointAt(0)) &&
            (capitalization==DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
            (capitalizationUsage != null && capitalizationUsage[usage.ordinal()]) )) {
        // Note, won't have capitalizationUsage != null && capitalizationUsage[usage.ordinal()]
        // unless capitalization is CAPITALIZATION_FOR_UI_LIST_OR_MENU or CAPITALIZATION_FOR_STANDALONE
        synchronized (this) {
            if (capitalizationBrkIter == null) {
                // should only happen when deserializing, etc.
                capitalizationBrkIter = BreakIterator.getSentenceInstance(locale);
            }
            return UCharacter.toTitleCase(locale, name, capitalizationBrkIter,
                    UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
        }
    }
    return name;
}
 
Example 2
Source File: CaseMapImpl.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
public static BreakIterator getTitleBreakIterator(
        Locale locale, int options, BreakIterator iter) {
    options &= TITLECASE_ITERATOR_MASK;
    if (options != 0 && iter != null) {
        throw new IllegalArgumentException(
                "titlecasing iterator option together with an explicit iterator");
    }
    if (iter == null) {
        switch (options) {
        case 0:
            iter = BreakIterator.getWordInstance(locale);
            break;
        case TITLECASE_WHOLE_STRING:
            iter = new WholeStringBreakIterator();
            break;
        case TITLECASE_SENTENCES:
            iter = BreakIterator.getSentenceInstance(locale);
            break;
        default:
            throw new IllegalArgumentException("unknown titlecasing iterator option");
        }
    }
    return iter;
}
 
Example 3
Source File: CaseMapImpl.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
public static BreakIterator getTitleBreakIterator(
        ULocale locale, int options, BreakIterator iter) {
    options &= TITLECASE_ITERATOR_MASK;
    if (options != 0 && iter != null) {
        throw new IllegalArgumentException(
                "titlecasing iterator option together with an explicit iterator");
    }
    if (iter == null) {
        switch (options) {
        case 0:
            iter = BreakIterator.getWordInstance(locale);
            break;
        case TITLECASE_WHOLE_STRING:
            iter = new WholeStringBreakIterator();
            break;
        case TITLECASE_SENTENCES:
            iter = BreakIterator.getSentenceInstance(locale);
            break;
        default:
            throw new IllegalArgumentException("unknown titlecasing iterator option");
        }
    }
    return iter;
}
 
Example 4
Source File: SegmenterObject.java    From es6draft with MIT License 6 votes vote down vote up
private BreakIterator createBreakIterator() {
    ULocale locale = ULocale.forLanguageTag(this.locale);
    if ("line".equals(granularity)) {
        // "strictness" cannot be set through unicode extensions (u-lb-strict), handle here:
        locale = locale.setKeywordValue("lb", strictness);
    }
    BreakIterator breakIterator;
    switch (granularity) {
    case "grapheme":
        breakIterator = BreakIterator.getCharacterInstance(locale);
        break;
    case "word":
        breakIterator = BreakIterator.getWordInstance(locale);
        break;
    case "sentence":
        breakIterator = BreakIterator.getSentenceInstance(locale);
        break;
    case "line":
        breakIterator = BreakIterator.getLineInstance(locale);
        break;
    default:
        throw new AssertionError();
    }
    return breakIterator;
}
 
Example 5
Source File: GlobalizationPreferences.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * This function can be overridden by subclasses to use different heuristics.
 * <b>It MUST return a 'safe' value,
 * one whose modification will not affect this object.</b>
 *
 * @param type
 * @draft ICU 3.6
 * @provisional This API might change or be removed in a future release.
 */
protected BreakIterator guessBreakIterator(int type) {
    BreakIterator bitr = null;
    ULocale brkLocale = getAvailableLocale(TYPE_BREAKITERATOR);
    if (brkLocale == null) {
        brkLocale = ULocale.ROOT;
    }
    switch (type) {
    case BI_CHARACTER:
        bitr = BreakIterator.getCharacterInstance(brkLocale);
        break;
    case BI_TITLE:
        bitr = BreakIterator.getTitleInstance(brkLocale);
        break;
    case BI_WORD:
        bitr = BreakIterator.getWordInstance(brkLocale);
        break;
    case BI_LINE:
        bitr = BreakIterator.getLineInstance(brkLocale);
        break;
    case BI_SENTENCE:
        bitr = BreakIterator.getSentenceInstance(brkLocale);
        break;
    default:
        throw new IllegalArgumentException("Unknown break iterator type");
    }
    return bitr;
}
 
Example 6
Source File: RelativeDateFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
@Override
public void setContext(DisplayContext context) {
    super.setContext(context);
    if (!capitalizationInfoIsSet &&
          (context==DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU || context==DisplayContext.CAPITALIZATION_FOR_STANDALONE)) {
        initCapitalizationContextInfo(fLocale);
        capitalizationInfoIsSet = true;
    }
    if (capitalizationBrkIter == null && (context==DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
          (context==DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU && capitalizationOfRelativeUnitsForListOrMenu) ||
          (context==DisplayContext.CAPITALIZATION_FOR_STANDALONE && capitalizationOfRelativeUnitsForStandAlone) )) {
        capitalizationBrkIter = BreakIterator.getSentenceInstance(fLocale);
    }
}
 
Example 7
Source File: SpellCheckIterator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a new spell check iterator.
 *
 * @param document the document containing the specified partition
 * @param region the region to spell check
 * @param locale the locale to use for spell checking
 * @param breakIterator the break-iterator
 */
public SpellCheckIterator(IDocument document, IRegion region, Locale locale, BreakIterator breakIterator) {
	fOffset= region.getOffset();
	fWordIterator= breakIterator;
	fDelimiter= TextUtilities.getDefaultLineDelimiter(document);

	String content;
	try {

		content= document.get(region.getOffset(), region.getLength());
		if (content.startsWith(NLSElement.TAG_PREFIX))
			content= ""; //$NON-NLS-1$

	} catch (Exception exception) {
		content= ""; //$NON-NLS-1$
	}
	fContent= content;

	fWordIterator.setText(content);
	fPredecessor= fWordIterator.first();
	fSuccessor= fWordIterator.next();

	final BreakIterator iterator= BreakIterator.getSentenceInstance(locale);
	iterator.setText(content);

	int offset= iterator.current();
	while (offset != BreakIterator.DONE) {

		fSentenceBreaks.add(new Integer(offset));
		offset= iterator.next();
	}
}
 
Example 8
Source File: LocaleDisplayNamesImpl.java    From fitnotifications with Apache License 2.0 4 votes vote down vote up
public LocaleDisplayNamesImpl(ULocale locale, DisplayContext... contexts) {
    DialectHandling dialectHandling = DialectHandling.STANDARD_NAMES;
    DisplayContext capitalization = DisplayContext.CAPITALIZATION_NONE;
    DisplayContext nameLength = DisplayContext.LENGTH_FULL;
    DisplayContext substituteHandling = DisplayContext.SUBSTITUTE;
    for (DisplayContext contextItem : contexts) {
        switch (contextItem.type()) {
        case DIALECT_HANDLING:
            dialectHandling = (contextItem.value()==DisplayContext.STANDARD_NAMES.value())?
                    DialectHandling.STANDARD_NAMES: DialectHandling.DIALECT_NAMES;
            break;
        case CAPITALIZATION:
            capitalization = contextItem;
            break;
        case DISPLAY_LENGTH:
            nameLength = contextItem;
            break;
        case SUBSTITUTE_HANDLING:
            substituteHandling = contextItem;
            break;
        default:
            break;
        }
    }

    this.dialectHandling = dialectHandling;
    this.capitalization = capitalization;
    this.nameLength = nameLength;
    this.substituteHandling = substituteHandling;
    this.langData = LangDataTables.impl.get(locale, substituteHandling == DisplayContext.NO_SUBSTITUTE);
    this.regionData = RegionDataTables.impl.get(locale, substituteHandling == DisplayContext.NO_SUBSTITUTE);
    this.locale = ULocale.ROOT.equals(langData.getLocale()) ? regionData.getLocale() :
        langData.getLocale();

    // Note, by going through DataTable, this uses table lookup rather than straight lookup.
    // That should get us the same data, I think.  This way we don't have to explicitly
    // load the bundle again.  Using direct lookup didn't seem to make an appreciable
    // difference in performance.
    String sep = langData.get("localeDisplayPattern", "separator");
    if (sep == null || "separator".equals(sep)) {
        sep = "{0}, {1}";
    }
    StringBuilder sb = new StringBuilder();
    this.separatorFormat = SimpleFormatterImpl.compileToStringMinMaxArguments(sep, sb, 2, 2);

    String pattern = langData.get("localeDisplayPattern", "pattern");
    if (pattern == null || "pattern".equals(pattern)) {
        pattern = "{0} ({1})";
    }
    this.format = SimpleFormatterImpl.compileToStringMinMaxArguments(pattern, sb, 2, 2);
    if (pattern.contains("(")) {
        formatOpenParen = '(';
        formatCloseParen = ')';
        formatReplaceOpenParen = '[';
        formatReplaceCloseParen = ']';
    } else  {
        formatOpenParen = '(';
        formatCloseParen = ')';
        formatReplaceOpenParen = '[';
        formatReplaceCloseParen = ']';
    }

    String keyTypePattern = langData.get("localeDisplayPattern", "keyTypePattern");
    if (keyTypePattern == null || "keyTypePattern".equals(keyTypePattern)) {
        keyTypePattern = "{0}={1}";
    }
    this.keyTypeFormat = SimpleFormatterImpl.compileToStringMinMaxArguments(
            keyTypePattern, sb, 2, 2);

    // Get values from the contextTransforms data if we need them
    // Also check whether we will need a break iterator (depends on the data)
    boolean needBrkIter = false;
    if (capitalization == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU ||
            capitalization == DisplayContext.CAPITALIZATION_FOR_STANDALONE) {
        capitalizationUsage = new boolean[CapitalizationContextUsage.values().length]; // initialized to all false
        ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUData.ICU_BASE_NAME, locale);
        CapitalizationContextSink sink = new CapitalizationContextSink();
        try {
            rb.getAllItemsWithFallback("contextTransforms", sink);
        }
        catch (MissingResourceException e) {
            // Silently ignore.  Not every locale has contextTransforms.
        }
        needBrkIter = sink.hasCapitalizationUsage;
    }
    // Get a sentence break iterator if we will need it
    if (needBrkIter || capitalization == DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) {
        capitalizationBrkIter = BreakIterator.getSentenceInstance(locale);
    }

    this.currencyDisplayInfo = CurrencyData.provider.getInstance(locale, false);
}
 
Example 9
Source File: RelativeDateFormat.java    From fitnotifications with Apache License 2.0 4 votes vote down vote up
@Override
public StringBuffer format(Calendar cal, StringBuffer toAppendTo,
        FieldPosition fieldPosition) {

    String relativeDayString = null;
    DisplayContext capitalizationContext = getContext(DisplayContext.Type.CAPITALIZATION);

    if (fDateStyle != DateFormat.NONE) {
        // calculate the difference, in days, between 'cal' and now.
        int dayDiff = dayDifference(cal);

        // look up string
        relativeDayString = getStringForDay(dayDiff);
    }

    if (fDateTimeFormat != null) {
        if (relativeDayString != null && fDatePattern != null &&
                (fTimePattern == null || fCombinedFormat == null || combinedFormatHasDateAtStart) ) {
            // capitalize relativeDayString according to context for relative, set formatter no context
            if ( relativeDayString.length() > 0 && UCharacter.isLowerCase(relativeDayString.codePointAt(0)) &&
                 (capitalizationContext == DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE ||
                    (capitalizationContext == DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU && capitalizationOfRelativeUnitsForListOrMenu) ||
                    (capitalizationContext == DisplayContext.CAPITALIZATION_FOR_STANDALONE && capitalizationOfRelativeUnitsForStandAlone) )) {
                if (capitalizationBrkIter == null) {
                    // should only happen when deserializing, etc.
                    capitalizationBrkIter = BreakIterator.getSentenceInstance(fLocale);
                }
                relativeDayString = UCharacter.toTitleCase(fLocale, relativeDayString, capitalizationBrkIter,
                                UCharacter.TITLECASE_NO_LOWERCASE | UCharacter.TITLECASE_NO_BREAK_ADJUSTMENT);
            }
            fDateTimeFormat.setContext(DisplayContext.CAPITALIZATION_NONE);
        } else {
            // set our context for the formatter
            fDateTimeFormat.setContext(capitalizationContext);
        }
    }

    if (fDateTimeFormat != null && (fDatePattern != null || fTimePattern != null)) {
        // The new way
        if (fDatePattern == null) {
            // must have fTimePattern
            fDateTimeFormat.applyPattern(fTimePattern);
            fDateTimeFormat.format(cal, toAppendTo, fieldPosition);
        } else if (fTimePattern == null) {
            // must have fDatePattern
            if (relativeDayString != null) {
                toAppendTo.append(relativeDayString);
            } else {
                fDateTimeFormat.applyPattern(fDatePattern);
                fDateTimeFormat.format(cal, toAppendTo, fieldPosition);
            }
        } else {
            String datePattern = fDatePattern; // default;
            if (relativeDayString != null) {
                // Need to quote the relativeDayString to make it a legal date pattern
                datePattern = "'" + relativeDayString.replace("'", "''") + "'";
            }
            StringBuffer combinedPattern = new StringBuffer("");
            fCombinedFormat.format(new Object[] {fTimePattern, datePattern}, combinedPattern, new FieldPosition(0));
            fDateTimeFormat.applyPattern(combinedPattern.toString());
            fDateTimeFormat.format(cal, toAppendTo, fieldPosition);
        }
    } else if (fDateFormat != null) {
        // A subset of the old way, for serialization compatibility
        // (just do the date part)
        if (relativeDayString != null) {
            toAppendTo.append(relativeDayString);
        } else {
            fDateFormat.format(cal, toAppendTo, fieldPosition);
        }
    }

    return toAppendTo;
}