{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
module Duckling.Quantity.MN.Rules
( rules
) where
import Data.String
import Data.Text (Text)
import Prelude
import qualified Data.Text as Text
import Duckling.Dimensions.Types
import Duckling.Numeral.Helpers
import Duckling.Quantity.Helpers
import Duckling.Regex.Types
import Duckling.Types
import qualified Duckling.Numeral.Types as TNumeral
import qualified Duckling.Quantity.Types as TQuantity
quantities :: [(Text, String, TQuantity.Unit, Double -> Double)]
quantities :: [(Text, String, Unit, Double -> Double)]
quantities =
[ (Text
"<quantity> milligrams", String
"(мг|миллиграмм)", Unit
TQuantity.Gram, (Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
1000))
, (Text
"<quantity> grams", String
"г(рамм?)?", Unit
TQuantity.Gram, Double -> Double
forall a. a -> a
id)
, (Text
"<quantity> kilograms", String
"(кг|килограмм?)", Unit
TQuantity.Gram, (Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1000))
, (Text
"<quantity> lb", String
"фунт", Unit
TQuantity.Pound, Double -> Double
forall a. a -> a
id)
, (Text
"<quantity> oz", String
"унц", Unit
TQuantity.Ounce, Double -> Double
forall a. a -> a
id)
]
ruleNumeralQuantities :: [Rule]
ruleNumeralQuantities :: [Rule]
ruleNumeralQuantities = ((Text, String, Unit, Double -> Double) -> Rule)
-> [(Text, String, Unit, Double -> Double)] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map (Text, String, Unit, Double -> Double) -> Rule
go [(Text, String, Unit, Double -> Double)]
quantities
where
go :: (Text, String, TQuantity.Unit, Double -> Double) -> Rule
go :: (Text, String, Unit, Double -> Double) -> Rule
go (Text
name, String
regexPattern, Unit
u, Double -> Double
convert) = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
name
, pattern :: Pattern
pattern =
[ (NumeralData -> Double) -> (Double -> Bool) -> PatternItem
forall t. (NumeralData -> t) -> (t -> Bool) -> PatternItem
numberWith NumeralData -> Double
TNumeral.value (Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
0), String -> PatternItem
regex String
regexPattern ]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
Numeral a
nd:[Token]
_) ->
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (Double -> Token) -> Double -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Token)
-> (Double -> QuantityData) -> Double -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unit -> Double -> QuantityData
quantity Unit
u (Double -> QuantityData)
-> (Double -> Double) -> Double -> QuantityData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double
convert (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ NumeralData -> Double
TNumeral.value a
NumeralData
nd
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleAQuantity :: [Rule]
ruleAQuantity :: [Rule]
ruleAQuantity = ((Text, String, Unit, Double -> Double) -> Rule)
-> [(Text, String, Unit, Double -> Double)] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map (Text, String, Unit, Double -> Double) -> Rule
go [(Text, String, Unit, Double -> Double)]
quantities
where
go :: (Text, String, TQuantity.Unit, Double -> Double) -> Rule
go :: (Text, String, Unit, Double -> Double) -> Rule
go (Text
name, String
regexPattern, Unit
u, Double -> Double
convert) = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
name
, pattern :: Pattern
pattern = [ String -> PatternItem
regex String
regexPattern ]
, prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (Double -> Token) -> Double -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Token)
-> (Double -> QuantityData) -> Double -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unit -> Double -> QuantityData
quantity Unit
u (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double -> Double
convert Double
1
}
rules :: [Rule]
rules :: [Rule]
rules = [Rule]
ruleNumeralQuantities [Rule] -> [Rule] -> [Rule]
forall a. [a] -> [a] -> [a]
++ [Rule]
ruleAQuantity