-- 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.


{-# LANGUAGE GADTs #-}
module Duckling.Ranking.Extraction
  ( extractFeatures
  ) where

import qualified Data.HashMap.Strict as HashMap
import Data.Maybe
import qualified Data.Text as Text
import Prelude
import TextShow (showt)

import Duckling.Dimensions.Types
import Duckling.Duration.Types (DurationData (DurationData))
import qualified Duckling.Duration.Types as TDuration
import Duckling.Ranking.Types
import Duckling.Time.Types (TimeData (TimeData))
import qualified Duckling.Time.Types as TTime
import Duckling.Types


-- | Feature extraction
-- | Features:
-- | 1) Concatenation of the names of the rules involved in parsing `Node`
-- | 2) Concatenation of the grains for time-like dimensions
extractFeatures :: Node -> BagOfFeatures
extractFeatures :: Node -> BagOfFeatures
extractFeatures Node
node =
  [(Text, Int)] -> BagOfFeatures
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(Text, Int)] -> BagOfFeatures) -> [(Text, Int)] -> BagOfFeatures
forall a b. (a -> b) -> a -> b
$ (Text
featRules, Int
1) (Text, Int) -> [(Text, Int)] -> [(Text, Int)]
forall a. a -> [a] -> [a]
: [ (Text
featGrain, Int
1) | Bool -> Bool
not ([Text] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Text]
grains) ]
  where
    featRules :: Text
featRules = [Text] -> Text
Text.concat ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (Node -> Maybe Text) -> [Node] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Node -> Maybe Text
rule (Node -> [Node]
children Node
node)
    grains :: [Text]
grains = (Node -> Maybe Text) -> [Node] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (\Node
x ->
      case Node -> Token
token Node
x of
        Token Dimension a
Duration DurationData{TDuration.grain = g} -> Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ Grain -> Text
forall a. TextShow a => a -> Text
showt Grain
g
        Token Dimension a
Time TimeData{TTime.timeGrain = g} -> Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ Grain -> Text
forall a. TextShow a => a -> Text
showt Grain
g
        Token Dimension a
TimeGrain a
g -> Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ a -> Text
forall a. TextShow a => a -> Text
showt a
g
        Token
_ -> Maybe Text
forall a. Maybe a
Nothing
      ) ([Node] -> [Text]) -> [Node] -> [Text]
forall a b. (a -> b) -> a -> b
$ Node -> [Node]
children Node
node
    featGrain :: Text
featGrain = [Text] -> Text
Text.concat [Text]
grains