-- 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 OverloadedStrings #-}
{-# LANGUAGE NoRebindableSyntax #-}

module Duckling.Numeral.HI.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.Numeral.Types (NumeralData (..))
import Duckling.Regex.Types
import Duckling.Types
import qualified Duckling.Numeral.Types as TNumeral

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

devanagariToArab :: Char -> Char
devanagariToArab :: Char -> Char
devanagariToArab 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
devanagariMap

ruleDevanagari :: Rule
ruleDevanagari :: Rule
ruleDevanagari = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"devanagari forms"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"([०१२३४५६७८९]{1,18})"
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (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
devanagariToArab 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
  }

ruleZeroToNinetyNineMap :: HashMap Text Integer
ruleZeroToNinetyNineMap :: HashMap Text Integer
ruleZeroToNinetyNineMap = [(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
6 )
  , ( Text
"सात", Integer
7 )
  , ( Text
"आठ", Integer
8 )
  , ( Text
"नौ", Integer
9 )
  , ( 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 )
  , ( 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 )
  , ( Text
"इकतीस", Integer
31 )
  , ( Text
"इकत्तीस", Integer
31 )
  , ( Text
"बत्तीस", Integer
32 )
  , ( Text
"तैंतीस", Integer
33 )
  , ( Text
"चौंतीस", Integer
34 )
  , ( Text
"पैंतीस", Integer
35 )
  , ( Text
"छत्तीस", Integer
36 )
  , ( Text
"सैंतीस", Integer
37 )
  , ( Text
"अड़तीस", Integer
38 )
  , ( Text
"उनतालीस", Integer
39 )
  , ( Text
"इकतालीस", Integer
41 )
  , ( Text
"बयालीस", Integer
42 )
  , ( Text
"तैंतालीस", Integer
43 )
  , ( Text
"चौवालीस", Integer
44 )
  , ( Text
"पैंतालीस", Integer
45 )
  , ( Text
"छियालीस", Integer
46 )
  , ( Text
"सैंतालीस", Integer
47 )
  , ( Text
"अड़तालीस", Integer
48 )
  , ( Text
"उनचास", Integer
49 )
  , ( Text
"इक्यावन", Integer
51 )
  , ( Text
"बावन", Integer
52 )
  , ( Text
"तिरेपन", Integer
53 )
  , ( Text
"चौवन", Integer
54 )
  , ( Text
"पचपन", Integer
55 )
  , ( Text
"छप्पन", Integer
56 )
  , ( Text
"सत्तावन", Integer
57 )
  , ( Text
"अट्ठावन", Integer
58 )
  , ( Text
"उनसठ", Integer
59 )
  , ( Text
"इकसठ", Integer
61 )
  , ( Text
"बासठ", Integer
62 )
  , ( Text
"तिरेसठ", Integer
63 )
  , ( Text
"चौंसठ", Integer
64 )
  , ( Text
"पैंसठ", Integer
65 )
  , ( Text
"छियासठ", Integer
66 )
  , ( Text
"सड़सठ", Integer
67 )
  , ( Text
"अड़सठ", Integer
68 )
  , ( Text
"उनहत्तर", Integer
69 )
  , ( Text
"इकहत्तर", Integer
71 )
  , ( Text
"बहत्तर", Integer
72 )
  , ( Text
"तिहत्तर", Integer
73 )
  , ( Text
"चौहत्तर", Integer
74 )
  , ( Text
"पचहत्तर", Integer
75 )
  , ( Text
"छिहत्तर", Integer
76 )
  , ( Text
"सतहत्तर", Integer
77 )
  , ( Text
"अठहत्तर", Integer
78 )
  , ( Text
"उनासी", Integer
79 )
  , ( Text
"इक्यासी", Integer
81 )
  , ( Text
"बयासी", Integer
82 )
  , ( Text
"तिरासी", Integer
83 )
  , ( Text
"चौरासी", Integer
84 )
  , ( Text
"पचासी", Integer
85 )
  , ( Text
"छियासी", Integer
86 )
  , ( Text
"सतासी", Integer
87 )
  , ( Text
"अट्ठासी", Integer
88 )
  , ( Text
"नवासी", Integer
89 )
  , ( Text
"इक्यानवे", Integer
91 )
  , ( Text
"बानवे", Integer
92 )
  , ( Text
"तिरानवे", Integer
93 )
  , ( Text
"चौरानवे", Integer
94 )
  , ( Text
"पचानवे", Integer
95 )
  , ( Text
"छियानवे", Integer
96 )
  , ( Text
"सत्तानवे", Integer
97 )
  , ( Text
"अट्ठानवे", Integer
98 )
  , ( Text
"निन्यानवे", Integer
99 )
  ]

ruleZeroToNinetyNine :: Rule
ruleZeroToNinetyNine :: Rule
ruleZeroToNinetyNine = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"number (0..99)"
  , 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
match HashMap Text Integer
ruleZeroToNinetyNineMap 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
10)
  , ( 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
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
  }

rulePowersOfTen :: Rule
rulePowersOfTen :: Rule
rulePowersOfTen = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"powers of tens"
  , pattern :: Pattern
pattern = [String -> PatternItem
regex String
"(सौ|हज़ार|हज़ार)"]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
RegexMatch (GroupMatch (match : _)) : [Token]
_) ->
        case Text -> Text
Text.toLower Text
match of
          Text
"सौ" -> Double -> Maybe Token
double Double
1e2 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Token -> Maybe Token
withGrain Int
2 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable
          Text
"हज़ार" -> Double -> Maybe Token
double Double
1e3 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Token -> Maybe Token
withGrain Int
3 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable
          Text
"हज़ार" -> Double -> Maybe Token
double Double
1e3 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Token -> Maybe Token
withGrain Int
3 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable
          Text
_ -> Maybe Token
forall a. Maybe a
Nothing
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleMultiply :: Rule
ruleMultiply :: Rule
ruleMultiply = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"compose by multiplication"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    , Predicate -> PatternItem
Predicate Predicate
isMultipliable
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token
token1:Token
token2:[Token]
_) -> Token -> Token -> Maybe Token
multiply Token
token1 Token
token2
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleCompositeHundreds :: Rule
ruleCompositeHundreds :: Rule
ruleCompositeHundreds = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"integer 100s.."
  , pattern :: Pattern
pattern =
    [ [Double] -> PatternItem
oneOf [Double
100,Double
200..Double
5000]
    , String -> PatternItem
regex String
"[\\s\\-]+"
    , Double -> Double -> PatternItem
numberBetween Double
1 Double
99
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
Numeral NumeralData{TNumeral.value = hundreds}:
       Token
_:
       Token Dimension a
Numeral NumeralData{TNumeral.value = remaining}:
       [Token]
_) -> Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
hundreds Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
remaining
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }


ruleCompositeThousands :: Rule
ruleCompositeThousands :: Rule
ruleCompositeThousands = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"integer 100s.."
  , pattern :: Pattern
pattern =
    [ [Double] -> PatternItem
oneOf [Double
1000,Double
2000..Double
50000]
    , String -> PatternItem
regex String
"[\\s\\-]+"
    , Double -> Double -> PatternItem
numberBetween Double
1 Double
999
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
Numeral NumeralData{TNumeral.value = thousands}:
       Token
_:
       Token Dimension a
Numeral NumeralData{TNumeral.value = remaining}:
       [Token]
_) -> Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
thousands Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
remaining
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

rules :: [Rule]
rules :: [Rule]
rules =
  [ Rule
ruleDevanagari
  , Rule
ruleZeroToNinetyNine
  , Rule
ruleTens
  , Rule
rulePowersOfTen
  , Rule
ruleMultiply
  , Rule
ruleCompositeHundreds
  , Rule
ruleCompositeThousands
  ]