-- 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 LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Duckling.Volume.ZH.Rules
  ( rules ) where

import Data.Text (Text)
import Prelude
import Data.String

import Duckling.Dimensions.Types
import Duckling.Types
import Duckling.Volume.Helpers
import Duckling.Numeral.Helpers (isPositive)
import qualified Duckling.Volume.Types as TVolume
import qualified Duckling.Numeral.Types as TNumeral

volumes :: [(Text, String, TVolume.Unit)]
volumes :: [(Text, String, Unit)]
volumes = [ (Text
"<latent vol> ml", String
"cc|ml|毫升", Unit
TVolume.Millilitre)
          , (Text
"<vol> liters", String
"l|L|公升|升", Unit
TVolume.Litre)
          , (Text
"<latent vol> gallon", String
"加侖", Unit
TVolume.Gallon)
          ]

rulesVolumes :: [Rule]
rulesVolumes :: [Rule]
rulesVolumes = ((Text, String, Unit) -> Rule) -> [(Text, String, Unit)] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map (Text, String, Unit) -> Rule
go [(Text, String, Unit)]
volumes
  where
    go :: (Text, String, TVolume.Unit) -> Rule
    go :: (Text, String, Unit) -> Rule
go (Text
name, String
regexPattern, Unit
u) = Rule :: Text -> Pattern -> Production -> Rule
Rule
      { name :: Text
name = Text
name
      , pattern :: Pattern
pattern =
        [ String -> PatternItem
regex String
regexPattern
        ]
      , prod :: Production
prod = \[Token]
_ -> Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
      }

ruleUnitTeaspoon :: Rule
ruleUnitTeaspoon :: Rule
ruleUnitTeaspoon = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<numeral> teaspoon"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    , String -> PatternItem
regex String
"茶匙"
    ]
    , prod :: Production
prod = \case
      (Token Dimension a
Numeral TNumeral.NumeralData{TNumeral.value = v}:[Token]
_) ->
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> Double -> VolumeData
volume Unit
TVolume.Millilitre (Double
5Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
v)
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleUnitSoupspoon :: Rule
ruleUnitSoupspoon :: Rule
ruleUnitSoupspoon = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<numeral> soupspoon"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    , String -> PatternItem
regex String
"湯匙"
    ]
    , prod :: Production
prod = \case
      (Token Dimension a
Numeral TNumeral.NumeralData{TNumeral.value = v}:[Token]
_) ->
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> Double -> VolumeData
volume Unit
TVolume.Millilitre (Double
15Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
v)
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

rulePrecision :: Rule
rulePrecision :: Rule
rulePrecision = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"about <volume>"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"\\~|大約|約"
    , Dimension VolumeData -> PatternItem
forall a. Typeable a => Dimension a -> PatternItem
dimension Dimension VolumeData
Volume
    ]
    , prod :: Production
prod = \case
      (Token
_:Token
token:[Token]
_) -> Token -> Maybe Token
forall a. a -> Maybe a
Just Token
token
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalBetweenNumeral :: Rule
ruleIntervalBetweenNumeral :: Rule
ruleIntervalBetweenNumeral = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"between|from <numeral> and|to <volume>"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isPositive
    , String -> PatternItem
regex String
"-|~|至|到"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    ]
  , prod :: Production
prod = \case
      (Token Dimension a
Numeral TNumeral.NumeralData{TNumeral.value = from}:
       Token
_:
       Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just to
                                  , TVolume.unit = Just u}:
       [Token]
_) | Double
from Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
to ->
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Double) -> VolumeData -> VolumeData
withInterval (Double
from, Double
to) (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalBetween :: Rule
ruleIntervalBetween :: Rule
ruleIntervalBetween = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"between|from <volume> to|and <volume>"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    , String -> PatternItem
regex String
"-|~|至|到"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    ]
  , prod :: Production
prod = \case
      (Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just from
                                  , TVolume.unit = Just u1}:
       Token
_:
       Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just to
                                  , TVolume.unit = Just u2}:
       [Token]
_) | Double
from Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
< Double
to Bool -> Bool -> Bool
&& Unit
u1 Unit -> Unit -> Bool
forall a. Eq a => a -> a -> Bool
== Unit
u2 ->
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Double, Double) -> VolumeData -> VolumeData
withInterval (Double
from, Double
to) (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u1
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalMax :: Rule
ruleIntervalMax :: Rule
ruleIntervalMax = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"at most <volume>"
  , pattern :: Pattern
pattern =
    [ String -> PatternItem
regex String
"最多"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    ]
  , prod :: Production
prod = \case
      (Token
_:
       Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just to
                                  , TVolume.unit = Just u}:
       [Token]
_) ->
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> VolumeData -> VolumeData
withMax Double
to (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalMax2 :: Rule
ruleIntervalMax2 :: Rule
ruleIntervalMax2 = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<volume> or below"
  , pattern :: Pattern
pattern =
    [ Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    , String -> PatternItem
regex String
"(或)?以下"
    ]
  , prod :: Production
prod = \case
      (Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just to
                                  , TVolume.unit = Just u}:
       [Token]
_) ->
        Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> VolumeData -> VolumeData
withMax Double
to (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
      [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
  }

ruleIntervalMin :: Rule
ruleIntervalMin :: Rule
ruleIntervalMin = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"more than <volume>"
  , pattern :: Pattern
pattern =
      [ String -> PatternItem
regex String
"至少|最少|起碼"
      , Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
      ]
    , prod :: Production
prod = \case
        (Token
_:
         Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just from
                                    , TVolume.unit = Just u}:
         [Token]
_) ->
          Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> VolumeData -> VolumeData
withMin Double
from (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
        [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
    }

ruleIntervalMin2 :: Rule
ruleIntervalMin2 :: Rule
ruleIntervalMin2 = Rule :: Text -> Pattern -> Production -> Rule
Rule
  { name :: Text
name = Text
"<volume> or above"
  , pattern :: Pattern
pattern =
      [ Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
      , String -> PatternItem
regex String
"(或)?以上"
      ]
    , prod :: Production
prod = \case
        (Token Dimension a
Volume TVolume.VolumeData{TVolume.value = Just from
                                    , TVolume.unit = Just u}:
         [Token]
_) ->
          Token -> Maybe Token
forall a. a -> Maybe a
Just (Token -> Maybe Token)
-> (VolumeData -> Token) -> VolumeData -> Maybe Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dimension VolumeData -> VolumeData -> Token
forall a.
(Resolve a, Eq a, Hashable a, Show a, NFData a) =>
Dimension a -> a -> Token
Token Dimension VolumeData
Volume (VolumeData -> Token)
-> (VolumeData -> VolumeData) -> VolumeData -> Token
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> VolumeData -> VolumeData
withMin Double
from (VolumeData -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> VolumeData
unitOnly Unit
u
        [Token]
_ -> Maybe Token
forall a. Maybe a
Nothing
    }


rules :: [Rule]
rules :: [Rule]
rules = [ Rule
ruleUnitTeaspoon
        , Rule
ruleUnitSoupspoon
        , Rule
rulePrecision
        , Rule
ruleIntervalBetweenNumeral
        , Rule
ruleIntervalBetween
        , Rule
ruleIntervalMax
        , Rule
ruleIntervalMax2
        , Rule
ruleIntervalMin
        , Rule
ruleIntervalMin2
        ]
        [Rule] -> [Rule] -> [Rule]
forall a. [a] -> [a] -> [a]
++ [Rule]
rulesVolumes