module Duckling.Duration.EN.Rules
( rules ) where
import Control.Monad (join)
import qualified Data.Text as Text
import Prelude
import Data.String
import Duckling.Dimensions.Types
import Duckling.Duration.Helpers
import Duckling.Numeral.Helpers (parseInt)
import Duckling.Numeral.Types (NumeralData(..))
import qualified Duckling.Numeral.Types as TNumeral
import Duckling.Regex.Types
import qualified Duckling.TimeGrain.Types as TG
import Duckling.Types
ruleDurationQuarterOfAnHour :: Rule
ruleDurationQuarterOfAnHour = Rule
{ name = "quarter of an hour"
, pattern = [ regex "(1/4\\s?h(our)?|(a\\s)?quarter of an hour)" ]
, prod = \_ -> Just . Token Duration $ duration TG.Minute 15
}
ruleDurationHalfAnHour :: Rule
ruleDurationHalfAnHour = Rule
{ name = "half an hour"
, pattern = [regex "(1/2\\s?h(our)?|half an? hour)"]
, prod = \_ -> Just . Token Duration $ duration TG.Minute 30
}
ruleDurationThreeQuartersOfAnHour :: Rule
ruleDurationThreeQuartersOfAnHour = Rule
{ name = "three-quarters of an hour"
, pattern = [regex "(3/4\\s?h(our)?|three(\\s|-)quarters of an hour)"]
, prod = \_ -> Just . Token Duration $ duration TG.Minute 45
}
ruleDurationFortnight :: Rule
ruleDurationFortnight = Rule
{ name = "fortnight"
, pattern = [regex "(a|one)? fortnight"]
, prod = \_ -> Just . Token Duration $ duration TG.Day 14
}
ruleNumeralQuotes :: Rule
ruleNumeralQuotes = Rule
{ name = "<integer> + '\""
, pattern =
[ Predicate isNatural
, regex "(['\"])"
]
, prod = \tokens -> case tokens of
(Token Numeral (NumeralData {TNumeral.value = v}):
Token RegexMatch (GroupMatch (x:_)):
_) -> case x of
"'" -> Just . Token Duration . duration TG.Minute $ floor v
"\"" -> Just . Token Duration . duration TG.Second $ floor v
_ -> Nothing
_ -> Nothing
}
ruleDurationNumeralMore :: Rule
ruleDurationNumeralMore = Rule
{ name = "<integer> more <unit-of-duration>"
, pattern =
[ Predicate isNatural
, regex "more|less"
, dimension TimeGrain
]
, prod = \tokens -> case tokens of
(Token Numeral nd:_:Token TimeGrain grain:_) ->
Just . Token Duration . duration grain . floor $ TNumeral.value nd
_ -> Nothing
}
ruleDurationDotNumeralHours :: Rule
ruleDurationDotNumeralHours = Rule
{ name = "number.number hours"
, pattern = [regex "(\\d+)\\.(\\d+) *hours?"]
, prod = \tokens -> case tokens of
(Token RegexMatch (GroupMatch (h:d:_)):_) -> do
hh <- parseInt h
dec <- parseInt d
let divisor = floor $ (fromIntegral (10 :: Integer) :: Float) **
fromIntegral (Text.length d 1)
numerator = fromIntegral $ 6 * dec
Just . Token Duration . duration TG.Minute $
60 * hh + quot numerator divisor
_ -> Nothing
}
ruleDurationAndHalfHour :: Rule
ruleDurationAndHalfHour = Rule
{ name = "<integer> and an half hour"
, pattern =
[ Predicate isNatural
, regex "and (an? )?half hours?"
]
, prod = \tokens -> case tokens of
(Token Numeral (NumeralData {TNumeral.value = v}):_) ->
Just . Token Duration . duration TG.Minute $ 30 + 60 * floor v
_ -> Nothing
}
ruleDurationA :: Rule
ruleDurationA = Rule
{ name = "a <unit-of-duration>"
, pattern =
[ regex "an?"
, dimension TimeGrain
]
, prod = \tokens -> case tokens of
(_:Token TimeGrain grain:_) -> Just . Token Duration $ duration grain 1
_ -> Nothing
}
ruleDurationPrecision :: Rule
ruleDurationPrecision = Rule
{ name = "about|exactly <duration>"
, pattern =
[ regex "(about|around|approximately|exactly)"
, dimension Duration
]
, prod = \tokens -> case tokens of
(_:token:_) -> Just token
_ -> Nothing
}
rules :: [Rule]
rules =
[ ruleDurationQuarterOfAnHour
, ruleDurationHalfAnHour
, ruleDurationThreeQuartersOfAnHour
, ruleDurationFortnight
, ruleDurationNumeralMore
, ruleDurationDotNumeralHours
, ruleDurationAndHalfHour
, ruleDurationA
, ruleDurationPrecision
, ruleNumeralQuotes
]