mirror of
https://github.com/facebook/duckling.git
synced 2024-11-28 16:54:59 +03:00
2254034e62
Summary: - add `Node` field to `Entity` - ignore `Node` field of `Entity` for toJSON for now (will be fixed later) - change `Debug.hs` so that we print the respective Entity's Node - add wildcard for the new Node field in `parseTest` of Api/Tests.hs` Reviewed By: patapizza Differential Revision: D7174696 fbshipit-source-id: 240e4c53b72323b500ac58a74f873ce247bb3387
64 lines
2.1 KiB
Haskell
64 lines
2.1 KiB
Haskell
-- 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 RecordWildCards #-}
|
|
{-# LANGUAGE NoRebindableSyntax #-}
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
module Duckling.Api
|
|
( analyze
|
|
, formatToken
|
|
, parse
|
|
, supportedDimensions
|
|
) where
|
|
|
|
import Data.HashMap.Strict (HashMap)
|
|
import Data.HashSet (HashSet)
|
|
import Data.Text (Text)
|
|
import Prelude
|
|
import TextShow
|
|
import qualified Data.HashMap.Strict as HashMap
|
|
import qualified Data.HashSet as HashSet
|
|
import qualified Data.Text as Text
|
|
|
|
import Duckling.Dimensions.Types
|
|
import Duckling.Dimensions
|
|
import Duckling.Engine
|
|
import Duckling.Locale
|
|
import Duckling.Ranking.Classifiers
|
|
import Duckling.Ranking.Rank
|
|
import Duckling.Resolve
|
|
import Duckling.Rules
|
|
import Duckling.Types
|
|
|
|
-- | Parses `input` and returns a curated list of entities found.
|
|
parse :: Text -> Context -> [Some Dimension] -> [Entity]
|
|
parse input ctx = map (formatToken input) . analyze input ctx . HashSet.fromList
|
|
|
|
supportedDimensions :: HashMap Lang [Some Dimension]
|
|
supportedDimensions =
|
|
HashMap.fromList [ (l, allDimensions l) | l <- [minBound..maxBound] ]
|
|
|
|
-- | Returns a curated list of resolved tokens found
|
|
-- When `targets` is non-empty, returns only tokens of such dimensions.
|
|
analyze :: Text -> Context -> HashSet (Some Dimension) -> [ResolvedToken]
|
|
analyze input context@Context{..} targets =
|
|
rank (classifiers locale) targets
|
|
. filter (\Resolved{node = Node{token = (Token d _)}} ->
|
|
HashSet.null targets || HashSet.member (This d) targets
|
|
)
|
|
$ parseAndResolve (rulesFor locale targets) input context
|
|
|
|
-- | Converts the resolved token to the API format
|
|
formatToken :: Text -> ResolvedToken -> Entity
|
|
formatToken sentence
|
|
(Resolved (Range start end) node@Node{token = Token dimension _} value) =
|
|
Entity (toName dimension) body value start end node
|
|
where
|
|
body = Text.drop start $ Text.take end sentence
|