{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoRebindableSyntax #-}
module Duckling.Numeral.LO.Rules
( rules
) where
import Data.HashMap.Strict (HashMap)
import Data.Maybe
import Data.String
import Data.Text (Text)
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.Numeral.Types (NumeralData (..))
import Duckling.Regex.Types
import Duckling.Types
import qualified Duckling.Numeral.Types as TNumeral
ruleNumeralMap :: HashMap Text Integer
ruleNumeralMap :: HashMap Text Integer
ruleNumeralMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ ( Text
"ສູນ", Integer
0 )
, ( Text
"ໜຶ່ງ", Integer
1 )
, ( Text
"ສອງ", Integer
2 )
, ( Text
"ສາມ", Integer
3 )
, ( Text
"ສີ່", Integer
4 )
, ( Text
"ຫ້າ", Integer
5)
, ( Text
"ຫົກ", Integer
6)
, ( Text
"ເຈັດ", Integer
7)
, ( Text
"ແປດ", Integer
8)
, ( Text
"ເກົ້າ", Integer
9)
, ( Text
"ສິບ", Integer
10)
]
ruleNumeral :: Rule
ruleNumeral :: Rule
ruleNumeral = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number (0..10)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(ສູນ|ໜຶ່ງ|ສອງ|ສາມ|ສີ່|ຫ້າ|ຫົກ|ເຈັດ|ແປດ|ເກົ້າ|ສິບ)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) ->
Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Integer
ruleNumeralMap Maybe Integer -> (Integer -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Integer -> Maybe Token
integer
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
elevenToNineteenMap :: HashMap Text Integer
elevenToNineteenMap :: HashMap Text Integer
elevenToNineteenMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ ( Text
"ສິບເອັດ", Integer
11 )
, ( Text
"ສິບສອງ", Integer
12 )
, ( Text
"ສິບສາມ", Integer
13 )
, ( Text
"ສິບສີ່", Integer
14 )
, ( Text
"ສິບຫ້າ", Integer
15 )
, ( Text
"ສິບຫົກ", Integer
16 )
, ( Text
"ສິບເຈັດ", Integer
17 )
, ( Text
"ສິບແປດ", Integer
18 )
, ( Text
"ສິບເກົ້າ", Integer
19 )
]
ruleElevenToNineteen :: Rule
ruleElevenToNineteen :: Rule
ruleElevenToNineteen = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number (11..19)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(ສິບເອັດ|ສິບສອງ|ສິບສາມ|ສິບສີ່|ສິບຫ້າ|ສິບຫົກ|ສິບເຈັດ|ສິບແປດ|ສິບເກົ້າ)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) ->
Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Integer
elevenToNineteenMap Maybe Integer -> (Integer -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Integer -> Maybe Token
integer
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
twentyoneToTwentynineMap :: HashMap Text Integer
twentyoneToTwentynineMap :: HashMap Text Integer
twentyoneToTwentynineMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ ( Text
"ຊາວເອັດ", Integer
21 )
, ( Text
"ຊາວສອງ", Integer
22 )
, ( Text
"ຊາວສາມ", Integer
23 )
, ( Text
"ຊາວສີ່", Integer
24 )
, ( Text
"ຊາວຫ້າ", Integer
25 )
, ( Text
"ຊາວຫົກ", Integer
26 )
, ( Text
"ຊາວເຈັດ", Integer
27 )
, ( Text
"ຊາວແປດ", Integer
28 )
, ( Text
"ຊາວເກົ້າ", Integer
29 )
]
ruleTwentyoneToTwentynine :: Rule
ruleTwentyoneToTwentynine :: Rule
ruleTwentyoneToTwentynine = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number (21..29)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(ຊາວເອັດ|ຊາວສອງ|ຊາວສາມ|ຊາວສີ່|ຊາວຫ້າ|ຊາວຫົກ|ຊາວເຈັດ|ຊາວແປດ|ຊາວເກົ້າ)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) ->
Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Integer
twentyoneToTwentynineMap Maybe Integer -> (Integer -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Integer -> Maybe Token
integer
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
tensMap :: HashMap Text Integer
tensMap :: HashMap Text Integer
tensMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ ( Text
"ຊາວ", Integer
20 )
, ( Text
"ສາມສິບ", Integer
30 )
, ( Text
"ສິບສີ່", Integer
40 )
, ( Text
"ຫ້າສິບ", Integer
50 )
, ( Text
"ຫົກສິບ", Integer
60 )
, ( Text
"ເຈັດສິບ", Integer
70 )
, ( Text
"ແປດສິບ", Integer
80 )
, ( Text
"ເກົ້າສິບ", Integer
90 )
]
ruleTens :: Rule
ruleTens :: Rule
ruleTens = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer (20,30..90)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(ຊາວ|ສາມສິບ|ສິບສີ່|ຫ້າສິບ|ຫົກສິບ|ເຈັດສິບ|ແປດສິບ|ເກົ້າສິບ)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) ->
Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Integer
tensMap Maybe Integer -> (Integer -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Integer -> Maybe Token
integer
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleCompositeTens :: Rule
ruleCompositeTens :: Rule
ruleCompositeTens = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer ([3-9][1-9])"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(ສາມສິບ|ສິບສີ່|ຫ້າສິບ|ຫົກສິບ|ເຈັດສິບ|ແປດສິບ|ເກົ້າສິບ)(ໜຶ່ງ|ສອງ|ສາມ|ສີ່|ຫ້າ|ຫົກ|ເຈັດ|ແປດ|ເກົ້າ)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (m1:m2:_)):[Token]
_) -> do
Integer
v1 <- Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
m1) HashMap Text Integer
tensMap
Integer
v2 <- Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
m2) HashMap Text Integer
ruleNumeralMap
Integer -> Maybe Token
integer (Integer -> Maybe Token) -> Integer -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Integer
v1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
v2
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
rules :: [Rule]
rules :: [Rule]
rules =
[ Rule
ruleNumeral
, Rule
ruleElevenToNineteen
, Rule
ruleTwentyoneToTwentynine
, Rule
ruleTens
, Rule
ruleCompositeTens
]