{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Duckling.Quantity.KM.Rules
( rules
) where
import Data.HashMap.Strict (HashMap)
import Data.Text (Text, toLower)
import Data.String
import Prelude
import qualified Data.HashMap.Strict as HashMap
import qualified Data.Text as Text
import Duckling.Dimensions.Types
import Duckling.Numeral.Helpers
import Duckling.Quantity.Helpers
import Duckling.Regex.Types
import Duckling.Types
import Duckling.Numeral.Types (NumeralData (..))
import Duckling.Quantity.Types (QuantityData(..))
import qualified Duckling.Numeral.Types as TNumeral
import qualified Duckling.Quantity.Types as TQuantity
ruleQuantityOfProduct :: Rule
ruleQuantityOfProduct :: Rule
ruleQuantityOfProduct = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<quantity> of product"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(មនុស្ស|បងប្អូន|សត្វ|ឆ្កែ|ឆ្មា|ដើមឈើ|ផ្កា|កុលាប|ផ្ទះ)"
, Dimension QuantityData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension QuantityData
Quantity
]
, prod :: Production
prod = \case
(Token Dimension a
RegexMatch (GroupMatch (match:_)):
Token Dimension a
Quantity a
qd:
[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (QuantityData -> Token) -> QuantityData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Maybe Token) -> QuantityData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Text -> QuantityData -> QuantityData
withProduct Text
match a
QuantityData
qd
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
unitsMap :: HashMap Text TQuantity.Unit
unitsMap :: HashMap Text Unit
unitsMap = [(Text, Unit)] -> HashMap Text Unit
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (Text
"ចាន", Unit
TQuantity.Bowl)
, (Text
"ពែង", Unit
TQuantity.Cup)
, (Text
"កែវ", Unit
TQuantity.Cup)
, (Text
"ថូ", Unit
TQuantity.Pint)
, (Text
"ស្លាបព្រា", Unit
TQuantity.Tablespoon)
, (Text
"ស្លាបព្រាបាយ", Unit
TQuantity.Tablespoon)
, (Text
"ស្លាបព្រាកាហ្វេ", Unit
TQuantity.Teaspoon)
, (Text
"នាក់", Text -> Unit
TQuantity.Custom Text
"For Persons")
, (Text
"ក្បាល", Text -> Unit
TQuantity.Custom Text
"For Animals")
, (Text
"ដើម", Text -> Unit
TQuantity.Custom Text
"For Trees")
, (Text
"ទង", Text -> Unit
TQuantity.Custom Text
"For Flowers")
, (Text
"ខ្នង", Text -> Unit
TQuantity.Custom Text
"For Buildings")
, (Text
"គ្រឿង", Text -> Unit
TQuantity.Custom Text
"For Vehicles/Devices")
, (Text
"កញ្ចប់", Text -> Unit
TQuantity.Custom Text
"For Packages")
, (Text
"ឈុត", Text -> Unit
TQuantity.Custom Text
"Sets")
]
ruleNumeralUnits :: Rule
ruleNumeralUnits :: Rule
ruleNumeralUnits = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<number><units>"
, pattern :: Pattern
pattern =
[ Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral
, String -> PatternItem
regex String
"(ចាន|ពែង|កែវ|ថូ|ស្លាបព្រា|ស្លាបព្រាបាយ|ស្លាបព្រាកាហ្វេ|នាក់|ក្បាល|ដើម|ទង|ខ្នង|គ្រឿង|កញ្ចប់|ឈុត)"
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = v}:
Token Dimension a
RegexMatch (GroupMatch (match:_)):
[Token]
_) -> do
Unit
unit <- Text -> HashMap Text Unit -> Maybe Unit
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
match HashMap Text Unit
unitsMap
Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (QuantityData -> Token) -> QuantityData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Maybe Token) -> QuantityData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> Double -> QuantityData
quantity Unit
unit Double
v
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
quantities :: [(Text, String, TQuantity.Unit)]
quantities :: [(Text, String, Unit)]
quantities =
[ (Text
"<quantity> grams", String
"((មីលី|គីឡូ)?ក្រាម)", Unit
TQuantity.Gram)
]
opsMap :: HashMap Text (Double -> Double)
opsMap :: HashMap Text (Double -> Double)
opsMap = [(Text, Double -> Double)] -> HashMap Text (Double -> Double)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ ( Text
"មីលីក្រាម" , (Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
1000))
, ( Text
"គីឡូក្រាម" , (Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1000))
]
ruleNumeralUnits2 :: [Rule]
ruleNumeralUnits2 :: [Rule]
ruleNumeralUnits2 = ((Text, String, Unit) -> Rule) -> [(Text, String, Unit)] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map (Text, String, Unit) -> Rule
go [(Text, String, Unit)]
quantities
where
go :: (Text, String, TQuantity.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 = [Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral, String -> PatternItem
regex String
regexPattern]
, prod :: Production
prod = \case
(Token Dimension a
Numeral a
nd:
Token Dimension a
RegexMatch (GroupMatch (match:_)):
[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (QuantityData -> Token) -> QuantityData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Maybe Token) -> QuantityData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> Double -> QuantityData
quantity Unit
u Double
value
where value :: Double
value = HashMap Text (Double -> Double) -> Text -> Double -> Double
getValue HashMap Text (Double -> Double)
opsMap Text
match (Double -> Double) -> Double -> Double
forall a b. (a -> b) -> a -> b
$ NumeralData -> Double
TNumeral.value a
NumeralData
nd
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
rulePrecision :: Rule
rulePrecision :: Rule
rulePrecision = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"about|exactly <quantity>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"\\~|ប្រហែល"
, Dimension QuantityData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension QuantityData
Quantity
]
, 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 <quantity>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"ចន្លោះ(ពី)?|ចាប់ពី"
, Predicate -> PatternItem
Predicate Predicate
isPositive
, String -> PatternItem
regex String
"និង|ដល់"
, Predicate -> PatternItem
Predicate Predicate
isSimpleQuantity
]
, prod :: Production
prod = \case
(Token
_:
Token Dimension a
Numeral NumeralData{TNumeral.value = from}:
Token
_:
Token Dimension a
Quantity QuantityData{TQuantity.value = Just to
, TQuantity.unit = Just u
, TQuantity.aproduct = Nothing}:
[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)
-> (QuantityData -> Token) -> QuantityData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Token)
-> (QuantityData -> QuantityData) -> QuantityData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Double) -> QuantityData -> QuantityData
withInterval (Double
from, Double
to) (QuantityData -> Maybe Token) -> QuantityData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> QuantityData
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 <quantity> to|and <quantity>"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"ចន្លោះ(ពី)?|ចាប់ពី"
, Predicate -> PatternItem
Predicate Predicate
isSimpleQuantity
, String -> PatternItem
regex String
"និង|ដល់"
, Predicate -> PatternItem
Predicate Predicate
isSimpleQuantity
]
, prod :: Production
prod = \case
(Token
_:
Token Dimension a
Quantity QuantityData{TQuantity.value = Just from
, TQuantity.unit = Just u1
, TQuantity.aproduct = Nothing}:
Token
_:
Token Dimension a
Quantity QuantityData{TQuantity.value = Just to
, TQuantity.unit = Just u2
, TQuantity.aproduct = Nothing}:
[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)
-> (QuantityData -> Token) -> QuantityData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Token)
-> (QuantityData -> QuantityData) -> QuantityData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Double) -> QuantityData -> QuantityData
withInterval (Double
from, Double
to) (QuantityData -> Maybe Token) -> QuantityData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> QuantityData
unitOnly Unit
u1
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleIntervalNumeralDash :: Rule
ruleIntervalNumeralDash :: Rule
ruleIntervalNumeralDash = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<numeral> - <quantity>"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isPositive
, String -> PatternItem
regex String
"\\-"
, Predicate -> PatternItem
Predicate Predicate
isSimpleQuantity
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = from}:
Token
_:
Token Dimension a
Quantity QuantityData{TQuantity.value = Just to
, TQuantity.unit = Just u
, TQuantity.aproduct = Nothing}:
[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)
-> (QuantityData -> Token) -> QuantityData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Token)
-> (QuantityData -> QuantityData) -> QuantityData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Double) -> QuantityData -> QuantityData
withInterval (Double
from, Double
to) (QuantityData -> Maybe Token) -> QuantityData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> QuantityData
unitOnly Unit
u
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleIntervalDash :: Rule
ruleIntervalDash :: Rule
ruleIntervalDash = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<quantity> - <quantity>"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isSimpleQuantity
, String -> PatternItem
regex String
"\\-"
, Predicate -> PatternItem
Predicate Predicate
isSimpleQuantity
]
, prod :: Production
prod = \case
(Token Dimension a
Quantity QuantityData{TQuantity.value = Just from
, TQuantity.unit = Just u1
, TQuantity.aproduct = Nothing}:
Token
_:
Token Dimension a
Quantity QuantityData{TQuantity.value = Just to
, TQuantity.unit = Just u2
, TQuantity.aproduct = Nothing}:
[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)
-> (QuantityData -> Token) -> QuantityData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Token)
-> (QuantityData -> QuantityData) -> QuantityData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Double) -> QuantityData -> QuantityData
withInterval (Double
from, Double
to) (QuantityData -> Maybe Token) -> QuantityData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> QuantityData
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
"Max Rule"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"ក្រោម|តិចជាង|មិនដល់|យ៉ាងច្រើន|មិនលើស"
, Predicate -> PatternItem
Predicate Predicate
isSimpleQuantity
]
, prod :: Production
prod = \case
(Token
_:
Token Dimension a
Quantity QuantityData{TQuantity.value = Just to
, TQuantity.unit = Just u
, TQuantity.aproduct = Nothing}:
[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (QuantityData -> Token) -> QuantityData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Token)
-> (QuantityData -> QuantityData) -> QuantityData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> QuantityData -> QuantityData
withMax Double
to (QuantityData -> Maybe Token) -> QuantityData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> QuantityData
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
"Min Rule"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"លើស(ពី)?|មិនតិចជាង|លើ|ច្រើនជាង|យ៉ាងតិច|យ៉ាងហោច"
, Predicate -> PatternItem
Predicate Predicate
isSimpleQuantity
]
, prod :: Production
prod = \case
(Token
_:
Token Dimension a
Quantity QuantityData{TQuantity.value = Just from
, TQuantity.unit = Just u
, TQuantity.aproduct = Nothing}:
[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (QuantityData -> Token) -> QuantityData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension QuantityData -> QuantityData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension QuantityData
Quantity (QuantityData -> Token)
-> (QuantityData -> QuantityData) -> QuantityData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> QuantityData -> QuantityData
withMin Double
from (QuantityData -> Maybe Token) -> QuantityData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> QuantityData
unitOnly Unit
u
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
rules :: [Rule]
rules :: [Rule]
rules =
[ Rule
ruleNumeralUnits
, Rule
ruleQuantityOfProduct
, Rule
rulePrecision
, Rule
ruleIntervalBetweenNumeral
, Rule
ruleIntervalBetween
, Rule
ruleIntervalNumeralDash
, Rule
ruleIntervalDash
, Rule
ruleIntervalMax
, Rule
ruleIntervalMin
]
[Rule] -> [Rule] -> [Rule]
forall a. [a] -> [a] -> [a]
++[Rule]
ruleNumeralUnits2