2017-05-25 9 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文を移動した場合

print(sentence.content) 

はまた、私はエラーを取得しないが、テキストの大きなブロックの最後の文は(期待できるとして)印刷されます。

私は全く新しいプログラミングです。誰かが私が間違ってやっていることを簡単に説明し、正しい方向に向けると本当に感謝します。完全なスクリプト

マイク

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

Correct-に次のいずれかを変更することにより、離れて行きます。私は本当に助けに時間を割いていただきありがとうございます。 – Mike

関連する問題