-- 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.Ordinal.HU.Rules
  ( rules ) where

import Control.Monad (join)
import Data.HashMap.Strict ( HashMap)
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 (parseInt)
import Duckling.Ordinal.Helpers
import Duckling.Regex.Types
import Duckling.Types

ordinalsMap :: HashMap Text Int
ordinalsMap :: HashMap Text Int
ordinalsMap = [(Text, Int)] -> HashMap Text Int
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
  [ ( Text
"első", Int
1 )
  , ( Text
"második", Int
2 )
  , ( Text
"harmadik", Int
3 )
  , ( Text
"negyedik", Int
4 )
  , ( Text
"ötödik", Int
5 )
  , ( Text
"hatodik", Int
6 )
  , ( Text
"hetedik", Int
7 )
  , ( Text
"nyolcadik", Int
8 )
  , ( Text
"kilencedik", Int
9 )
  , ( Text
"tizedik", Int
10 )
  , ( Text
"huszadik", Int
20 )
  , ( Text
"harmincadik", Int
30 )
  , ( Text
"negyvenedik", Int
40 )
  , ( Text
"ötvenedik", Int
50 )
  , ( Text
"hatvanadik", Int
60 )
  , ( Text
"hetvenedik", Int
70 )
  , ( Text
"nyolcvanadik", Int
80 )
  , ( Text
"kilencvenedik", Int
90 )
  ]

ordinalsMap2 :: HashMap Text Int
ordinalsMap2 :: HashMap Text Int
ordinalsMap2 = [(Text, Int)] -> HashMap Text Int
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
    [ ( Text
"egyedik", Int
1 )
    , ( Text
"kettedik", Int
2 )
    , ( Text
"harmadik", Int
3 )
    , ( Text
"negyedik", Int
4 )
    , ( Text
"ötödik", Int
5 )
    , ( Text
"hatodik", Int
6 )
    , ( Text
"hetedik", Int
7 )
    , ( Text
"nyolcadik", Int
8 )
    , ( Text
"kilencedik", Int
9 )
    ]

cardinalsMap :: HashMap Text Int
cardinalsMap :: HashMap Text Int
cardinalsMap = [(Text, Int)] -> HashMap Text Int
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
  [ ( Text
"tizen", Int
10 )
  , ( Text
"huszon", Int
20 )
  , ( Text
"harminc", Int
30 )
  , ( Text
"negyven", Int
40 )
  , ( Text
"ötven", Int
50 )
  , ( Text
"hatvan", Int
60 )
  , ( Text
"hetven", Int
70 )
  , ( Text
"nyolcvan", Int
80 )
  , ( Text
"kilencven", Int
90 )
  ]

ruleOrdinals :: Rule
ruleOrdinals :: Rule
ruleOrdinals = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"ordinals (first..twentieth,thirtieth,...)"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(első|második|harmadik|negyedik|ötödik|hatodik|hetedik|nyolcadik|kilencedik|tizedik|huszadik|harmincadik|negyvenedik|ötvenedik|hatvanadik|hetvenedik|nyolcvanadik|kilencvenedik)"
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) ->
        Int -> Token
ordinal (Int -> Token) -> Maybe Int -> Maybe Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> HashMap Text Int -> Maybe Int
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Int
ordinalsMap
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
    }

ruleCompositeOrdinals :: Rule
ruleCompositeOrdinals :: Rule
ruleCompositeOrdinals = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"ordinals (composite, e.g., eighty-seven)"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(tizen|huszon|harminc|negyven|ötven|hatvan|hetven|nyolcvan|kilencven)\\-?(egyedik|kettedik|harmadik|negyedik|ötödik|hatodik|hetedik|nyolcadik|kilencedik)"
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
RegexMatch (GroupMatch (tens:units:_)):[Token]
_) -> do
        Int
tt <- Text -> HashMap Text Int -> Maybe Int
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
tens) HashMap Text Int
cardinalsMap
        Int
uu <- Text -> HashMap Text Int -> Maybe Int
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
units) HashMap Text Int
ordinalsMap2
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token) -> (Int -> Token) -> Int -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Token
ordinal (Int -> Maybe Token) -> Int -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Int
tt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
uu
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleOrdinalDigits :: Rule
ruleOrdinalDigits :: Rule
ruleOrdinalDigits = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"ordinal (digits)"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"0*(\\d+)\\."
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) -> Int -> Token
ordinal (Int -> Token) -> Maybe Int -> Maybe Token
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Maybe Int
parseInt Text
match
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

rules :: [Rule]
rules :: [Rule]
rules =
  [ Rule
ruleOrdinals
  , Rule
ruleCompositeOrdinals
  , Rule
ruleOrdinalDigits
  ]