-- 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 #-}
{-# LANGUAGE NoRebindableSyntax #-}

module Duckling.Ordinal.EN.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
"first", Int
1 )
  , ( Text
"second", Int
2 )
  , ( Text
"third", Int
3 )
  , ( Text
"fourth", Int
4 )
  , ( Text
"fifth", Int
5 )
  , ( Text
"sixth", Int
6 )
  , ( Text
"seventh", Int
7 )
  , ( Text
"eighth", Int
8 )
  , ( Text
"ninth", Int
9 )
  , ( Text
"tenth", Int
10 )
  , ( Text
"eleventh", Int
11 )
  , ( Text
"twelfth", Int
12 )
  , ( Text
"thirteenth", Int
13 )
  , ( Text
"fourteenth", Int
14 )
  , ( Text
"fifteenth", Int
15 )
  , ( Text
"sixteenth", Int
16 )
  , ( Text
"seventeenth", Int
17 )
  , ( Text
"eighteenth", Int
18 )
  , ( Text
"nineteenth", Int
19 )
  , ( Text
"twentieth", Int
20 )
  , ( Text
"thirtieth", Int
30 )
  , ( Text
"fortieth", Int
40 )
  , ( Text
"fiftieth", Int
50 )
  , ( Text
"sixtieth", Int
60 )
  , ( Text
"seventieth", Int
70 )
  , ( Text
"eightieth", Int
80 )
  , ( Text
"ninetieth", 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
"twenty", Int
20 )
  , ( Text
"thirty", Int
30 )
  , ( Text
"forty", Int
40 )
  , ( Text
"fifty", Int
50 )
  , ( Text
"sixty", Int
60 )
  , ( Text
"seventy", Int
70 )
  , ( Text
"eighty", Int
80 )
  , ( Text
"ninety", 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
"(first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|eleventh|twelfth|thirteenth|fourteenth|fifteenth|sixteenth|seventeenth|eighteenth|nineteenth|twentieth|thirtieth|fortieth|fiftieth|sixtieth|seventieth|eightieth|ninetieth)"]
  , 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, forty—seventh, twenty ninth, thirtythird)"
  , pattern :: Pattern
pattern = [String -> PatternItem
regex String
"(twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety)[\\s\\-\\—]?(first|second|third|fourth|fifth|sixth|seventh|eighth|ninth)"]
  , 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
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+) ?(st|nd|rd|th)"]
  , 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
  ]