module Duckling.Api
( analyze
, formatToken
, parse
, supportedDimensions
) where
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.HashSet (HashSet)
import qualified Data.HashSet as HashSet
import Data.Text (Text)
import qualified Data.Text as Text
import Prelude
import TextShow
import Duckling.Dimensions.Types
import Duckling.Dimensions
import Duckling.Engine
import Duckling.Lang
import Duckling.Ranking.Classifiers
import Duckling.Ranking.Rank
import Duckling.Resolve
import Duckling.Rules
import Duckling.Types
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] ]
analyze :: Text -> Context -> HashSet (Some Dimension) -> [ResolvedToken]
analyze input context@Context{..} targets =
rank (classifiers lang) targets
. filter (\(Resolved{node = Node{token = (Token d _)}}) ->
HashSet.null targets || HashSet.member (This d) targets
)
$ parseAndResolve (rulesFor lang targets) input context
formatToken :: Text -> ResolvedToken -> Entity
formatToken sentence (Resolved (Range start end) (Node{token=Token dimension _}) jsonValue) =
Entity (toName dimension) body val start end
where
body = Text.drop start $ Text.take end sentence
val = toJText jsonValue