1custom_content: | 2 #### Analyzing sentiment 3 With Cloud Natural Language, you can analyze the sentiment of text. Add the following imports at the top of your file: 4 5 ``` java 6 import com.google.cloud.language.v1.LanguageServiceClient; 7 import com.google.cloud.language.v1.Document; 8 import com.google.cloud.language.v1.Document.Type; 9 import com.google.cloud.language.v1.Sentiment; 10 ``` 11 Then, to analyze the sentiment of some text, use the following code: 12 13 ``` java 14 // Instantiates a client 15 LanguageServiceClient language = LanguageServiceClient.create(); 16 17 // The text to analyze 18 String[] texts = {"I love this!", "I hate this!"}; 19 for (String text : texts) { 20 Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build(); 21 // Detects the sentiment of the text 22 Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment(); 23 24 System.out.printf("Text: \"%s\"%n", text); 25 System.out.printf( 26 "Sentiment: score = %s, magnitude = %s%n", 27 sentiment.getScore(), sentiment.getMagnitude()); 28 } 29 ``` 30 31 #### Complete source code 32 33 In [AnalyzeSentiment.java](https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-examples/src/main/java/com/google/cloud/examples/language/snippets/AnalyzeSentiment.java) we put the code shown above into a complete program.