duckling/Duckling/Temperature/Helpers.hs
l5t de8af2395d EN/FR support temperature with decimal values (#408)
Summary:
Allowing the support of decimal values like in human body temperature. Example: 98.6°F
Pull Request resolved: https://github.com/facebook/duckling/pull/408

Reviewed By: girifb

Differential Revision: D17401806

Pulled By: patapizza

fbshipit-source-id: f04b768e2f6cb48c9c50977a5807d62e38f8d545
2019-09-17 10:34:25 -07:00

101 lines
2.8 KiB
Haskell

-- 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 #-}
module Duckling.Temperature.Helpers
( isLatent
, isValueOnly
, isSimpleTemperature
, withUnit
, withInterval
, withValue
, valueOnly
, unitOnly
, withMin
, withMax
) where
import Data.Maybe
import Prelude
import Duckling.Dimensions.Types
import qualified Duckling.Temperature.Types as TTemperature
import Duckling.Temperature.Types (TemperatureData(..))
import Duckling.Types hiding (isLatent)
-- -----------------------------------------------------------------
-- Patterns
isLatent :: Predicate
isLatent (Token Temperature TemperatureData{TTemperature.unit = Nothing}) =
True
isLatent _ = False
isValueOnly :: Bool -> Predicate
isValueOnly allowDegree (Token Temperature TemperatureData
{ TTemperature.unit = u, TTemperature.value = Just _
, TTemperature.minValue = Nothing
, TTemperature.maxValue = Nothing})
= isNothing u || (allowDegree && u == Just TTemperature.Degree)
isValueOnly _ _ = False
isSimpleTemperature :: Predicate
isSimpleTemperature (Token Temperature TemperatureData
{TTemperature.value = Just _}) = True
isSimpleTemperature _ = False
-- -----------------------------------------------------------------
-- Production
withUnit :: TTemperature.TemperatureUnit -> TemperatureData -> TemperatureData
withUnit u td = td {TTemperature.unit = Just u}
withInterval :: (Double, Double) -> TemperatureData -> TemperatureData
withInterval (from, to) td = td
{ TTemperature.value = Nothing
, TTemperature.minValue = Just from
, TTemperature.maxValue = Just to
}
withValue :: Double -> TemperatureData -> TemperatureData
withValue v td = td
{ TTemperature.value = Just v
, TTemperature.minValue = Nothing
, TTemperature.maxValue = Nothing
}
valueOnly :: Double -> TemperatureData
valueOnly v = TemperatureData
{ TTemperature.unit = Nothing
, TTemperature.value = Just v
, TTemperature.minValue = Nothing
, TTemperature.maxValue = Nothing
}
unitOnly :: TTemperature.TemperatureUnit -> TemperatureData
unitOnly u = TemperatureData
{ TTemperature.unit = Just u
, TTemperature.value = Nothing
, TTemperature.minValue = Nothing
, TTemperature.maxValue = Nothing
}
withMax :: Double -> TemperatureData -> TemperatureData
withMax v td = td
{ TTemperature.value = Nothing
, TTemperature.minValue = Nothing
, TTemperature.maxValue = Just v
}
withMin :: Double -> TemperatureData -> TemperatureData
withMin v td = td
{ TTemperature.value = Nothing
, TTemperature.minValue = Just v
, TTemperature.maxValue = Nothing
}