|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.language.it; |
| 17 | + |
| 18 | +import static com.google.cloud.language.v1.Document.Type; |
| 19 | +import static com.google.cloud.language.v1.PartOfSpeech.Tag; |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.cloud.language.v1.AnalyzeEntitiesRequest; |
| 24 | +import com.google.cloud.language.v1.AnalyzeEntitiesResponse; |
| 25 | +import com.google.cloud.language.v1.AnalyzeEntitySentimentRequest; |
| 26 | +import com.google.cloud.language.v1.AnalyzeEntitySentimentResponse; |
| 27 | +import com.google.cloud.language.v1.AnalyzeSentimentResponse; |
| 28 | +import com.google.cloud.language.v1.AnalyzeSyntaxRequest; |
| 29 | +import com.google.cloud.language.v1.ClassificationCategory; |
| 30 | +import com.google.cloud.language.v1.ClassifyTextRequest; |
| 31 | +import com.google.cloud.language.v1.ClassifyTextResponse; |
| 32 | +import com.google.cloud.language.v1.Document; |
| 33 | +import com.google.cloud.language.v1.EncodingType; |
| 34 | +import com.google.cloud.language.v1.Entity; |
| 35 | +import com.google.cloud.language.v1.EntityMention; |
| 36 | +import com.google.cloud.language.v1.LanguageServiceClient; |
| 37 | +import com.google.cloud.language.v1.Sentiment; |
| 38 | +import com.google.cloud.language.v1.Token; |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.ArrayList; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.List; |
| 43 | +import org.junit.After; |
| 44 | +import org.junit.Before; |
| 45 | +import org.junit.Test; |
| 46 | + |
| 47 | +public class ITSystemTest { |
| 48 | + |
| 49 | + private static LanguageServiceClient client; |
| 50 | + private static final String ANALYZE_TEXT = |
| 51 | + "Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets."; |
| 52 | + |
| 53 | + @Before |
| 54 | + public void setUp() throws IOException { |
| 55 | + client = LanguageServiceClient.create(); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public void tearDown() { |
| 60 | + client.close(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void analyzeEntitiesFileTest() { |
| 65 | + Document doc = |
| 66 | + Document.newBuilder() |
| 67 | + .setGcsContentUri("gs://cloud-samples-data/language/android.txt") |
| 68 | + .setType(Type.PLAIN_TEXT) |
| 69 | + .build(); |
| 70 | + AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder().setDocument(doc).build(); |
| 71 | + AnalyzeEntitiesResponse response = client.analyzeEntities(request); |
| 72 | + for (Entity entity : response.getEntitiesList()) { |
| 73 | + if (entity.getName().equals("Android")) { |
| 74 | + assertEquals(Entity.Type.CONSUMER_GOOD, entity.getType()); |
| 75 | + assertEquals(0.69612604, entity.getSalience(), 10); |
| 76 | + assertEquals(2, entity.getMentionsCount()); |
| 77 | + assertEquals(2, entity.getMetadataMap().size()); |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void analyzeEntitiesTextTest() { |
| 85 | + Document doc = Document.newBuilder().setContent(ANALYZE_TEXT).setType(Type.PLAIN_TEXT).build(); |
| 86 | + AnalyzeEntitiesRequest request = |
| 87 | + AnalyzeEntitiesRequest.newBuilder() |
| 88 | + .setDocument(doc) |
| 89 | + .setEncodingType(EncodingType.UTF16) |
| 90 | + .build(); |
| 91 | + AnalyzeEntitiesResponse response = client.analyzeEntities(request); |
| 92 | + for (Entity entity : response.getEntitiesList()) { |
| 93 | + if (entity.getName().equals("Android")) { |
| 94 | + assertEquals(Entity.Type.CONSUMER_GOOD, entity.getType()); |
| 95 | + assertEquals(0.69612604, entity.getSalience(), 10); |
| 96 | + assertEquals(2, entity.getMentionsCount()); |
| 97 | + assertEquals(2, entity.getMetadataMap().size()); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void analyzeEntitySentimentFileTest() { |
| 105 | + Document doc = |
| 106 | + Document.newBuilder() |
| 107 | + .setGcsContentUri("gs://cloud-samples-data/language/president.txt") |
| 108 | + .setType(Type.PLAIN_TEXT) |
| 109 | + .build(); |
| 110 | + AnalyzeEntitySentimentRequest request = |
| 111 | + AnalyzeEntitySentimentRequest.newBuilder() |
| 112 | + .setDocument(doc) |
| 113 | + .setEncodingType(EncodingType.UTF16) |
| 114 | + .build(); |
| 115 | + AnalyzeEntitySentimentResponse response = client.analyzeEntitySentiment(request); |
| 116 | + List<String> actual = new ArrayList<>(); |
| 117 | + for (Entity entity : response.getEntitiesList()) { |
| 118 | + actual.add(entity.getName()); |
| 119 | + } |
| 120 | + assertEquals(Arrays.asList("Kennedy", "White House"), actual); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void analyzeEntitySentimentTextWithExpectedResultTest() { |
| 125 | + Document doc = |
| 126 | + Document.newBuilder() |
| 127 | + .setContent( |
| 128 | + "Oranges, grapes, and apples can be found in the cafeterias located in Mountain View, Seattle, and London.") |
| 129 | + .setType(Type.PLAIN_TEXT) |
| 130 | + .build(); |
| 131 | + AnalyzeEntitySentimentRequest request = |
| 132 | + AnalyzeEntitySentimentRequest.newBuilder() |
| 133 | + .setDocument(doc) |
| 134 | + .setEncodingType(EncodingType.UTF16) |
| 135 | + .build(); |
| 136 | + AnalyzeEntitySentimentResponse response = client.analyzeEntitySentiment(request); |
| 137 | + List<String> actual = new ArrayList<>(); |
| 138 | + for (Entity entity : response.getEntitiesList()) { |
| 139 | + actual.add(entity.getName()); |
| 140 | + } |
| 141 | + assertTrue(actual.contains("Seattle")); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void analyzeEntitySentimentTextWithEncodedExpectedResultTest() { |
| 146 | + Document doc = Document.newBuilder().setContent("foo→bar").setType(Type.PLAIN_TEXT).build(); |
| 147 | + AnalyzeEntitySentimentRequest request = |
| 148 | + AnalyzeEntitySentimentRequest.newBuilder() |
| 149 | + .setDocument(doc) |
| 150 | + .setEncodingType(EncodingType.UTF16) |
| 151 | + .build(); |
| 152 | + AnalyzeEntitySentimentResponse response = client.analyzeEntitySentiment(request); |
| 153 | + List<Integer> actual = new ArrayList<>(); |
| 154 | + for (Entity entity : response.getEntitiesList()) { |
| 155 | + for (EntityMention mention : entity.getMentionsList()) { |
| 156 | + actual.add(mention.getText().getBeginOffset()); |
| 157 | + } |
| 158 | + } |
| 159 | + assertTrue(actual.contains(4)); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void analyzeSentimentFileWithReturnPositiveTest() { |
| 164 | + Document doc = |
| 165 | + Document.newBuilder() |
| 166 | + .setGcsContentUri("gs://cloud-samples-data/language/sentiment-positive.txt") |
| 167 | + .setType(Type.PLAIN_TEXT) |
| 168 | + .build(); |
| 169 | + AnalyzeSentimentResponse response = client.analyzeSentiment(doc); |
| 170 | + Sentiment actual = response.getDocumentSentiment(); |
| 171 | + assertTrue(actual.getMagnitude() > 0.0F); |
| 172 | + assertTrue(actual.getScore() > 0.0F); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void analyzeSentimentTextWithReturnPositiveTest() { |
| 177 | + Document doc = |
| 178 | + Document.newBuilder() |
| 179 | + .setContent("Tom Cruise is one of the finest actors in hollywood and a great star!") |
| 180 | + .setType(Type.PLAIN_TEXT) |
| 181 | + .build(); |
| 182 | + Sentiment sentiment = client.analyzeSentiment(doc).getDocumentSentiment(); |
| 183 | + assertTrue(sentiment.getMagnitude() > 0.0F); |
| 184 | + assertTrue(sentiment.getScore() > 0.0F); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void analyzeSentimentFileWithReturnNegativeTest() { |
| 189 | + Document doc = |
| 190 | + Document.newBuilder() |
| 191 | + .setGcsContentUri("gs://cloud-samples-data/language/sentiment-negative.txt") |
| 192 | + .setType(Type.PLAIN_TEXT) |
| 193 | + .build(); |
| 194 | + Sentiment sentiment = client.analyzeSentiment(doc).getDocumentSentiment(); |
| 195 | + assertTrue(sentiment.getMagnitude() > 0.0F); |
| 196 | + assertTrue(sentiment.getScore() < 0.0F); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void analyzeSentimentTextWithReturnNegativeTest() { |
| 201 | + Document doc = |
| 202 | + Document.newBuilder() |
| 203 | + .setContent("That was the worst performance I've seen in a while.") |
| 204 | + .setType(Type.PLAIN_TEXT) |
| 205 | + .build(); |
| 206 | + Sentiment sentiment = client.analyzeSentiment(doc).getDocumentSentiment(); |
| 207 | + assertTrue(sentiment.getMagnitude() > 0.0F); |
| 208 | + assertTrue(sentiment.getScore() < 0.0F); |
| 209 | + } |
| 210 | + |
| 211 | + @Test |
| 212 | + public void analyzeSyntaxFileTest() { |
| 213 | + Document doc = |
| 214 | + Document.newBuilder() |
| 215 | + .setGcsContentUri("gs://cloud-samples-data/language/syntax-sentence.txt") |
| 216 | + .setType(Type.PLAIN_TEXT) |
| 217 | + .build(); |
| 218 | + AnalyzeSyntaxRequest request = |
| 219 | + AnalyzeSyntaxRequest.newBuilder() |
| 220 | + .setDocument(doc) |
| 221 | + .setEncodingType(EncodingType.UTF16) |
| 222 | + .build(); |
| 223 | + List<Token> tokens = client.analyzeSyntax(request).getTokensList(); |
| 224 | + List<Tag> expected = Arrays.asList(Tag.DET, Tag.VERB, Tag.DET, Tag.ADJ, Tag.NOUN, Tag.PUNCT); |
| 225 | + List<Tag> actual = new ArrayList<>(); |
| 226 | + for (Token token : tokens) { |
| 227 | + actual.add(token.getPartOfSpeech().getTag()); |
| 228 | + } |
| 229 | + assertEquals(expected, actual); |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + public void analyzeSyntaxTextTest() { |
| 234 | + Document doc = |
| 235 | + Document.newBuilder() |
| 236 | + .setContent("President Obama was elected for the second term") |
| 237 | + .setType(Type.PLAIN_TEXT) |
| 238 | + .build(); |
| 239 | + AnalyzeSyntaxRequest request = |
| 240 | + AnalyzeSyntaxRequest.newBuilder() |
| 241 | + .setDocument(doc) |
| 242 | + .setEncodingType(EncodingType.UTF16) |
| 243 | + .build(); |
| 244 | + List<Token> tokens = client.analyzeSyntax(request).getTokensList(); |
| 245 | + List<Tag> expected = |
| 246 | + Arrays.asList(Tag.NOUN, Tag.NOUN, Tag.VERB, Tag.VERB, Tag.ADP, Tag.DET, Tag.ADJ, Tag.NOUN); |
| 247 | + List<Tag> actual = new ArrayList<>(); |
| 248 | + for (Token token : tokens) { |
| 249 | + actual.add(token.getPartOfSpeech().getTag()); |
| 250 | + } |
| 251 | + assertEquals(expected, actual); |
| 252 | + } |
| 253 | + |
| 254 | + @Test |
| 255 | + public void classifyFileTest() { |
| 256 | + Document doc = |
| 257 | + Document.newBuilder() |
| 258 | + .setGcsContentUri("gs://cloud-samples-data/language/android.txt") |
| 259 | + .setType(Type.PLAIN_TEXT) |
| 260 | + .build(); |
| 261 | + ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(doc).build(); |
| 262 | + ClassifyTextResponse response = client.classifyText(request); |
| 263 | + List<String> expected = |
| 264 | + Arrays.asList( |
| 265 | + "/Computers & Electronics", |
| 266 | + "/Internet & Telecom/Mobile & Wireless/Mobile Apps & Add-Ons"); |
| 267 | + List<String> actual = new ArrayList<>(); |
| 268 | + for (ClassificationCategory category : response.getCategoriesList()) { |
| 269 | + actual.add(category.getName()); |
| 270 | + } |
| 271 | + assertEquals(expected, actual); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + public void classifyTextTest() { |
| 276 | + Document doc = Document.newBuilder().setContent(ANALYZE_TEXT).setType(Type.PLAIN_TEXT).build(); |
| 277 | + ClassifyTextRequest request = ClassifyTextRequest.newBuilder().setDocument(doc).build(); |
| 278 | + ClassifyTextResponse response = client.classifyText(request); |
| 279 | + List<String> expected = |
| 280 | + Arrays.asList( |
| 281 | + "/Computers & Electronics", |
| 282 | + "/Internet & Telecom/Mobile & Wireless/Mobile Apps & Add-Ons"); |
| 283 | + List<String> actual = new ArrayList<>(); |
| 284 | + for (ClassificationCategory category : response.getCategoriesList()) { |
| 285 | + actual.add(category.getName()); |
| 286 | + } |
| 287 | + assertEquals(expected, actual); |
| 288 | + } |
| 289 | +} |
0 commit comments