0
votes

I am working with the Google Cloud Natural Language API . My goal is to extract the sentences and sentiment inside a larger block of text and run sentiment analysis on them.

I am getting the following "unexpected indent" error. Based on my research, its doesn't appear to be a "basic" indent error (such as an rogue space etc.).

print('Sentence {} has a sentiment score of {}'.format(index,sentence_sentiment)
IndentationError:unexpected indent

the following line of code inside the for loop (see full code below) is causing the problem. If I remove it the issue goes away.

 print(sentence.content)

Also if I move this print statement outside the loop, I don't get an error, but only the last sentence of the large block of text is printed (as could be expected).

I am totally new to programming - so if someone can explain what I am doing wrong in very simple terms and point me in the right direction I would be really appreciative.

Full script below

Mike

from google.cloud import language

text = 'Terrible, Terrible service. I cant believe how bad this was.'
client = language.Client()
document = client.document_from_text(text)
sent_analysis = document.analyze_sentiment()
sentiment = sent_analysis.sentiment
annotations = document.annotate_text(include_sentiment=True, include_syntax=True, include_entities=True)

print ('this is the full text to be analysed:')
print(text)
print('Here is the sentiment score and magnitude for the full text')
print(sentiment.score, sentiment.magnitude)

#now for the  individual sentence analyses
for index, sentence in enumerate(annotations.sentences):
    sentence_sentiment = sentence.sentiment.score
    print(sentence.content)
    print('Sentence {} has a sentiment score of {}'.format(index, sentence_sentiment))
1

1 Answers

0
votes

This looks completely correct, though there may be a tab/space issue lurking there that did not survive being posted in your question. Can you get your text editor to display whitespace characters? There is usually an option to that. If it is a Python-aware editor, there will be a option to change tabs to spaces.

You may be able to make the problem go away by deleting the line

print(sentence.content)

and changing the following one to

print('{}\nSentence {} has a sentiment score of {}'.format(sentence.content, index, sentence_sentiment))