-- Copyright (c) 2016-present, Facebook, Inc.
-- All rights reserved.
--
-- This source code is licensed under the BSD-style license found in the
-- LICENSE file in the root directory of this source tree.


{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoRebindableSyntax #-}

module Duckling.Numeral.TE.Rules
  ( rules
  ) where

import Data.HashMap.Strict (HashMap)
import Data.Maybe
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.Regex.Types
import Duckling.Types

teluguMap :: HashMap Char Char
teluguMap :: HashMap Char Char
teluguMap = [(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' )
  ]

teluguToArab :: Char -> Char
teluguToArab :: Char -> Char
teluguToArab 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
teluguMap

ruleTelugu :: Rule
ruleTelugu :: Rule
ruleTelugu = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"telugu 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
teluguToArab 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
  }

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
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
  }

hundredsMap :: HashMap Text Integer
hundredsMap :: HashMap Text Integer
hundredsMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
  [ ( Text
"వంద", Integer
100 )
  , ( Text
"వెయ్యి", Integer
1000 )
  , ( Text
"లక్ష", Integer
100000 )
  , ( Text
"కోటి", Integer
10000000 )
  ]

rulehundreds :: Rule
rulehundreds :: Rule
rulehundreds = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"integer (100,1000,100000,10000000)"
  , 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
hundredsMap 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
ruleTelugu
  , Rule
ruleZeroToNine
  , Rule
ruleTenToNineteen
  , Rule
ruleTens
  , Rule
rulehundreds
  ]