duckling/Duckling/Numeral/ES/VE/Rules.hs
Jiaxu Zhu 0527be1ce0 Adding Locales for ES Numeral
Summary:
Adding locale rules for ES Numeral because Spain use "," as decimal but south american country use "." as decimal.

Wiki: https://en.wikipedia.org/wiki/Decimal_separator

Reviewed By: haoxuany

Differential Revision: D20040111

fbshipit-source-id: e2a4bfc2928df19976ef98e90ee82e7d21b52313
2020-02-25 16:01:45 -08:00

59 lines
1.7 KiB
Haskell

-- 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 #-}
module Duckling.Numeral.ES.VE.Rules (rules) where
import Data.Maybe
import Data.String
import qualified Data.Text as Text
import Prelude
import Duckling.Dimensions.Types
import Duckling.Numeral.Helpers
import Duckling.Regex.Types
import Duckling.Types
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 : _)) : _) ->
let fmt = Text.replace "," "." $ Text.replace "." Text.empty match
in parseDouble fmt >>= double
_ -> Nothing
}
ruleDecimalNumeral :: Rule
ruleDecimalNumeral = Rule
{ name = "decimal number ,"
, pattern = [regex "(\\d*,\\d+)"]
, prod = \tokens -> case tokens of
(Token RegexMatch (GroupMatch (match : _)) : _) ->
parseDecimal False match
_ -> Nothing
}
ruleIntegerWithThousandsSeparator :: Rule
ruleIntegerWithThousandsSeparator = Rule
{ name = "integer with thousands separator ."
, pattern = [regex "(\\d{1,3}(\\.\\d\\d\\d){1,5})"]
, prod = \tokens -> case tokens of
(Token RegexMatch (GroupMatch (match : _)) : _) ->
parseDouble (Text.replace "." Text.empty match) >>= double
_ -> Nothing
}
rules :: [Rule]
rules =
[ ruleDecimalNumeral
, ruleDecimalWithThousandsSeparator
, ruleIntegerWithThousandsSeparator
]