2017-03-08 21:33:55 +03:00
|
|
|
-- 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. An additional grant
|
|
|
|
-- of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
|
|
|
|
{-# LANGUAGE GADTs #-}
|
2017-10-17 23:12:26 +03:00
|
|
|
{-# LANGUAGE NoRebindableSyntax #-}
|
2017-03-08 21:33:55 +03:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
module Duckling.Numeral.NL.Rules
|
2017-10-17 23:12:26 +03:00
|
|
|
( rules
|
|
|
|
) where
|
2017-03-08 21:33:55 +03:00
|
|
|
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
|
|
import Data.Maybe
|
2017-10-17 23:12:26 +03:00
|
|
|
import Data.String
|
2017-03-08 21:33:55 +03:00
|
|
|
import Data.Text (Text)
|
|
|
|
import Prelude
|
2017-10-17 23:12:26 +03:00
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
|
|
|
import qualified Data.Text as Text
|
2017-03-08 21:33:55 +03:00
|
|
|
|
|
|
|
import Duckling.Dimensions.Types
|
2017-03-16 23:42:15 +03:00
|
|
|
import Duckling.Numeral.Helpers
|
|
|
|
import Duckling.Numeral.Types (NumeralData (..))
|
2017-03-08 21:33:55 +03:00
|
|
|
import Duckling.Regex.Types
|
|
|
|
import Duckling.Types
|
2017-10-17 23:12:26 +03:00
|
|
|
import qualified Duckling.Numeral.Types as TNumeral
|
2017-03-08 21:33:55 +03:00
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
ruleNumeralsPrefixWithNegativeOrMinus :: Rule
|
|
|
|
ruleNumeralsPrefixWithNegativeOrMinus = Rule
|
2017-03-08 21:33:55 +03:00
|
|
|
{ name = "numbers prefix with -, negative or minus"
|
|
|
|
, pattern =
|
|
|
|
[ regex "-|min|minus|negatief"
|
2018-02-21 22:52:26 +03:00
|
|
|
, Predicate isPositive
|
2017-03-08 21:33:55 +03:00
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
2017-03-16 23:42:15 +03:00
|
|
|
(_:Token Numeral nd:_) -> double (TNumeral.value nd * (-1))
|
2017-03-08 21:33:55 +03:00
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleFew :: Rule
|
|
|
|
ruleFew = Rule
|
|
|
|
{ name = "few"
|
|
|
|
, pattern =
|
|
|
|
[ regex "meerdere"
|
|
|
|
]
|
|
|
|
, prod = \_ -> integer 3
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleTen :: Rule
|
|
|
|
ruleTen = Rule
|
|
|
|
{ name = "ten"
|
|
|
|
, pattern =
|
|
|
|
[ regex "tien"
|
|
|
|
]
|
|
|
|
, prod = \_ -> integer 10 >>= withGrain 1
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleDecimalWithThousandsSeparator :: Rule
|
|
|
|
ruleDecimalWithThousandsSeparator = Rule
|
|
|
|
{ name = "decimal with thousands separator"
|
|
|
|
, pattern =
|
|
|
|
[ regex "(\\d+(\\.\\d\\d\\d)+,\\d+)"
|
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
|
|
|
(Token RegexMatch (GroupMatch (match:_)):
|
2017-10-17 23:12:26 +03:00
|
|
|
_) -> let fmt = Text.replace "," "." $ Text.replace "." Text.empty match
|
2017-03-08 21:33:55 +03:00
|
|
|
in parseDouble fmt >>= double
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
ruleDecimalNumeral :: Rule
|
|
|
|
ruleDecimalNumeral = Rule
|
2017-03-08 21:33:55 +03:00
|
|
|
{ name = "decimal number"
|
|
|
|
, pattern =
|
|
|
|
[ regex "(\\d*,\\d+)"
|
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
|
|
|
(Token RegexMatch (GroupMatch (match:_)):
|
|
|
|
_) -> parseDecimal False match
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
2017-11-15 21:41:33 +03:00
|
|
|
-- TODO: Single-word composition (#110)
|
2017-03-08 21:33:55 +03:00
|
|
|
ruleInteger3 :: Rule
|
|
|
|
ruleInteger3 = Rule
|
|
|
|
{ name = "integer ([2-9][1-9])"
|
|
|
|
, pattern =
|
2017-09-07 22:33:31 +03:00
|
|
|
[ regex "(een|twee|drie|vier|vijf|zes|zeven|acht|negen)(?:e|ë)n(twintig|dertig|veertig|vijftig|zestig|zeventig|tachtig|negentig)"
|
2017-03-08 21:33:55 +03:00
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
|
|
|
(Token RegexMatch (GroupMatch (m1:m2:_)):_) -> do
|
|
|
|
v1 <- HashMap.lookup (Text.toLower m1) zeroNineteenMap
|
|
|
|
v2 <- HashMap.lookup (Text.toLower m2) dozenMap
|
|
|
|
integer $ v1 + v2
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleMultiply :: Rule
|
|
|
|
ruleMultiply = Rule
|
|
|
|
{ name = "compose by multiplication"
|
|
|
|
, pattern =
|
2017-03-14 23:19:13 +03:00
|
|
|
[ dimension Numeral
|
2018-02-21 22:52:26 +03:00
|
|
|
, Predicate isMultipliable
|
2017-03-08 21:33:55 +03:00
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
|
|
|
(token1:token2:_) -> multiply token1 token2
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleIntersect :: Rule
|
|
|
|
ruleIntersect = Rule
|
|
|
|
{ name = "intersect"
|
|
|
|
, pattern =
|
2018-04-14 05:25:21 +03:00
|
|
|
[ Predicate hasGrain
|
2018-02-21 22:52:26 +03:00
|
|
|
, Predicate $ and . sequence [not . isMultipliable, isPositive]
|
2017-03-08 21:33:55 +03:00
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
2018-01-30 03:41:23 +03:00
|
|
|
(Token Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
|
|
|
|
Token Numeral NumeralData{TNumeral.value = val2}:
|
2017-03-08 21:33:55 +03:00
|
|
|
_) | (10 ** fromIntegral g) > val2 -> double $ val1 + val2
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
ruleNumeralsSuffixesKMG :: Rule
|
|
|
|
ruleNumeralsSuffixesKMG = Rule
|
2017-03-08 21:33:55 +03:00
|
|
|
{ name = "numbers suffixes (K, M, G)"
|
|
|
|
, pattern =
|
2017-03-14 23:19:13 +03:00
|
|
|
[ dimension Numeral
|
2017-09-07 22:33:31 +03:00
|
|
|
, regex "([kmg])(?=[\\W\\$€]|$)"
|
2017-03-08 21:33:55 +03:00
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
2018-01-30 03:41:23 +03:00
|
|
|
(Token Numeral NumeralData{TNumeral.value = v}:
|
2017-03-08 21:33:55 +03:00
|
|
|
Token RegexMatch (GroupMatch (match:_)):
|
|
|
|
_) -> case Text.toLower match of
|
|
|
|
"k" -> double $ v * 1e3
|
|
|
|
"m" -> double $ v * 1e6
|
|
|
|
"g" -> double $ v * 1e9
|
|
|
|
_ -> Nothing
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:42:15 +03:00
|
|
|
ruleNumeralsEn :: Rule
|
|
|
|
ruleNumeralsEn = Rule
|
2017-03-08 21:33:55 +03:00
|
|
|
{ name = "numbers en"
|
|
|
|
, pattern =
|
|
|
|
[ numberBetween 1 10
|
2018-07-16 15:35:02 +03:00
|
|
|
, regex "-?en-?"
|
2017-03-08 21:33:55 +03:00
|
|
|
, oneOf [20, 30 .. 90]
|
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
2018-01-30 03:41:23 +03:00
|
|
|
(Token Numeral NumeralData{TNumeral.value = v1}:
|
2017-03-08 21:33:55 +03:00
|
|
|
_:
|
2018-01-30 03:41:23 +03:00
|
|
|
Token Numeral NumeralData{TNumeral.value = v2}:
|
2017-03-08 21:33:55 +03:00
|
|
|
_) -> double $ v1 + v2
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
rulePowersOfTen :: Rule
|
|
|
|
rulePowersOfTen = Rule
|
|
|
|
{ name = "powers of tens"
|
|
|
|
, pattern =
|
|
|
|
[ regex "(honderd|duizend|miljoen)"
|
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
|
|
|
(Token RegexMatch (GroupMatch (match:_)):_) -> case Text.toLower match of
|
|
|
|
"honderd" -> double 1e2 >>= withGrain 2 >>= withMultipliable
|
|
|
|
"duizend" -> double 1e3 >>= withGrain 3 >>= withMultipliable
|
|
|
|
"miljoen" -> double 1e6 >>= withGrain 6 >>= withMultipliable
|
|
|
|
_ -> Nothing
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleCouple :: Rule
|
|
|
|
ruleCouple = Rule
|
|
|
|
{ name = "couple"
|
|
|
|
, pattern =
|
|
|
|
[ regex "(een )?paar"
|
|
|
|
]
|
|
|
|
, prod = \_ -> integer 2
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleDozen :: Rule
|
|
|
|
ruleDozen = Rule
|
|
|
|
{ name = "dozen"
|
|
|
|
, pattern =
|
|
|
|
[ regex "dozijn"
|
|
|
|
]
|
|
|
|
, prod = \_ -> integer 12 >>= withGrain 1
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:28:28 +03:00
|
|
|
ruleGross :: Rule
|
|
|
|
ruleGross = Rule
|
|
|
|
{ name = "gros"
|
|
|
|
, pattern =
|
|
|
|
[ regex "gros"
|
|
|
|
]
|
|
|
|
, prod = \_ -> integer 144 >>= withGrain 1
|
|
|
|
}
|
|
|
|
|
2017-03-08 21:33:55 +03:00
|
|
|
zeroNineteenMap :: HashMap Text Integer
|
|
|
|
zeroNineteenMap = HashMap.fromList
|
|
|
|
[ ("niks", 0)
|
|
|
|
, ("nul", 0)
|
|
|
|
, ("geen", 0)
|
2017-09-07 22:33:31 +03:00
|
|
|
, ("één", 1)
|
2017-03-08 21:33:55 +03:00
|
|
|
, ("een", 1)
|
|
|
|
, ("twee", 2)
|
|
|
|
, ("drie", 3)
|
|
|
|
, ("vier", 4)
|
|
|
|
, ("vijf", 5)
|
|
|
|
, ("zes", 6)
|
|
|
|
, ("zeven", 7)
|
|
|
|
, ("acht", 8)
|
|
|
|
, ("negen", 9)
|
|
|
|
, ("tien", 10)
|
|
|
|
, ("elf", 11)
|
|
|
|
, ("twaalf", 12)
|
|
|
|
, ("dertien", 13)
|
|
|
|
, ("veertien", 14)
|
|
|
|
, ("vijftien", 15)
|
|
|
|
, ("zestien", 16)
|
|
|
|
, ("zeventien", 17)
|
|
|
|
, ("achtien", 18)
|
|
|
|
, ("negentien", 19)
|
|
|
|
]
|
|
|
|
|
|
|
|
ruleInteger :: Rule
|
|
|
|
ruleInteger = Rule
|
|
|
|
{ name = "integer (0..19)"
|
|
|
|
, pattern =
|
2017-09-07 22:33:31 +03:00
|
|
|
[ regex "(geen|nul|niks|een|één|twee|drie|vier|vijftien|vijf|zestien|zes|zeventien|zeven|achtien|acht|negentien|negen|tien|elf|twaalf|dertien|veertien)"
|
2017-03-08 21:33:55 +03:00
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
|
|
|
(Token RegexMatch (GroupMatch (match:_)):_) ->
|
|
|
|
HashMap.lookup (Text.toLower match) zeroNineteenMap >>= integer
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
dozenMap :: HashMap Text Integer
|
|
|
|
dozenMap = HashMap.fromList
|
|
|
|
[ ("twintig", 20)
|
|
|
|
, ("dertig", 30)
|
|
|
|
, ("veertig", 40)
|
|
|
|
, ("vijftig", 50)
|
|
|
|
, ("zestig", 60)
|
|
|
|
, ("zeventig", 70)
|
|
|
|
, ("tachtig", 80)
|
|
|
|
, ("negentig", 90)
|
|
|
|
]
|
|
|
|
|
|
|
|
ruleInteger2 :: Rule
|
|
|
|
ruleInteger2 = Rule
|
|
|
|
{ name = "integer (20..90)"
|
|
|
|
, pattern =
|
|
|
|
[ regex "(twintig|dertig|veertig|vijftig|zestig|zeventig|tachtig|negentig)"
|
|
|
|
]
|
|
|
|
, prod = \tokens -> case tokens of
|
|
|
|
(Token RegexMatch (GroupMatch (match:_)):_) ->
|
|
|
|
HashMap.lookup (Text.toLower match) dozenMap >>= integer
|
|
|
|
_ -> Nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
rules :: [Rule]
|
|
|
|
rules =
|
|
|
|
[ ruleCouple
|
2017-03-16 23:42:15 +03:00
|
|
|
, ruleDecimalNumeral
|
2017-03-08 21:33:55 +03:00
|
|
|
, ruleDecimalWithThousandsSeparator
|
|
|
|
, ruleDozen
|
2017-10-13 22:28:28 +03:00
|
|
|
, ruleGross
|
2017-03-08 21:33:55 +03:00
|
|
|
, ruleFew
|
|
|
|
, ruleInteger
|
|
|
|
, ruleInteger2
|
|
|
|
, ruleInteger3
|
|
|
|
, ruleIntersect
|
|
|
|
, ruleMultiply
|
2017-03-16 23:42:15 +03:00
|
|
|
, ruleNumeralsEn
|
|
|
|
, ruleNumeralsPrefixWithNegativeOrMinus
|
|
|
|
, ruleNumeralsSuffixesKMG
|
2017-03-08 21:33:55 +03:00
|
|
|
, rulePowersOfTen
|
|
|
|
, ruleTen
|
|
|
|
]
|