Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Dimension doesn't return the actual value! #734

Open
sbilgil opened this issue Mar 8, 2024 · 0 comments
Open

Custom Dimension doesn't return the actual value! #734

sbilgil opened this issue Mar 8, 2024 · 0 comments

Comments

@sbilgil
Copy link

sbilgil commented Mar 8, 2024

For our companies need i added some CustomDimension's. I saw that duckling is extracting and returning me the dimension. But it didn't return the value extracted from the text. I looked at the ExampleMain.hs for CustomDimension example. I've tried something to make it work but i couldn't succeed. Here is my last code:

`
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoRebindableSyntax #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}

module AccessKeyPrebuiltDimension (AccessKeyPrebuiltDimension(..)) 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.Text (unpack)
import Data.Typeable
import GHC.Generics
import Prelude
import qualified Data.HashSet as HashSet
import qualified TextShow as TS

import Duckling.Debug
import Debug.Trace
import Duckling.Locale
import Duckling.Regex.Types (GroupMatch(..))
import Duckling.Resolve (Resolve(..))
import Duckling.Types

data AccessKeyPrebuiltDimension = AccessKeyPrebuiltDimension deriving (Eq, Show, Typeable)

instance CustomDimension AccessKeyPrebuiltDimension where
type DimensionData AccessKeyPrebuiltDimension = ResponseData
dimRules _ = [accessKeyRule1, accessKeyRule2]
dimLangRules _ _ = []
dimLocaleRules _ _ = []
dimDependents _ = HashSet.empty

data ResponseData = ResponseData
{ iField :: Int
, bField :: Bool
, tField :: Text
}
deriving (Eq, Generic, Hashable, Ord, Show, NFData)

instance Resolve ResponseData where
type ResolvedValue ResponseData = ResponseValue
resolve _ _ ResponseData{..} = Just
( ResponseValue $ TS.showt iField <> "," <> TS.showt bField <> "," <> tField
, False )

newtype ResponseValue = ResponseValue { value :: Text }
deriving (Eq, Ord, Show)

instance ToJSON ResponseValue where
toJSON (ResponseValue value) = object [ "value" .= value ]

accessKeyPrebuiltDimensionPredicate :: Predicate
accessKeyPrebuiltDimensionPredicate (Token (CustomDimension (dim :: a)) dimData)
| Just Refl <- eqT @A @AccessKeyPrebuiltDimension, ResponseData{..} <- dimData =
iField == 42 && bField
accessKeyPrebuiltDimensionPredicate _ = False

accessKeyRule1 :: Rule
accessKeyRule1 = Rule
{ name = "AccessKey rule1"
, pattern =
[ regex "(?<![A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])"
]

, prod = \case
(:) -> Just . Token (CustomDimension AccessKeyPrebuiltDimension) $ ResponseData
{ iField = 1
, bField = True
, tField = "AccessRule1"
}
_ -> Nothing
}

accessKeyRule2 :: Rule
accessKeyRule2 = Rule
{ name = "AccessKey rule2"
, pattern =
[ regex "(?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])"
]

, prod = \case
(:) -> Just . Token (CustomDimension AccessKeyPrebuiltDimension) $ ResponseData
{ iField = 1
, bField = True
, tField = "AccessRule2"
}
_ -> Nothing
}

`

This code result in new dimension but as i said before duckling response is like this:

[ { "body": "Js6IDrwAIkvSY+8fSJ5bcep05ENlNvXgc+JRRr7Y", "start": 0, "value": { "value": "1,True,swiftCode" }, "end": 40, "dim": "AccessKeyPrebuiltDimension", "latent": false } ]

But actually i want the extracted value in "value" item. How i can succeed it? I've looked at other dimensions like credit-card and phone-number etc. And i tried to rewrite rule like this:

accessKeyRule2 :: Rule accessKeyRule2 = Rule { name = "AccessKey rule2" , pattern = [ regex "(?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])" ] , prod = \case (Token RegexMatch (GroupMatch (code:_)):_) -> Just . Token (CustomDimension AccessKeyPrebuiltDimension) $ ResponseData { iField = 1 , bField = True , tField = code } _ -> Nothing }

but this time duckling start not returning anything but "[]"

I dont know how to return extracted value in "value" item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant