2017-03-08 21:33:55 +03:00
|
|
|
-- Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
-- All rights reserved.
|
|
|
|
--
|
|
|
|
-- This source code is licensed under the BSD-style license found in the
|
|
|
|
-- LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
-- of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
|
|
|
|
{-# LANGUAGE GADTs #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2017-03-17 00:34:53 +03:00
|
|
|
module Duckling.AmountOfMoney.Rules
|
2017-03-08 21:33:55 +03:00
|
|
|
( rules
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.String
|
|
|
|
import Data.Text (Text)
|
|
|
|
import Prelude
|
2017-06-10 05:27:18 +03:00
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
|
|
|
import qualified Data.Text as Text
|
2017-03-08 21:33:55 +03:00
|
|
|
|
2017-03-17 00:34:53 +03:00
|
|
|
import Duckling.AmountOfMoney.Helpers
|
|
|
|
import Duckling.AmountOfMoney.Types (Currency(..), AmountOfMoneyData (..))
|
2017-03-08 21:33:55 +03:00
|
|
|
import Duckling.Dimensions.Types
|
2017-03-16 23:42:15 +03:00
|
|
|
import Duckling.Numeral.Types (NumeralData (..))
|
2017-03-08 21:33:55 +03:00
|
|
|
import Duckling.Regex.Types
|
|
|
|
import Duckling.Types
|
2017-06-10 05:27:18 +03:00
|
|
|
import qualified Duckling.AmountOfMoney.Types as TAmountOfMoney
|
|
|
|
import qualified Duckling.Numeral.Types as TNumeral
|
2017-03-08 21:33:55 +03:00
|
|
|
|
|
|
|
currencies :: HashMap Text Currency
|
|
|
|
currencies = HashMap.fromList
|
|
|
|
[ ("aed", AED)
|
|
|
|
, ("aud", AUD)
|
2017-08-14 19:18:27 +03:00
|
|
|
, ("bgn", BGN)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("brl", BRL)
|
2017-12-12 23:13:25 +03:00
|
|
|
, ("byn", BYN)
|
2017-09-07 22:33:31 +03:00
|
|
|
, ("¢", Cent)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("c", Cent)
|
|
|
|
, ("$", Dollar)
|
2018-01-03 00:51:34 +03:00
|
|
|
, ("dinar", Dinar)
|
|
|
|
, ("dinars", Dinar)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("dollar", Dollar)
|
|
|
|
, ("dollars", Dollar)
|
|
|
|
, ("egp", EGP)
|
2017-09-07 22:33:31 +03:00
|
|
|
, ("€", EUR)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("eur", EUR)
|
|
|
|
, ("euro", EUR)
|
|
|
|
, ("euros", EUR)
|
|
|
|
, ("eurs", EUR)
|
2017-09-07 22:33:31 +03:00
|
|
|
, ("€ur", EUR)
|
|
|
|
, ("€uro", EUR)
|
|
|
|
, ("€uros", EUR)
|
|
|
|
, ("€urs", EUR)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("gbp", GBP)
|
2017-04-26 19:11:06 +03:00
|
|
|
, ("hrk", HRK)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("idr", IDR)
|
2018-01-03 00:51:34 +03:00
|
|
|
, ("ils", ILS)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("inr", INR)
|
2018-01-03 00:51:34 +03:00
|
|
|
, ("iqd", IQD)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("rs", INR)
|
|
|
|
, ("rs.", INR)
|
|
|
|
, ("rupee", INR)
|
|
|
|
, ("rupees", INR)
|
2018-01-03 00:51:34 +03:00
|
|
|
, ("jod", JOD)
|
2017-09-07 22:33:31 +03:00
|
|
|
, ("¥", JPY)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("jpy", JPY)
|
|
|
|
, ("yen", JPY)
|
|
|
|
, ("krw", KRW)
|
|
|
|
, ("kwd", KWD)
|
|
|
|
, ("lbp", LBP)
|
2018-01-03 00:51:34 +03:00
|
|
|
, ("mad", MAD)
|
2017-06-10 05:27:18 +03:00
|
|
|
, ("myr", MYR)
|
|
|
|
, ("rm", MYR)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("nok", NOK)
|
2017-09-07 22:33:31 +03:00
|
|
|
, ("£", Pound)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("pt", PTS)
|
|
|
|
, ("pta", PTS)
|
|
|
|
, ("ptas", PTS)
|
|
|
|
, ("pts", PTS)
|
|
|
|
, ("qar", QAR)
|
2017-12-12 23:13:25 +03:00
|
|
|
, ("₽", RUB)
|
2018-01-03 00:51:34 +03:00
|
|
|
, ("rial", Rial)
|
|
|
|
, ("rials", Rial)
|
|
|
|
, ("riyal", Riyal)
|
|
|
|
, ("riyals", Riyal)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("ron", RON)
|
2017-12-12 23:13:25 +03:00
|
|
|
, ("rub", RUB)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("sar", SAR)
|
|
|
|
, ("sek", SEK)
|
|
|
|
, ("sgd", SGD)
|
2018-01-03 00:51:34 +03:00
|
|
|
, ("shekel", ILS)
|
|
|
|
, ("shekels", ILS)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("usd", USD)
|
|
|
|
, ("us$", USD)
|
|
|
|
, ("vnd", VND)
|
|
|
|
]
|
|
|
|
|
|
|
|
ruleCurrencies :: Rule
|
|
|
|
ruleCurrencies = Rule
|
|
|
|
{ name = "currencies"
|
|
|
|
, pattern =
|
2018-01-03 00:51:34 +03:00
|
|
|
[ regex "(aed|aud|bgn|brl|byn|¢|c|\\$|dinars?|dollars?|egp|(e|€)uro?s?|€|gbp|hrk|idr|ils|inr|iqd|jod|¥|jpy|krw|kwd|lbp|mad|myr|rm|nok|£|pta?s?|qar|₽|rs\\.?|riy?als?|ron|rub|rupees?|sar|sek|sgb|shekels?|us(d|\\$)|vnd|yen)"
|
2017-03-08 21:33:55 +03:00
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
|
|
|
(Token RegexMatch (GroupMatch (match:_)):_) -> do
|
|
|
|
c <- HashMap.lookup (Text.toLower match) currencies
|
2017-03-17 00:34:53 +03:00
|
|
|
Just . Token AmountOfMoney $ currencyOnly c
|
2017-03-08 21:33:55 +03:00
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleAmountUnit :: Rule
|
|
|
|
ruleAmountUnit = Rule
|
|
|
|
{ name = "<amount> <unit>"
|
|
|
|
, pattern =
|
2017-03-14 23:19:13 +03:00
|
|
|
[ dimension Numeral
|
2017-03-17 00:34:53 +03:00
|
|
|
, financeWith TAmountOfMoney.value isNothing
|
2017-03-08 21:33:55 +03:00
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
2018-01-30 03:41:23 +03:00
|
|
|
(Token Numeral NumeralData{TNumeral.value = v}:
|
|
|
|
Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.currency = c}:
|
2017-03-17 00:34:53 +03:00
|
|
|
_) -> Just . Token AmountOfMoney . withValue v $ currencyOnly c
|
2017-03-08 21:33:55 +03:00
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleUnitAmount :: Rule
|
|
|
|
ruleUnitAmount = Rule
|
|
|
|
{ name = "<unit> <amount>"
|
|
|
|
, pattern =
|
2017-03-17 00:34:53 +03:00
|
|
|
[ financeWith TAmountOfMoney.value isNothing
|
2017-03-14 23:19:13 +03:00
|
|
|
, dimension Numeral
|
2017-03-08 21:33:55 +03:00
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
2018-01-30 03:41:23 +03:00
|
|
|
(Token AmountOfMoney AmountOfMoneyData{TAmountOfMoney.currency = c}:
|
|
|
|
Token Numeral NumeralData{TNumeral.value = v}:
|
2017-03-17 00:34:53 +03:00
|
|
|
_) -> Just . Token AmountOfMoney . withValue v $ currencyOnly c
|
2017-03-08 21:33:55 +03:00
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
rules :: [Rule]
|
|
|
|
rules =
|
|
|
|
[ ruleAmountUnit
|
|
|
|
, ruleCurrencies
|
|
|
|
, ruleUnitAmount
|
|
|
|
]
|