-- 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 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.Numeral.Helpers (parseInt) import Duckling.Numeral.Types (NumeralData (..)) import Duckling.Ordinal.Types (OrdinalData (..)) import Duckling.Regex.Types import Duckling.Time.Helpers import Duckling.Time.Types (TimeData (..)) import Duckling.Types 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 isNotLatent ] , prod = \tokens -> case tokens of (Token Time td1:Token Time td2:_) -> Token Time <$> 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 <$> 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 } ruleAbsorbInMonth :: Rule ruleAbsorbInMonth = Rule { name = "in " , pattern = [ regex "in" , Predicate isAMonth ] , prod = \tokens -> case tokens of (_:token:_) -> Just token _ -> Nothing } ruleAbsorbCommaTOD :: Rule ruleAbsorbCommaTOD = Rule { name = "absorption of , after named day" , pattern = [ Predicate isADayOfWeek , regex "," ] , prod = \tokens -> case tokens of (token:_) -> Just token _ -> Nothing } instants :: [(Text, String, TG.Grain, Int)] instants = [ ("right now", "((just|right)\\s*)now|immediately", TG.Second, 0) , ("today", "todays?|(at this time)", TG.Day, 0) , ("tomorrow", "(tmrw?|tomm?or?rows?)", TG.Day, 1) , ("yesterday", "yesterdays?", TG.Day, - 1) , ("end of month", "(the )?(EOM|end of (the )?month)", TG.Month, 1) , ("end of year", "(the )?(EOY|end of (the )?year)", TG.Year, 1) ] ruleInstants :: [Rule] ruleInstants = map go instants where go (name, regexPattern, grain, n) = Rule { name = name , pattern = [regex regexPattern] , prod = \_ -> tt $ cycleNth grain n } 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