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.
  
  
  {-# LANGUAGE GADTs #-}
  {-# LANGUAGE OverloadedStrings #-}
  
  module Duckling.Numeral.RU.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
  
  tensMap :: HashMap Text Integer
  tensMap = HashMap.fromList
    [ ( "двадцать", 20)
    , ( "двадцати", 20)
    , ( "тридцать", 30)
    , ( "тридцати", 30)
    , ( "сорок", 40)
    , ( "сорока", 40)
    , ( "пятьдесят", 50)
    , ( "пятидесяти", 50)
    , ( "шестьдесят", 60)
    , ( "шестидесяти", 60)
    , ( "шестидесят", 60)
    , ( "семьдесят", 70)
    , ( "семидесяти", 70)
    , ( "семидесят", 70)
    , ( "восемьдесят", 80)
    , ( "восьмидесяти", 80)
    , ( "восьмидесят", 80)
    , ( "восемьдесяти", 80)
    , ( "девяносто", 90)
    , ( "девяноста", 90)
    ]
  
  ruleInteger5 :: Rule
  ruleInteger5 = Rule
    { name = "integer (20..90)"
    , pattern =
      [ regex "(двадцат(ь|и)|тридцат(ь|и)|сорока?|пят(ь|и)десяти?|шест(ь|и)десяти?|сем(ь|и)десяти?|вос(е|ь)м(ь|и)десяти?|девяност(о|а))"
      ]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match:_)):_) ->
          HashMap.lookup (Text.toLower match) tensMap >>= integer
        _ -> Nothing
    }
  
  ruleDecimalWithThousandsSeparator :: Rule
  ruleDecimalWithThousandsSeparator = Rule
    { name = "decimal with thousands separator"
    , pattern =
      [ regex "(\\d+(,\\d\\d\\d)+\\.\\d+)"
      ]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match:_)):_) ->
          parseDouble (Text.replace "," Text.empty match) >>= double
        _ -> Nothing
    }
  
  ruleDecimalNumeral :: Rule
  ruleDecimalNumeral = Rule
    { name = "decimal number"
    , pattern =
      [ regex "(\\d*\\.\\d+)"
      ]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match:_)):_) -> parseDecimal True match
        _ -> Nothing
    }
  
  ruleInteger3 :: Rule
  ruleInteger3 = Rule
    { name = "integer 2"
    , pattern =
      [ regex "(дв(а|е|ух|ое)|пар(а|у|очк(у|а)))"
      ]
    , prod = \_ -> integer 2
    }
  
  ruleDecimalOneAndAHalf :: Rule
  ruleDecimalOneAndAHalf = Rule
    { name = "decimal one and a half"
     , pattern =
      [ regex "(полтора|полторы|полутора)"
      ]
     , prod = \_ -> double 1.5
    }
  
  ruleIntegerAndAHalf :: Rule
  ruleIntegerAndAHalf = Rule
    { name = "<integer> and a half"
    , pattern =
      [ Predicate isNatural
      , regex "с половиной"
      ]
    , prod = \tokens -> case tokens of
        (Token Numeral NumeralData{TNumeral.value = v}:_) -> double $ v + 0.5
        _ -> Nothing
    }
  
  hundredsMap :: HashMap Text Integer
  hundredsMap = HashMap.fromList
    [ ( "сто", 100)
    , ( "двести", 200)
    , ( "триста", 300)
    , ( "четыреста", 400)
    , ( "пятьсот", 500)
    , ( "шестьсот", 600)
    , ( "семьсот", 700)
    , ( "восемьсот", 800)
    , ( "девятьсот", 900)
    ]
  
  ruleInteger6 :: Rule
  ruleInteger6 = Rule
    { name = "integer (100..900)"
    , pattern =
      [ regex "(сто|двести|триста|четыреста|пятьсот|шестьсот|семьсот|восемьсот|девятьсот)"
      ]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match:_)):_) ->
          HashMap.lookup (Text.toLower match) hundredsMap >>= integer
        _ -> Nothing
    }
  
  ruleNumeralsPrefixWithMinus :: Rule
  ruleNumeralsPrefixWithMinus = Rule
    { name = "numbers prefix with -, minus"
    , pattern =
      [ regex "-|минус"
      , Predicate isPositive
      ]
    , prod = \tokens -> case tokens of
        (_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
        _ -> Nothing
    }
  
  ruleNumeralsSuffixesKMG :: Rule
  ruleNumeralsSuffixesKMG = Rule
    { name = "numbers suffixes (K, M, G)"
    , pattern =
      [ dimension Numeral
      , regex "((к|м|г)|(К|М|Г))(?=[\\W\\$€]|$)"
      ]
    , prod = \tokens -> case tokens of
        (Token Numeral NumeralData{TNumeral.value = v}:
         Token RegexMatch (GroupMatch (match:_)):
         _) -> case Text.toLower match of
           "к" -> double $ v * 1e3
           "К" -> double $ v * 1e3
           "м" -> double $ v * 1e6
           "М" -> double $ v * 1e6
           "г" -> double $ v * 1e9
           "Г" -> double $ v * 1e9
           _   -> Nothing
        _ -> Nothing
    }
  
  ruleInteger7 :: Rule
  ruleInteger7 = Rule
    { name = "integer 21..99"
    , pattern =
      [ oneOf [70, 20, 60, 50, 40, 90, 30, 80]
      , Predicate $ numberBetween 1 10
      ]
    , prod = \tokens -> case tokens of
        (Token Numeral NumeralData{TNumeral.value = v1}:
         Token Numeral NumeralData{TNumeral.value = v2}:
         _) -> double $ v1 + v2
        _ -> Nothing
    }
  
  ruleInteger8 :: Rule
  ruleInteger8 = Rule
    { name = "integer 101..999"
    , pattern =
      [ oneOf [300, 600, 500, 100, 800, 200, 900, 700, 400]
      , Predicate $ numberBetween 1 100
      ]
    , prod = \tokens -> case tokens of
        (Token Numeral NumeralData{TNumeral.value = v1}:
         Token Numeral NumeralData{TNumeral.value = v2}:
         _) -> double $ v1 + v2
        _ -> Nothing
    }
  
  ruleInteger :: Rule
  ruleInteger = Rule
    { name = "integer 0"
    , pattern =
      [ regex "(ноль|нуля|нисколько)"
      ]
    , prod = \_ -> integer 0
    }
  
  threeToNineteenMap:: HashMap Text Integer
  threeToNineteenMap = HashMap.fromList
    [ ( "три", 3)
    , ( "четыре", 4)
    , ( "пять", 5)
    , ( "шесть", 6)
    , ( "семь", 7)
    , ( "восемь", 8)
    , ( "девять", 9)
    , ( "десять", 10)
    , ( "одиннадцать", 11)
    , ( "двенадцать", 12)
    , ( "тринадцать", 13)
    , ( "четырнадцать", 14)
    , ( "пятнадцать", 15)
    , ( "шестнадцать", 16)
    , ( "семнадцать", 17)
    , ( "восемнадцать", 18)
    , ( "девятнадцать", 19)
    ]
  
  threeToNineteenMapGenitive :: HashMap Text Integer
  threeToNineteenMapGenitive = HashMap.fromList
    [ ( "трех", 3)
    , ( "четырех", 4)
    , ( "пяти", 5)
    , ( "шести", 6)
    , ( "семи", 7)
    , ( "восьми", 8)
    , ( "девяти", 9)
    , ( "десяти", 10)
    , ( "одиннадцати", 11)
    , ( "двенадцати", 12)
    , ( "тринадцати", 13)
    , ( "четырнадцати", 14)
    , ( "пятнадцати", 15)
    , ( "шестнадцати", 16)
    , ( "семнадцати", 17)
    , ( "восемнадцати", 18)
    , ( "девятнадцати", 19)
    ]
  
  ruleInteger4 :: Rule
  ruleInteger4 = Rule
    { name = "integer (3..19)"
    , pattern =
      [ regex "(три|четырнадцать|четыре|пятнадцать|пять|шестнадцать|шесть|семнадцать|семь|восемнадцать|восемь|девятнадцать|девять|десять|одиннадцать|двенадцать|тринадцать)"
      ]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match:_)):_) ->
          HashMap.lookup (Text.toLower match) threeToNineteenMap >>= integer
        _ -> Nothing
    }
  
  ruleInteger4Genitive :: Rule
  ruleInteger4Genitive = Rule
    { name = "integer (3..19)"
    , pattern =
      [ regex "(трех|четырнадцати|четырех|пятнадцати|пяти|шестнадцати|шести|семнадцати|семи|восемнадцати|восьми|девятнадцати|девяти|десяти|одиннадцати|двенадцати|тринадцати)"
      ]
    , prod = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match:_)):_) ->
          HashMap.lookup (Text.toLower match) threeToNineteenMapGenitive >>= integer
        _ -> Nothing
    }
  
  ruleInteger2 :: Rule
  ruleInteger2 = Rule
    { name = "integer 1"
    , pattern =
      [ regex "(один|одна|одну)"
      ]
    , prod = \_ -> integer 1
    }
  
  ruleNumeralDotNumeral :: Rule
  ruleNumeralDotNumeral = Rule
    { name = "number dot number"
    , pattern =
      [ dimension Numeral
      , regex "точка"
      , Predicate $ not . hasGrain
      ]
    , prod = \tokens -> case tokens of
        (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 = \tokens -> case tokens of
        (Token RegexMatch (GroupMatch (match:_)):_) ->
          parseDouble (Text.replace "," Text.empty match) >>= double
        _ -> Nothing
    }
  
  rules :: [Rule]
  rules =
    [ ruleDecimalNumeral
    , ruleDecimalWithThousandsSeparator
    , ruleInteger
    , ruleInteger2
    , ruleInteger3
    , ruleInteger4
    , ruleInteger4Genitive
    , ruleInteger5
    , ruleInteger6
    , ruleInteger7
    , ruleInteger8
    , ruleIntegerAndAHalf
    , ruleDecimalOneAndAHalf
    , ruleIntegerWithThousandsSeparator
    , ruleNumeralDotNumeral
    , ruleNumeralsPrefixWithMinus
    , ruleNumeralsSuffixesKMG
    ]
  