{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
module Duckling.Numeral.IT.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
import Duckling.Numeral.Types (NumeralData (..))
import Duckling.Regex.Types
import Duckling.Types
import qualified Duckling.Numeral.Types as TNumeral
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
"-|meno|negativo"
, Predicate -> PatternItem
Predicate Predicate
isPositive
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token
_:Token Dimension a
Numeral a
nd:[Token]
_) -> Double -> Maybe Token
double (NumeralData -> Double
TNumeral.value a
NumeralData
nd Double -> Double -> Double
forall a. Num a => a -> a -> a
* (-Double
1))
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDecimalWithThousandsSeparator :: Rule
ruleDecimalWithThousandsSeparator :: Rule
ruleDecimalWithThousandsSeparator = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"decimal with thousands separator"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(\\d+(([\\. ])\\d\\d\\d)+,\\d+)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match:_:sep:_)):
[Token]
_) -> let fmt :: Text
fmt = Text -> Text -> Text -> Text
Text.replace Text
"," Text
"." (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text -> Text
Text.replace Text
sep Text
Text.empty Text
match
in Text -> Maybe Double
parseDouble Text
fmt Maybe Double -> (Double -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Double -> Maybe Token
double
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleDecimalNumeral :: Rule
ruleDecimalNumeral :: Rule
ruleDecimalNumeral = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"decimal number"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(\\d*,\\d+)"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match:_)):
[Token]
_) -> Bool -> Text -> Maybe Token
parseDecimal Bool
False Text
match
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
tensMap :: HashMap Text Integer
tensMap :: HashMap Text Integer
tensMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (Text
"venti", Integer
20)
, (Text
"trenta", Integer
30)
, (Text
"quaranta", Integer
40)
, (Text
"cinquanta", Integer
50)
, (Text
"sessanta", Integer
60)
, (Text
"settanta", Integer
70)
, (Text
"ottanta", Integer
80)
, (Text
"novanta", 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
"(venti|trenta|quaranta|cinquanta|sessanta|settanta|ottanta|novanta)"
]
, 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
tensMap 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
}
zeroNineteenMap :: HashMap Text Integer
zeroNineteenMap :: HashMap Text Integer
zeroNineteenMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (Text
"zero", Integer
0)
, (Text
"niente", Integer
0)
, (Text
"nulla", Integer
0)
, (Text
"un", Integer
1)
, (Text
"uno", Integer
1)
, (Text
"due", Integer
2)
, (Text
"tre", Integer
3)
, (Text
"quattro", Integer
4)
, (Text
"cinque", Integer
5)
, (Text
"sei", Integer
6)
, (Text
"sette", Integer
7)
, (Text
"otto", Integer
8)
, (Text
"nove", Integer
9)
, (Text
"dieci", Integer
10)
, (Text
"undici", Integer
11)
, (Text
"dodici", Integer
12)
, (Text
"tredici", Integer
13)
, (Text
"quattordici", Integer
14)
, (Text
"quindici", Integer
15)
, (Text
"sedici", Integer
16)
, (Text
"diciassette", Integer
17)
, (Text
"diciotto", Integer
18)
, (Text
"diciannove", Integer
19)
]
ruleNumeral :: Rule
ruleNumeral :: Rule
ruleNumeral = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number (0..19)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(zero|nulla|niente|uno|due|tredici|tre|quattro|cinque|sei|sette|otto|nove|dieci|undici|dodici|quattordici|quindici|sedici|diciassette|diciotto|diciannove|un)"
]
, 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
zeroNineteenMap 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
}
hundredsMap :: HashMap Text Integer
hundredsMap :: HashMap Text Integer
hundredsMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (Text
"cento", Integer
100)
, (Text
"duecento", Integer
200)
, (Text
"trecento", Integer
300)
, (Text
"quattrocento", Integer
400)
, (Text
"cinquecento", Integer
500)
, (Text
"seicento", Integer
600)
, (Text
"settecento", Integer
700)
, (Text
"ottocento", Integer
800)
, (Text
"novecento", Integer
900)
, (Text
"mila", Integer
1000)
, (Text
"mille", Integer
1000)
]
ruleNumeral5 :: Rule
ruleNumeral5 :: Rule
ruleNumeral5 = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number 100..1000 "
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(due|tre|quattro|cinque|sei|sette|otto|nove)?cento|mil(a|le)"
]
, 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
hundredsMap 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 (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
"e"
, 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
}
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
}
twentyoneNinetynineMap :: HashMap Text Integer
twentyoneNinetynineMap :: HashMap Text Integer
twentyoneNinetynineMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ (Text
"ventuno", Integer
21)
, (Text
"ventidue", Integer
22)
, (Text
"ventitre", Integer
23)
, (Text
"ventitré", Integer
23)
, (Text
"ventiquattro", Integer
24)
, (Text
"venticinque", Integer
25)
, (Text
"ventisei", Integer
26)
, (Text
"ventisette", Integer
27)
, (Text
"ventotto", Integer
28)
, (Text
"ventinove", Integer
29)
, (Text
"trentuno", Integer
31)
, (Text
"trentadue", Integer
32)
, (Text
"trentatre", Integer
33)
, (Text
"trentatré", Integer
33)
, (Text
"trentaquattro", Integer
34)
, (Text
"trentacinque", Integer
35)
, (Text
"trentasei", Integer
36)
, (Text
"trentasette", Integer
37)
, (Text
"trentotto", Integer
38)
, (Text
"trentanove", Integer
39)
, (Text
"quarantuno", Integer
41)
, (Text
"quarantadue", Integer
42)
, (Text
"quarantatre", Integer
43)
, (Text
"quarantatré", Integer
43)
, (Text
"quarantaquattro", Integer
44)
, (Text
"quarantacinque", Integer
45)
, (Text
"quarantasei", Integer
46)
, (Text
"quarantasette", Integer
47)
, (Text
"quarantotto", Integer
48)
, (Text
"quarantanove", Integer
49)
, (Text
"cinquantuno", Integer
51)
, (Text
"cinquantadue", Integer
52)
, (Text
"cinquantatre", Integer
53)
, (Text
"cinquantatré", Integer
53)
, (Text
"cinquantaquattro", Integer
54)
, (Text
"cinquantacinque", Integer
55)
, (Text
"cinquantasei", Integer
56)
, (Text
"cinquantasette", Integer
57)
, (Text
"cinquantotto", Integer
58)
, (Text
"cinquantanove", Integer
59)
, (Text
"sessantuno", Integer
61)
, (Text
"sessantadue", Integer
62)
, (Text
"sessantatré", Integer
63)
, (Text
"sessantatre", Integer
63)
, (Text
"sessantaquattro", Integer
64)
, (Text
"sessantacinque", Integer
65)
, (Text
"sessantasei", Integer
66)
, (Text
"sessantasette", Integer
67)
, (Text
"sessantotto", Integer
68)
, (Text
"sessantanove", Integer
69)
, (Text
"settantuno", Integer
71)
, (Text
"settantadue", Integer
72)
, (Text
"settantatré", Integer
73)
, (Text
"settantatre", Integer
73)
, (Text
"settantaquattro", Integer
74)
, (Text
"settantacinque", Integer
75)
, (Text
"settantasei", Integer
76)
, (Text
"settantasette", Integer
77)
, (Text
"settantotto", Integer
78)
, (Text
"settantanove", Integer
79)
, (Text
"ottantuno", Integer
81)
, (Text
"ottantadue", Integer
82)
, (Text
"ottantatré", Integer
83)
, (Text
"ottantatre", Integer
83)
, (Text
"ottantaquattro", Integer
84)
, (Text
"ottantacinque", Integer
85)
, (Text
"ottantasei", Integer
86)
, (Text
"ottantasette", Integer
87)
, (Text
"ottantotto", Integer
88)
, (Text
"ottantanove", Integer
89)
, (Text
"novantuno", Integer
91)
, (Text
"novantadue", Integer
92)
, (Text
"novantatre", Integer
93)
, (Text
"novantatré", Integer
93)
, (Text
"novantaquattro", Integer
94)
, (Text
"novantacinque", Integer
95)
, (Text
"novantasei", Integer
96)
, (Text
"novantasette", Integer
97)
, (Text
"novantotto", Integer
98)
, (Text
"novantanove", Integer
99)
]
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 =
[ String -> PatternItem
regex String
"((venti|trenta|quaranta|cinquanta|sessanta|settanta|ottanta|novanta)(due|tre|tré|quattro|cinque|sei|sette|nove))|((vent|trent|quarant|cinquant|sessant|settant|ottant|novant)(uno|otto))"
]
, 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
twentyoneNinetynineMap 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
}
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 Dimension a
Numeral NumeralData{TNumeral.value = v2}:
Token Dimension a
Numeral NumeralData{TNumeral.value = v3}:
[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 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
v3
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleIntegerWithThousandsSeparator :: Rule
ruleIntegerWithThousandsSeparator :: Rule
ruleIntegerWithThousandsSeparator = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer with thousands separator ."
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(\\d{1,3}(([\\. ])\\d\\d\\d){1,5})"
]
, prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
(Token Dimension a
RegexMatch (GroupMatch (match:_:sep:_)):
[Token]
_) -> let fmt :: Text
fmt = Text -> Text -> Text -> Text
Text.replace Text
sep Text
Text.empty Text
match
in Text -> Maybe Double
parseDouble Text
fmt Maybe Double -> (Double -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Double -> Maybe Token
double
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
rules :: [Rule]
rules :: [Rule]
rules =
[ Rule
ruleDecimalNumeral
, Rule
ruleDecimalWithThousandsSeparator
, Rule
ruleIntegerWithThousandsSeparator
, Rule
ruleNumeral
, Rule
ruleNumeral2
, Rule
ruleNumeral3
, Rule
ruleNumeral4
, Rule
ruleNumeral5
, Rule
ruleNumerals
, Rule
ruleNumeralsPrefixWithNegativeOrMinus
, Rule
ruleNumeralsSuffixesKMG
]