-- 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 GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE NoRebindableSyntax #-} {-# LANGUAGE OverloadedStrings #-} module Duckling.Time.EN.Rules ( rules ) where import Data.Maybe import Data.Text (Text) import Prelude import qualified Data.Text as Text import Duckling.Dimensions.Types import Duckling.Duration.Helpers (duration) import Duckling.Duration.Types (DurationData (..)) import Duckling.Numeral.Helpers (parseInt) import Duckling.Numeral.Types (NumeralData (..)) import Duckling.Ordinal.Types (OrdinalData (..)) import Duckling.Regex.Types import Duckling.Time.Computed import Duckling.Time.Helpers import Duckling.Time.Types (TimeData (..)) import Duckling.Types import qualified Duckling.Duration.Types as TDuration import qualified Duckling.Numeral.Types as TNumeral import qualified Duckling.Ordinal.Types as TOrdinal import qualified Duckling.Time.Types as TTime import qualified Duckling.TimeGrain.Types as TG ruleIntersect :: Rule ruleIntersect = Rule { name = "intersect" , pattern = [ Predicate isNotLatent , Predicate $ or . sequence [isNotLatent, isGrainOfTime TG.Year] ] , prod = \tokens -> case tokens of (Token Time td1:Token Time td2:_) -> Token Time . notLatent <$> intersect td1 td2 _ -> Nothing } ruleIntersectOf :: Rule ruleIntersectOf = Rule { name = "intersect by \",\", \"of\", \"from\", \"'s\"" , pattern = [ Predicate isNotLatent , regex "of|from|for|'s|," , Predicate $ or . sequence [isNotLatent, isGrainOfTime TG.Year] ] , prod = \tokens -> case tokens of (Token Time td1:_:Token Time td2:_) -> Token Time . notLatent <$> intersect td1 td2 _ -> Nothing } ruleAbsorbOnTime :: Rule ruleAbsorbOnTime = Rule { name = "on " , pattern = [ regex "on" , dimension Time ] , prod = \tokens -> case tokens of (_:token:_) -> Just token _ -> Nothing } ruleAbsorbOnADOW :: Rule ruleAbsorbOnADOW = Rule { name = "on a " , pattern = [ regex "on a" , Predicate isADayOfWeek ] , prod = \tokens -> case tokens of (_:token:_) -> Just token _ -> Nothing } ruleAbsorbInMonthYear :: Rule ruleAbsorbInMonthYear = Rule { name = "in |year" , pattern = [ regex "in" , Predicate $ or . sequence [isAMonth, isGrainOfTime TG.Year] ] , prod = \tokens -> case tokens of (_:Token Time td:_) -> tt $ notLatent td _ -> Nothing } ruleAbsorbCommaTOD :: Rule ruleAbsorbCommaTOD = Rule { name = "absorption of , after named day" , pattern = [ Predicate isADayOfWeek , regex "," ] , prod = \tokens -> case tokens of (token:_) -> Just token _ -> Nothing } ruleInstants :: [Rule] ruleInstants = mkRuleInstants [ ("right now" , TG.Second, 0 , "((just|right)\\s*)now|immediately") , ("today" , TG.Day , 0 , "todays?|(at this time)" ) , ("tomorrow" , TG.Day , 1 , "(tmrw?|tomm?or?rows?)" ) , ("yesterday" , TG.Day , - 1, "yesterdays?" ) ] ruleNow :: Rule ruleNow = Rule { name = "now" , pattern = [ regex "now" ] , prod = \_ -> tt now } ruleNextDOW :: Rule ruleNextDOW = Rule { name = "this|next " , pattern = [ regex "this|next" , Predicate isADayOfWeek ] , prod = \tokens -> case tokens of (_:Token Time td:_) -> tt $ predNth 0 True td _ -> Nothing } ruleThisTime :: Rule ruleThisTime = Rule { name = "this