{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoRebindableSyntax #-}
{-# LANGUAGE LambdaCase #-}
module Duckling.Numeral.TA.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
zeroToNineMap :: HashMap Text Integer
zeroToNineMap :: HashMap Text Integer
zeroToNineMap = [(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 )
]
ruleZeroToNine :: Rule
ruleZeroToNine :: Rule
ruleZeroToNine = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer (0..9)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(பூஜ்ஜியம்|ஒன்று|இரண்டு|மூன்று|நான்கு|ஐந்து|ஆறு|ஏழு|எட்டு|ஒன்பது)"
]
, prod :: Production
prod = \case
(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
zeroToNineMap 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
}
tenToNineteenMap :: HashMap Text Integer
tenToNineteenMap :: HashMap Text Integer
tenToNineteenMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ ( Text
"பத்து", Integer
10 )
, ( 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 )
]
ruleTenToNineteen :: Rule
ruleTenToNineteen :: Rule
ruleTenToNineteen = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer (10..19)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(பத்து|பதினொன்று|பன்னிரண்டு|பதின்மூன்று|பதினான்கு|பதினைந்து|பதினாறு|பதினேழு|பதினெட்டு|பத்தொன்பது)"
]
, prod :: Production
prod = \case
(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
tenToNineteenMap 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
20 )
, ( Text
"முப்பது", Integer
30 )
, ( Text
"முப்பத்து", Integer
30 )
, ( Text
"நாற்பது", Integer
40 )
, ( Text
"நாற்பத்து", Integer
40 )
, ( Text
"ஐம்பது", Integer
50 )
, ( Text
"ஐம்பத்தி", Integer
50 )
, ( Text
"அறுபது", Integer
60 )
, ( Text
"அறுபத்", Integer
60 )
, ( Text
"எழுபது", Integer
70 )
, ( Text
"எழுபத்தி", Integer
70 )
, ( Text
"எண்பது", Integer
80 )
, ( Text
"எண்பத்", Integer
80 )
, ( Text
"தொண்ணூறு", Integer
90 )
, ( Text
"தொண்ணுற்று", Integer
90 )
]
ruleTens :: Rule
ruleTens :: Rule
ruleTens = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer (20..90)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(இருபது|முப்பது|நாற்பது|ஐம்பது|அறுபது|எழுபது|எண்பது|தொண்ணூறு)"
]
, prod :: Production
prod = \case
(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 ([2-9][1-9])"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(இருபத்தி|முப்பத்து|நாற்பத்து|ஐம்பத்தி|அறுபத்|எழுபத்தி|எண்பத்|தொண்ணுற்று)(ஒன்று|இரண்டு|மூன்று|நான்கு|ஐந்து|ஆறு|ஏழு|எட்டு|ஒன்பது)"
]
, prod :: Production
prod = \case
(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
zeroToNineMap
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
ruleZeroToNine
, Rule
ruleTenToNineteen
, Rule
ruleTens
, Rule
ruleCompositeTens
]