-- 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 NamedFieldPuns #-} {-# LANGUAGE NoRebindableSyntax #-} {-# LANGUAGE OverloadedStrings #-} module Duckling.Time.EN.Rules where import Control.Applicative ((<|>)) import Control.Monad (guard) import Data.Maybe import Prelude import qualified Data.Text as Text import Duckling.Dimensions.Types import Duckling.Duration.Helpers (duration, isGrain) import Duckling.Duration.Types (DurationData (..)) import Duckling.Numeral.Helpers (isNatural, 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.HolidayHelpers import Duckling.Time.Types (TimeData (..), TimeIntervalType (..)) 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 $ isGrainFinerThan TG.Year , Predicate $ or . sequence [isNotLatent, isGrainOfTime TG.Year] ] , prod = \tokens -> case tokens of (Token Time td1:Token Time td2:_) | not (TTime.latent td1) || not (TTime.latent 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 isNotLatent ] , prod = \tokens -> case tokens of (Token Time td1:_:Token Time td2:_) -> Token Time . notLatent <$> intersect td1 td2 _ -> Nothing } ruleIntersectYear :: Rule ruleIntersectYear = Rule { name = "intersect by \",\", \"of\", \"from\" for year" , pattern = [ Predicate isNotLatent , regex "of|from|," , Predicate $ isGrainOfTime TG.Year ] , prod = \tokens -> case tokens of (Token Time td1:_:Token Time td2:_) -> Token Time . notLatent <$> intersect td1 td2 _ -> Nothing } ruleAbsorbOnDay :: Rule ruleAbsorbOnDay = Rule { name = "on " , pattern = [ regex "on" , Predicate $ isGrainOfTime TG.Day ] , 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|during |year" , pattern = [ regex "in|during" , 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|at\\sthe\\smoment|atm" ] , prod = \_ -> tt now } ruleASAP :: Rule ruleASAP = Rule { name = "as soon as possible" , pattern = [ regex "asap|as\\ssoon\\sas\\spossible" ] , prod = \_ -> tt $ withDirection TTime.After now } ruleNextDOW :: Rule ruleNextDOW = Rule { name = "this|next " , pattern = [ regex "(this|next)" , Predicate isADayOfWeek ] , prod = \case ( Token RegexMatch (GroupMatch (match:_)): Token Time dow: _) -> do td <- case Text.toLower match of "this" -> Just $ predNth 0 True dow "next" -> intersect dow $ cycleNth TG.Week 1 _ -> Nothing tt td _ -> Nothing } ruleThisTime :: Rule ruleThisTime = Rule { name = "this