forked from baozkan/duckling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomDimensionExample.hs
82 lines (70 loc) · 2.11 KB
/
CustomDimensionExample.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
-- 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. An additional grant
-- of patent rights can be found in the PATENTS file in the same directory.
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoRebindableSyntax #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}
module Main (main) where
import Control.DeepSeq
import Control.Monad
import Data.Aeson
import Data.Hashable
import Data.Semigroup ((<>))
import Data.Some
import Data.Text (Text)
import Data.Typeable
import GHC.Generics
import Prelude
import qualified Data.HashSet as HashSet
import qualified TextShow as TS
import Duckling.Debug
import Duckling.Locale
import Duckling.Resolve (Resolve(..))
import Duckling.Types
data MyDimension = MyDimension deriving (Eq, Show, Typeable)
instance CustomDimension MyDimension where
type DimensionData MyDimension = MyData
dimRules _ = [myRule]
dimLangRules _ _ = []
dimLocaleRules _ _ = []
dimDependents _ = HashSet.empty
data MyData = MyData
{ iField :: Int
, bField :: Bool
, tField :: Text
}
deriving (Eq, Generic, Hashable, Ord, Show, NFData)
instance Resolve MyData where
type ResolvedValue MyData = MyValue
resolve _ _ MyData{..} = Just
( MyValue $ TS.showt iField <> "," <> TS.showt bField <> "," <> tField
, False )
newtype MyValue = MyValue { value :: Text }
deriving (Eq, Ord, Show)
instance ToJSON MyValue where
toJSON (MyValue value) = object [ "value" .= value ]
myRule :: Rule
myRule = Rule
{ name = "my dimension"
, pattern =
[ regex "my dimension"
]
, prod = \case
(_:_) -> Just . Token (CustomDimension MyDimension) $ MyData
{ iField = 42
, bField = True
, tField = "hello world"
}
_ -> Nothing
}
main :: IO ()
main = do
let en = makeLocale EN Nothing
debug en "input for my dimension" [This (CustomDimension MyDimension)] >>= print