-- 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.FI.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

ruleIntegerNumeric :: Rule
ruleIntegerNumeric :: Rule
ruleIntegerNumeric = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"integer (numeric)"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(\\d{1,18})"
    ]
  , prod :: Production
prod = \case
      (Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) -> do
        Int
v <- Text -> Maybe Int
parseInt Text
match
        Integer -> Maybe Token
integer (Integer -> Maybe Token) -> Integer -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
v
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

numeralMap :: HashMap Text Integer
numeralMap :: HashMap Text Integer
numeralMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
  [ ( Text
"nolla", Integer
0 )
  , ( Text
"yksi", Integer
1 )
  , ( Text
"kaksi", Integer
2 )
  , ( Text
"kolme", Integer
3 )
  , ( Text
"neljä", Integer
4 )
  , ( Text
"viisi", Integer
5 )
  , ( Text
"kuusi", Integer
6 )
  , ( Text
"seitsemän", Integer
7 )
  , ( Text
"kahdeksan", Integer
8 )
  , ( Text
"yhdeksän", Integer
9 )
  , ( Text
"kymmenen", 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
"(nolla|yksi|kaksi|kolme|neljä|viisi|kuusi|seitsemän|kahdeksan|yhdeksän|kymmenen)"
    ]
  , 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
numeralMap 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
"yksitoista", Integer
11 )
  , ( Text
"kaksitoista", Integer
12 )
  , ( Text
"kolmetoista", Integer
13 )
  , ( Text
"neljätoista", Integer
14 )
  , ( Text
"viisitoista", Integer
15 )
  , ( Text
"kuusitoista", Integer
16 )
  , ( Text
"seitsemäntoista", Integer
17 )
  , ( Text
"kahdeksantoista", Integer
18 )
  , ( Text
"yhdeksäntoista", 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
"(yksitoista|kaksitoista|kolmetoista|neljätoista|viisitoista|kuusitoista|seitsemäntoista|kahdeksantoista|yhdeksäntoista)"
    ]
  , 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
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
  }

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
"kaksikymmentä", Integer
20 )
  , ( Text
"kolmekymmentä", Integer
30 )
  , ( Text
"neljäkymmentä", Integer
40 )
  , ( Text
"viisikymmentä", Integer
50 )
  , ( Text
"kuusikymmentä", Integer
60 )
  , ( Text
"seitsemänkymmentä", Integer
70 )
  , ( Text
"kahdeksankymmentä", Integer
80 )
  , ( Text
"yhdeksänkymmentä", 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
"(kaksikymmentä|kolmekymmentä|neljäkymmentä|viisikymmentä|kuusikymmentä|seitsemänkymmentä|kahdeksankymmentä|yhdeksänkymmentä)"
    ]
  , 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
"(kaksikymmentä|kolmekymmentä|neljäkymmentä|viisikymmentä|kuusikymmentä|seitsemänkymmentä|kahdeksankymmentä|yhdeksänkymmentä)(yksi|kaksi|kolme|neljä|viisi|kuusi|seitsemän|kahdeksan|yhdeksän)"
    ]
  , 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
numeralMap
        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
ruleIntegerNumeric
  , Rule
ruleNumeral
  , Rule
ruleElevenToNineteen
  , Rule
ruleTens
  , Rule
ruleCompositeTens
  ]