-- 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.


{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Duckling.Volume.AR.Rules
  ( rules
  ) where

import Data.String
import Data.Text (Text)
import Prelude

import Duckling.Dimensions.Types
import Duckling.Types
import Duckling.Regex.Types
import Duckling.Volume.Helpers
import Duckling.Numeral.Helpers (isPositive)
import qualified Duckling.Volume.Types as TVolume
import qualified Duckling.Numeral.Types as TNumeral

volumes :: [(Text, String, TVolume.Unit)]
volumes :: [(Text, String, Unit)]
volumes = [ (Text
"<latent vol> ml"    , String
"مي?لي?( ?لي?تي?ر)?"  , Unit
TVolume.Millilitre)
          , (Text
"<vol> hectoliters"  , String
"(هي?كتو ?لي?تر)"     , Unit
TVolume.Hectolitre)
          , (Text
"<vol> liters"       , String
"لي?تي?ر(ات)?"        , Unit
TVolume.Litre)
          , (Text
"<latent vol> gallon", String
"[جغق]الون(ين|ان|ات)?", Unit
TVolume.Gallon)
          ]

rulesVolumes :: [Rule]
rulesVolumes :: [Rule]
rulesVolumes = ((Text, String, Unit) -> Rule) -> [(Text, String, Unit)] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map (Text, String, Unit) -> Rule
go [(Text, String, Unit)]
volumes
  where
    go :: (Text, String, TVolume.Unit) -> Rule
    go :: (Text, String, Unit) -> Rule
go (Text
name, String
regexPattern, Unit
u) = 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)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
      }

ruleQuarterLiter :: Rule
ruleQuarterLiter :: Rule
ruleQuarterLiter = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"quarter liter"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"ربع لي?تي?ر"
    ]
  , prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unit -> VolumeData -> VolumeData
withUnit Unit
TVolume.Litre (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double -> VolumeData
valueOnly Double
0.25
  }

ruleHalfLiter :: Rule
ruleHalfLiter :: Rule
ruleHalfLiter = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"half liter"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"نصف? لي?تي?ر"
    ]
  , prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unit -> VolumeData -> VolumeData
withUnit Unit
TVolume.Litre (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double -> VolumeData
valueOnly Double
0.5
  }

ruleLiterAndQuarter :: Rule
ruleLiterAndQuarter :: Rule
ruleLiterAndQuarter = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"liter and quarter"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"لي?تي?ر و ?ربع"
    ]
  , prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unit -> VolumeData -> VolumeData
withUnit Unit
TVolume.Litre (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double -> VolumeData
valueOnly Double
1.25
  }

ruleLiterAndHalf :: Rule
ruleLiterAndHalf :: Rule
ruleLiterAndHalf = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"liter and half"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"لي?تي?ر و ?نصف?"
    ]
  , prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unit -> VolumeData -> VolumeData
withUnit Unit
TVolume.Litre (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double -> VolumeData
valueOnly Double
1.5
  }

ruleTwoLiters :: Rule
ruleTwoLiters :: Rule
ruleTwoLiters = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"two liters"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"لي?تي?ر(ان|ين)"
    ]
  , prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Unit -> VolumeData -> VolumeData
withUnit Unit
TVolume.Litre (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double -> VolumeData
valueOnly Double
2
  }

rules :: [Rule]
rules :: [Rule]
rules = [ Rule
ruleHalfLiter
        , Rule
ruleQuarterLiter
        , Rule
ruleTwoLiters
        , Rule
ruleLiterAndHalf
        , Rule
ruleLiterAndQuarter
        ]
        [Rule] -> [Rule] -> [Rule]
forall a. [a] -> [a] -> [a]
++ [Rule]
rulesVolumes