mirror of
https://github.com/facebook/duckling.git
synced 2025-01-06 04:53:13 +03:00
Numeral: don't compose negative numbers, fix double negatives
Summary: * added `isMultipliable` helper and used that in patterns along with `isPositive` * fixed double negatives in most languages Reviewed By: niteria Differential Revision: D7034982 fbshipit-source-id: a0bb67056d3107167830ece0c34d761c5563c5a7
This commit is contained in:
parent
55b0d1d437
commit
aed5b8f779
@ -32,8 +32,8 @@ ruleNumeralsPrefixWithNegativeOrMinus :: Rule
|
||||
ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|minus\\s?|negativ\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|minus|negativ"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
|
||||
@ -111,7 +111,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, dimension Numeral
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -195,7 +195,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
|
@ -34,7 +34,7 @@ ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|minus|negativ"
|
||||
, dimension Numeral
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
|
||||
@ -140,7 +140,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
@ -152,7 +152,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, dimension Numeral
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
|
@ -178,8 +178,8 @@ ruleNegative :: Rule
|
||||
ruleNegative = Rule
|
||||
{ name = "negative numbers"
|
||||
, pattern =
|
||||
[ regex "-|μείον\\s"
|
||||
, numberWith TNumeral.value (>0)
|
||||
[ regex "-|μείον"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
@ -191,7 +191,7 @@ ruleSum = Rule
|
||||
{ name = "intersect 2 numbers"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -205,7 +205,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
@ -215,7 +215,9 @@ ruleMultiply = Rule
|
||||
ruleDecimals :: Rule
|
||||
ruleDecimals = Rule
|
||||
{ name = "decimal number"
|
||||
, pattern = [regex "(\\d+,\\d+)"]
|
||||
, pattern =
|
||||
[ regex "(\\d+,\\d+)"
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token RegexMatch (GroupMatch (match:_)):_) ->
|
||||
parseDecimal True (Text.replace "," "." match)
|
||||
@ -239,7 +241,9 @@ ruleCommaSpelledOut = Rule
|
||||
ruleDots :: Rule
|
||||
ruleDots = Rule
|
||||
{ name = "dot-separated numbers"
|
||||
, pattern = [regex "(\\d+(\\.\\d\\d\\d)+(,\\d+)?)"]
|
||||
, pattern =
|
||||
[ regex "(\\d+(\\.\\d\\d\\d)+(,\\d+)?)"
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token RegexMatch (GroupMatch (match:_)):_) ->
|
||||
parseDouble (
|
||||
|
@ -160,4 +160,8 @@ allExamples = concat
|
||||
, examples (NumeralValue 2200000)
|
||||
[ "two point two million"
|
||||
]
|
||||
, examples (NumeralValue 3000000000)
|
||||
[ "three billions"
|
||||
, "three thousand millions"
|
||||
]
|
||||
]
|
||||
|
@ -243,7 +243,7 @@ ruleNegative = Rule
|
||||
{ name = "negative numbers"
|
||||
, pattern =
|
||||
[ regex "(-|minus|negative)(?!\\s*-)"
|
||||
, numberWith TNumeral.value (>0)
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
@ -255,7 +255,7 @@ ruleSum = Rule
|
||||
{ name = "intersect 2 numbers"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -270,7 +270,7 @@ ruleSumAnd = Rule
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, regex "and"
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -285,7 +285,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
|
@ -31,7 +31,7 @@ ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|menos"
|
||||
, dimension Numeral
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
|
||||
|
@ -33,7 +33,7 @@ ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|miinus|negatiivne"
|
||||
, dimension Numeral
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:
|
||||
@ -122,7 +122,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -154,7 +154,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
|
@ -271,7 +271,7 @@ ruleSum = Rule
|
||||
{ name = "intersect 2 numbers"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -285,7 +285,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
|
@ -32,8 +32,8 @@ ruleNumeralsPrefixWithNegativeOrMinus :: Rule
|
||||
ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|m(í|i)neas(\\sa)?\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|m(í|i)neas(\\sa)?"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:
|
||||
|
@ -39,7 +39,7 @@ ruleIntersectNumerals = Rule
|
||||
{ name = "intersect numbers"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -54,7 +54,7 @@ ruleIntersectWithAnd = Rule
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, regex "ו"
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -99,7 +99,7 @@ ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|מינוס"
|
||||
, dimension Numeral
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
|
@ -30,7 +30,7 @@ ruleNumbersPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|minus|negativ"
|
||||
, dimension Numeral
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
@ -159,7 +159,7 @@ ruleSum = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -337,7 +337,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
|
@ -14,6 +14,7 @@ module Duckling.Numeral.Helpers
|
||||
, double
|
||||
, integer
|
||||
, multiply
|
||||
, isMultipliable
|
||||
, isNatural
|
||||
, isPositive
|
||||
, divide
|
||||
@ -103,6 +104,10 @@ isPositive :: Predicate
|
||||
isPositive (Token Numeral NumeralData{value = v}) = v >= 0
|
||||
isPositive _ = False
|
||||
|
||||
isMultipliable :: Predicate
|
||||
isMultipliable (Token Numeral nd) = multipliable nd
|
||||
isMultipliable _ = False
|
||||
|
||||
oneOf :: [Double] -> PatternItem
|
||||
oneOf vs = Predicate $ \x ->
|
||||
case x of
|
||||
|
@ -74,8 +74,8 @@ ruleNumeralsPrefixWithNegativeOrMinus :: Rule
|
||||
ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|minus\\s?|negatif\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|minus|negatif"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
@ -109,7 +109,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
@ -147,7 +147,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
|
@ -32,7 +32,7 @@ ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|meno|negativo"
|
||||
, dimension Numeral
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
|
@ -41,7 +41,7 @@ ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|マイナス\\s?|負\\s?"
|
||||
, dimension Numeral
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
|
@ -207,7 +207,7 @@ ruleSum = Rule
|
||||
{ name = "intersect 2 numbers"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -221,7 +221,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
|
@ -34,7 +34,7 @@ ruleIntersectWithAnd = Rule
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, regex "og"
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -48,8 +48,8 @@ ruleNumeralsPrefixWithNegativeOrMinus :: Rule
|
||||
ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|minus\\s?|negativ\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|minus|negativ"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
@ -83,7 +83,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
@ -130,7 +130,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
|
@ -34,7 +34,7 @@ ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|min|minus|negatief"
|
||||
, dimension Numeral
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
@ -104,7 +104,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
@ -116,7 +116,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, dimension Numeral
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
|
@ -67,7 +67,7 @@ ruleIntersectWithAnd = Rule
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, regex "i|a"
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -81,8 +81,8 @@ ruleNumeralsPrefixWithNegativeOrMinus :: Rule
|
||||
ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|minus\\s?|negative\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|minus|negative"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
@ -329,7 +329,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -343,7 +343,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
|
@ -314,8 +314,8 @@ ruleNegative :: Rule
|
||||
ruleNegative = Rule
|
||||
{ name = "negative numbers"
|
||||
, pattern =
|
||||
[ regex "(-|menos|negativo)(?!\\s*-)"
|
||||
, numberWith TNumeral.value (>0)
|
||||
[ regex "(-|menos|negativo)(?!\\s*\\-)"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double $ TNumeral.value nd * (-1)
|
||||
@ -327,7 +327,7 @@ ruleSum = Rule
|
||||
{ name = "intersect 2 numbers"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -342,7 +342,7 @@ ruleSumAnd = Rule
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, regex "e"
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -357,7 +357,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
|
@ -32,8 +32,8 @@ ruleNumeralsPrefixWithOrMinus :: Rule
|
||||
ruleNumeralsPrefixWithOrMinus = Rule
|
||||
{ name = "numbers prefix with - or minus"
|
||||
, pattern =
|
||||
[ regex "-|minus\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|minus"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
@ -98,7 +98,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
@ -110,7 +110,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, dimension Numeral
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -125,7 +125,7 @@ ruleIntersectCuI = Rule
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, regex "(s|ș)i"
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -139,8 +139,8 @@ ruleNumeralsSuffixesWithNegativ :: Rule
|
||||
ruleNumeralsSuffixesWithNegativ = Rule
|
||||
{ name = "numbers suffixes with (negativ)"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, regex "(negativ|neg)"
|
||||
[ Predicate isPositive
|
||||
, regex "neg(ativ)?"
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = v}:
|
||||
|
@ -134,8 +134,8 @@ ruleNumeralsPrefixWithMinus :: Rule
|
||||
ruleNumeralsPrefixWithMinus = Rule
|
||||
{ name = "numbers prefix with -, minus"
|
||||
, pattern =
|
||||
[ regex "-|минус\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|минус"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
|
@ -34,7 +34,7 @@ ruleIntersectWithAnd = Rule
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, regex "och"
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -48,8 +48,8 @@ ruleNumeralsPrefixWithNegativeOrMinus :: Rule
|
||||
ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|minus\\s?|negativ\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|minus|negativ"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:
|
||||
@ -120,7 +120,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -134,7 +134,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
|
@ -58,8 +58,8 @@ ruleNumeralsPrefixWithNegativeOrMinus :: Rule
|
||||
ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|eksi\\s?|negatif\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|eksi|negatif"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
|
||||
@ -112,7 +112,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1}:
|
||||
@ -306,7 +306,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
|
@ -45,26 +45,9 @@ rulePowersOfTen = Rule
|
||||
[ regex "(trăm?|nghìn?|triệu?|tỷ?)"
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token RegexMatch (GroupMatch (match:_)):_) ->
|
||||
do
|
||||
(value, grain) <- HashMap.lookup (Text.toLower match) powersOfTenMap
|
||||
double value >>= withGrain grain >>= withMultipliable
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
ruleIntersectWithAnd :: Rule
|
||||
ruleIntersectWithAnd = Rule
|
||||
{ name = "intersect (with and)"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, regex "and"
|
||||
, numberWith TNumeral.multipliable not
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
_:
|
||||
Token Numeral NumeralData{TNumeral.value = val2}:
|
||||
_) | (10 ** fromIntegral g) > val2 -> double $ val1 + val2
|
||||
(Token RegexMatch (GroupMatch (match:_)):_) -> do
|
||||
(value, grain) <- HashMap.lookup (Text.toLower match) powersOfTenMap
|
||||
double value >>= withGrain grain >>= withMultipliable
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
@ -72,8 +55,8 @@ ruleNumeralsPrefixWithM :: Rule
|
||||
ruleNumeralsPrefixWithM = Rule
|
||||
{ name = "numbers prefix with -, âm"
|
||||
, pattern =
|
||||
[ regex "-|âm\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "\\-|âm"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
@ -148,7 +131,7 @@ ruleIntersect = Rule
|
||||
{ name = "intersect"
|
||||
, pattern =
|
||||
[ numberWith (fromMaybe 0 . TNumeral.grain) (>1)
|
||||
, numberWith TNumeral.multipliable not
|
||||
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
||||
@ -162,7 +145,7 @@ ruleMultiply = Rule
|
||||
{ name = "compose by multiplication"
|
||||
, pattern =
|
||||
[ dimension Numeral
|
||||
, numberWith TNumeral.multipliable id
|
||||
, Predicate isMultipliable
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(token1:token2:_) -> multiply token1 token2
|
||||
@ -187,61 +170,47 @@ ruleNumeralsSuffixesKMG = Rule
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
ruleNumeralNghn :: Rule
|
||||
ruleNumeralNghn = Rule
|
||||
{ name = "number nghìn"
|
||||
, pattern =
|
||||
[ numberBetween 1 1000
|
||||
, numberWith TNumeral.value (== 1000)
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token Numeral NumeralData{TNumeral.value = v1}:
|
||||
Token Numeral NumeralData{TNumeral.value = v2, TNumeral.grain = Just g}:
|
||||
_) -> double (v1 * v2) >>= withGrain g
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
integerMap :: HashMap.HashMap Text.Text Integer
|
||||
integerMap = HashMap.fromList
|
||||
[ ("không", 0)
|
||||
, ("một", 1)
|
||||
, ("linh một", 1)
|
||||
, ("lẻ một", 1)
|
||||
, ("hai", 2)
|
||||
, ("lẻ hai", 2)
|
||||
, ("linh hai", 2)
|
||||
, ("ba", 3)
|
||||
, ("lẻ", 3)
|
||||
, ("linh ba", 3)
|
||||
, ("lẻ bốn", 4)
|
||||
, ("linh bốn", 4)
|
||||
, ("bốn", 4)
|
||||
, ("năm", 5)
|
||||
, ("lẻ năm", 5)
|
||||
, ("linh năm", 5)
|
||||
, ("linh sáu", 6)
|
||||
, ("sáu", 6)
|
||||
, ("lẻ sáu", 6)
|
||||
, ("linh bảy", 7)
|
||||
, ("lẻ bảy", 7)
|
||||
, ("bảy", 7)
|
||||
, ("lẻ tám", 8)
|
||||
, ("linh tám", 8)
|
||||
, ("tám", 8)
|
||||
, ("lẻ chín", 9)
|
||||
, ("chín", 9)
|
||||
, ("linh chín", 9)
|
||||
, ("linh mười", 10)
|
||||
, ("mười", 10)
|
||||
, ("lẻ mười", 10)
|
||||
, ("mười một", 11)
|
||||
, ("mười hai", 12)
|
||||
, ("mười ba", 13)
|
||||
, ("mười bốn", 14)
|
||||
, ("mười lăm", 15)
|
||||
, ("mười sáu", 16)
|
||||
, ("mười bảy", 17)
|
||||
, ("mười tám", 18)
|
||||
[ ("không", 0)
|
||||
, ("một", 1)
|
||||
, ("linh một", 1)
|
||||
, ("lẻ một", 1)
|
||||
, ("hai", 2)
|
||||
, ("lẻ hai", 2)
|
||||
, ("linh hai", 2)
|
||||
, ("ba", 3)
|
||||
, ("lẻ", 3)
|
||||
, ("linh ba", 3)
|
||||
, ("lẻ bốn", 4)
|
||||
, ("linh bốn", 4)
|
||||
, ("bốn", 4)
|
||||
, ("năm", 5)
|
||||
, ("lẻ năm", 5)
|
||||
, ("linh năm", 5)
|
||||
, ("linh sáu", 6)
|
||||
, ("sáu", 6)
|
||||
, ("lẻ sáu", 6)
|
||||
, ("linh bảy", 7)
|
||||
, ("lẻ bảy", 7)
|
||||
, ("bảy", 7)
|
||||
, ("lẻ tám", 8)
|
||||
, ("linh tám", 8)
|
||||
, ("tám", 8)
|
||||
, ("lẻ chín", 9)
|
||||
, ("chín", 9)
|
||||
, ("linh chín", 9)
|
||||
, ("linh mười", 10)
|
||||
, ("mười", 10)
|
||||
, ("lẻ mười", 10)
|
||||
, ("mười một", 11)
|
||||
, ("mười hai", 12)
|
||||
, ("mười ba", 13)
|
||||
, ("mười bốn", 14)
|
||||
, ("mười lăm", 15)
|
||||
, ("mười sáu", 16)
|
||||
, ("mười bảy", 17)
|
||||
, ("mười tám", 18)
|
||||
, ("mười chín", 19)
|
||||
]
|
||||
|
||||
@ -259,13 +228,13 @@ ruleInteger = Rule
|
||||
|
||||
tensMap :: HashMap.HashMap Text.Text Integer
|
||||
tensMap = HashMap.fromList
|
||||
[ ("hai mươi", 20)
|
||||
, ("ba mươi", 30)
|
||||
, ("bốn mươi", 40)
|
||||
, ("năm mươi", 50)
|
||||
, ("sáu mươi", 60)
|
||||
, ("bảy mươi", 70)
|
||||
, ("tám mươi", 80)
|
||||
[ ("hai mươi", 20)
|
||||
, ("ba mươi", 30)
|
||||
, ("bốn mươi", 40)
|
||||
, ("năm mươi", 50)
|
||||
, ("sáu mươi", 60)
|
||||
, ("bảy mươi", 70)
|
||||
, ("tám mươi", 80)
|
||||
, ("chín mươi", 90)
|
||||
]
|
||||
|
||||
@ -324,10 +293,8 @@ rules =
|
||||
, ruleInteger3
|
||||
, ruleIntegerWithThousandsSeparator
|
||||
, ruleIntersect
|
||||
, ruleIntersectWithAnd
|
||||
, ruleMultiply
|
||||
, ruleNumeralDot
|
||||
, ruleNumeralNghn
|
||||
, ruleNumerals
|
||||
, ruleNumerals2
|
||||
, ruleNumeralsPrefixWithM
|
||||
|
@ -61,8 +61,8 @@ ruleNumeralsPrefixWithNegativeOrMinus :: Rule
|
||||
ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
||||
{ name = "numbers prefix with -, negative or minus"
|
||||
, pattern =
|
||||
[ regex "-|负\\s?|負\\s?"
|
||||
, dimension Numeral
|
||||
[ regex "-|负|負"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
||||
|
@ -28,6 +28,7 @@ tests :: TestTree
|
||||
tests = testGroup "EN Tests"
|
||||
[ makeCorpusTest [This Numeral] corpus
|
||||
, surroundTests
|
||||
, intersectTests
|
||||
, rangeTests
|
||||
]
|
||||
|
||||
@ -48,6 +49,13 @@ surroundTests = testCase "Surround Tests" $
|
||||
]
|
||||
]
|
||||
|
||||
intersectTests :: TestTree
|
||||
intersectTests = testCase "Intersect Test" $
|
||||
mapM_ (analyzedNTest testContext . withTargets [This Numeral]) xs
|
||||
where
|
||||
xs = [ ("10 millions minus 10", 2)
|
||||
]
|
||||
|
||||
rangeTests :: TestTree
|
||||
rangeTests = testCase "Range Test" $
|
||||
mapM_ (analyzedRangeTest testContext . withTargets [This Numeral]) xs
|
||||
|
Loading…
Reference in New Issue
Block a user