Package org.languagetool.rules
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractUnitConversionRule.Message
-
Field Summary
Fields Modifier and Type Field Description protected List<Pattern>
convertedPatterns
protected static javax.measure.Unit<javax.measure.quantity.Temperature>
FAHRENHEIT
protected static javax.measure.Unit<javax.measure.quantity.Length>
FEET
protected static javax.measure.Unit<javax.measure.quantity.Volume>
IMP_FL_OUNCE
protected static javax.measure.Unit<javax.measure.quantity.Volume>
IMP_GALLON
protected static javax.measure.Unit<javax.measure.quantity.Volume>
IMP_PINT
protected static javax.measure.Unit<javax.measure.quantity.Volume>
IMP_QUART
protected static javax.measure.Unit<javax.measure.quantity.Length>
INCH
protected List<javax.measure.Unit>
metricUnits
protected static javax.measure.Unit<javax.measure.quantity.Length>
MILE
protected static String
NUMBER_REGEX
protected Pattern
numberRangePart
protected static javax.measure.Unit<javax.measure.quantity.Mass>
OUNCE
protected static javax.measure.Unit<javax.measure.quantity.Mass>
POUND
protected Map<Pattern,Map.Entry<javax.measure.Unit,Function<MatchResult,Double>>>
specialPatterns
protected Map<Pattern,javax.measure.Unit>
unitPatterns
protected Map<javax.measure.Unit,List<String>>
unitSymbols
protected static javax.measure.Unit<javax.measure.quantity.Volume>
US_CUP
protected static javax.measure.Unit<javax.measure.quantity.Volume>
US_FL_OUNCE
protected static javax.measure.Unit<javax.measure.quantity.Volume>
US_GALLON
protected static javax.measure.Unit<javax.measure.quantity.Volume>
US_PINT
protected static javax.measure.Unit<javax.measure.quantity.Volume>
US_QUART
protected static javax.measure.Unit<javax.measure.quantity.Length>
YARD
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractUnitConversionRule(ResourceBundle messages)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
addUnit(String pattern, javax.measure.Unit base, String symbol, double factor, boolean metric)
Associate a notation with a given unit.protected boolean
detectNumberRange(AnalyzedSentence sentence, Matcher matcher)
protected @Nullable List<String>
formatMeasurement(double value, @NotNull javax.measure.Unit unit)
protected String
formatRounded(String s)
Override in subclasses.protected String
getMessage(AbstractUnitConversionRule.Message message)
Override in subclassesprotected @Nullable List<Map.Entry<javax.measure.Unit,Double>>
getMetricEquivalent(double value, @NotNull javax.measure.Unit unit)
protected NumberFormat
getNumberFormat()
Override in subclassesprotected String
getShortMessage(AbstractUnitConversionRule.Message message)
Override in subclassesprotected String
getSuggestion(String original, String converted)
Format suggestion.RuleMatch[]
match(AnalyzedSentence sentence)
Check whether the given sentence matches this error rule, i.e.-
Methods inherited from class org.languagetool.rules.Rule
addExamplePair, estimateContextForSureMatch, getAntiPatterns, getCategory, getConfigureText, getCorrectExamples, getDefaultValue, getDescription, getErrorTriggeringExamples, getId, getIncorrectExamples, getLocQualityIssueType, getMaxConfigurableValue, getMinConfigurableValue, getSentenceWithImmunization, getUrl, hasConfigurableValue, isDefaultOff, isDefaultTempOff, isDictionaryBasedSpellingRule, isOfficeDefaultOff, isOfficeDefaultOn, makeAntiPatterns, setCategory, setCorrectExamples, setDefaultOff, setDefaultOn, setDefaultTempOff, setErrorTriggeringExamples, setIncorrectExamples, setLocQualityIssueType, setOfficeDefaultOff, setOfficeDefaultOn, setUrl, supportsLanguage, toRuleMatchArray, useInOffice
-
-
-
-
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
-
NUMBER_REGEX
protected static final String NUMBER_REGEX
- See Also:
- Constant Field Values
-
numberRangePart
protected final Pattern numberRangePart
-
specialPatterns
protected Map<Pattern,Map.Entry<javax.measure.Unit,Function<MatchResult,Double>>> specialPatterns
-
metricUnits
protected final List<javax.measure.Unit> metricUnits
-
-
Constructor Detail
-
AbstractUnitConversionRule
protected AbstractUnitConversionRule(ResourceBundle messages)
-
-
Method Detail
-
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 textconverted
- 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 patternsymbol
- 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 convertunit
- 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)
-
detectNumberRange
protected boolean detectNumberRange(AnalyzedSentence sentence, Matcher matcher)
-
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 classRule
- Parameters:
sentence
- a pre-analyzed sentence- Returns:
- an array of
RuleMatch
objects - Throws:
IOException
-
-