Document how to pass dimensions to the example application

Summary: External users are repeatedly confused by lack of results from the duckling example executable. We should just go through all dimensions for the duckling call in the example app.

Reviewed By: patapizza

Differential Revision: D25468199

fbshipit-source-id: 6cf56b130d4d0aa3181f098d6a7c9a133bfa85ff
This commit is contained in:
Daniel Cartwright 2020-12-14 14:54:06 -08:00 committed by Facebook GitHub Bot
parent 12b1db3794
commit 17f11135f2
2 changed files with 16 additions and 4 deletions

View File

@ -32,6 +32,13 @@ This runs a basic HTTP server. Example request:
```
$ curl -XPOST http://0.0.0.0:8000/parse --data 'locale=en_GB&text=tomorrow at eight'
```
In the example application, all dimensions are enabled by default. Provide the parameter `dims` to specify which ones you want. Examples:
```
Identify credit card numbers only:
$ curl -XPOST http://0.0.0.0:8000/parse --data 'locale=en_US&text="4111-1111-1111-1111"&dims="[\"credit-card-number\"]"'
If you want multiple dimensions, comma-separate them in the array:
$ curl -XPOST http://0.0.0.0:8000/parse --data 'locale=en_US&text="3 cups of sugar"&dims="[\"quantity\",\"numeral\"]"'
```
See `exe/ExampleMain.hs` for an example on how to integrate Duckling in your
project.

View File

@ -6,6 +6,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
import Control.Applicative hiding (empty)
import Control.Arrow ((***))
@ -34,6 +35,7 @@ import Snap.Http.Server
import Duckling.Core
import Duckling.Data.TimeZone
import Duckling.Dimensions (allDimensions)
import Duckling.Resolve (DucklingTime)
createIfMissing :: FilePath -> IO ()
@ -92,14 +94,18 @@ parseHandler tzs = do
let timezone = parseTimeZone tz
now <- liftIO $ currentReftime tzs timezone
let
lang = parseLang l
context = Context
{ referenceTime = maybe now (parseRefTime timezone) ref
, locale = maybe (makeLocale (parseLang l) Nothing) parseLocale loc
, locale = maybe (makeLocale lang Nothing) parseLocale loc
}
options = Options {withLatent = parseLatent latent}
dimParse = fromMaybe [] $ decode $ LBS.fromStrict $ fromMaybe "" ds
dims = mapMaybe parseDimension dimParse
dims = fromMaybe (allDimensions lang) $ do
queryDims <- ds
txtDims <- decode @[Text] $ LBS.fromStrict queryDims
pure $ mapMaybe parseDimensions txtDims
parsedResult = parse (Text.decodeUtf8 tx) context options dims
@ -144,4 +150,3 @@ parseHandler tzs = do
parseLatent :: Maybe ByteString -> Bool
parseLatent x = fromMaybe defaultLatent
(readMaybe (Text.unpack $ Text.toTitle $ Text.decodeUtf8 $ fromMaybe empty x)::Maybe Bool)