content: -- 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. An additional grant
  -- of patent rights can be found in the PATENTS file in the same directory.
  
  
  {-# LANGUAGE GADTs #-}
  {-# LANGUAGE LambdaCase #-}
  {-# LANGUAGE OverloadedStrings #-}
  
  module Duckling.Numeral.SK.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
  
  ruleIntersectWithAnd :: Rule
  ruleIntersectWithAnd = Rule
    { name = "intersect (with and)"
    , pattern =
      [ Predicate hasGrain
      , regex "a"
      , Predicate $ and . sequence [not . isMultipliable, isPositive]
      ]
    , prod = \case
        (Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
         _:
         Token Numeral NumeralData{TNumeral.value = val2}:
         _) | (10 ** fromIntegral g) > val2 -> double $ val1 + val2
        _ -> Nothing
    }
  
  ruleNumeralsPrefixWithNegativeOrMinus :: Rule
  ruleNumeralsPrefixWithNegativeOrMinus = Rule
    { name = "numbers prefix with -, negative or minus"
    , pattern =
      [ regex "-|mínus|záporné"
      , Predicate isPositive
      ]
    , prod = \case
        (_:
         Token Numeral NumeralData{TNumeral.value = v}:
         _) -> double $ v * (-1)
        _ -> Nothing
    }
  
  ruleFew :: Rule
  ruleFew = Rule
    { name = "few"
    , pattern =
      [ regex "(zo)?pár"
      ]
    , prod = \_ -> integer 3
    }
  
  ruleDecimalWithThousandsSeparator :: Rule
  ruleDecimalWithThousandsSeparator = Rule
    { name = "decimal with thousands separator"
    , pattern =
      [ regex "(\\d+(\\.\\d\\d\\d)+\\,\\d+)"
      ]
    , prod = \case
        (Token RegexMatch (GroupMatch (match:_)):
         _) -> let fmt = Text.replace "," "." $ Text.replace "." Text.empty match
          in parseDouble fmt >>= double
        _ -> Nothing
    }
  
  ruleDecimalNumeral :: Rule
  ruleDecimalNumeral = Rule
    { name = "decimal number"
    , pattern =
      [ regex "(\\d*,\\d+)"
      ]
    , prod = \case
        (Token RegexMatch (GroupMatch (match:_)):
         _) -> parseDecimal False match
        _ -> Nothing
    }
  
  ruleIntegerCompositeTens :: Rule
  ruleIntegerCompositeTens = Rule
    { name = "integer 21..99"
    , pattern =
      [ oneOf [20, 30..90]
      , Predicate $ numberBetween 1 10
      ]
    , prod = \case
        (Token Numeral NumeralData{TNumeral.value = tens}:
         Token Numeral NumeralData{TNumeral.value = units}:
         _) -> double $ tens + units
        _ -> Nothing
    }
  
  ruleSingle :: Rule
  ruleSingle = Rule
    { name = "single"
    , pattern =
      [ regex "jed(en|no|na)"
      ]
    , prod = \_ -> integer 1 >>= withGrain 1
    }
  
  ruleSum :: Rule
  ruleSum = Rule
    { name = "intersect"
    , pattern =
      [ Predicate hasGrain
      , Predicate $ and . sequence [not . isMultipliable, isPositive]
      ]
    , prod = \case
        (Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
         Token Numeral NumeralData{TNumeral.value = val2}:
         _) | (10 ** fromIntegral g) > val2 -> double $ val1 + val2
        _ -> Nothing
    }
  
  ruleMultiply :: Rule
  ruleMultiply = Rule
    { name = "compose by multiplication"
    , pattern =
      [ dimension Numeral
      , Predicate isMultipliable
      ]
    , prod = \case
        (token1:token2:_) -> multiply token1 token2
        _ -> Nothing
    }
  
  ruleNumeralsSuffixesKMG :: Rule
  ruleNumeralsSuffixesKMG = Rule
    { name = "numbers suffixes (K, M, G)"
    , pattern =
      [ dimension Numeral
      , regex "([kmg])(?=[\\W\\$€]|$)"
      ]
    , prod = \case
        (Token Numeral NumeralData{TNumeral.value = v}:
         Token RegexMatch (GroupMatch (match:_)):
         _) -> case Text.toLower match of
           "k" -> double $ v * 1e3
           "m" -> double $ v * 1e6
           "g" -> double $ v * 1e9
           _   -> Nothing
        _ -> Nothing
    }
  
  rulePowersOfTen :: Rule
  rulePowersOfTen = Rule
    { name = "powers of tens"
    , pattern =
      [ regex "(sto(vky)?|tisíce?|milióny?)"
      ]
    , prod = \case
        (Token RegexMatch (GroupMatch (match:_)):_) -> case Text.toLower match of
          "sto"     -> double 1e2 >>= withGrain 2 >>= withMultipliable
          "stovky"  -> double 1e2 >>= withGrain 2 >>= withMultipliable
          "tisíc"   -> double 1e3 >>= withGrain 3 >>= withMultipliable
          "tisíce"  -> double 1e3 >>= withGrain 3 >>= withMultipliable
          "milión"  -> double 1e6 >>= withGrain 6 >>= withMultipliable
          "milióny" -> double 1e6 >>= withGrain 6 >>= withMultipliable
          _         -> Nothing
        _ -> Nothing
    }
  
  ruleCouple :: Rule
  ruleCouple = Rule
    { name = "couple, a pair"
    , pattern =
      [ regex "páry?"
      ]
    , prod = \_ -> integer 2
    }
  
  ruleDozen :: Rule
  ruleDozen = Rule
    { name = "dozen"
    , pattern =
      [ regex "tucet"
      ]
    , prod = \_ -> integer 12 >>= withGrain 1 >>= withMultipliable
    }
  
  zeroToNineteenMap :: HashMap Text Integer
  zeroToNineteenMap = HashMap.fromList
    [ ( "nula"    , 0 )
    , ( "jeden"   , 1 )
    , ( "jedna"   , 1 )
    , ( "jedno"   , 1 )
    , ( "dva"     , 2 )
    , ( "dve"     , 2 )
    , ( "tri"     , 3 )
    , ( "štyri"   , 4 )
    , ( "päť"     , 5 )
    , ( "šesť"    , 6 )
    , ( "sedem"   , 7 )
    , ( "osem"    , 8 )
    , ( "deväť"   , 9 )
    , ( "desať"   , 10 )
    , ( "jedenásť", 11 )
    , ( "dvanásť" , 12 )
    , ( "trinásť" , 13 )
    , ( "štrnásť" , 14 )
    , ( "pätnásť" , 15 )
    , ( "šestnásť", 16 )
    , ( "sedemnásť", 17 )
    , ( "osemnásť" , 18 )
    , ( "devätnásť", 19 )
    ]
  
  ruleInteger :: Rule
  ruleInteger = Rule
    { name = "integer (0..19)"
    -- e.g. jedenásť must be before jeden, otherwise jeden will always shadow jedenásť
    , pattern =
      [ regex "(nula|jed(enásť|en|na|no)|dv(anásť|a|e)|trinásť|tri|štrnásť|štyri|pätnásť|päť|šestnásť|šesť|sedemnásť|sedem|osemnásť|osem|devätnásť|deväť|desať)"
      ]
    , prod = \case
        (Token RegexMatch (GroupMatch (match:_)):_) ->
          HashMap.lookup (Text.toLower match) zeroToNineteenMap >>= integer
        _ -> Nothing
    }
  
  dozenMap :: HashMap Text Integer
  dozenMap = HashMap.fromList
    [ ( "dvadsať"        , 20)
    , ( "tridsať"        , 30)
    , ( "štyridsať"      , 40)
    , ( "päťdesiat"      , 50)
    , ( "šesťdesiat"     , 60)
    , ( "sedemdesiat"    , 70)
    , ( "osemdesiat"     , 80)
    , ( "devätdesiat"    , 90)
    ]
  
  ruleInteger2 :: Rule
  ruleInteger2 = Rule
    { name = "integer (20..90)"
    , pattern =
      [ regex "((dva|tri|štyri)dsať|(päť|šesť|sedem|osem|devät)desiat)"
      ]
    , prod = \case
        (Token RegexMatch (GroupMatch (match:_)):_) ->
          HashMap.lookup (Text.toLower match) dozenMap >>= integer
        _ -> Nothing
    }
  
  ruleNumeralDotNumeral :: Rule
  ruleNumeralDotNumeral = Rule
    { name = "number dot number"
    , pattern =
      [ dimension Numeral
      , regex "celá|celých|celé"
      , Predicate $ not . hasGrain
      ]
    , prod = \case
        (Token Numeral nd1:_:Token Numeral nd2:_) ->
          double $ TNumeral.value nd1 + decimalsToDouble (TNumeral.value nd2)
        _ -> Nothing
    }
  
  ruleIntegerWithThousandsSeparator :: Rule
  ruleIntegerWithThousandsSeparator = Rule
    { name = "integer with thousands separator ."
    , pattern =
      [ regex "(\\d{1,3}(\\.\\d\\d\\d){1,5})"
      ]
    , prod = \case
        (Token RegexMatch (GroupMatch (match:_)):
         _) -> let fmt = Text.replace "." Text.empty match
          in parseDouble fmt >>= double
        _ -> Nothing
    }
  
  rules :: [Rule]
  rules =
    [ ruleCouple
    , ruleDecimalNumeral
    , ruleDecimalWithThousandsSeparator
    , ruleDozen
    , ruleFew
    , ruleInteger
    , ruleInteger2
    , ruleIntegerCompositeTens
    , ruleIntegerWithThousandsSeparator
    , ruleSum
    , ruleIntersectWithAnd
    , ruleMultiply
    , ruleNumeralDotNumeral
    , ruleNumeralsPrefixWithNegativeOrMinus
    , ruleNumeralsSuffixesKMG
    , rulePowersOfTen
    , ruleSingle
    ]
  