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 classAbstractUnitConversionRule.Message
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<java.util.regex.Pattern>convertedPatternsprivate static doubleDELTAprotected static javax.measure.Unit<javax.measure.quantity.Temperature>FAHRENHEITprotected static javax.measure.Unit<javax.measure.quantity.Length>FEETprotected static javax.measure.Unit<javax.measure.quantity.Volume>IMP_FL_OUNCEprotected static javax.measure.Unit<javax.measure.quantity.Volume>IMP_GALLONprotected static javax.measure.Unit<javax.measure.quantity.Volume>IMP_PINTprotected static javax.measure.Unit<javax.measure.quantity.Volume>IMP_QUARTprotected static javax.measure.Unit<javax.measure.quantity.Length>INCHprivate static intMAX_SUGGESTIONSprotected java.util.List<javax.measure.Unit>metricUnitsprotected static javax.measure.Unit<javax.measure.quantity.Length>MILEprotected static java.lang.StringNUMBER_REGEXprotected java.util.regex.PatternnumberRangePartprotected static javax.measure.Unit<javax.measure.quantity.Mass>OUNCEprotected static javax.measure.Unit<javax.measure.quantity.Mass>POUNDprivate static doubleROUNDING_DELTAprotected 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>>>specialPatternsprotected java.util.Map<java.util.regex.Pattern,javax.measure.Unit>unitPatternsprotected java.util.Map<javax.measure.Unit,java.util.List<java.lang.String>>unitSymbolsprotected static javax.measure.Unit<javax.measure.quantity.Volume>US_CUPprotected static javax.measure.Unit<javax.measure.quantity.Volume>US_FL_OUNCEprotected static javax.measure.Unit<javax.measure.quantity.Volume>US_GALLONprotected static javax.measure.Unit<javax.measure.quantity.Volume>US_PINTprotected static javax.measure.Unit<javax.measure.quantity.Volume>US_QUARTprivate static intWHITESPACE_LIMITprotected static javax.measure.Unit<javax.measure.quantity.Length>YARD
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractUnitConversionRule(java.util.ResourceBundle messages)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddUnit(java.lang.String pattern, javax.measure.Unit base, java.lang.String symbol, double factor, boolean metric)Associate a notation with a given unit.private java.net.URLbuildURLForExplanation(java.lang.String original)protected booleandetectNumberRange(AnalyzedSentence sentence, java.util.regex.Matcher matcher)protected @Nullable java.util.List<java.lang.String>formatMeasurement(double value, @NotNull javax.measure.Unit unit)protected java.lang.StringformatRounded(java.lang.String s)Override in subclasses.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.protected java.lang.StringgetMessage(AbstractUnitConversionRule.Message message)Override in subclassesprotected @Nullable java.util.List<java.util.Map.Entry<javax.measure.Unit,java.lang.Double>>getMetricEquivalent(double value, @NotNull javax.measure.Unit unit)protected java.text.NumberFormatgetNumberFormat()Override in subclassesprotected java.lang.StringgetShortMessage(AbstractUnitConversionRule.Message message)Override in subclassesprotected java.lang.StringgetSuggestion(java.lang.String original, java.lang.String converted)Format suggestion.RuleMatch[]match(AnalyzedSentence sentence)Check whether the given sentence matches this error rule, i.e.private voidmatchUnits(AnalyzedSentence sentence, java.util.List<RuleMatch> matches, java.util.List<java.util.Map.Entry<java.lang.Integer,java.lang.Integer>> ignoreRanges, boolean isMetric)private voidsortByNaturalness(java.util.List<java.util.Map.Entry<javax.measure.Unit,java.lang.Double>> conversions)private voidtryConversion(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)-
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 java.lang.String NUMBER_REGEX
- See Also:
- Constant Field Values
-
numberRangePart
protected final java.util.regex.Pattern numberRangePart
-
DELTA
private static final double DELTA
- See Also:
- Constant Field Values
-
ROUNDING_DELTA
private static final double ROUNDING_DELTA
- See Also:
- Constant Field Values
-
MAX_SUGGESTIONS
private static final int MAX_SUGGESTIONS
- See Also:
- Constant Field Values
-
WHITESPACE_LIMIT
private static final int WHITESPACE_LIMIT
- See Also:
- Constant Field Values
-
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
-
-
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
-
getMessage
protected java.lang.String getMessage(AbstractUnitConversionRule.Message message)
Override in subclasses
-
getShortMessage
protected java.lang.String getShortMessage(AbstractUnitConversionRule.Message message)
Override in subclasses
-
getSuggestion
protected java.lang.String getSuggestion(java.lang.String original, java.lang.String converted)Format suggestion.- Parameters:
original- matched in the textconverted- 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 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 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 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 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:RuleCheck 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).
-
-