2017-05-25 54 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

相關問題