mirror of
https://github.com/facebook/duckling.git
synced 2024-11-29 09:54:49 +03:00
170 lines
3.9 KiB
Haskell
170 lines
3.9 KiB
Haskell
|
-- 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 #-}
|
||
|
|
||
|
module Duckling.Finance.SV.Rules
|
||
|
( rules ) where
|
||
|
|
||
|
import Data.Maybe
|
||
|
import Prelude
|
||
|
import Data.String
|
||
|
|
||
|
import Duckling.Dimensions.Types
|
||
|
import Duckling.Finance.Helpers
|
||
|
import Duckling.Finance.Types (Currency(..), FinanceData (..))
|
||
|
import qualified Duckling.Finance.Types as TFinance
|
||
|
import Duckling.Number.Types (NumberData (..))
|
||
|
import qualified Duckling.Number.Types as TNumber
|
||
|
import Duckling.Types
|
||
|
|
||
|
ruleIntersectAndNumber :: Rule
|
||
|
ruleIntersectAndNumber = Rule
|
||
|
{ name = "intersect (and number)"
|
||
|
, pattern =
|
||
|
[ financeWith TFinance.value isJust
|
||
|
, regex "och"
|
||
|
, dimension DNumber
|
||
|
]
|
||
|
, prod = \tokens -> case tokens of
|
||
|
(Token Finance fd:
|
||
|
_:
|
||
|
Token DNumber (NumberData {TNumber.value = c}):
|
||
|
_) -> Just . Token Finance $ withCents c fd
|
||
|
_ -> Nothing
|
||
|
}
|
||
|
|
||
|
ruleAboutAmountofmoney :: Rule
|
||
|
ruleAboutAmountofmoney = Rule
|
||
|
{ name = "about <amount-of-money>"
|
||
|
, pattern =
|
||
|
[ regex "omkring|cirka|runt|ca"
|
||
|
, financeWith TFinance.value isJust
|
||
|
]
|
||
|
, prod = \tokens -> case tokens of
|
||
|
(_:token:_) -> Just token
|
||
|
_ -> Nothing
|
||
|
}
|
||
|
|
||
|
ruleCent :: Rule
|
||
|
ruleCent = Rule
|
||
|
{ name = "cent"
|
||
|
, pattern =
|
||
|
[ regex "cents?|penn(y|ies)|\x00f6re"
|
||
|
]
|
||
|
, prod = \_ -> Just . Token Finance $ currencyOnly Cent
|
||
|
}
|
||
|
|
||
|
ruleExactlyAmountofmoney :: Rule
|
||
|
ruleExactlyAmountofmoney = Rule
|
||
|
{ name = "exactly <amount-of-money>"
|
||
|
, pattern =
|
||
|
[ regex "exakt|precis"
|
||
|
, financeWith TFinance.value isJust
|
||
|
]
|
||
|
, prod = \tokens -> case tokens of
|
||
|
(_:token:_) -> Just token
|
||
|
_ -> Nothing
|
||
|
}
|
||
|
|
||
|
ruleIntersectXCents :: Rule
|
||
|
ruleIntersectXCents = Rule
|
||
|
{ name = "intersect (X cents)"
|
||
|
, pattern =
|
||
|
[ financeWith TFinance.value isJust
|
||
|
, financeWith TFinance.currency (== Cent)
|
||
|
]
|
||
|
, prod = \tokens -> case tokens of
|
||
|
(Token Finance fd:
|
||
|
Token Finance (FinanceData {TFinance.value = Just c}):
|
||
|
_) -> Just . Token Finance $ withCents c fd
|
||
|
_ -> Nothing
|
||
|
}
|
||
|
|
||
|
ruleNok :: Rule
|
||
|
ruleNok = Rule
|
||
|
{ name = "NOK"
|
||
|
, pattern =
|
||
|
[ regex "norska kronor|nkr"
|
||
|
]
|
||
|
, prod = \_ -> Just . Token Finance $ currencyOnly NOK
|
||
|
}
|
||
|
|
||
|
rulePounds :: Rule
|
||
|
rulePounds = Rule
|
||
|
{ name = "£"
|
||
|
, pattern =
|
||
|
[ regex "pund?"
|
||
|
]
|
||
|
, prod = \_ -> Just . Token Finance $ currencyOnly Pound
|
||
|
}
|
||
|
|
||
|
ruleIntersectAndXCents :: Rule
|
||
|
ruleIntersectAndXCents = Rule
|
||
|
{ name = "intersect (and X cents)"
|
||
|
, pattern =
|
||
|
[ financeWith TFinance.value isJust
|
||
|
, regex "och"
|
||
|
, financeWith TFinance.currency (== Cent)
|
||
|
]
|
||
|
, prod = \tokens -> case tokens of
|
||
|
(Token Finance fd:
|
||
|
_:
|
||
|
Token Finance (FinanceData {TFinance.value = Just c}):
|
||
|
_) -> Just . Token Finance $ withCents c fd
|
||
|
_ -> Nothing
|
||
|
}
|
||
|
|
||
|
ruleIntersect :: Rule
|
||
|
ruleIntersect = Rule
|
||
|
{ name = "intersect"
|
||
|
, pattern =
|
||
|
[ financeWith TFinance.value isJust
|
||
|
, dimension DNumber
|
||
|
]
|
||
|
, prod = \tokens -> case tokens of
|
||
|
(Token Finance fd:
|
||
|
Token DNumber (NumberData {TNumber.value = c}):
|
||
|
_) -> Just . Token Finance $ withCents c fd
|
||
|
_ -> Nothing
|
||
|
}
|
||
|
|
||
|
ruleSek :: Rule
|
||
|
ruleSek = Rule
|
||
|
{ name = "SEK"
|
||
|
, pattern =
|
||
|
[ regex "kr(onor)?"
|
||
|
]
|
||
|
, prod = \_ -> Just . Token Finance $ currencyOnly SEK
|
||
|
}
|
||
|
|
||
|
ruleDirham :: Rule
|
||
|
ruleDirham = Rule
|
||
|
{ name = "AED"
|
||
|
, pattern =
|
||
|
[ regex "dirhams?"
|
||
|
]
|
||
|
, prod = \_ -> Just . Token Finance $ currencyOnly AED
|
||
|
}
|
||
|
|
||
|
rules :: [Rule]
|
||
|
rules =
|
||
|
[ ruleAboutAmountofmoney
|
||
|
, ruleCent
|
||
|
, ruleDirham
|
||
|
, ruleExactlyAmountofmoney
|
||
|
, ruleIntersect
|
||
|
, ruleIntersectAndNumber
|
||
|
, ruleIntersectAndXCents
|
||
|
, ruleIntersectXCents
|
||
|
, ruleNok
|
||
|
, rulePounds
|
||
|
, ruleSek
|
||
|
]
|