2017-05-25 55 views
0

我正在使用Google Cloud Natural Language API。我的目标是在大块文本中提取句子和情绪,并对它们进行情感分析。Google Cloud Natural Language API - 句子提取(Python 2.7)

我收到以下“unexpected indent”错误。根据我的研究,它似乎不是一个“基本”缩进错误(如流氓空间等)。

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

的代码内的for循环以下行(见下文全码)引起的问题。如果我删除它,问题就会消失。

print(sentence.content) 

另外,如果我提出这个print语句的循环之外,我没有得到一个错误,但只有文字的大块的最后一句印刷(如可以预期)。

我对编程完全陌生 - 所以如果有人能够用非常简单的术语解释我做错了什么,并指出我在正确的方向,我会非常感激。

完整的脚本如下

迈克

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

回答

0

这看起来完全正确的,虽然有可能是一个选项卡/空间的问题潜伏在那里,没有生存被张贴在你的问题。你能让你的文本编辑器显示空白字符吗?通常有一个选项。如果它是一个支持Python的编辑器,则会有一个选项将选项卡更改为空格。

您可以使问题通过删除线

print(sentence.content) 

和更改下列之一

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

正确─白色空间的问题消失。我非常感谢您花时间帮忙。 – Mike

相关问题