mirror of
https://github.com/facebook/duckling.git
synced 2024-09-11 21:27:13 +03:00
extend AmountOfMoney rules
Summary: Add rules: - `hkd` as HKD, and related rules (prefix and suffix) - dollar and <amount-of-money> rule - dollar and a half rule - intersection for <amount-of-money> and `a half` Changed: - dime and dollar rules now have improved coverage Reviewed By: girifb Differential Revision: D26191724 Pulled By: chessai fbshipit-source-id: bf63b6eaa751fb96dcf341fa2b66db06a6eeca79
This commit is contained in:
parent
041a81ad1a
commit
776b1ec64d
@ -128,7 +128,7 @@ ruleCurrencies :: Rule
|
||||
ruleCurrencies = Rule
|
||||
{ name = "currencies"
|
||||
, pattern =
|
||||
[ regex "(aed|aud|bgn|brl|byn|¢|cad|chf|cny|c|\\$|dinars?|dkk|dollars?|egp|(e|€)uro?s?|€|gbp|gel|\x20BE|hrk|idr|ils|₪|inr|iqd|jmd|jod|¥|jpy|lari|krw|kwd|lbp|mad|₮|mnt|tugriks?|myr|rm|nis|nok|nzd|£|pkr|pln|pta?s?|qar|₽|rs\\.?|riy?als?|ron|rub|rupees?|sar|sek|sgb|shekels?|thb|ttd|₴|uah|us(d|\\$)|vnd|yen|yuan|zar)"
|
||||
[ regex "(aed|aud|bgn|brl|byn|¢|cad|chf|cny|c|\\$|dinars?|dkk|dollars?|egp|(e|€)uro?s?|€|gbp|gel|\x20BE|hkd|hrk|idr|ils|₪|inr|iqd|jmd|jod|¥|jpy|lari|krw|kwd|lbp|mad|₮|mnt|tugriks?|myr|rm|nis|nok|nzd|£|pkr|pln|pta?s?|qar|₽|rs\\.?|riy?als?|ron|rub|rupees?|sar|sek|sgb|shekels?|thb|ttd|₴|uah|us(d|\\$)|vnd|yen|yuan|zar)"
|
||||
]
|
||||
, prod = \tokens -> case tokens of
|
||||
(Token RegexMatch (GroupMatch (match:_)):_) -> do
|
||||
|
@ -28,12 +28,14 @@ allExamples = concat
|
||||
[ examples (simple Cent 5)
|
||||
[ "五分"
|
||||
, "5分"
|
||||
, "五仙"
|
||||
]
|
||||
, examples (simple Cent 20)
|
||||
[ "20分"
|
||||
, "二十分"
|
||||
, "2角"
|
||||
, "两毛"
|
||||
, "两毫"
|
||||
]
|
||||
, examples (simple Cent 25)
|
||||
[ "25分"
|
||||
@ -44,11 +46,27 @@ allExamples = concat
|
||||
, examples (simple Dollar 7)
|
||||
[ "7块"
|
||||
, "七元"
|
||||
, "七蚊"
|
||||
]
|
||||
, examples (simple Dollar 5.5)
|
||||
[ "五個半"
|
||||
]
|
||||
, examples (simple Dollar 1.5)
|
||||
[ "個半"
|
||||
, "一個半"
|
||||
]
|
||||
, examples (simple Dollar 1.9)
|
||||
[ "個九"
|
||||
]
|
||||
, examples (simple CNY 3.14)
|
||||
[ "3.14人民币"
|
||||
, "人民幣3.14"
|
||||
]
|
||||
, examples (simple HKD 3.14)
|
||||
[ "3.14港幣"
|
||||
, "港幣3.14"
|
||||
, "港幣三個一毫四"
|
||||
]
|
||||
, examples (under Dollar 1.2)
|
||||
[ "1.2元以下"
|
||||
, "最多一块二角"
|
||||
|
@ -16,12 +16,11 @@ module Duckling.AmountOfMoney.ZH.Rules
|
||||
import Data.Maybe
|
||||
import Data.String
|
||||
import Prelude
|
||||
import qualified Data.Text as Text
|
||||
|
||||
import Duckling.AmountOfMoney.Helpers
|
||||
import Duckling.AmountOfMoney.Types (Currency (..), AmountOfMoneyData (..))
|
||||
import Duckling.Dimensions.Types
|
||||
import Duckling.Numeral.Helpers (isNatural, isPositive, oneOf)
|
||||
import Duckling.Numeral.Helpers (isNatural, isPositive, oneOf, numberBetween, decimalsToDouble)
|
||||
import Duckling.Numeral.Types (NumeralData (..))
|
||||
import Duckling.Regex.Types
|
||||
import Duckling.Types
|
||||
@ -34,7 +33,7 @@ ruleCNY = Rule
|
||||
, pattern =
|
||||
[ regex "人民币|人民幣"
|
||||
]
|
||||
, prod = \_ -> Just . Token AmountOfMoney $ currencyOnly CNY
|
||||
, prod = \_ -> Just $ Token AmountOfMoney $ currencyOnly CNY
|
||||
}
|
||||
|
||||
ruleCNYPrefix :: Rule
|
||||
@ -46,7 +45,29 @@ ruleCNYPrefix = Rule
|
||||
]
|
||||
, prod = \case
|
||||
(_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
|
||||
Just . Token AmountOfMoney . withValue v $ currencyOnly CNY
|
||||
Just $ Token AmountOfMoney $ withValue v $ currencyOnly CNY
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
ruleHKD :: Rule
|
||||
ruleHKD = Rule
|
||||
{ name = "HKD"
|
||||
, pattern =
|
||||
[ regex "港幣"
|
||||
]
|
||||
, prod = \_ -> Just $ Token AmountOfMoney $ currencyOnly HKD
|
||||
}
|
||||
|
||||
ruleHKDPrefix :: Rule
|
||||
ruleHKDPrefix = Rule
|
||||
{ name = "hkd prefix"
|
||||
, pattern =
|
||||
[ regex "港幣"
|
||||
, Predicate isPositive
|
||||
]
|
||||
, prod = \case
|
||||
(_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
|
||||
Just $ Token AmountOfMoney $ withValue v $ currencyOnly HKD
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
@ -54,9 +75,9 @@ ruleCent :: Rule
|
||||
ruleCent = Rule
|
||||
{ name = "cent"
|
||||
, pattern =
|
||||
[ regex "分"
|
||||
[ regex "分|仙"
|
||||
]
|
||||
, prod = \_ -> Just . Token AmountOfMoney $ currencyOnly Cent
|
||||
, prod = \_ -> Just $ Token AmountOfMoney $ currencyOnly Cent
|
||||
}
|
||||
|
||||
ruleDime :: Rule
|
||||
@ -64,11 +85,11 @@ ruleDime = Rule
|
||||
{ name = "dime"
|
||||
, pattern =
|
||||
[ Predicate isPositive
|
||||
, regex "角|毛"
|
||||
, regex "角|毛|毫"
|
||||
]
|
||||
, prod = \case
|
||||
(Token Numeral NumeralData{TNumeral.value = v}:_) ->
|
||||
Just . Token AmountOfMoney $
|
||||
Just $ Token AmountOfMoney $
|
||||
withCents (v * 10) $ currencyOnly Cent
|
||||
_ -> Nothing
|
||||
}
|
||||
@ -77,9 +98,9 @@ ruleDollar :: Rule
|
||||
ruleDollar = Rule
|
||||
{ name = "dollar"
|
||||
, pattern =
|
||||
[ regex "元|圆|块"
|
||||
[ regex "元|圆|块|蚊|個"
|
||||
]
|
||||
, prod = \_ -> Just . Token AmountOfMoney $ currencyOnly Dollar
|
||||
, prod = \_ -> Just $ Token AmountOfMoney $ currencyOnly Dollar
|
||||
}
|
||||
|
||||
rulePrecision :: Rule
|
||||
@ -117,7 +138,7 @@ ruleIntersectDimesAndCents = Rule
|
||||
(Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just d}:
|
||||
Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just c}:
|
||||
_) ->
|
||||
Just . Token AmountOfMoney $ withCents (c + d) $ currencyOnly Cent
|
||||
Just $ Token AmountOfMoney $ withCents (c + d) $ currencyOnly Cent
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
@ -131,7 +152,20 @@ ruleIntersectDollarsAndDimesCents = Rule
|
||||
, prod = \case
|
||||
(Token AmountOfMoney fd:
|
||||
Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just c}:
|
||||
_) -> Just . Token AmountOfMoney $ withCents c fd
|
||||
_) -> Just $ Token AmountOfMoney $ withCents c fd
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
ruleIntersectDollarsAndAHalf :: Rule
|
||||
ruleIntersectDollarsAndAHalf = Rule
|
||||
{ name = "<amount-of-money> and a half"
|
||||
, pattern =
|
||||
[ Predicate $ and . sequence [isSimpleAmountOfMoney, isWithoutCents]
|
||||
, regex "半"
|
||||
]
|
||||
, prod = \case
|
||||
(Token AmountOfMoney fd:_) ->
|
||||
Just $ Token AmountOfMoney $ withCents 50 fd
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
@ -146,7 +180,7 @@ ruleIntersect = Rule
|
||||
, prod = \case
|
||||
(Token AmountOfMoney fd:_:
|
||||
Token Numeral NumeralData{TNumeral.value = c}:
|
||||
_) -> Just . Token AmountOfMoney $ withCents c fd
|
||||
_) -> Just $ Token AmountOfMoney $ withCents c fd
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
@ -160,7 +194,7 @@ ruleIntersect2 = Rule
|
||||
, prod = \case
|
||||
(Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just v}:
|
||||
Token Numeral NumeralData{TNumeral.value = c}:
|
||||
_) -> Just . Token AmountOfMoney $ withCents (v + c) $ currencyOnly Cent
|
||||
_) -> Just $ Token AmountOfMoney $ withCents (v + c) $ currencyOnly Cent
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
@ -174,7 +208,29 @@ ruleIntersect3 = Rule
|
||||
, prod = \case
|
||||
(Token AmountOfMoney fd:
|
||||
Token Numeral NumeralData{TNumeral.value = d}:
|
||||
_) -> Just . Token AmountOfMoney $ withCents (d * 10) fd
|
||||
_) -> Just $ Token AmountOfMoney $ withCents (d * 10) fd
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
ruleOneDollarAndAHalf :: Rule
|
||||
ruleOneDollarAndAHalf = Rule
|
||||
{ name = "one dollar and a half (short form)"
|
||||
, pattern =
|
||||
[ regex "個半"
|
||||
]
|
||||
, prod = \_ -> Just $ Token AmountOfMoney $ withValue 1.5 (currencyOnly Dollar)
|
||||
}
|
||||
|
||||
ruleOneDollarAnd :: Rule
|
||||
ruleOneDollarAnd = Rule
|
||||
{ name = "one dollar and x dimes (short form)"
|
||||
, pattern =
|
||||
[ regex "個"
|
||||
, numberBetween 1 10
|
||||
]
|
||||
, prod = \case
|
||||
(_:Token Numeral NumeralData{TNumeral.value = v}:_) ->
|
||||
Just $ Token AmountOfMoney $ withValue (1 + decimalsToDouble(v)) (currencyOnly Dollar)
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
@ -192,7 +248,7 @@ ruleIntervalNumeralDash = Rule
|
||||
Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just to,
|
||||
TAmountOfMoney.currency = c}:
|
||||
_) | from < to ->
|
||||
Just . Token AmountOfMoney . withInterval (from, to) $ currencyOnly c
|
||||
Just $ Token AmountOfMoney $ withInterval (from, to) $ currencyOnly c
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
@ -211,7 +267,7 @@ ruleIntervalDash = Rule
|
||||
Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just to,
|
||||
TAmountOfMoney.currency = c2}:
|
||||
_) | from < to && c1 == c2 ->
|
||||
Just . Token AmountOfMoney . withInterval (from, to) $ currencyOnly c1
|
||||
Just $ Token AmountOfMoney $ withInterval (from, to) $ currencyOnly c1
|
||||
_ -> Nothing
|
||||
}
|
||||
|
||||
@ -227,9 +283,9 @@ ruleIntervalBound = Rule
|
||||
Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.value = Just to,
|
||||
TAmountOfMoney.currency = c}:
|
||||
_) -> case match of
|
||||
"最多" -> Just . Token AmountOfMoney . withMax to $ currencyOnly c
|
||||
"最少" -> Just . Token AmountOfMoney . withMin to $ currencyOnly c
|
||||
"至少" -> Just . Token AmountOfMoney . withMin to $ currencyOnly c
|
||||
"最多" -> Just $ Token AmountOfMoney $ withMax to $ currencyOnly c
|
||||
"最少" -> Just $ Token AmountOfMoney $ withMin to $ currencyOnly c
|
||||
"至少" -> Just $ Token AmountOfMoney $ withMin to $ currencyOnly c
|
||||
_ -> Nothing
|
||||
_ -> Nothing
|
||||
}
|
||||
@ -246,8 +302,8 @@ ruleIntervalBound2 = Rule
|
||||
TAmountOfMoney.currency = c}:
|
||||
Token RegexMatch (GroupMatch (match:_)):
|
||||
_) -> case match of
|
||||
"以下" -> Just . Token AmountOfMoney . withMax to $ currencyOnly c
|
||||
"以上" -> Just . Token AmountOfMoney . withMin to $ currencyOnly c
|
||||
"以下" -> Just $ Token AmountOfMoney $ withMax to $ currencyOnly c
|
||||
"以上" -> Just $ Token AmountOfMoney $ withMin to $ currencyOnly c
|
||||
_ -> Nothing
|
||||
_ -> Nothing
|
||||
}
|
||||
@ -257,6 +313,8 @@ rules =
|
||||
[ ruleCent
|
||||
, ruleCNY
|
||||
, ruleCNYPrefix
|
||||
, ruleHKD
|
||||
, ruleHKDPrefix
|
||||
, ruleDime
|
||||
, ruleDollar
|
||||
, ruleIntersect
|
||||
@ -264,10 +322,13 @@ rules =
|
||||
, ruleIntersect3
|
||||
, ruleIntersectDimesAndCents
|
||||
, ruleIntersectDollarsAndDimesCents
|
||||
, ruleIntersectDollarsAndAHalf
|
||||
, ruleIntervalDash
|
||||
, ruleIntervalNumeralDash
|
||||
, ruleIntervalBound
|
||||
, ruleIntervalBound2
|
||||
, rulePrecision
|
||||
, rulePrecision2
|
||||
, ruleOneDollarAndAHalf
|
||||
, ruleOneDollarAnd
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user