{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Duckling.Numeral.ZH.Rules
( rules
) where
import Data.Maybe
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
ruleInteger :: Rule
ruleInteger :: Rule
ruleInteger = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer (0..10)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(〇|零|一|二|两|兩|三|四|五|六|七|八|九|十|壹|貳|參|肆|伍|陸|柒|捌|玖|拾)"
]
, prod :: Production
prod = \case
(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
match HashMap Text Integer
integerMap 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
}
integerMap :: HashMap.HashMap Text Integer
integerMap :: HashMap Text Integer
integerMap = [(Text, Integer)] -> HashMap Text Integer
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList
[ ( Text
"〇", Integer
0 )
, ( Text
"零", Integer
0 )
, ( Text
"一", Integer
1 )
, ( Text
"壹", Integer
1 )
, ( Text
"兩", Integer
2 )
, ( Text
"两", Integer
2 )
, ( Text
"二", Integer
2 )
, ( Text
"貳", Integer
2 )
, ( Text
"三", Integer
3 )
, ( Text
"參", Integer
3 )
, ( Text
"四", Integer
4 )
, ( Text
"肆", Integer
4 )
, ( Text
"五", Integer
5 )
, ( Text
"伍", Integer
5 )
, ( Text
"六", Integer
6 )
, ( Text
"陸", Integer
6 )
, ( Text
"七", Integer
7 )
, ( Text
"柒", Integer
7 )
, ( Text
"八", Integer
8 )
, ( Text
"捌", Integer
8 )
, ( Text
"九", Integer
9 )
, ( Text
"玖", Integer
9 )
, ( Text
"十", Integer
10 )
, ( Text
"拾", Integer
10 )
]
tensMap :: HashMap.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
"廿" , Integer
20 )
, ( Text
"卅" , Integer
30 )
, ( Text
"卌" , Integer
40 )
]
ruleTens :: Rule
ruleTens :: Rule
ruleTens = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer (20,30,40)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(廿|卅|卌)"
]
, 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
}
ruleCompositeTens :: Rule
ruleCompositeTens :: Rule
ruleCompositeTens = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer 21..49"
, pattern :: Pattern
pattern =
[ [Double] -> PatternItem
oneOf [Double
20,Double
30,Double
40]
, 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
}
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
"-|负|負"
, Predicate -> PatternItem
Predicate Predicate
isPositive
]
, prod :: Production
prod = \case
(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 = \case
(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
}
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 = \case
(Token Dimension a
RegexMatch (GroupMatch (match:_)):[Token]
_) -> Bool -> Text -> Maybe Token
parseDecimal Bool
True Text
match
[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
"點"
, 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
}
ruleFraction :: Rule
ruleFraction :: Rule
ruleFraction = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"fraction"
, pattern :: Pattern
pattern =
[ Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral
, String -> PatternItem
regex String
"分之|分|份"
, Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral
]
, 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
v2 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
v1
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleMixedFraction :: Rule
ruleMixedFraction :: Rule
ruleMixedFraction = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
" mixed fraction"
, pattern :: Pattern
pattern =
[ Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral
, String -> PatternItem
regex String
"又"
, Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral
, String -> PatternItem
regex String
"分之|分|份"
, Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral
]
, 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
_:
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
v3 Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
v2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
v1
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleNumeral :: Rule
ruleNumeral :: Rule
ruleNumeral = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"<number>个/個"
, pattern :: Pattern
pattern =
[ Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral
, String -> PatternItem
regex String
"个|個"
]
, prod :: Production
prod = \case
(Token
token:[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just Token
token
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleHalf :: Rule
ruleHalf :: Rule
ruleHalf = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"half"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"(1|一)?半(半|个|個)?"
]
, prod :: Production
prod = \case
(Token
_:[Token]
_) -> Double -> Maybe Token
double Double
0.5 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
}
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
"打"
]
, 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
}
rulePair :: Rule
rulePair :: Rule
rulePair = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"a pair"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"雙|對"
]
, prod :: Production
prod = \[Token]
_ -> Integer -> Maybe Token
integer Integer
2 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
}
numeralSuffixList :: [(Text, Maybe Token)]
numeralSuffixList :: [(Text, Maybe Token)]
numeralSuffixList =
[ (Text
"K", Double -> Maybe Token
double Double
1e3 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
3 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable)
, (Text
"M", Double -> Maybe Token
double Double
1e6 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
6 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable)
, (Text
"G", Double -> Maybe Token
double Double
1e9 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
9 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable)
, (Text
"十|拾", Double -> Maybe Token
double Double
1e1 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
1 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable)
, (Text
"百|佰", Double -> Maybe Token
double Double
1e2 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
2 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable)
, (Text
"千|仟", Double -> Maybe Token
double Double
1e3 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
3 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable)
, (Text
"万|萬", Double -> Maybe Token
double Double
1e4 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
4 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable)
, (Text
"亿|億", Double -> Maybe Token
double Double
1e8 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
8 Maybe Token -> (Token -> Maybe Token) -> Maybe Token
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Token -> Maybe Token
withMultipliable)
]
ruleNumeralSuffixes :: [Rule]
ruleNumeralSuffixes :: [Rule]
ruleNumeralSuffixes = (Text -> Maybe Token -> Rule) -> (Text, Maybe Token) -> Rule
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Text -> Maybe Token -> Rule
constructNumeralSuffixRule ((Text, Maybe Token) -> Rule) -> [(Text, Maybe Token)] -> [Rule]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Text, Maybe Token)]
numeralSuffixList
where
constructNumeralSuffixRule :: Text -> Maybe Token -> Rule
constructNumeralSuffixRule :: Text -> Maybe Token -> Rule
constructNumeralSuffixRule Text
suffixName Maybe Token
production = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"number suffix: " Text -> Text -> Text
forall a. Monoid a => a -> a -> a
`mappend` Text
suffixName
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex (String -> PatternItem) -> String -> PatternItem
forall a b. (a -> b) -> a -> b
$ Text -> String
Text.unpack Text
suffixName
]
, prod :: Production
prod = Maybe Token -> Production
forall a b. a -> b -> a
const Maybe Token
production
}
ruleMultiply :: Rule
ruleMultiply :: Rule
ruleMultiply = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"compose by multiplication"
, pattern :: Pattern
pattern =
[ Dimension NumeralData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension NumeralData
Numeral
, Predicate -> PatternItem
Predicate Predicate
isMultipliable
]
, prod :: Production
prod = \case
(Token
token1:Token
token2:[Token]
_) -> Token -> Token -> Maybe Token
multiply Token
token1 Token
token2
[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 = \case
(Token Dimension a
RegexMatch (GroupMatch (match:_)):
[Token]
_) -> let fmt :: Text
fmt = Text -> Text -> Text -> Text
Text.replace Text
"," 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
}
ruleNumeralsIntersectNonconsectiveUnit :: Rule
ruleNumeralsIntersectNonconsectiveUnit :: Rule
ruleNumeralsIntersectNonconsectiveUnit = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer with nonconsecutive unit modifiers"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isPositive
, String -> PatternItem
regex String
"零|〇"
, Predicate -> PatternItem
Predicate Predicate
isPositive
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = v1}:Token
_:
Token Dimension a
Numeral NumeralData{TNumeral.value = v2}:[Token]
_) ->
Double -> Double -> Int -> Maybe Double
sumConnectedNumbers Double
v1 Double
v2 (Double -> Double -> Int
diffIntegerDigits Double
v1 Double
v2)
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
}
where
sumConnectedNumbers :: Double -> Double -> Int -> Maybe Double
sumConnectedNumbers :: Double -> Double -> Int -> Maybe Double
sumConnectedNumbers Double
v1 Double
v2 Int
d
| Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
1 = Maybe Double
forall a. Maybe a
Nothing
| Bool
otherwise = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double) -> Double -> Maybe Double
forall a b. (a -> b) -> a -> b
$ Double
v1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
v2
ruleNumeralsIntersectConsecutiveUnit :: Rule
ruleNumeralsIntersectConsecutiveUnit :: Rule
ruleNumeralsIntersectConsecutiveUnit = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"integer with consecutive unit modifiers"
, pattern :: Pattern
pattern =
[ Predicate -> PatternItem
Predicate Predicate
isPositive
, Predicate -> PatternItem
Predicate Predicate
isPositive
]
, prod :: Production
prod = \case
(Token Dimension a
Numeral NumeralData{TNumeral.value = v1}:
Token Dimension a
Numeral NumeralData{TNumeral.value = v2}:[Token]
_) ->
Double -> Double -> Int -> Maybe Double
sumConnectedNumbers Double
v1 Double
v2 (Double -> Double -> Int
diffIntegerDigits Double
v1 Double
v2)
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
}
where
sumConnectedNumbers :: Double -> Double -> Int -> Maybe Double
sumConnectedNumbers :: Double -> Double -> Int -> Maybe Double
sumConnectedNumbers Double
v1 Double
v2 Int
d
| Int
d Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = Double -> Maybe Double
forall a. a -> Maybe a
Just (Double -> Maybe Double) -> Double -> Maybe Double
forall a b. (a -> b) -> a -> b
$ Double
v1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
v2
| Bool
otherwise = Maybe Double
forall a. Maybe a
Nothing
ruleHundredPrefix :: Rule
ruleHundredPrefix :: Rule
ruleHundredPrefix = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"one hundred and <integer> (short form)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"百|佰"
, 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 -> 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
vDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
10
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleThousandPrefix :: Rule
ruleThousandPrefix :: Rule
ruleThousandPrefix = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"one thousand and <integer> (short form)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"千|仟"
, 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 -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
1000 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
vDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
100
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
ruleTenThousandPrefix :: Rule
ruleTenThousandPrefix :: Rule
ruleTenThousandPrefix = Rule :: Text -> Pattern -> Production -> Rule
Rule
{ name :: Text
name = Text
"ten thousand and <integer> (short form)"
, pattern :: Pattern
pattern =
[ String -> PatternItem
regex String
"万|萬"
, 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 -> Maybe Token) -> Double -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Double
10000 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
vDouble -> Double -> Double
forall a. Num a => a -> a -> a
*Double
1000
[Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
}
rules :: [Rule]
rules :: [Rule]
rules =
[ Rule
ruleDecimalNumeral
, Rule
ruleDecimalWithThousandsSeparator
, Rule
ruleInteger
, Rule
ruleIntegerWithThousandsSeparator
, Rule
ruleNumeral
, Rule
ruleNumeralsIntersectConsecutiveUnit
, Rule
ruleNumeralsIntersectNonconsectiveUnit
, Rule
ruleNumeralsPrefixWithNegativeOrMinus
, Rule
ruleMultiply
, Rule
ruleTens
, Rule
ruleCompositeTens
, Rule
ruleHalf
, Rule
ruleDotSpelledOut
, Rule
ruleDozen
, Rule
rulePair
, Rule
ruleFraction
, Rule
ruleMixedFraction
, Rule
ruleHundredPrefix
, Rule
ruleThousandPrefix
, Rule
ruleTenThousandPrefix
]
[Rule] -> [Rule] -> [Rule]
forall a. [a] -> [a] -> [a]
++ [Rule]
ruleNumeralSuffixes