{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Duckling.Volume.EN.Rules
( rules ) where
import Data.Text (Text)
import Prelude
import Data.String
import Duckling.Dimensions.Types
import Duckling.Types
import Duckling.Regex.Types
import Duckling.Volume.Helpers
import Duckling.Numeral.Helpers (isPositive)
import qualified Duckling.Volume.Types as TVolume
import qualified Duckling.Numeral.Types as TNumeral
volumes :: [(Text, String, TVolume.Unit)]
volumes :: [(Text, String, Unit)]
volumes = [ (Text
"<latent vol> ml", String
"m(l(s?)|illilit(er|re)s?)", Unit
TVolume.Millilitre)
, (Text
"<vol> hectoliters", String
"hectolit(er|re)s?", Unit
TVolume.Hectolitre)
, (Text
"<vol> liters", String
"l(it(er|re)s?)?", Unit
TVolume.Litre)
, (Text
"<latent vol> gallon", String
"gal((l?ons?)|s)?", Unit
TVolume.Gallon)
]
rulesVolumes :: [Rule]
rulesVolumes :: [Rule]
rulesVolumes = ((Text, String, Unit) -> Rule) -> [(Text, String, Unit)] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map (Text, String, Unit) -> Rule
go [(Text, String, Unit)]
volumes
where
go :: (Text, String, TVolume.Unit) -> Rule
go :: (Text, String, Unit) -> Rule
go (Text
name, String
regexPattern, Unit
u) = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
name
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
regexPattern
]
, prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
}
fractions :: [(Text, String, Double)]
fractions :: [(Text, String, Double)]
fractions = [ (Text
"one", String
"an? ", Double
1)
, (Text
"half", String
"half(-|(( of)?( a(n?))?))?", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2)
, (Text
"third", String
"third(-|(( of)?( a(n?))?))?", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
3)
, (Text
"fourth", String
"(quarter|fourth)(-|(( of)?( a(n?))?))?", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
4)
, (Text
"fifth", String
"fifth(-|(( of)?( a(n?))?))?", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
5)
, (Text
"tenth", String
"tenth(-|(( of)?( a(n?))?))?", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
10)
]
rulesFractionalVolume :: [Rule]
rulesFractionalVolume :: [Rule]
rulesFractionalVolume = ((Text, String, Double) -> Rule)
-> [(Text, String, Double)] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map (Text, String, Double) -> Rule
go [(Text, String, Double)]
fractions
where
go :: (Text, String, Double) -> Rule
go :: (Text, String, Double) -> Rule
go (Text
name, String
regexPattern, Double
f) = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
name
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
regexPattern
, Predicate -> PatternItem
Predicate Predicate
isUnitOnly
]
, prod :: Production
prod = \case
(Token
_:
Token Dimension a
Volume TVolume.VolumeData{TVolume.unit = Just u}:
[Token]
_) ->
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> Double -> VolumeData
volume Unit
u Double
f
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
rulePrecision :: Rule
rulePrecision :: Rule
rulePrecision = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"about <volume>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"\\~|exactly|precisely|about|approx(\\.|imately)?|close to|near( to)?|around|almost"
, Dimension VolumeData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension VolumeData
Volume
]
, 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
}
ruleIntervalBetweenNumeral :: Rule
ruleIntervalBetweenNumeral :: Rule
ruleIntervalBetweenNumeral = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"between|from <numeral> and|to <volume>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"between|from"
, Predicate -> PatternItem
Predicate Predicate
isPositive
, String -> PatternItem
regex String
"to|and"
, Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
]
, prod :: Production
prod = \case
(Token
_:
Token Dimension a
Numeral TNumeral.NumeralData{TNumeral.value = from}:
Token
_:
Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just to
, TVolume.unit = Just u}:
[Token]
_) | Double
from Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
to ->
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Double) -> VolumeData -> VolumeData
withInterval (Double
from, Double
to) (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleIntervalBetween :: Rule
ruleIntervalBetween :: Rule
ruleIntervalBetween = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"between|from <volume> to|and <volume>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"between|from"
, Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
, String -> PatternItem
regex String
"to|and"
, Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
]
, prod :: Production
prod = \case
(Token
_:
Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just from
, TVolume.unit = Just u1}:
Token
_:
Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just to
, TVolume.unit = Just u2}:
[Token]
_) | Double
from Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
to Bool -> Bool -> Bool
&& Unit
u1 Unit -> Unit -> Bool
forall a. Eq a => a -> a -> Bool
== Unit
u2 ->
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Double) -> VolumeData -> VolumeData
withInterval (Double
from, Double
to) (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u1
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleIntervalMax :: Rule
ruleIntervalMax :: Rule
ruleIntervalMax = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"at most <volume>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"under|below|at most|(less|lower|not? more) than"
, Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
]
, prod :: Production
prod = \case
(Token
_:
Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just to
, TVolume.unit = Just u}:
[Token]
_) ->
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> VolumeData -> VolumeData
withMax Double
to (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleIntervalMin :: Rule
ruleIntervalMin :: Rule
ruleIntervalMin = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"more than <volume>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"over|above|exceeding|beyond|at least|(more|larger|bigger|heavier) than"
, Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
]
, prod :: Production
prod = \case
(Token
_:
Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just from
, TVolume.unit = Just u}:
[Token]
_) ->
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> VolumeData -> VolumeData
withMin Double
from (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
rules :: [Rule]
rules :: [Rule]
rules = [ Rule
rulePrecision
, Rule
ruleIntervalBetweenNumeral
, Rule
ruleIntervalBetween
, Rule
ruleIntervalMax
, Rule
ruleIntervalMin
]
[Rule] -> [Rule] -> [Rule]
forall a. [a] -> [a] -> [a]
++ [Rule]
rulesVolumes
[Rule] -> [Rule] -> [Rule]
forall a. [a] -> [a] -> [a]
++ [Rule]
rulesFractionalVolume