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

import Data.Maybe
import Data.String
import Prelude

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

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
"(deg(ree?)?s?\\.?)|°"
    ]
  , 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(el[cs]?(ius)?)?\\.?"
    ]
  , 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(ah?rh?eh?n(h?eit)?)?\\.?"
    ]
  , 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
"<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
"below zero"
    ]
  , 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
  }

ruleIntervalBetween :: Rule
ruleIntervalBetween :: Rule
ruleIntervalBetween = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"between|from <temp> and|to <temp>"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"between|from"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleTemperature
    , String -> PatternItem
regex String
"to|and"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleTemperature
    ]
  , prod :: Production
prod = \case
      (Token
_:
       Token Dimension a
Temperature TemperatureData
        {TTemperature.unit = u1 , TTemperature.value = Just from}:
       Token
_:
       Token Dimension a
Temperature TemperatureData
        {TTemperature.unit = Just u2, TTemperature.value = Just to}:
       [Token]
_) | Double
from Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
to Bool -> Bool -> Bool
&& Maybe TemperatureUnit -> TemperatureUnit -> Bool
unitsAreCompatible Maybe TemperatureUnit
u1 TemperatureUnit
u2 ->
        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
. (Double, Double) -> TemperatureData -> TemperatureData
withInterval (Double
from, Double
to) (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ TemperatureUnit -> TemperatureData
unitOnly TemperatureUnit
u2
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalDash :: Rule
ruleIntervalDash :: Rule
ruleIntervalDash = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<temp> - <temp>"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isSimpleTemperature
    , String -> PatternItem
regex String
"-"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleTemperature
    ]
  , prod :: Production
prod = \case
      (Token Dimension a
Temperature TemperatureData
        {TTemperature.unit = u1, TTemperature.value = Just from}:
       Token
_:
       Token Dimension a
Temperature TemperatureData
        {TTemperature.unit = Just u2, TTemperature.value = Just to}:
       [Token]
_) | Double
from Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
to Bool -> Bool -> Bool
&& Maybe TemperatureUnit -> TemperatureUnit -> Bool
unitsAreCompatible Maybe TemperatureUnit
u1 TemperatureUnit
u2 ->
        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
. (Double, Double) -> TemperatureData -> TemperatureData
withInterval (Double
from, Double
to) (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ TemperatureUnit -> TemperatureData
unitOnly TemperatureUnit
u2
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalMax :: Rule
ruleIntervalMax :: Rule
ruleIntervalMax = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"under/less/lower/no more than <temp>"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"under|(less|lower|not? more) than"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleTemperature
    ]
  , prod :: Production
prod = \case
      (Token
_:
       Token Dimension a
Temperature TemperatureData{TTemperature.value = Just to,
                                         TTemperature.unit = Just u}:
       [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 -> Token)
-> (TemperatureData -> TemperatureData) -> TemperatureData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> TemperatureData -> TemperatureData
withMax Double
to (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ TemperatureUnit -> TemperatureData
unitOnly TemperatureUnit
u
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalMin :: Rule
ruleIntervalMin :: Rule
ruleIntervalMin = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"over/above/at least/more than <temp>"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"over|above|at least|more than"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleTemperature
    ]
  , prod :: Production
prod = \case
      (Token
_:
       Token Dimension a
Temperature TemperatureData{TTemperature.value = Just from,
                                         TTemperature.unit = Just u}:
       [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 -> Token)
-> (TemperatureData -> TemperatureData) -> TemperatureData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> TemperatureData -> TemperatureData
withMin Double
from (TemperatureData -> Maybe Token) -> TemperatureData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ TemperatureUnit -> TemperatureData
unitOnly TemperatureUnit
u
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

rules :: [Rule]
rules :: [Rule]
rules =
  [ Rule
ruleTemperatureDegrees
  , Rule
ruleTemperatureCelsius
  , Rule
ruleTemperatureFahrenheit
  , Rule
ruleTemperatureBelowZero
  , Rule
ruleIntervalBetween
  , Rule
ruleIntervalDash
  , Rule
ruleIntervalMin
  , Rule
ruleIntervalMax
  ]