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

import Data.Maybe
import Prelude
import Data.String

import Duckling.Dimensions.Types
import Duckling.Temperature.Helpers
import qualified Duckling.Temperature.Types as TTemperature
import Duckling.Temperature.Types (TemperatureData(..))
import Duckling.Types

ruleTemperatureDegrees :: Rule
ruleTemperatureDegrees :: Rule
ruleTemperatureDegrees = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<latent temp> degrees"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ Bool -> Predicate
isValueOnly Bool
False
    , String -> PatternItem
regex String
"derece|°"
    ]
  , prod :: Production
prod = \case
    (Token Dimension a
Temperature a
td:[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (TemperatureData -> Token) -> TemperatureData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension TemperatureData -> TemperatureData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension TemperatureData
Temperature (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$
      TemperatureUnit -> TemperatureData -> TemperatureData
withUnit TemperatureUnit
TTemperature.Degree a
TemperatureData
td
    [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleTemperatureCelsius :: Rule
ruleTemperatureCelsius :: Rule
ruleTemperatureCelsius = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<temp> Celsius"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ Bool -> Predicate
isValueOnly Bool
True
    , String -> PatternItem
regex String
"c|santigrat"
    ]
  , prod :: Production
prod = \case
    (Token Dimension a
Temperature a
td:[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (TemperatureData -> Token) -> TemperatureData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension TemperatureData -> TemperatureData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension TemperatureData
Temperature (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$
      TemperatureUnit -> TemperatureData -> TemperatureData
withUnit TemperatureUnit
TTemperature.Celsius a
TemperatureData
td
    [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleTemperatureFahrenheit :: Rule
ruleTemperatureFahrenheit :: Rule
ruleTemperatureFahrenheit = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<temp> Fahrenheit"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ Bool -> Predicate
isValueOnly Bool
True
    , String -> PatternItem
regex String
"f(ahrenh?ayt)?"
    ]
  , prod :: Production
prod = \case
    (Token Dimension a
Temperature a
td:[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (TemperatureData -> Token) -> TemperatureData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension TemperatureData -> TemperatureData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension TemperatureData
Temperature (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$
      TemperatureUnit -> TemperatureData -> TemperatureData
withUnit TemperatureUnit
TTemperature.Fahrenheit a
TemperatureData
td
    [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleTemperatureBelowZero :: Rule
ruleTemperatureBelowZero :: Rule
ruleTemperatureBelowZero = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"below zero <temp>"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"s\305f\305r\305n alt\305nda"
    , Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ Bool -> Predicate
isValueOnly Bool
True
    ]
  , prod :: Production
prod = \case
      (Token
_:Token Dimension a
Temperature td :: a
td@TemperatureData{TTemperature.value = Just v}:[Token]
_) ->
        case TemperatureData -> Maybe TemperatureUnit
TTemperature.unit a
TemperatureData
td of
          Maybe TemperatureUnit
Nothing -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (TemperatureData -> Token) -> TemperatureData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension TemperatureData -> TemperatureData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension TemperatureData
Temperature (TemperatureData -> Token)
-> (TemperatureData -> TemperatureData) -> TemperatureData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TemperatureUnit -> TemperatureData -> TemperatureData
withUnit TemperatureUnit
TTemperature.Degree (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$
            a
TemperatureData
td {value :: Maybe Double
TTemperature.value = Double -> Maybe Double
forall a. a -> Maybe a
Just (- Double
v)}
          Maybe TemperatureUnit
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (TemperatureData -> Token) -> TemperatureData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension TemperatureData -> TemperatureData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension TemperatureData
Temperature (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ a
TemperatureData
td {value :: Maybe Double
TTemperature.value = Double -> Maybe Double
forall a. a -> Maybe a
Just (- Double
v)}
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleTemperatureBelowZeroReverse :: Rule
ruleTemperatureBelowZeroReverse :: Rule
ruleTemperatureBelowZeroReverse = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<temp> below zero"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ Bool -> Predicate
isValueOnly Bool
True
    , String -> PatternItem
regex String
"s\305f\305r\305n alt\305nda"
    ]
  , prod :: Production
prod = \case
      (Token Dimension a
Temperature td :: a
td@TemperatureData{TTemperature.value = Just v}:[Token]
_) ->
        case TemperatureData -> Maybe TemperatureUnit
TTemperature.unit a
TemperatureData
td of
          Maybe TemperatureUnit
Nothing -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (TemperatureData -> Token) -> TemperatureData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension TemperatureData -> TemperatureData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension TemperatureData
Temperature (TemperatureData -> Token)
-> (TemperatureData -> TemperatureData) -> TemperatureData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TemperatureUnit -> TemperatureData -> TemperatureData
withUnit TemperatureUnit
TTemperature.Degree (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$
            a
TemperatureData
td {value :: Maybe Double
TTemperature.value = Double -> Maybe Double
forall a. a -> Maybe a
Just (- Double
v)}
          Maybe TemperatureUnit
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (TemperatureData -> Token) -> TemperatureData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension TemperatureData -> TemperatureData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension TemperatureData
Temperature (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ a
TemperatureData
td {value :: Maybe Double
TTemperature.value = Double -> Maybe Double
forall a. a -> Maybe a
Just (- Double
v)}
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

rules :: [Rule]
rules :: [Rule]
rules =
  [ Rule
ruleTemperatureDegrees
  , Rule
ruleTemperatureCelsius
  , Rule
ruleTemperatureFahrenheit
  , Rule
ruleTemperatureBelowZero
  , Rule
ruleTemperatureBelowZeroReverse
  ]