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