{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoRebindableSyntax #-}
{-# LANGUAGE LambdaCase #-}
module Duckling.Numeral.BN.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
bengaliMap :: HashMap Char Char
bengaliMap :: HashMap Char Char
bengaliMap = [(Char, Char)] -> HashMap Char Char
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ ( Char
'০', Char
'0' )
, ( Char
'১', Char
'1' )
, ( Char
'২', Char
'2' )
, ( Char
'৩', Char
'3' )
, ( Char
'৪', Char
'4' )
, ( Char
'৫', Char
'5' )
, ( Char
'৬', Char
'6' )
, ( Char
'৭', Char
'7' )
, ( Char
'৮', Char
'8' )
, ( Char
'৯', Char
'9' )
]
bengaliToArab :: Char -> Char
bengaliToArab :: Char -> Char
bengaliToArab Char
c = Char -> Char -> HashMap Char Char -> Char
forall k v. (Eq k, Hashable k) => v -> k -> HashMap k v -> v
HashMap.lookupDefault Char
c Char
c HashMap Char Char
bengaliMap
ruleBengali :: Rule
ruleBengali :: Rule
ruleBengali = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"bengali forms"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"([০১২৩৪৫৬৭৮৯]{1,18})"
]
, prod :: Production
prod = \case
(Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) ->
Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Maybe Int -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe Int
parseInt ((Char -> Char) -> Text -> Text
Text.map Char -> Char
bengaliToArab Text
match) 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
}
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 = \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
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 = \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
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 = \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
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..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
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
}
rules :: [Rule]
rules :: [Rule]
rules =
[ Rule
ruleBengali
, Rule
ruleNumeral
, Rule
ruleElevenToNineteen
, Rule
ruleTwentyoneToTwentynine
, Rule
ruleTens
]