{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
module Duckling.Numeral.ES.Rules (rules) where
import qualified Data.HashMap.Strict as HashMap
import Data.Maybe
import Data.String
import qualified Data.Text as Text
import Prelude
import Duckling.Dimensions.Types
import Duckling.Numeral.Helpers
import Duckling.Numeral.Types (NumeralData(..))
import qualified Duckling.Numeral.Types as TNumeral
import Duckling.Regex.Types
import Duckling.Types
ruleNumeralsPrefixWithNegativeOrMinus :: Rule
ruleNumeralsPrefixWithNegativeOrMinus :: Rule
ruleNumeralsPrefixWithNegativeOrMinus = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"numbers prefix with -, negative or minus"
, pattern :: Pattern
pattern = [String -> PatternItem
regex String
"-|menos", Predicate -> PatternItem
Predicate Predicate
isPositive]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token
_ : Token Dimension a
Numeral NumeralData { TNumeral.value = v } : [Token]
_) ->
Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
v Double -> Double -> Double
forall a. Num a => a -> a -> a
* (-Double
1)
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
byTensMap :: HashMap.HashMap Text.Text Integer
byTensMap :: HashMap Text Integer
byTensMap =
[(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (Text
"veinte", Integer
20)
, (Text
"treinta", Integer
30)
, (Text
"cuarenta", Integer
40)
, (Text
"cincuenta", Integer
50)
, (Text
"sesenta", Integer
60)
, (Text
"setenta", Integer
70)
, (Text
"ochenta", Integer
80)
, (Text
"noventa", Integer
90)
]
ruleNumeral2 :: Rule
ruleNumeral2 :: Rule
ruleNumeral2 = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number (20..90)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex
String
"(veinte|treinta|cuarenta|cincuenta|sesenta|setenta|ochenta|noventa)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match : _)) : [Token]
_) ->
Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Integer
byTensMap Maybe Integer -> (Integer -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Integer -> Maybe Token
integer
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
zeroToFifteenMap :: HashMap.HashMap Text.Text Integer
zeroToFifteenMap :: HashMap Text Integer
zeroToFifteenMap =
[(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (Text
"zero", Integer
0)
, (Text
"cero", Integer
0)
, (Text
"un", Integer
1)
, (Text
"una", Integer
1)
, (Text
"uno", Integer
1)
, (Text
"dos", Integer
2)
, (Text
"trés", Integer
3)
, (Text
"tres", Integer
3)
, (Text
"cuatro", Integer
4)
, (Text
"cinco", Integer
5)
, (Text
"seis", Integer
6)
, (Text
"séis", Integer
6)
, (Text
"siete", Integer
7)
, (Text
"ocho", Integer
8)
, (Text
"nueve", Integer
9)
, (Text
"diez", Integer
10)
, (Text
"dies", Integer
10)
, (Text
"once", Integer
11)
, (Text
"doce", Integer
12)
, (Text
"trece", Integer
13)
, (Text
"catorce", Integer
14)
, (Text
"quince", Integer
15)
]
ruleNumeral :: Rule
ruleNumeral :: Rule
ruleNumeral = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number (0..15)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex
String
"((c|z)ero|un(o|a)?|dos|tr(é|e)s|cuatro|cinco|s(e|é)is|siete|ocho|nueve|die(z|s)|once|doce|trece|catorce|quince)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match : _)) : [Token]
_) ->
Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Integer
zeroToFifteenMap Maybe Integer -> (Integer -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Integer -> Maybe Token
integer
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
sixteenToTwentyNineMap :: HashMap.HashMap Text.Text Integer
sixteenToTwentyNineMap :: HashMap Text Integer
sixteenToTwentyNineMap =
[(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (Text
"dieciseis", Integer
16)
, (Text
"diesiséis", Integer
16)
, (Text
"diesiseis", Integer
16)
, (Text
"dieciséis", Integer
16)
, (Text
"diecisiete", Integer
17)
, (Text
"dieciocho", Integer
18)
, (Text
"diecinueve", Integer
19)
, (Text
"veintiuno", Integer
21)
, (Text
"veintiuna", Integer
21)
, (Text
"veintidos", Integer
22)
, (Text
"veintidós", Integer
22)
, (Text
"veintitrés", Integer
23)
, (Text
"veintitres", Integer
23)
, (Text
"veinticuatro", Integer
24)
, (Text
"veinticinco", Integer
25)
, (Text
"veintiséis", Integer
26)
, (Text
"veintiseis", Integer
26)
, (Text
"veintisiete", Integer
27)
, (Text
"veintiocho", Integer
28)
, (Text
"veintinueve", Integer
29)
]
ruleNumeral5 :: Rule
ruleNumeral5 :: Rule
ruleNumeral5 = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number (16..19 21..29)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex
String
"(die(c|s)is(é|e)is|diecisiete|dieciocho|diecinueve|veintiun(o|a)|veintid(o|ó)s|veintitr(é|e)s|veinticuatro|veinticinco|veintis(é|e)is|veintisiete|veintiocho|veintinueve|treinta)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match : _)) : [Token]
_) ->
Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Integer
sixteenToTwentyNineMap Maybe Integer -> (Integer -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Integer -> Maybe Token
integer
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleNumeral3 :: Rule
ruleNumeral3 :: Rule
ruleNumeral3 = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number (16..19)"
, pattern :: Pattern
pattern = [(NumeralData -> Double) -> (Double -> Bool) -> PatternItem
forall t. (NumeralData -> t) -> (t -> Bool) -> PatternItem
numberWith NumeralData -> Double
TNumeral.value (Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
10), String -> PatternItem
regex String
"y", Double -> Double -> PatternItem
numberBetween Double
6 Double
10]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token
_ : Token
_ : Token Dimension a
Numeral NumeralData { TNumeral.value = v } : [Token]
_) ->
Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
10 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
v
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleNumeralsSuffixesKMG :: Rule
ruleNumeralsSuffixesKMG :: Rule
ruleNumeralsSuffixesKMG = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"numbers suffixes (K, M, G)"
, pattern :: Pattern
pattern = [Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral, String -> PatternItem
regex String
"([kmg])(?=[\\W\\$€]|$)"]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
Numeral NumeralData { TNumeral.value = v } : Token Dimension a
RegexMatch (GroupMatch (match : _)) : [Token]
_) ->
case Text -> Text
Text.toLower Text
match of
Text
"k" -> Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
v Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1e3
Text
"m" -> Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
v Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1e6
Text
"g" -> Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
v Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
1e9
Text
_ -> Maybe Token
forall a. Maybe a
Nothing
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
oneHundredToThousandMap :: HashMap.HashMap Text.Text Integer
oneHundredToThousandMap :: HashMap Text Integer
oneHundredToThousandMap =
[(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (Text
"cien", Integer
100)
, (Text
"cientos", Integer
100)
, (Text
"ciento", Integer
100)
, (Text
"doscientos", Integer
200)
, (Text
"trescientos", Integer
300)
, (Text
"cuatrocientos", Integer
400)
, (Text
"quinientos", Integer
500)
, (Text
"seiscientos", Integer
600)
, (Text
"setecientos", Integer
700)
, (Text
"ochocientos", Integer
800)
, (Text
"novecientos", Integer
900)
, (Text
"mil", Integer
1000)
]
ruleNumeral6 :: Rule
ruleNumeral6 :: Rule
ruleNumeral6 = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number 100..1000 "
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex
String
"(cien(to)?s?|doscientos|trescientos|cuatrocientos|quinientos|seiscientos|setecientos|ochocientos|novecientos|mil)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match : _)) : [Token]
_) ->
Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup (Text -> Text
Text.toLower Text
match) HashMap Text Integer
oneHundredToThousandMap Maybe Integer -> (Integer -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Integer -> Maybe Token
integer
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleNumeral4 :: Rule
ruleNumeral4 :: Rule
ruleNumeral4 = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number (21..29 31..39 41..49 51..59 61..69 71..79 81..89 91..99)"
, pattern :: Pattern
pattern =
[[Double] -> PatternItem
oneOf [Double
70, Double
20, Double
60, Double
50, Double
40, Double
90, Double
30, Double
80], String -> PatternItem
regex String
"y", Double -> Double -> PatternItem
numberBetween Double
1 Double
10]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
Numeral NumeralData { TNumeral.value = v1 } : Token
_ : Token Dimension a
Numeral NumeralData { TNumeral.value = v2 } : [Token]
_) ->
Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
v1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
v2
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleNumerals :: Rule
ruleNumerals :: Rule
ruleNumerals = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"numbers 200..999"
, pattern :: Pattern
pattern =
[ Double -> Double -> PatternItem
numberBetween Double
2 Double
10
, (NumeralData -> Double) -> (Double -> Bool) -> PatternItem
forall t. (NumeralData -> t) -> (t -> Bool) -> PatternItem
numberWith NumeralData -> Double
TNumeral.value (Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== Double
100)
, Double -> Double -> PatternItem
numberBetween Double
0 Double
100
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
Numeral NumeralData { TNumeral.value = v1 } : Token
_ : Token Dimension a
Numeral NumeralData { TNumeral.value = v2 } : [Token]
_) ->
Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
100 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
v1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
v2
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleNumeralDotNumeral :: Rule
ruleNumeralDotNumeral :: Rule
ruleNumeralDotNumeral = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number dot number"
, pattern :: Pattern
pattern = [Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral, String -> PatternItem
regex String
"punto", Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ Bool -> Bool
not (Bool -> Bool) -> Predicate -> Predicate
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Predicate
hasGrain]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
Numeral NumeralData { TNumeral.value = v1 } : Token
_ : Token Dimension a
Numeral NumeralData { TNumeral.value = v2 } : [Token]
_) ->
Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
v1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
decimalsToDouble Double
v2
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleBelowTenWithTwoDigits :: Rule
ruleBelowTenWithTwoDigits :: Rule
ruleBelowTenWithTwoDigits = Rule :: Text -> Pattern -> Production -> Rule
Rule
{
name :: Text
name = Text
"integer (0-9) with two digits"
, pattern :: Pattern
pattern =
[
String -> PatternItem
regex String
"((c|z)ero)|0"
, Double -> Double -> PatternItem
numberBetween Double
1 Double
10
]
, prod :: Production
prod = \case
(
Token
_:
Token Dimension a
Numeral NumeralData { TNumeral.value = v }:
[Token]
_
) -> Double -> Maybe Token
double Double
v
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
rules :: [Rule]
rules :: [Rule]
rules =
[ Rule
ruleBelowTenWithTwoDigits
, Rule
ruleNumeral
, Rule
ruleNumeral2
, Rule
ruleNumeral3
, Rule
ruleNumeral4
, Rule
ruleNumeral5
, Rule
ruleNumeral6
, Rule
ruleNumeralDotNumeral
, Rule
ruleNumerals
, Rule
ruleNumeralsPrefixWithNegativeOrMinus
, Rule
ruleNumeralsSuffixesKMG
]