duckling/Duckling/Quantity/Helpers.hs
Sergei Rybalkin 70681e3302 latent entities
Summary:
Adding latent matching rules.
Matching Numerical to QuantityData with Unnamed as unit

Reviewed By: chinmay87

Differential Revision: D17225711

fbshipit-source-id: 8e423454e5e7b83eb8de4cabfd4f85a2a35b7a6d
2019-09-10 10:46:42 -07:00

95 lines
3.1 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.Quantity.Helpers
( getValue
, isSimpleQuantity
, quantity
, unitOnly
, valueOnly
, withProduct
, withUnit
, withValue
, withInterval
, withMin
, withMax
, mkLatent
) where
import Data.HashMap.Strict (HashMap)
import Data.Text (Text)
import Prelude
import qualified Data.HashMap.Strict as HashMap
import qualified Data.Text as Text
import Duckling.Dimensions.Types
import Duckling.Quantity.Types (QuantityData(..))
import Duckling.Types
import qualified Duckling.Quantity.Types as TQuantity
getValue :: HashMap Text (Double -> Double) -> Text -> Double -> Double
getValue opsMap match = HashMap.lookupDefault id (Text.toLower match) opsMap
-- -----------------------------------------------------------------
-- Patterns
isSimpleQuantity :: Predicate
isSimpleQuantity (Token Quantity QuantityData {TQuantity.unit = Just _
, TQuantity.value = Just _})
= True
isSimpleQuantity _ = False
-- -----------------------------------------------------------------
-- Production
quantity :: TQuantity.Unit -> Double -> QuantityData
quantity u v = QuantityData {TQuantity.unit = Just u
, TQuantity.value = Just v
, TQuantity.aproduct = Nothing
, TQuantity.minValue = Nothing
, TQuantity.maxValue = Nothing
, TQuantity.latent = False}
unitOnly :: TQuantity.Unit -> QuantityData
unitOnly u = QuantityData {TQuantity.unit = Just u
, TQuantity.value = Nothing
, TQuantity.aproduct = Nothing
, TQuantity.minValue = Nothing
, TQuantity.maxValue = Nothing
, TQuantity.latent = False}
valueOnly :: Double -> QuantityData
valueOnly v = QuantityData {TQuantity.unit = Nothing
, TQuantity.value = Just v
, TQuantity.aproduct = Nothing
, TQuantity.minValue = Nothing
, TQuantity.maxValue = Nothing
, TQuantity.latent = False}
withProduct :: Text -> QuantityData -> QuantityData
withProduct p qd = qd {TQuantity.aproduct = Just p}
withUnit :: TQuantity.Unit -> QuantityData -> QuantityData
withUnit u qd = qd {TQuantity.unit = Just u}
withValue :: Double -> QuantityData -> QuantityData
withValue value qd = qd {TQuantity.value = Just value}
withInterval :: (Double, Double) -> QuantityData -> QuantityData
withInterval (from, to) qd = qd {minValue = Just from, maxValue = Just to}
withMin :: Double -> QuantityData -> QuantityData
withMin from qd = qd {minValue = Just from}
withMax :: Double -> QuantityData -> QuantityData
withMax to qd = qd {maxValue = Just to}
mkLatent :: QuantityData -> QuantityData
mkLatent qd = qd {TQuantity.latent = True}