-- 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.SW.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
"sufuri", Integer
0 )
  , ( Text
"zero", Integer
0 )
  , ( Text
"moja", Integer
1 )
  , ( Text
"mbili", Integer
2 )
  , ( Text
"tatu", Integer
3 )
  , ( Text
"nne", Integer
4 )
  , ( Text
"tano", Integer
5)
  , ( Text
"sita", Integer
6)
  , ( Text
"saba", Integer
7)
  , ( Text
"nane", Integer
8)
  , ( Text
"tisa", Integer
9)
  , ( Text
"kumi", 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
"(sufuri|zero|moja|mbili|tatu|nne|tano|sita|saba|nane|tisa|kumi)"
    ]
  , 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
  }

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
"ishirini", Integer
20 )
  , ( Text
"thelathini", Integer
30 )
  , ( Text
"arubaini", Integer
40 )
  , ( Text
"arobaini", Integer
40 )
  , ( Text
"hamsini", Integer
50 )
  , ( Text
"sitini", Integer
60 )
  , ( Text
"sabini", Integer
70 )
  , ( Text
"themanini", Integer
80 )
  , ( Text
"tisini", 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
"(ishirini|thelathini|arubaini|arobaini|hamsini|sitini|sabini|themanini|tisini)"
    ]
  , 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 11..19 21..29 .. 91..99"
  , pattern :: Pattern
pattern = [[Double] -> PatternItem
oneOf [Double
20,Double
30..Double
90]
  , String -> PatternItem
regex String
"-?na-?"
  , Double -> Double -> PatternItem
numberBetween Double
1 Double
10]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
Numeral NumeralData{TNumeral.value = v1}:
       Token Dimension a
Numeral NumeralData{TNumeral.value = v2}:
       [Token]
_) -> Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
v1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
v2
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }


rules :: [Rule]
rules :: [Rule]
rules =
  [ Rule
ruleNumeral
  , Rule
ruleTens
  , Rule
ruleCompositeTens
  ]