Class AbstractUnitConversionRule

java.lang.Object
org.languagetool.rules.Rule
org.languagetool.rules.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 Details

    • 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
    • NUMBER_REGEX

      protected static final String NUMBER_REGEX
      See Also:
    • numberRangePart

      protected final Pattern numberRangePart
    • DELTA

      private static final double DELTA
      See Also:
    • ROUNDING_DELTA

      private static final double ROUNDING_DELTA
      See Also:
    • MAX_SUGGESTIONS

      private static final int MAX_SUGGESTIONS
      See Also:
    • WHITESPACE_LIMIT

      private static final int WHITESPACE_LIMIT
      See Also:
    • unitPatterns

      protected Map<Pattern,javax.measure.Unit> unitPatterns
    • specialPatterns

      protected Map<Pattern,Map.Entry<javax.measure.Unit,Function<MatchResult,Double>>> specialPatterns
    • unitSymbols

      protected Map<javax.measure.Unit,List<String>> unitSymbols
    • convertedPatterns

      protected List<Pattern> convertedPatterns
    • metricUnits

      protected final List<javax.measure.Unit> metricUnits
  • Constructor Details

    • AbstractUnitConversionRule

      protected AbstractUnitConversionRule(ResourceBundle messages)
  • Method Details

    • buildURLForExplanation

      private URL buildURLForExplanation(String original)
    • getNumberFormat

      protected NumberFormat getNumberFormat()
      Override in subclasses
      Returns:
      locale-specific number format
    • getMessage

      protected String getMessage(AbstractUnitConversionRule.Message message)
      Override in subclasses
    • getShortMessage

      protected String getShortMessage(AbstractUnitConversionRule.Message message)
      Override in subclasses
    • getSuggestion

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

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

      protected void addUnit(String pattern, javax.measure.Unit base, 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 List<Map.Entry<javax.measure.Unit,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 List<String> formatMeasurement(double value, @NotNull @NotNull javax.measure.Unit unit)
    • getFormattedConversions

      @NotNull private @NotNull List<String> getFormattedConversions(List<Map.Entry<javax.measure.Unit,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(List<Map.Entry<javax.measure.Unit,Double>> conversions)
    • matchUnits

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

      protected boolean detectNumberRange(AnalyzedSentence sentence, Matcher matcher)
    • tryConversion

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

      public RuleMatch[] match(AnalyzedSentence sentence) throws 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:
      IOException