diff --git a/language/analysis/README.md b/language/analysis/README.md index cc641081102..b120baf6081 100644 --- a/language/analysis/README.md +++ b/language/analysis/README.md @@ -3,7 +3,7 @@ This sample demonstrates the use of the [Google Cloud Natural Language API][NL-Docs] for entity recognition. -[NL-Docs]: https://cloud.google.com/language/docs/ +[NL-Docs]: https://cloud.google.com/natural-language/docs/ ## Java Version diff --git a/language/analysis/pom.xml b/language/analysis/pom.xml index b378ae0cc38..314192392d4 100644 --- a/language/analysis/pom.xml +++ b/language/analysis/pom.xml @@ -23,14 +23,9 @@ limitations under the License. - com.google.apis - google-api-services-language - v1-rev1-1.22.0 - - - com.google.api-client - google-api-client - 1.22.0 + com.google.cloud + google-cloud-language + 0.7.0 com.google.guava diff --git a/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java b/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java index fd84f68a387..be89dfd6895 100644 --- a/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java +++ b/language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java @@ -16,28 +16,21 @@ package com.google.cloud.language.samples; -import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; -import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; -import com.google.api.client.http.HttpRequest; -import com.google.api.client.http.HttpRequestInitializer; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.json.jackson2.JacksonFactory; -import com.google.api.services.language.v1.CloudNaturalLanguage; -import com.google.api.services.language.v1.CloudNaturalLanguageScopes; -import com.google.api.services.language.v1.model.AnalyzeEntitiesRequest; -import com.google.api.services.language.v1.model.AnalyzeEntitiesResponse; -import com.google.api.services.language.v1.model.AnalyzeSentimentRequest; -import com.google.api.services.language.v1.model.AnalyzeSentimentResponse; -import com.google.api.services.language.v1.model.AnalyzeSyntaxRequest; -import com.google.api.services.language.v1.model.AnalyzeSyntaxResponse; -import com.google.api.services.language.v1.model.AnnotateTextRequest; -import com.google.api.services.language.v1.model.AnnotateTextResponse; -import com.google.api.services.language.v1.model.Document; -import com.google.api.services.language.v1.model.Entity; -import com.google.api.services.language.v1.model.EntityMention; -import com.google.api.services.language.v1.model.Features; -import com.google.api.services.language.v1.model.Sentiment; -import com.google.api.services.language.v1.model.Token; +import com.google.cloud.language.spi.v1.LanguageServiceClient; + +import com.google.cloud.language.v1.AnalyzeEntitiesRequest; +import com.google.cloud.language.v1.AnalyzeEntitiesResponse; +import com.google.cloud.language.v1.AnalyzeSentimentResponse; +import com.google.cloud.language.v1.AnalyzeSyntaxRequest; +import com.google.cloud.language.v1.AnalyzeSyntaxResponse; +import com.google.cloud.language.v1.Document; +import com.google.cloud.language.v1.Document.Type; +import com.google.cloud.language.v1.EncodingType; +import com.google.cloud.language.v1.Entity; +import com.google.cloud.language.v1.EntityMention; +import com.google.cloud.language.v1.Sentiment; +import com.google.cloud.language.v1.Token; +import com.google.protobuf.Descriptors; import java.io.IOException; import java.io.PrintStream; @@ -49,16 +42,7 @@ * A sample application that uses the Natural Language API to perform * entity, sentiment and syntax analysis. */ -@SuppressWarnings("serial") public class Analyze { - /** - * Be sure to specify the name of your application. If the application name is {@code null} or - * blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0". - */ - private static final String APPLICATION_NAME = "Google-LanguagAPISample/1.0"; - - private static final int MAX_RESULTS = 4; - /** * Detects entities,sentiment and syntax in a document using the Natural Language API. */ @@ -73,7 +57,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept String command = args[0]; String text = args[1]; - Analyze app = new Analyze(getLanguageService()); + Analyze app = new Analyze(LanguageServiceClient.create()); if (command.equals("entities")) { printEntities(System.out, app.analyzeEntities(text)); @@ -97,15 +81,17 @@ public static void printEntities(PrintStream out, List entities) { out.printf("%s\n", entity.getName()); out.printf("\tSalience: %.3f\n", entity.getSalience()); out.printf("\tType: %s\n", entity.getType()); - if (entity.getMetadata() != null) { - for (Map.Entry metadata : entity.getMetadata().entrySet()) { + if (entity.getMetadataMap() != null) { + for (Map.Entry metadata : entity.getMetadataMap().entrySet()) { out.printf("\tMetadata: %s = %s\n", metadata.getKey(), metadata.getValue()); } } - if (entity.getMentions() != null) { - for (EntityMention mention : entity.getMentions()) { - for (Map.Entry mentionSetMember : mention.entrySet()) { - out.printf("\tMention: %s = %s\n", mentionSetMember.getKey(), mentionSetMember.getValue()); + if (entity.getMentionsList() != null) { + for (EntityMention mention : entity.getMentionsList()) { + for (Map.Entry mentionSetMember : + mention.getAllFields().entrySet()) { + out.printf("\tMention: %s = %s\n", mentionSetMember.getKey(), + mentionSetMember.getValue()); } } } @@ -154,32 +140,13 @@ public static void printSyntax(PrintStream out, List tokens) { } } - /** - * Connects to the Natural Language API using Application Default Credentials. - */ - public static CloudNaturalLanguage getLanguageService() - throws IOException, GeneralSecurityException { - GoogleCredential credential = - GoogleCredential.getApplicationDefault().createScoped(CloudNaturalLanguageScopes.all()); - JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); - return new CloudNaturalLanguage.Builder( - GoogleNetHttpTransport.newTrustedTransport(), - jsonFactory, new HttpRequestInitializer() { - @Override - public void initialize(HttpRequest request) throws IOException { - credential.initialize(request); - } - }) - .setApplicationName(APPLICATION_NAME) - .build(); - } - private final CloudNaturalLanguage languageApi; + private final LanguageServiceClient languageApi; /** * Constructs a {@link Analyze} which connects to the Cloud Natural Language API. */ - public Analyze(CloudNaturalLanguage languageApi) { + public Analyze(LanguageServiceClient languageApi) { this.languageApi = languageApi; } @@ -187,28 +154,22 @@ public Analyze(CloudNaturalLanguage languageApi) { * Gets {@link Entity}s from the string {@code text}. */ public List analyzeEntities(String text) throws IOException { - AnalyzeEntitiesRequest request = - new AnalyzeEntitiesRequest() - .setDocument(new Document().setContent(text).setType("PLAIN_TEXT")) - .setEncodingType("UTF16"); - CloudNaturalLanguage.Documents.AnalyzeEntities analyze = - languageApi.documents().analyzeEntities(request); - - AnalyzeEntitiesResponse response = analyze.execute(); - return response.getEntities(); + Document doc = Document.newBuilder() + .setContent(text).setType(Type.PLAIN_TEXT).build(); + AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16).build(); + AnalyzeEntitiesResponse response = languageApi.analyzeEntities(request); + return response.getEntitiesList(); } /** * Gets {@link Sentiment} from the string {@code text}. */ public Sentiment analyzeSentiment(String text) throws IOException { - AnalyzeSentimentRequest request = - new AnalyzeSentimentRequest() - .setDocument(new Document().setContent(text).setType("PLAIN_TEXT")); - CloudNaturalLanguage.Documents.AnalyzeSentiment analyze = - languageApi.documents().analyzeSentiment(request); - - AnalyzeSentimentResponse response = analyze.execute(); + Document doc = Document.newBuilder() + .setContent(text).setType(Type.PLAIN_TEXT).build(); + AnalyzeSentimentResponse response = languageApi.analyzeSentiment(doc); return response.getDocumentSentiment(); } @@ -216,13 +177,12 @@ public Sentiment analyzeSentiment(String text) throws IOException { * Gets {@link Token}s from the string {@code text}. */ public List analyzeSyntax(String text) throws IOException { - AnalyzeSyntaxRequest request = - new AnalyzeSyntaxRequest() - .setDocument(new Document().setContent(text).setType("PLAIN_TEXT")) - .setEncodingType("UTF16"); - CloudNaturalLanguage.Documents.AnalyzeSyntax analyze = - languageApi.documents().analyzeSyntax(request); - AnalyzeSyntaxResponse response = analyze.execute(); - return response.getTokens(); + Document doc = Document.newBuilder() + .setContent(text).setType(Type.PLAIN_TEXT).build(); + AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder() + .setDocument(doc) + .setEncodingType(EncodingType.UTF16).build(); + AnalyzeSyntaxResponse response = languageApi.analyzeSyntax(request); + return response.getTokensList(); } } diff --git a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java index 4284dea0f58..aa1a40fddc6 100644 --- a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java +++ b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java @@ -18,9 +18,11 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.api.services.language.v1.model.Entity; -import com.google.api.services.language.v1.model.Sentiment; -import com.google.api.services.language.v1.model.Token; +import com.google.cloud.language.spi.v1.LanguageServiceClient; +import com.google.cloud.language.v1.Entity; +import com.google.cloud.language.v1.PartOfSpeech.Tag; +import com.google.cloud.language.v1.Sentiment; +import com.google.cloud.language.v1.Token; import org.junit.Before; import org.junit.Test; @@ -40,7 +42,7 @@ public class AnalyzeIT { private Analyze analyzeApp; @Before public void setup() throws Exception { - analyzeApp = new Analyze(Analyze.getLanguageService()); + analyzeApp = new Analyze(LanguageServiceClient.create()); } @Test public void analyzeEntities_withEntities_returnsLarryPage() throws Exception { @@ -85,11 +87,11 @@ public class AnalyzeIT { analyzeApp.analyzeSyntax( "President Obama was elected for the second term"); - List got = token.stream().map(e -> e.getPartOfSpeech().getTag()) + List got = token.stream().map(e -> e.getPartOfSpeech().getTag()) .collect(Collectors.toList()); // Assert - assertThat(got).containsExactly("NOUN", "NOUN", "VERB", - "VERB", "ADP", "DET", "ADJ", "NOUN").inOrder(); + assertThat(got).containsExactly(Tag.NOUN, Tag.NOUN, Tag.VERB, + Tag.VERB, Tag.ADP, Tag.DET, Tag.ADJ, Tag.NOUN).inOrder(); } } diff --git a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java index 1e52ae0639f..a1a13c1c032 100644 --- a/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java +++ b/language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java @@ -18,7 +18,9 @@ import static com.google.common.truth.Truth.assertThat; -import com.google.api.services.language.v1.model.Entity; +import com.google.cloud.language.v1.Entity; +import com.google.cloud.language.v1.Entity.Builder; +import com.google.cloud.language.v1.Entity.Type; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -67,20 +69,31 @@ public class AnalyzeTest { // Arrange ByteArrayOutputStream bout = new ByteArrayOutputStream(); PrintStream out = new PrintStream(bout); + + // Mock natural-language entities based on actual data. ImmutableList entities = ImmutableList.of( - new Entity().setName("Larry Page").setSalience(0.426f).setType("PERSON").setMetadata( - ImmutableMap.builder() - .put("knowledge_graph_mid", "/m/0gjpq") - .put("wikipedia_url", "http://en.wikipedia.org/wiki/index.html?curid=60903") - .build()), - new Entity().setName("search engine").setSalience(0.188f).setType("CONSUMER_GOOD"), - new Entity().setName("something")); - - // Act + Entity.newBuilder().setName("Larry Page") + .setSalience(0.426f) + .setType(Type.PERSON) + .putAllMetadata( + ImmutableMap.builder() + .put("knowledge_graph_mid", "/m/0gjpq") + .put("wikipedia_url", + "http://en.wikipedia.org/wiki/index.html?curid=60903") + .build()) + .build(), + Entity.newBuilder() + .setName("search engine") + .setSalience(0.188f) + .setType(Type.CONSUMER_GOOD) + .build(), + Entity.newBuilder().setName("something").build()); + + // Act on sample code with mock data. Analyze.printEntities(out, entities); - // Assert + // Assert output from sample matches expected output. String got = bout.toString(); assertThat(got).contains("Found 3 entities."); assertThat(got).contains("Larry Page");