{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoRebindableSyntax #-}
{-# LANGUAGE OverloadedStrings #-}
module Duckling.Duration.EN.Rules
( rules
) where
import Data.Semigroup ((<>))
import Prelude
import qualified Data.Text as Text
import Duckling.Dimensions.Types
import Duckling.Duration.Helpers
import Duckling.Duration.Types (DurationData(..))
import Duckling.Numeral.Helpers (parseInteger)
import Duckling.Numeral.Types (NumeralData(..))
import Duckling.Regex.Types
import Duckling.Types
import qualified Duckling.Duration.Types as TDuration
import qualified Duckling.Numeral.Types as TNumeral
import qualified Duckling.TimeGrain.Types as TG
ruleDurationQuarterOfAnHour :: Rule
ruleDurationQuarterOfAnHour :: Rule
ruleDurationQuarterOfAnHour = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"quarter of an hour"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(1/4\\s?h(our)?|(a\\s)?quarter of an hour)"
]
, prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (DurationData -> Token) -> DurationData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Maybe Token) -> DurationData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Grain -> Int -> DurationData
duration Grain
TG.Minute Int
15
}
ruleDurationHalfAnHourAbbrev :: Rule
ruleDurationHalfAnHourAbbrev :: Rule
ruleDurationHalfAnHourAbbrev = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"half an hour (abbrev)."
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"1/2\\s?h"
]
, prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (DurationData -> Token) -> DurationData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Maybe Token) -> DurationData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Grain -> Int -> DurationData
duration Grain
TG.Minute Int
30
}
ruleDurationThreeQuartersOfAnHour :: Rule
ruleDurationThreeQuartersOfAnHour :: Rule
ruleDurationThreeQuartersOfAnHour = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"three-quarters of an hour"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(3/4\\s?h(our)?|three(\\s|-)quarters of an hour)"
]
, prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (DurationData -> Token) -> DurationData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Maybe Token) -> DurationData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Grain -> Int -> DurationData
duration Grain
TG.Minute Int
45
}
ruleDurationFortnight :: Rule
ruleDurationFortnight :: Rule
ruleDurationFortnight = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"fortnight"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(a|one)? fortnight"
]
, prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (DurationData -> Token) -> DurationData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Maybe Token) -> DurationData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Grain -> Int -> DurationData
duration Grain
TG.Day Int
14
}
ruleNumeralQuotes :: Rule
ruleNumeralQuotes :: Rule
ruleNumeralQuotes = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<integer> + '\""
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isNatural
, String -> PatternItem
regex String
"(['\"])"
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = v}:
Token Dimension a
RegexMatch (GroupMatch (x:_)):
[Token]
_) -> case Text
x of
Text
"'" -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token) -> (Int -> Token) -> Int -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token) -> (Int -> DurationData) -> Int -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Grain -> Int -> DurationData
duration Grain
TG.Minute (Int -> Maybe Token) -> Int -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
v
Text
"\"" -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token) -> (Int -> Token) -> Int -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token) -> (Int -> DurationData) -> Int -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Grain -> Int -> DurationData
duration Grain
TG.Second (Int -> Maybe Token) -> Int -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
v
Text
_ -> Maybe Token
forall a. Maybe a
Nothing
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationNumeralMore :: Rule
ruleDurationNumeralMore :: Rule
ruleDurationNumeralMore = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<integer> more <unit-of-duration>"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isNatural
, String -> PatternItem
regex String
"more|additional|extra|less|fewer"
, Dimension Grain -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension Grain
TimeGrain
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral a
nd:Token
_:Token Dimension a
TimeGrain a
grain:[Token]
_) ->
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (Double -> Token) -> Double -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token)
-> (Double -> DurationData) -> Double -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Grain -> Int -> DurationData
duration a
Grain
grain (Int -> DurationData) -> (Double -> Int) -> Double -> DurationData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ NumeralData -> Double
TNumeral.value a
NumeralData
nd
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationDotNumeralHours :: Rule
ruleDurationDotNumeralHours :: Rule
ruleDurationDotNumeralHours = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number.number hours"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(\\d+)\\.(\\d+)"
, Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ Grain -> Predicate
isGrain Grain
TG.Hour
]
, prod :: Production
prod = \case
(Token Dimension a
RegexMatch (GroupMatch (h:m:_)):[Token]
_) -> do
Integer
hh <- Text -> Maybe Integer
parseInteger Text
h
Integer
mnum <- Text -> Maybe Integer
parseInteger Text
m
let mden :: Integer
mden = Integer
10 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Text -> Int
Text.length Text
m
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (DurationData -> Token) -> DurationData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Maybe Token) -> DurationData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> Integer -> DurationData
minutesFromHourMixedFraction Integer
hh Integer
mnum Integer
mden
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationAndHalfHour :: Rule
ruleDurationAndHalfHour :: Rule
ruleDurationAndHalfHour = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<integer> and an half hour"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isNatural
, String -> PatternItem
regex String
"and (an? )?half hours?"
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = v}:[Token]
_) ->
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token) -> (Int -> Token) -> Int -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token) -> (Int -> DurationData) -> Int -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Grain -> Int -> DurationData
duration Grain
TG.Minute (Int -> Maybe Token) -> Int -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Int
30 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
60 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
v
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationAndHalfMinute :: Rule
ruleDurationAndHalfMinute :: Rule
ruleDurationAndHalfMinute = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<integer> and a half minutes"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isNatural
, String -> PatternItem
regex String
"and (an? )?half min(ute)?s?"
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = v}:[Token]
_) ->
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token) -> (Int -> Token) -> Int -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token) -> (Int -> DurationData) -> Int -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Grain -> Int -> DurationData
duration Grain
TG.Second (Int -> Maybe Token) -> Int -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Int
30 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
60 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
v
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationA :: Rule
ruleDurationA :: Rule
ruleDurationA = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"a <unit-of-duration>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"an?"
, Dimension Grain -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension Grain
TimeGrain
]
, prod :: Production
prod = \case
(Token
_:Token Dimension a
TimeGrain a
grain:[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (DurationData -> Token) -> DurationData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Maybe Token) -> DurationData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Grain -> Int -> DurationData
duration a
Grain
grain Int
1
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationHalfATimeGrain :: Rule
ruleDurationHalfATimeGrain :: Rule
ruleDurationHalfATimeGrain = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"half a <time-grain>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(1/2|half)( an?)?"
, Dimension Grain -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension Grain
TimeGrain
]
, prod :: Production
prod = \case
(Token
_:Token Dimension a
TimeGrain a
grain:[Token]
_) -> Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token) -> Maybe DurationData -> Maybe Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Grain -> Int -> Maybe DurationData
nPlusOneHalf a
Grain
grain Int
0
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationOneGrainAndHalf :: Rule
ruleDurationOneGrainAndHalf :: Rule
ruleDurationOneGrainAndHalf = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"a <unit-of-duration> and a half"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"an?|one"
, Dimension Grain -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension Grain
TimeGrain
, String -> PatternItem
regex String
"and (a )?half"
]
, prod :: Production
prod = \case
(Token
_:Token Dimension a
TimeGrain a
grain:[Token]
_) -> Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token) -> Maybe DurationData -> Maybe Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Grain -> Int -> Maybe DurationData
nPlusOneHalf a
Grain
grain Int
1
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationHoursAndMinutes :: Rule
ruleDurationHoursAndMinutes :: Rule
ruleDurationHoursAndMinutes = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<integer> hour and <integer>"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isNatural
, String -> PatternItem
regex String
"hours?( and)?"
, Predicate -> PatternItem
Predicate Predicate
isNatural
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral a
h:
Token
_:
Token Dimension a
Numeral a
m:
[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token) -> (Int -> Token) -> Int -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token) -> (Int -> DurationData) -> Int -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Grain -> Int -> DurationData
duration Grain
TG.Minute (Int -> Maybe Token) -> Int -> Maybe Token
forall a b. (a -> b) -> a -> b
$
(Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (Double -> Int) -> Double -> Int
forall a b. (a -> b) -> a -> b
$ NumeralData -> Double
TNumeral.value a
NumeralData
m) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
60 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor (NumeralData -> Double
TNumeral.value a
NumeralData
h)
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationPrecision :: Rule
ruleDurationPrecision :: Rule
ruleDurationPrecision = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"about|exactly <duration>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(about|around|approximately|exactly)"
, Dimension DurationData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension DurationData
Duration
]
, prod :: Production
prod = \case
(Token
_:Token
token:[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just Token
token
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleCompositeDurationCommasAnd :: Rule
ruleCompositeDurationCommasAnd :: Rule
ruleCompositeDurationCommasAnd = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"composite <duration> (with ,/and)"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isNatural
, Dimension Grain -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension Grain
TimeGrain
, String -> PatternItem
regex String
",|and"
, Dimension DurationData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension DurationData
Duration
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = v}:
Token Dimension a
TimeGrain a
g:
Token
_:
Token Dimension a
Duration dd :: a
dd@DurationData{TDuration.grain = dg}:
[Token]
_) | a
g a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
Grain
dg -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (DurationData -> Token) -> DurationData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Maybe Token) -> DurationData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Grain -> Int -> DurationData
duration a
Grain
g (Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
v) DurationData -> DurationData -> DurationData
forall a. Semigroup a => a -> a -> a
<> a
DurationData
dd
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleCompositeDuration :: Rule
ruleCompositeDuration :: Rule
ruleCompositeDuration = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"composite <duration>"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isNatural
, Dimension Grain -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension Grain
TimeGrain
, Dimension DurationData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension DurationData
Duration
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = v}:
Token Dimension a
TimeGrain a
g:
Token Dimension a
Duration dd :: a
dd@DurationData{TDuration.grain = dg}:
[Token]
_) | a
g a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
Grain
dg -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (DurationData -> Token) -> DurationData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Maybe Token) -> DurationData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Grain -> Int -> DurationData
duration a
Grain
g (Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
v) DurationData -> DurationData -> DurationData
forall a. Semigroup a => a -> a -> a
<> a
DurationData
dd
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleCompositeDurationAnd :: Rule
ruleCompositeDurationAnd :: Rule
ruleCompositeDurationAnd = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"composite <duration> and <duration>"
, pattern :: Pattern
pattern =
[ Dimension DurationData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension DurationData
Duration
, String -> PatternItem
regex String
",|and"
, Dimension DurationData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension DurationData
Duration
]
, prod :: Production
prod = \case
(Token Dimension a
Duration DurationData{TDuration.value = v, TDuration.grain = g}:
Token
_:
Token Dimension a
Duration dd :: a
dd@DurationData{TDuration.grain = dg}:
[Token]
_) | Grain
g Grain -> Grain -> Bool
forall a. Ord a => a -> a -> Bool
> Grain
dg -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (DurationData -> Token) -> DurationData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Maybe Token) -> DurationData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Grain -> Int -> DurationData
duration Grain
g (Int
v) DurationData -> DurationData -> DurationData
forall a. Semigroup a => a -> a -> a
<> a
DurationData
dd
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationDotNumeralMinutes :: Rule
ruleDurationDotNumeralMinutes :: Rule
ruleDurationDotNumeralMinutes = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number.number minutes"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(\\d+)\\.(\\d+)"
, Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ Grain -> Predicate
isGrain Grain
TG.Minute
]
, prod :: Production
prod = \case
(Token Dimension a
RegexMatch (GroupMatch (m:s:_)):[Token]
_) -> do
Integer
mm <- Text -> Maybe Integer
parseInteger Text
m
Integer
ss <- Text -> Maybe Integer
parseInteger Text
s
let sden :: Integer
sden = Integer
10 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Text -> Int
Text.length Text
s
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token) -> Token -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token) -> DurationData -> Token
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> Integer -> DurationData
secondsFromHourMixedFraction Integer
mm Integer
ss Integer
sden
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDurationNumeralAndQuarterHour :: Rule
ruleDurationNumeralAndQuarterHour :: Rule
ruleDurationNumeralAndQuarterHour = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<Integer> and <Integer> quarter of hour"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isNatural
, String -> PatternItem
regex String
"and (a |an |one |two |three )?quarters?( of)?( an)?"
, Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ Grain -> Predicate
isGrain Grain
TG.Hour
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = h}:
Token Dimension a
RegexMatch (GroupMatch (match:_)):
[Token]
_) -> do
Int
q <- case Text -> Text
Text.strip (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
Text.toLower Text
match of Text
"a" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1
Text
"an" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1
Text
"one" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1
Text
"two" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
2
Text
"three" -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
3
Text
_ -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token) -> (Int -> Token) -> Int -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension DurationData -> DurationData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension DurationData
Duration (DurationData -> Token) -> (Int -> DurationData) -> Int -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Grain -> Int -> DurationData
duration Grain
TG.Minute (Int -> Maybe Token) -> Int -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Int
15 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
q Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
60 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Double -> Int
forall a b. (RealFrac a, Integral b) => a -> b
floor Double
h
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
rules :: [Rule]
rules :: [Rule]
rules =
[ Rule
ruleCompositeDurationCommasAnd
, Rule
ruleDurationQuarterOfAnHour
, Rule
ruleDurationHalfAnHourAbbrev
, Rule
ruleDurationThreeQuartersOfAnHour
, Rule
ruleDurationFortnight
, Rule
ruleDurationNumeralMore
, Rule
ruleDurationDotNumeralHours
, Rule
ruleDurationAndHalfHour
, Rule
ruleDurationAndHalfMinute
, Rule
ruleDurationA
, Rule
ruleDurationHalfATimeGrain
, Rule
ruleDurationOneGrainAndHalf
, Rule
ruleDurationHoursAndMinutes
, Rule
ruleDurationPrecision
, Rule
ruleNumeralQuotes
, Rule
ruleCompositeDuration
, Rule
ruleCompositeDurationAnd
, Rule
ruleCompositeDurationCommasAnd
, Rule
ruleDurationDotNumeralMinutes
, Rule
ruleDurationNumeralAndQuarterHour
]