2

I'm using this code from Google NL processor API:

require __DIR__ . '/vendor/autoload.php';

use Google\Cloud\NaturalLanguage\NaturalLanguageClient;

$projectId = 'YOUR_PROJECT_ID';

$language = new NaturalLanguageClient([
    'projectId' => $projectId
]);

$text = 'Hello, world!'; //THIS LINE IS THE ISSUE
# Detects the sentiment of the text
$annotation = $language->analyzeSentiment($text);
$sentiment = $annotation->sentiment();
echo 'Text: ' . $text . '
Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
return $sentiment;

Everything works fine with whatever text I put in. However, the API is capable of analyzing each individual line of text separately (you can try it here: https://cloud.google.com/natural-language/). If you put in two different lines, then you get sentiments for each.

I'd like to recreate this, but no matter how I try to include a line break (using all the methods here: Writing TXT File with PHP, Want to Add an Actual Line Break), it's all being analyzed as one big text block.

Google says that content is sent in the following format (if this helps):

{
 "document":{
  "type":"PLAIN_TEXT",
  "language": "EN",
  "content":"'Lawrence of Arabia' is a highly rated film biography about \
            British Lieutenant T. E. Lawrence. Peter O'Toole plays \
            Lawrence in the film."
  },
  "encodingType":"UTF8"
}
2
  • How are you adding new lines to your text? It would also be helpful to know what the API interprets as new line Commented Dec 19, 2016 at 22:11
  • @dan08 Yes, I tried simply having them on different lines, \n '\r\' and several others. I've looked all through the documentation, but can't find anything. Commented Dec 19, 2016 at 22:12

0

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.