{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.Comprehend.DetectSentiment
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Inspects text and returns an inference of the prevailing sentiment
-- (@POSITIVE@, @NEUTRAL@, @MIXED@, or @NEGATIVE@).
module Amazonka.Comprehend.DetectSentiment
  ( -- * Creating a Request
    DetectSentiment (..),
    newDetectSentiment,

    -- * Request Lenses
    detectSentiment_text,
    detectSentiment_languageCode,

    -- * Destructuring the Response
    DetectSentimentResponse (..),
    newDetectSentimentResponse,

    -- * Response Lenses
    detectSentimentResponse_sentiment,
    detectSentimentResponse_sentimentScore,
    detectSentimentResponse_httpStatus,
  )
where

import Amazonka.Comprehend.Types
import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response

-- | /See:/ 'newDetectSentiment' smart constructor.
data DetectSentiment = DetectSentiment'
  { -- | A UTF-8 text string. Each string must contain fewer that 5,000 bytes of
    -- UTF-8 encoded characters.
    DetectSentiment -> Sensitive Text
text :: Core.Sensitive Prelude.Text,
    -- | The language of the input documents. You can specify any of the primary
    -- languages supported by Amazon Comprehend. All documents must be in the
    -- same language.
    DetectSentiment -> LanguageCode
languageCode :: LanguageCode
  }
  deriving (DetectSentiment -> DetectSentiment -> Bool
(DetectSentiment -> DetectSentiment -> Bool)
-> (DetectSentiment -> DetectSentiment -> Bool)
-> Eq DetectSentiment
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DetectSentiment -> DetectSentiment -> Bool
$c/= :: DetectSentiment -> DetectSentiment -> Bool
== :: DetectSentiment -> DetectSentiment -> Bool
$c== :: DetectSentiment -> DetectSentiment -> Bool
Prelude.Eq, Int -> DetectSentiment -> ShowS
[DetectSentiment] -> ShowS
DetectSentiment -> String
(Int -> DetectSentiment -> ShowS)
-> (DetectSentiment -> String)
-> ([DetectSentiment] -> ShowS)
-> Show DetectSentiment
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DetectSentiment] -> ShowS
$cshowList :: [DetectSentiment] -> ShowS
show :: DetectSentiment -> String
$cshow :: DetectSentiment -> String
showsPrec :: Int -> DetectSentiment -> ShowS
$cshowsPrec :: Int -> DetectSentiment -> ShowS
Prelude.Show, (forall x. DetectSentiment -> Rep DetectSentiment x)
-> (forall x. Rep DetectSentiment x -> DetectSentiment)
-> Generic DetectSentiment
forall x. Rep DetectSentiment x -> DetectSentiment
forall x. DetectSentiment -> Rep DetectSentiment x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DetectSentiment x -> DetectSentiment
$cfrom :: forall x. DetectSentiment -> Rep DetectSentiment x
Prelude.Generic)

-- |
-- Create a value of 'DetectSentiment' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'text', 'detectSentiment_text' - A UTF-8 text string. Each string must contain fewer that 5,000 bytes of
-- UTF-8 encoded characters.
--
-- 'languageCode', 'detectSentiment_languageCode' - The language of the input documents. You can specify any of the primary
-- languages supported by Amazon Comprehend. All documents must be in the
-- same language.
newDetectSentiment ::
  -- | 'text'
  Prelude.Text ->
  -- | 'languageCode'
  LanguageCode ->
  DetectSentiment
newDetectSentiment :: Text -> LanguageCode -> DetectSentiment
newDetectSentiment Text
pText_ LanguageCode
pLanguageCode_ =
  DetectSentiment' :: Sensitive Text -> LanguageCode -> DetectSentiment
DetectSentiment'
    { $sel:text:DetectSentiment' :: Sensitive Text
text =
        Tagged Text (Identity Text)
-> Tagged (Sensitive Text) (Identity (Sensitive Text))
forall a. Iso' (Sensitive a) a
Core._Sensitive (Tagged Text (Identity Text)
 -> Tagged (Sensitive Text) (Identity (Sensitive Text)))
-> Text -> Sensitive Text
forall t b. AReview t b -> b -> t
Lens.# Text
pText_,
      $sel:languageCode:DetectSentiment' :: LanguageCode
languageCode = LanguageCode
pLanguageCode_
    }

-- | A UTF-8 text string. Each string must contain fewer that 5,000 bytes of
-- UTF-8 encoded characters.
detectSentiment_text :: Lens.Lens' DetectSentiment Prelude.Text
detectSentiment_text :: (Text -> f Text) -> DetectSentiment -> f DetectSentiment
detectSentiment_text = (DetectSentiment -> Sensitive Text)
-> (DetectSentiment -> Sensitive Text -> DetectSentiment)
-> Lens
     DetectSentiment DetectSentiment (Sensitive Text) (Sensitive Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectSentiment' {Sensitive Text
text :: Sensitive Text
$sel:text:DetectSentiment' :: DetectSentiment -> Sensitive Text
text} -> Sensitive Text
text) (\s :: DetectSentiment
s@DetectSentiment' {} Sensitive Text
a -> DetectSentiment
s {$sel:text:DetectSentiment' :: Sensitive Text
text = Sensitive Text
a} :: DetectSentiment) ((Sensitive Text -> f (Sensitive Text))
 -> DetectSentiment -> f DetectSentiment)
-> ((Text -> f Text) -> Sensitive Text -> f (Sensitive Text))
-> (Text -> f Text)
-> DetectSentiment
-> f DetectSentiment
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (Text -> f Text) -> Sensitive Text -> f (Sensitive Text)
forall a. Iso' (Sensitive a) a
Core._Sensitive

-- | The language of the input documents. You can specify any of the primary
-- languages supported by Amazon Comprehend. All documents must be in the
-- same language.
detectSentiment_languageCode :: Lens.Lens' DetectSentiment LanguageCode
detectSentiment_languageCode :: (LanguageCode -> f LanguageCode)
-> DetectSentiment -> f DetectSentiment
detectSentiment_languageCode = (DetectSentiment -> LanguageCode)
-> (DetectSentiment -> LanguageCode -> DetectSentiment)
-> Lens DetectSentiment DetectSentiment LanguageCode LanguageCode
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectSentiment' {LanguageCode
languageCode :: LanguageCode
$sel:languageCode:DetectSentiment' :: DetectSentiment -> LanguageCode
languageCode} -> LanguageCode
languageCode) (\s :: DetectSentiment
s@DetectSentiment' {} LanguageCode
a -> DetectSentiment
s {$sel:languageCode:DetectSentiment' :: LanguageCode
languageCode = LanguageCode
a} :: DetectSentiment)

instance Core.AWSRequest DetectSentiment where
  type
    AWSResponse DetectSentiment =
      DetectSentimentResponse
  request :: DetectSentiment -> Request DetectSentiment
request = Service -> DetectSentiment -> Request DetectSentiment
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy DetectSentiment
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse DetectSentiment)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse DetectSentiment))
-> Logger
-> Service
-> Proxy DetectSentiment
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse DetectSentiment)))
forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveJSON
      ( \Int
s ResponseHeaders
h Object
x ->
          Maybe SentimentType
-> Maybe SentimentScore -> Int -> DetectSentimentResponse
DetectSentimentResponse'
            (Maybe SentimentType
 -> Maybe SentimentScore -> Int -> DetectSentimentResponse)
-> Either String (Maybe SentimentType)
-> Either
     String (Maybe SentimentScore -> Int -> DetectSentimentResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Either String (Maybe SentimentType)
forall a. FromJSON a => Object -> Text -> Either String (Maybe a)
Core..?> Text
"Sentiment")
            Either
  String (Maybe SentimentScore -> Int -> DetectSentimentResponse)
-> Either String (Maybe SentimentScore)
-> Either String (Int -> DetectSentimentResponse)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Either String (Maybe SentimentScore)
forall a. FromJSON a => Object -> Text -> Either String (Maybe a)
Core..?> Text
"SentimentScore")
            Either String (Int -> DetectSentimentResponse)
-> Either String Int -> Either String DetectSentimentResponse
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Int -> Either String Int
forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (Int -> Int
forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable DetectSentiment

instance Prelude.NFData DetectSentiment

instance Core.ToHeaders DetectSentiment where
  toHeaders :: DetectSentiment -> ResponseHeaders
toHeaders =
    ResponseHeaders -> DetectSentiment -> ResponseHeaders
forall a b. a -> b -> a
Prelude.const
      ( [ResponseHeaders] -> ResponseHeaders
forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"X-Amz-Target"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"Comprehend_20171127.DetectSentiment" ::
                          Prelude.ByteString
                      ),
            HeaderName
"Content-Type"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Core.ToJSON DetectSentiment where
  toJSON :: DetectSentiment -> Value
toJSON DetectSentiment' {Sensitive Text
LanguageCode
languageCode :: LanguageCode
text :: Sensitive Text
$sel:languageCode:DetectSentiment' :: DetectSentiment -> LanguageCode
$sel:text:DetectSentiment' :: DetectSentiment -> Sensitive Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"Text" Text -> Sensitive Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Sensitive Text
text),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"LanguageCode" Text -> LanguageCode -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= LanguageCode
languageCode)
          ]
      )

instance Core.ToPath DetectSentiment where
  toPath :: DetectSentiment -> ByteString
toPath = ByteString -> DetectSentiment -> ByteString
forall a b. a -> b -> a
Prelude.const ByteString
"/"

instance Core.ToQuery DetectSentiment where
  toQuery :: DetectSentiment -> QueryString
toQuery = QueryString -> DetectSentiment -> QueryString
forall a b. a -> b -> a
Prelude.const QueryString
forall a. Monoid a => a
Prelude.mempty

-- | /See:/ 'newDetectSentimentResponse' smart constructor.
data DetectSentimentResponse = DetectSentimentResponse'
  { -- | The inferred sentiment that Amazon Comprehend has the highest level of
    -- confidence in.
    DetectSentimentResponse -> Maybe SentimentType
sentiment :: Prelude.Maybe SentimentType,
    -- | An object that lists the sentiments, and their corresponding confidence
    -- levels.
    DetectSentimentResponse -> Maybe SentimentScore
sentimentScore :: Prelude.Maybe SentimentScore,
    -- | The response's http status code.
    DetectSentimentResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (DetectSentimentResponse -> DetectSentimentResponse -> Bool
(DetectSentimentResponse -> DetectSentimentResponse -> Bool)
-> (DetectSentimentResponse -> DetectSentimentResponse -> Bool)
-> Eq DetectSentimentResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: DetectSentimentResponse -> DetectSentimentResponse -> Bool
$c/= :: DetectSentimentResponse -> DetectSentimentResponse -> Bool
== :: DetectSentimentResponse -> DetectSentimentResponse -> Bool
$c== :: DetectSentimentResponse -> DetectSentimentResponse -> Bool
Prelude.Eq, Int -> DetectSentimentResponse -> ShowS
[DetectSentimentResponse] -> ShowS
DetectSentimentResponse -> String
(Int -> DetectSentimentResponse -> ShowS)
-> (DetectSentimentResponse -> String)
-> ([DetectSentimentResponse] -> ShowS)
-> Show DetectSentimentResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [DetectSentimentResponse] -> ShowS
$cshowList :: [DetectSentimentResponse] -> ShowS
show :: DetectSentimentResponse -> String
$cshow :: DetectSentimentResponse -> String
showsPrec :: Int -> DetectSentimentResponse -> ShowS
$cshowsPrec :: Int -> DetectSentimentResponse -> ShowS
Prelude.Show, (forall x.
 DetectSentimentResponse -> Rep DetectSentimentResponse x)
-> (forall x.
    Rep DetectSentimentResponse x -> DetectSentimentResponse)
-> Generic DetectSentimentResponse
forall x. Rep DetectSentimentResponse x -> DetectSentimentResponse
forall x. DetectSentimentResponse -> Rep DetectSentimentResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep DetectSentimentResponse x -> DetectSentimentResponse
$cfrom :: forall x. DetectSentimentResponse -> Rep DetectSentimentResponse x
Prelude.Generic)

-- |
-- Create a value of 'DetectSentimentResponse' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'sentiment', 'detectSentimentResponse_sentiment' - The inferred sentiment that Amazon Comprehend has the highest level of
-- confidence in.
--
-- 'sentimentScore', 'detectSentimentResponse_sentimentScore' - An object that lists the sentiments, and their corresponding confidence
-- levels.
--
-- 'httpStatus', 'detectSentimentResponse_httpStatus' - The response's http status code.
newDetectSentimentResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  DetectSentimentResponse
newDetectSentimentResponse :: Int -> DetectSentimentResponse
newDetectSentimentResponse Int
pHttpStatus_ =
  DetectSentimentResponse' :: Maybe SentimentType
-> Maybe SentimentScore -> Int -> DetectSentimentResponse
DetectSentimentResponse'
    { $sel:sentiment:DetectSentimentResponse' :: Maybe SentimentType
sentiment =
        Maybe SentimentType
forall a. Maybe a
Prelude.Nothing,
      $sel:sentimentScore:DetectSentimentResponse' :: Maybe SentimentScore
sentimentScore = Maybe SentimentScore
forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:DetectSentimentResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | The inferred sentiment that Amazon Comprehend has the highest level of
-- confidence in.
detectSentimentResponse_sentiment :: Lens.Lens' DetectSentimentResponse (Prelude.Maybe SentimentType)
detectSentimentResponse_sentiment :: (Maybe SentimentType -> f (Maybe SentimentType))
-> DetectSentimentResponse -> f DetectSentimentResponse
detectSentimentResponse_sentiment = (DetectSentimentResponse -> Maybe SentimentType)
-> (DetectSentimentResponse
    -> Maybe SentimentType -> DetectSentimentResponse)
-> Lens
     DetectSentimentResponse
     DetectSentimentResponse
     (Maybe SentimentType)
     (Maybe SentimentType)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectSentimentResponse' {Maybe SentimentType
sentiment :: Maybe SentimentType
$sel:sentiment:DetectSentimentResponse' :: DetectSentimentResponse -> Maybe SentimentType
sentiment} -> Maybe SentimentType
sentiment) (\s :: DetectSentimentResponse
s@DetectSentimentResponse' {} Maybe SentimentType
a -> DetectSentimentResponse
s {$sel:sentiment:DetectSentimentResponse' :: Maybe SentimentType
sentiment = Maybe SentimentType
a} :: DetectSentimentResponse)

-- | An object that lists the sentiments, and their corresponding confidence
-- levels.
detectSentimentResponse_sentimentScore :: Lens.Lens' DetectSentimentResponse (Prelude.Maybe SentimentScore)
detectSentimentResponse_sentimentScore :: (Maybe SentimentScore -> f (Maybe SentimentScore))
-> DetectSentimentResponse -> f DetectSentimentResponse
detectSentimentResponse_sentimentScore = (DetectSentimentResponse -> Maybe SentimentScore)
-> (DetectSentimentResponse
    -> Maybe SentimentScore -> DetectSentimentResponse)
-> Lens
     DetectSentimentResponse
     DetectSentimentResponse
     (Maybe SentimentScore)
     (Maybe SentimentScore)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectSentimentResponse' {Maybe SentimentScore
sentimentScore :: Maybe SentimentScore
$sel:sentimentScore:DetectSentimentResponse' :: DetectSentimentResponse -> Maybe SentimentScore
sentimentScore} -> Maybe SentimentScore
sentimentScore) (\s :: DetectSentimentResponse
s@DetectSentimentResponse' {} Maybe SentimentScore
a -> DetectSentimentResponse
s {$sel:sentimentScore:DetectSentimentResponse' :: Maybe SentimentScore
sentimentScore = Maybe SentimentScore
a} :: DetectSentimentResponse)

-- | The response's http status code.
detectSentimentResponse_httpStatus :: Lens.Lens' DetectSentimentResponse Prelude.Int
detectSentimentResponse_httpStatus :: (Int -> f Int)
-> DetectSentimentResponse -> f DetectSentimentResponse
detectSentimentResponse_httpStatus = (DetectSentimentResponse -> Int)
-> (DetectSentimentResponse -> Int -> DetectSentimentResponse)
-> Lens DetectSentimentResponse DetectSentimentResponse Int Int
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\DetectSentimentResponse' {Int
httpStatus :: Int
$sel:httpStatus:DetectSentimentResponse' :: DetectSentimentResponse -> Int
httpStatus} -> Int
httpStatus) (\s :: DetectSentimentResponse
s@DetectSentimentResponse' {} Int
a -> DetectSentimentResponse
s {$sel:httpStatus:DetectSentimentResponse' :: Int
httpStatus = Int
a} :: DetectSentimentResponse)

instance Prelude.NFData DetectSentimentResponse