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

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
"პირველი", Int
1)
  , (Text
"პირველ", Int
1)
  , (Text
"მეერთე", Int
1)
  , (Text
"მეორე", Int
2)
  , (Text
"მესამე", Int
3)
  , (Text
"მეოთხე", Int
4)
  , (Text
"მეხუთე", Int
5)
  , (Text
"მეექვსე", Int
6)
  , (Text
"მეშვიდე", Int
7)
  , (Text
"მერვე", Int
8)
  , (Text
"მეცხრე", Int
9)
  , (Text
"მეათე", Int
10)
  , (Text
"მეთერთმეტე", Int
11)
  , (Text
"მეთორმეტე", Int
12)
  , (Text
"მეცამეტე", Int
13)
  , (Text
"მეთოთხმეტე", Int
14)
  , (Text
"მეთხუთმეტე", Int
15)
  , (Text
"მეთქვსმეტე", Int
16)
  , (Text
"მეჩვიდმეტე", Int
17)
  , (Text
"მეთვრამეტე", Int
18)
  , (Text
"მეცხრამეტე", Int
19)
  , (Text
"მეოცე", Int
20)
  , (Text
"ოცდამეათე", Int
30)
  , (Text
"მეორმოცე", Int
40)
  , (Text
"ორმოცდამეათე", Int
50)
  , (Text
"მესამოცე", Int
60)
  , (Text
"სამოცდამეათე", Int
70)
  , (Text
"მეოთხმოცე", Int
80)
  , (Text
"ოთხმოცდამეათე", Int
90)
  ]

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
"ოცი", Int
20)
  , (Text
"ოცდა", Int
20)
  , (Text
"ოცდაათი", Int
30)
  , (Text
"ორმოცი", Int
40)
  , (Text
"ორმოცდა", Int
40)
  , (Text
"ორმოცდაათი", Int
50)
  , (Text
"სამოცი", Int
60)
  , (Text
"სამოცდა", Int
60)
  , (Text
"სამოცდაათი", Int
70)
  , (Text
"ოთხმოცი", Int
80)
  , (Text
"ოთხმოცდა", Int
80)
  , (Text
"ოთხმოცდაათი", 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
"(პირველი?|მეორე|მესამე|მეოთხე|მეხუთე|მეექვსე|მეშვიდე|მერვე|მეცხრე|მეათე|მეთერთმეტე|მეთოთხმეტე|მეცამეტე|მეთოთხმეტე|მეთხუთმეტე|მეთექვსმეტე|მეჩვიდმეტე|მეთვრამეტე|მეცხრამეტე|მეოცე|ოცდამეათე|მეორმოცე|ორმოცდამეათე|მესამოცე|სამოცდამეათე|მეოთხმოცე|ოთხმოცდამეათე)"
    ]
  , prod :: Production
prod = \case
      (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, forty—seventh, twenty ninth, thirtythird)"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(ოცდა|ორმოცდა|სამოცდა|ოთხმოცდა)[\\s\\-\\—]?(მეერთე|მეორე|მესამე|მეოთხე|მეხუთე|მეექვსე|მეშვიდე|მერვე|მეცხრე|მეათე|მეთერთმეტე|მეთოთხმეტე|მეცამეტე|მეთოთხმეტე|მეთხუთმეტე|მეთექვსმეტე|მეჩვიდმეტე|მეთვრამეტე|მეცხრამეტე)"
    ]
  , prod :: Production
prod = \case
      (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
ordinalsMap
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Int -> Token
ordinal (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 = \case
      (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
  }

ruleOrdinalDigits1 :: Rule
ruleOrdinalDigits1 :: Rule
ruleOrdinalDigits1 = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"ordinal (digits)"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"მე-? ?0*(\\d+)"
    ]
  , prod :: Production
prod = \case
      (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
  }

ruleOrdinalDigits2 :: Rule
ruleOrdinalDigits2 :: Rule
ruleOrdinalDigits2 = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"ordinal (digits)"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"მე-? ?0*(\\d+) ?(-ლი|-ე)"
    ]
  , prod :: Production
prod = \case
      (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
  , Rule
ruleOrdinalDigits1
  , Rule
ruleOrdinalDigits2
  ]