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

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

import Duckling.Dimensions.Types
import Duckling.Types
import Duckling.Regex.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
"m(l|illiliter[ns]?)", Unit
TVolume.Millilitre)
          , (Text
"<vol> hectoliters", String
"hektoliter[ns]?", Unit
TVolume.Hectolitre)
          , (Text
"<vol> liters", String
"l(iter[ns]?)?", Unit
TVolume.Litre)
          ]

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
      }

fractions :: [(Text, String, Double)]
fractions :: [(Text, String, Double)]
fractions = [ (Text
"one",String
"ein(e[ns])?", Double
1)
            , (Text
"half", String
"(ein(e[ns])? )?halb(e[rn])? ", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
2)
            , (Text
"third", String
"(ein(e[ns])? )?dritttel ", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
3)
            , (Text
"fourth", String
"(ein(e[ns])? )?viertel ", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
4)
            , (Text
"fifth", String
"(ein(e[ns])? )?fünftel ", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
5)
            , (Text
"tenth", String
"(ein(e[ns])? )?zehntel ", Double
1Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/Double
10)
            ]

rulesFractionalVolume :: [Rule]
rulesFractionalVolume :: [Rule]
rulesFractionalVolume = ((Text, String, Double) -> Rule)
-> [(Text, String, Double)] -> [Rule]
forall a b. (a -> b) -> [a] -> [b]
map (Text, String, Double) -> Rule
go [(Text, String, Double)]
fractions
  where
    go :: (Text, String, Double) -> Rule
    go :: (Text, String, Double) -> Rule
go (Text
name, String
regexPattern, Double
f) = Rule :: Text -> Pattern -> Production -> Rule
Rule
      { name :: Text
name = Text
name
      , pattern :: Pattern
pattern =
        [ String -> PatternItem
regex String
regexPattern
        , Predicate -> PatternItem
Predicate Predicate
isUnitOnly
        ]
      , prod :: Production
prod = \case
        (Token
_:
         Token Dimension a
Volume TVolume.VolumeData{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 -> Maybe Token) -> VolumeData -> Maybe Token
forall a b. (a -> b) -> a -> b
$ Unit -> Double -> VolumeData
volume Unit
u Double
f
        [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
"\\~|(ganz )?genau|präzise|(in )?etwa|ungefähr|um( die)?|fast"
    , 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 =
    [ String -> PatternItem
regex String
"zwischen|von"
    , Predicate -> PatternItem
Predicate Predicate
isPositive
    , String -> PatternItem
regex String
"und|bis( zu)?"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    ]
  , prod :: Production
prod = \case
      (Token
_:
       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 =
    [ String -> PatternItem
regex String
"zwischen|von"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    , String -> PatternItem
regex String
"bis( zu)?|und"
    , Predicate -> PatternItem
Predicate Predicate
isSimpleVolume
    ]
  , prod :: Production
prod = \case
      (Token
_:
       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
"unter|weniger( als)?|höchstens|nicht mehr als"
    , 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
  }

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
"über|mindestens|wenigstens|mehr als|größer( als)?"
      , 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
    }

rules :: [Rule]
rules :: [Rule]
rules = [ Rule
rulePrecision
        , Rule
ruleIntervalBetweenNumeral
        , Rule
ruleIntervalBetween
        , Rule
ruleIntervalMax
        , Rule
ruleIntervalMin
        ]
        [Rule] -> [Rule] -> [Rule]
forall a. [a] -> [a] -> [a]
++ [Rule]
rulesVolumes
        [Rule] -> [Rule] -> [Rule]
forall a. [a] -> [a] -> [a]
++ [Rule]
rulesFractionalVolume