0

I'm using Google's Natural Language API and it's working fine and returning data. I'm just not able to parse it correctly. I'd like to form a JSON object I can then use with AJAX or similar. What I need out of this are mainly the sentences and their sentiment. I'm struggling with this object that I get back:

    object(Google\Cloud\NaturalLanguage\Annotation)#21 (1) {
  ["info":"Google\Cloud\NaturalLanguage\Annotation":private]=>
  array(3) {
    ["documentSentiment"]=>
    array(2) {
      ["magnitude"]=>
      float(1.4)
      ["score"]=>
      int(0)
    }
    ["language"]=>
    string(2) "en"
    ["sentences"]=>
    array(2) {
      [0]=>
      array(2) {
        ["text"]=>
        array(2) {
          ["content"]=>
          string(19) "I love everything!\"
          ["beginOffset"]=>
          int(0)
        }
        ["sentiment"]=>
        array(2) {
          ["magnitude"]=>
          float(0.8)
          ["score"]=>
          float(0.8)
        }
      }
      [1]=>
      array(2) {
        ["text"]=>
        array(2) {
          ["content"]=>
          string(18) "I hate everything!"
          ["beginOffset"]=>
          int(21)
        }
        ["sentiment"]=>
        array(2) {
          ["magnitude"]=>
          float(0.6)
          ["score"]=>
          float(-0.6)
        }
      }
    }
  }
}

ADDED:

The very last bit of my PHP code is:

$annotation = $language->analyzeSentiment($text);
$sentiment = $annotation->sentiment();
echo 'Text: ' . $text . '
Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
return $sentiment;

This successfully returns the score and magnitude for the overall "document" as shown in the part of the array under "documentSentiment". What I need (in addition to this), is the data under sentences. In particular, content, magnitude and score.

7
  • can you show an example of what you want to get, in the desired format. It would then be easier to look at how to build the code structure to get there. Commented Dec 20, 2016 at 3:06
  • @Sean Thanks...added some more detail that I think might help. Commented Dec 20, 2016 at 3:10
  • Using json_encode() and json_decode() you can create a json feed out of an array. I'm not sure if that's what you want tough. Commented Dec 20, 2016 at 3:24
  • Since sentences is an array, you need to loop over the array to get the values from the content, magnitude, and score. Commented Dec 20, 2016 at 3:27
  • @Nicolas I tried that, but couldn't figure out what to actually json_encode(), so never got a good feed. Commented Dec 20, 2016 at 3:28

1 Answer 1

2

Since $sentiment['sentences'] is an array -

["sentences"]=>
    array(2) {
        ...
    }

you need to loop over the values, with foreach() for example -

....  
echo 'Text: ' . $text . '
Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];

foreach($sentiment['sentences'] as $sentence){
    echo 'Text: ' .$sentence["text"]["content"] . '
    Sentiment: ' . $sentence["sentiment"]["score"] . ', ' . $sentence["sentiment"]["magnitude"];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.