-- 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.Rules
  ( rules
  ) where

import Data.String
import Prelude
import Data.String

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

ruleNumeralAsVolume :: Rule
ruleNumeralAsVolume :: Rule
ruleNumeralAsVolume = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"number as volume"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    ]
  , prod :: Production
prod = \case
    (Token Dimension a
Numeral TNumeral.NumeralData {TNumeral.value = v}:
     [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
$ Double -> VolumeData
valueOnly Double
v
    [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleNumeralVolumes :: Rule
ruleNumeralVolumes :: Rule
ruleNumeralVolumes = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<number> <volume>"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    , Predicate -> PatternItem
Predicate Predicate
isUnitOnly
    ]
  , prod :: Production
prod = \case
    (Token Dimension a
Numeral TNumeral.NumeralData{TNumeral.value = v}:
     Token Dimension a
Volume TVolume.VolumeData{TVolume.unit = Just u}:
     [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 -> Double -> VolumeData
volume Unit
u Double
v
    [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalNumeralDash :: Rule
ruleIntervalNumeralDash :: Rule
ruleIntervalNumeralDash = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<numeral> - <volume>"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    , String -> PatternItem
regex String
"\\-"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    ]
  , prod :: Production
prod = \case
      (Token Dimension a
Numeral TNumeral.NumeralData{TNumeral.value = from}:
       Token
_:
       Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just to
                                  , TVolume.unit = Just u}:
       [Token]
_) | Double
from Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
to ->
        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
. (Double, Double) -> VolumeData -> VolumeData
withInterval (Double
from, Double
to) (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalDash :: Rule
ruleIntervalDash :: Rule
ruleIntervalDash = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<volume> - <volume>"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    , String -> PatternItem
regex String
"\\-"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    ]
  , prod :: Production
prod = \case
      (Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just from
                                  , TVolume.unit = Just u1}:
       Token
_:
       Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just to
                                  , TVolume.unit = Just u2}:
       [Token]
_) | Double
from Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
to Bool -> Bool -> Bool
&& Unit
u1 Unit -> Unit -> Bool
forall a. Eq a => a -> a -> Bool
== Unit
u2 ->
        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
. (Double, Double) -> VolumeData -> VolumeData
withInterval (Double
from, Double
to) (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u1
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

rules :: [Rule]
rules :: [Rule]
rules = [ Rule
ruleNumeralAsVolume
        , Rule
ruleNumeralVolumes
        , Rule
ruleIntervalNumeralDash
        , Rule
ruleIntervalDash
        ]