Package org.languagetool.rules
Class AbstractUnitConversionRule
java.lang.Object
org.languagetool.rules.Rule
org.languagetool.rules.AbstractUnitConversionRule
Base class providing support for detecting, parsing and converting between measurements in different units
- Since:
- 4.3
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double
protected static final javax.measure.Unit
<javax.measure.quantity.Temperature> protected static final javax.measure.Unit
<javax.measure.quantity.Length> protected static final javax.measure.Unit
<javax.measure.quantity.Volume> protected static final javax.measure.Unit
<javax.measure.quantity.Volume> protected static final javax.measure.Unit
<javax.measure.quantity.Volume> protected static final javax.measure.Unit
<javax.measure.quantity.Volume> protected static final javax.measure.Unit
<javax.measure.quantity.Length> private static final int
protected final List
<javax.measure.Unit> protected static final javax.measure.Unit
<javax.measure.quantity.Length> protected static final String
protected final Pattern
protected static final javax.measure.Unit
<javax.measure.quantity.Mass> protected static final javax.measure.Unit
<javax.measure.quantity.Mass> private static final double
protected static final javax.measure.Unit
<javax.measure.quantity.Volume> protected static final javax.measure.Unit
<javax.measure.quantity.Volume> protected static final javax.measure.Unit
<javax.measure.quantity.Volume> protected static final javax.measure.Unit
<javax.measure.quantity.Volume> protected static final javax.measure.Unit
<javax.measure.quantity.Volume> private static final int
protected static final javax.measure.Unit
<javax.measure.quantity.Length> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Associate a notation with a given unit.private URL
buildURLForExplanation
(String original) protected boolean
detectNumberRange
(AnalyzedSentence sentence, Matcher matcher) formatMeasurement
(double value, @NotNull javax.measure.Unit unit) protected String
Override in subclasses.getFormattedConversions
(List<Map.Entry<javax.measure.Unit, Double>> conversions) Adds different formatted variants of the given conversions up to MAX_SUGGESTIONS.protected String
Override in subclassesgetMetricEquivalent
(double value, @NotNull javax.measure.Unit unit) protected NumberFormat
Override in subclassesprotected String
Override in subclassesprotected String
getSuggestion
(String original, String converted) Format suggestion.match
(AnalyzedSentence sentence) Check whether the given sentence matches this error rule, i.e.private void
matchUnits
(AnalyzedSentence sentence, List<RuleMatch> matches, List<Map.Entry<Integer, Integer>> ignoreRanges, boolean isMetric) private void
sortByNaturalness
(List<Map.Entry<javax.measure.Unit, Double>> conversions) private void
tryConversion
(AnalyzedSentence sentence, List<RuleMatch> matches, Pattern unitPattern, Double customValue, javax.measure.Unit customUnit, Matcher unitMatcher, List<Map.Entry<Integer, 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 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
- See Also:
-
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
-
specialPatterns
-
unitSymbols
-
convertedPatterns
-
metricUnits
-
-
Constructor Details
-
AbstractUnitConversionRule
-
-
Method Details
-
buildURLForExplanation
-
getNumberFormat
Override in subclasses- Returns:
- locale-specific number format
-
getMessage
Override in subclasses -
getShortMessage
Override in subclasses -
getSuggestion
Format suggestion.- Parameters:
original
- matched in the textconverted
- computed by this rule
-
formatRounded
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
-
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
-
matchUnits
-
detectNumberRange
-
tryConversion
-
match
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
-