Class AbstractUnitConversionRule


  • public abstract class AbstractUnitConversionRule
    extends Rule
    Base class providing support for detecting, parsing and converting between measurements in different units
    Since:
    4.3
    • Field Detail

      • POUND

        protected static final javax.measure.Unit<javax.measure.quantity.Mass> POUND
      • OUNCE

        protected static final javax.measure.Unit<javax.measure.quantity.Mass> OUNCE
      • FEET

        protected static final javax.measure.Unit<javax.measure.quantity.Length> FEET
      • YARD

        protected static final javax.measure.Unit<javax.measure.quantity.Length> YARD
      • INCH

        protected static final javax.measure.Unit<javax.measure.quantity.Length> INCH
      • MILE

        protected static final javax.measure.Unit<javax.measure.quantity.Length> MILE
      • US_QUART

        protected static final javax.measure.Unit<javax.measure.quantity.Volume> US_QUART
      • US_GALLON

        protected static final javax.measure.Unit<javax.measure.quantity.Volume> US_GALLON
      • US_PINT

        protected static final javax.measure.Unit<javax.measure.quantity.Volume> US_PINT
      • US_CUP

        protected static final javax.measure.Unit<javax.measure.quantity.Volume> US_CUP
      • US_FL_OUNCE

        protected static final javax.measure.Unit<javax.measure.quantity.Volume> US_FL_OUNCE
      • IMP_PINT

        protected static final javax.measure.Unit<javax.measure.quantity.Volume> IMP_PINT
      • IMP_QUART

        protected static final javax.measure.Unit<javax.measure.quantity.Volume> IMP_QUART
      • IMP_GALLON

        protected static final javax.measure.Unit<javax.measure.quantity.Volume> IMP_GALLON
      • IMP_FL_OUNCE

        protected static final javax.measure.Unit<javax.measure.quantity.Volume> IMP_FL_OUNCE
      • FAHRENHEIT

        protected static final javax.measure.Unit<javax.measure.quantity.Temperature> FAHRENHEIT
      • numberRangePart

        protected final java.util.regex.Pattern numberRangePart
      • unitPatterns

        protected java.util.Map<java.util.regex.Pattern,​javax.measure.Unit> unitPatterns
      • specialPatterns

        protected java.util.Map<java.util.regex.Pattern,​java.util.Map.Entry<javax.measure.Unit,​java.util.function.Function<java.util.regex.MatchResult,​java.lang.Double>>> specialPatterns
      • unitSymbols

        protected java.util.Map<javax.measure.Unit,​java.util.List<java.lang.String>> unitSymbols
      • convertedPatterns

        protected java.util.List<java.util.regex.Pattern> convertedPatterns
      • metricUnits

        protected final java.util.List<javax.measure.Unit> metricUnits
    • Constructor Detail

      • AbstractUnitConversionRule

        protected AbstractUnitConversionRule​(java.util.ResourceBundle messages)
    • Method Detail

      • buildURLForExplanation

        private java.net.URL buildURLForExplanation​(java.lang.String original)
      • getNumberFormat

        protected java.text.NumberFormat getNumberFormat()
        Override in subclasses
        Returns:
        locale-specific number format
      • getSuggestion

        protected java.lang.String getSuggestion​(java.lang.String original,
                                                 java.lang.String converted)
        Format suggestion.
        Parameters:
        original - matched in the text
        converted - computed by this rule
      • formatRounded

        protected java.lang.String formatRounded​(java.lang.String s)
        Override in subclasses.
        Returns:
        formatting of rounded numbers according to locale
      • addUnit

        protected void addUnit​(java.lang.String pattern,
                               javax.measure.Unit base,
                               java.lang.String symbol,
                               double factor,
                               boolean metric)
        Associate a notation with a given unit.
        Parameters:
        pattern - Regex for recognizing the unit. Word boundaries and numbers are added to this pattern by addUnit itself.
        base - Unit to associate with the pattern
        symbol - Suffix used for suggestion.
        factor - Convenience parameter for prefixes for metric units, unit is multiplied with this. Defaults to 1 if not used.
        metric - Register this notation for suggestion.
      • getMetricEquivalent

        @Nullable
        protected @Nullable java.util.List<java.util.Map.Entry<javax.measure.Unit,​java.lang.Double>> getMetricEquivalent​(double value,
                                                                                                                               @NotNull
                                                                                                                               @NotNull javax.measure.Unit unit)
        Parameters:
        value - number to convert
        unit - unit used in text
        Returns:
        suggestions of the given number converted into metric units, sorted by naturalness or null if conversion is not necessary / was not possible
      • formatMeasurement

        @Nullable
        protected @Nullable java.util.List<java.lang.String> formatMeasurement​(double value,
                                                                               @NotNull
                                                                               @NotNull javax.measure.Unit unit)
      • getFormattedConversions

        @NotNull
        private @NotNull java.util.List<java.lang.String> getFormattedConversions​(java.util.List<java.util.Map.Entry<javax.measure.Unit,​java.lang.Double>> conversions)
        Adds different formatted variants of the given conversions up to MAX_SUGGESTIONS.
        Parameters:
        conversions - as computed by getMetricEquivalent
        Returns:
        formatted numbers, with various units and unit symbols, rounded to integers or according to getNumberFormat
      • sortByNaturalness

        private void sortByNaturalness​(java.util.List<java.util.Map.Entry<javax.measure.Unit,​java.lang.Double>> conversions)
      • matchUnits

        private void matchUnits​(AnalyzedSentence sentence,
                                java.util.List<RuleMatch> matches,
                                java.util.List<java.util.Map.Entry<java.lang.Integer,​java.lang.Integer>> ignoreRanges,
                                boolean isMetric)
      • detectNumberRange

        protected boolean detectNumberRange​(AnalyzedSentence sentence,
                                            java.util.regex.Matcher matcher)
      • tryConversion

        private void tryConversion​(AnalyzedSentence sentence,
                                   java.util.List<RuleMatch> matches,
                                   java.util.regex.Pattern unitPattern,
                                   java.lang.Double customValue,
                                   javax.measure.Unit customUnit,
                                   java.util.regex.Matcher unitMatcher,
                                   java.util.List<java.util.Map.Entry<java.lang.Integer,​java.lang.Integer>> ignoreRanges)
      • match

        public RuleMatch[] match​(AnalyzedSentence sentence)
                          throws java.io.IOException
        Description copied from class: Rule
        Check whether the given sentence matches this error rule, i.e. whether it contains the error detected by this rule. Note that the order in which this method is called is not always guaranteed, i.e. the sentence order in the text may be different than the order in which you get the sentences (this may be the case when LanguageTool is used as a LibreOffice/OpenOffice add-on, for example).
        Specified by:
        match in class Rule
        Parameters:
        sentence - a pre-analyzed sentence
        Returns:
        an array of RuleMatch objects
        Throws:
        java.io.IOException