-- 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.Numeral.EN.Rules
  ( rules
  ) where

import Control.Applicative ((<|>))
import Data.HashMap.Strict (HashMap)
import Data.Maybe
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

ruleDozen :: Rule
ruleDozen :: Rule
ruleDozen = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"a dozen of"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(a )?dozens?( of)?"
    ]
  , prod :: Production
prod = \[Token]
_ -> Integer -> Maybe Token
integer Integer
12 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
notOkForAnyTime
  }

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
"naught"   , Integer
0 )
  , ( Text
"nil"      , Integer
0 )
  , ( Text
"nought"   , Integer
0 )
  , ( Text
"none"     , Integer
0 )
  , ( Text
"zero"     , Integer
0 )
  , ( Text
"zilch"    , Integer
0 )
  , ( Text
"one"      , Integer
1 )
  , ( Text
"two"      , Integer
2 )
  , ( Text
"three"    , Integer
3 )
  , ( Text
"four"     , Integer
4 )
  , ( Text
"five"     , Integer
5 )
  , ( Text
"six"      , Integer
6 )
  , ( Text
"seven"    , Integer
7 )
  , ( Text
"eight"    , Integer
8 )
  , ( Text
"nine"     , Integer
9 )
  , ( Text
"ten"      , Integer
10 )
  , ( Text
"eleven"   , Integer
11 )
  , ( Text
"twelve"   , Integer
12 )
  , ( Text
"thirteen" , Integer
13 )
  , ( Text
"fourteen" , Integer
14 )
  , ( Text
"fifteen"  , Integer
15 )
  , ( Text
"sixteen"  , Integer
16 )
  , ( Text
"seventeen", Integer
17 )
  , ( Text
"eighteen" , Integer
18 )
  , ( Text
"nineteen" , Integer
19 )
  ]

informalMap :: HashMap Text Integer
informalMap :: HashMap Text Integer
informalMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
  [ ( Text
"single"     , Integer
1 )
  , ( Text
"a couple"   , Integer
2 )
  , ( Text
"a couple of", Integer
2 )
  , ( Text
"couple"     , Integer
2 )
  , ( Text
"couples"    , Integer
2 )
  , ( Text
"couple of"  , Integer
2 )
  , ( Text
"couples of" , Integer
2 )
  , ( Text
"a pair"     , Integer
2 )
  , ( Text
"a pair of"  , Integer
2 )
  , ( Text
"pair"       , Integer
2 )
  , ( Text
"pairs"      , Integer
2 )
  , ( Text
"pair of"    , Integer
2 )
  , ( Text
"pairs of"   , Integer
2 )
  , ( Text
"a few"      , Integer
3 )
  , ( Text
"few"        , Integer
3 )
  ]

ruleToNineteen :: Rule
ruleToNineteen :: Rule
ruleToNineteen = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"integer (0..19)"
  -- e.g. fourteen must be before four, otherwise four will always shadow fourteen
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(none|zilch|naught|nought|nil|zero|one|single|two|(a )?(pair|couple)s?( of)?|three|(a )?few|fourteen|four|fifteen|five|sixteen|six|seventeen|seven|eighteen|eight|nineteen|nine|ten|eleven|twelve|thirteen)"
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) ->
        let x :: Text
x = Text -> Text
Text.toLower Text
match in
        (Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
x 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) Maybe Token -> Maybe Token -> Maybe Token
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
        (Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
x HashMap Text Integer
informalMap Maybe Integer -> (Integer -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Integer -> Maybe Token
integer Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
notOkForAnyTime)
      [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
"twenty"  , Integer
20 )
  , ( Text
"thirty"  , Integer
30 )
  , ( Text
"forty"   , Integer
40 )
  , ( Text
"fourty"  , Integer
40 )
  , ( Text
"fifty"   , Integer
50 )
  , ( Text
"sixty"   , Integer
60 )
  , ( Text
"seventy" , Integer
70 )
  , ( Text
"eighty"  , Integer
80 )
  , ( Text
"ninety"  , Integer
90 )
  ]

ruleTens :: Rule
ruleTens :: Rule
ruleTens = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"integer (20..90)"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(twenty|thirty|fou?rty|fifty|sixty|seventy|eighty|ninety)"
    ]
  , 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
  }

powersOfTensMap :: HashMap Text Int
powersOfTensMap :: HashMap Text Int
powersOfTensMap = [(Text, Int)] -> HashMap Text Int
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
  [ (Text
"hundred", Int
2)
  , (Text
"thousand", Int
3)
  , (Text
"lakh", Int
5)
  , (Text
"lkh", Int
5)
  , (Text
"l", Int
5)
  , (Text
"lac", Int
5)
  , (Text
"million", Int
6)
  , (Text
"cr", Int
7)
  , (Text
"crore", Int
7)
  , (Text
"krore", Int
7)
  , (Text
"kr", Int
7)
  , (Text
"koti", Int
7)
  , (Text
"billion", Int
9)
  , (Text
"trillion", Int
12)
  ]

rulePowersOfTen :: Rule
rulePowersOfTen :: Rule
rulePowersOfTen = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"powers of tens"
  , pattern :: Pattern
pattern = [String -> PatternItem
regex String
"(hundred|thousand|l(ac|(a?kh)?)|million|((k|c)r(ore)?|koti)|billion)s?"]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
RegexMatch (GroupMatch (match : _)) : [Token]
_) -> do
        Int
grain <- 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
powersOfTensMap
        Double -> Maybe Token
double (Double
10 Double -> Int -> Double
forall a b. (Num a, Integral b) => a -> b -> a
^ Int
grain) Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Token -> Maybe Token
withGrain Int
grain Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleCompositeTens :: Rule
ruleCompositeTens :: Rule
ruleCompositeTens = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"integer 21..99"
  , pattern :: Pattern
pattern =
    [ [Double] -> PatternItem
oneOf [Double
20,Double
30..Double
90]
    , String -> PatternItem
regex String
"[\\s\\-]+"
    , Double -> Double -> PatternItem
numberBetween Double
1 Double
10
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
Numeral NumeralData{TNumeral.value = tens}:
       Token
_:
       Token Dimension a
Numeral NumeralData{TNumeral.value = units}:
       [Token]
_) -> Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
tens Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
units
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleSkipHundreds1 :: Rule
ruleSkipHundreds1 :: Rule
ruleSkipHundreds1 = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"one eleven"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(one|two|three|four|five|six|seven|eight|nine)"
    , String -> PatternItem
regex String
"(ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|fou?rty|fifty|sixty|seventy|eighty|ninety)"
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
RegexMatch (GroupMatch (m1:_)):
       Token Dimension a
RegexMatch (GroupMatch (m2:_)):
       [Token]
_) -> do
       let x1 :: Text
x1 = Text -> Text
Text.toLower Text
m1
       let x2 :: Text
x2 = Text -> Text
Text.toLower Text
m2
       Integer
hundreds <- Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
x1 HashMap Text Integer
zeroNineteenMap
       Integer
rest <- Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
x2 HashMap Text Integer
zeroNineteenMap Maybe Integer -> Maybe Integer -> Maybe Integer
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
x2 HashMap Text Integer
tensMap
       Integer -> Maybe Token
integer (Integer
hundreds Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
100 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
rest)
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleSkipHundreds2 :: Rule
ruleSkipHundreds2 :: Rule
ruleSkipHundreds2 = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"one twenty two"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(one|two|three|four|five|six|seven|eight|nine)"
    , String -> PatternItem
regex String
"(twenty|thirty|fou?rty|fifty|sixty|seventy|eighty|ninety)"
    , String -> PatternItem
regex String
"(one|two|three|four|five|six|seven|eight|nine)"
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
RegexMatch (GroupMatch (m1:_)):
       Token Dimension a
RegexMatch (GroupMatch (m2:_)):
       Token Dimension a
RegexMatch (GroupMatch (m3:_)):
       [Token]
_) -> do
       let x1 :: Text
x1 = Text -> Text
Text.toLower Text
m1
       let x2 :: Text
x2 = Text -> Text
Text.toLower Text
m2
       let x3 :: Text
x3 = Text -> Text
Text.toLower Text
m3
       Integer
hundreds <- Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
x1 HashMap Text Integer
zeroNineteenMap
       Integer
tens <- Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
x2 HashMap Text Integer
tensMap
       Integer
rest <- Text -> HashMap Text Integer -> Maybe Integer
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup Text
x3 HashMap Text Integer
zeroNineteenMap
       Integer -> Maybe Token
integer (Integer
hundreds Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
100 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
tens Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
rest)
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleDotSpelledOut :: Rule
ruleDotSpelledOut :: Rule
ruleDotSpelledOut = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"one point 2"
  , pattern :: Pattern
pattern =
    [ Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral
    , String -> PatternItem
regex String
"point|dot"
    , 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 a
nd1:Token
_:Token Dimension a
Numeral a
nd2:[Token]
_) ->
        Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ NumeralData -> Double
TNumeral.value a
NumeralData
nd1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double -> Double
decimalsToDouble (NumeralData -> Double
TNumeral.value a
NumeralData
nd2)
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleLeadingDotSpelledOut :: Rule
ruleLeadingDotSpelledOut :: Rule
ruleLeadingDotSpelledOut = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"point 77"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"point|dot"
    , 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
_:Token Dimension a
Numeral a
nd:[Token]
_) -> Double -> Maybe Token
double (Double -> Maybe Token)
-> (Double -> Double) -> Double -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Double
decimalsToDouble (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ NumeralData -> Double
TNumeral.value a
NumeralData
nd
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleDecimals :: Rule
ruleDecimals :: Rule
ruleDecimals = 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
True Text
match
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleCommas :: Rule
ruleCommas :: Rule
ruleCommas = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"comma-separated numbers"
  , 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:_)):[Token]
_) ->
        Text -> Maybe Double
parseDouble (Text -> Text -> Text -> Text
Text.replace Text
"," Text
Text.empty Text
match) 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
  }

ruleSuffixes :: Rule
ruleSuffixes :: Rule
ruleSuffixes = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"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
"(k|m|g)(?=[\\W$€¢£]|$)"
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
Numeral a
nd : Token Dimension a
RegexMatch (GroupMatch (match : _)):[Token]
_) -> do
        Double
x <- case Text -> Text
Text.toLower Text
match of
          Text
"k" -> Double -> Maybe Double
forall a. a -> Maybe a
Just Double
1e3
          Text
"m" -> Double -> Maybe Double
forall a. a -> Maybe a
Just Double
1e6
          Text
"g" -> Double -> Maybe Double
forall a. a -> Maybe a
Just Double
1e9
          Text
_ -> Maybe Double
forall a. Maybe a
Nothing
        Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ NumeralData -> Double
TNumeral.value a
NumeralData
nd Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleNegative :: Rule
ruleNegative :: Rule
ruleNegative = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"negative numbers"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"(-|minus|negative)(?!\\s*-)"
    , 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
  }

ruleSum :: Rule
ruleSum :: Rule
ruleSum = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"intersect 2 numbers"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([Bool] -> Bool) -> (Token -> [Bool]) -> Predicate
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Predicate] -> Token -> [Bool]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [Predicate
hasGrain, Predicate
isPositive]
    , Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([Bool] -> Bool) -> (Token -> [Bool]) -> Predicate
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Predicate] -> Token -> [Bool]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [Bool -> Bool
not (Bool -> Bool) -> Predicate -> Predicate
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Predicate
isMultipliable, Predicate
isPositive]
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
       Token Dimension a
Numeral NumeralData{TNumeral.value = val2}:
       [Token]
_) | (Double
10 Double -> Double -> Double
forall a. Floating a => a -> a -> a
** Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
g) Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
val2 -> Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
val1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
val2
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleSumAnd :: Rule
ruleSumAnd :: Rule
ruleSumAnd = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"intersect 2 numbers (with and)"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([Bool] -> Bool) -> (Token -> [Bool]) -> Predicate
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Predicate] -> Token -> [Bool]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [Predicate
hasGrain, Predicate
isPositive]
    , String -> PatternItem
regex String
"and"
    , Predicate -> PatternItem
Predicate (Predicate -> PatternItem) -> Predicate -> PatternItem
forall a b. (a -> b) -> a -> b
$ [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([Bool] -> Bool) -> (Token -> [Bool]) -> Predicate
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Predicate] -> Token -> [Bool]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [Bool -> Bool
not (Bool -> Bool) -> Predicate -> Predicate
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Predicate
isMultipliable, Predicate
isPositive]
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token Dimension a
Numeral NumeralData{TNumeral.value = val1, TNumeral.grain = Just g}:
       Token
_:
       Token Dimension a
Numeral NumeralData{TNumeral.value = val2}:
       [Token]
_) | (Double
10 Double -> Double -> Double
forall a. Floating a => a -> a -> a
** Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
g) Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
val2 -> Double -> Maybe Token
double (Double -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
val1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
val2
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleMultiply :: Rule
ruleMultiply :: Rule
ruleMultiply = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"compose by multiplication"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    , Predicate -> PatternItem
Predicate Predicate
isMultipliable
    ]
  , prod :: Production
prod = \[Token]
tokens -> case [Token]
tokens of
      (Token
token1:Token
token2:[Token]
_) -> Token -> Token -> Maybe Token
multiply Token
token1 Token
token2
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

rules :: [Rule]
rules :: [Rule]
rules =
  [ Rule
ruleToNineteen
  , Rule
ruleTens
  , Rule
rulePowersOfTen
  , Rule
ruleCompositeTens
  , Rule
ruleSkipHundreds1
  , Rule
ruleSkipHundreds2
  , Rule
ruleDotSpelledOut
  , Rule
ruleLeadingDotSpelledOut
  , Rule
ruleDecimals
  , Rule
ruleCommas
  , Rule
ruleSuffixes
  , Rule
ruleNegative
  , Rule
ruleSum
  , Rule
ruleSumAnd
  , Rule
ruleMultiply
  , Rule
ruleDozen
  ]