2017-03-08 21:33:55 +03:00
|
|
|
-- 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. An additional grant
|
|
|
|
-- of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
|
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
{-# LANGUAGE NoRebindableSyntax #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
module Duckling.Numeral.Types where
|
2017-03-08 21:33:55 +03:00
|
|
|
|
|
|
|
import Control.DeepSeq
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.Hashable
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.Text (Text)
|
|
|
|
import GHC.Generics
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
import Duckling.Resolve
|
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
data NumeralData = NumeralData
|
2017-03-08 21:33:55 +03:00
|
|
|
{ value :: Double
|
|
|
|
, grain :: Maybe Int
|
|
|
|
, multipliable :: Bool
|
2017-06-27 17:13:55 +03:00
|
|
|
-- Hack until other use cases pop up,
|
|
|
|
-- at which point we'll craft a generic solution.
|
|
|
|
-- This allows to explicitly flag Numerals that don't work well with Time.
|
|
|
|
-- See `ruleTODLatent`. Prevents things like "at single", "dozens o'clock".
|
|
|
|
, okForAnyTime :: Bool
|
2017-03-08 21:33:55 +03:00
|
|
|
}
|
|
|
|
deriving (Eq, Generic, Hashable, Ord, Show, NFData)
|
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
instance Resolve NumeralData where
|
|
|
|
type ResolvedValue NumeralData = NumeralValue
|
2018-03-20 00:34:58 +03:00
|
|
|
resolve _ _ NumeralData {value} = Just (NumeralValue {vValue = value}, False)
|
2017-03-08 21:33:55 +03:00
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
newtype NumeralValue = NumeralValue { vValue :: Double }
|
2017-03-08 21:33:55 +03:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
instance ToJSON NumeralValue where
|
|
|
|
toJSON (NumeralValue value) = object
|
2017-03-08 21:33:55 +03:00
|
|
|
[ "type" .= ("value" :: Text)
|
|
|
|
, "value" .= value
|
|
|
|
]
|
|
|
|
|
|
|
|
getIntValue :: Double -> Maybe Int
|
|
|
|
getIntValue x = if rest == 0 then Just int else Nothing
|
|
|
|
where
|
|
|
|
(int, rest) = properFraction x :: (Int, Double)
|
|
|
|
|
|
|
|
isInteger :: Double -> Bool
|
|
|
|
isInteger = isJust . getIntValue
|
|
|
|
|
|
|
|
isIntegerBetween :: Double -> Int -> Int -> Bool
|
|
|
|
isIntegerBetween x low high = case getIntValue x of
|
|
|
|
Just int -> low <= int && int <= high
|
|
|
|
Nothing -> False
|