Skip to content

Commit 81d550f

Browse files
committed
Updates to new client library and fixes broken link in README
1 parent c5b9b61 commit 81d550f

File tree

5 files changed

+67
-99
lines changed

5 files changed

+67
-99
lines changed

language/analysis/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This sample demonstrates the use of the [Google Cloud Natural Language API][NL-Docs]
44
for entity recognition.
55

6-
[NL-Docs]: https://cloud.google.com/language/docs/
6+
[NL-Docs]: https://cloud.google.com/natural-language/docs/
77

88
## Java Version
99

language/analysis/pom.xml

+3-8
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@ limitations under the License.
2323
<dependencies>
2424
<!-- [START dependencies] -->
2525
<dependency>
26-
<groupId>com.google.apis</groupId>
27-
<artifactId>google-api-services-language</artifactId>
28-
<version>v1-rev1-1.22.0</version>
29-
</dependency>
30-
<dependency>
31-
<groupId>com.google.api-client</groupId>
32-
<artifactId>google-api-client</artifactId>
33-
<version>1.22.0</version>
26+
<groupId>com.google.cloud</groupId>
27+
<artifactId>google-cloud-language</artifactId>
28+
<version>0.7.0</version>
3429
</dependency>
3530
<dependency>
3631
<groupId>com.google.guava</groupId>

language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java

+40-78
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,21 @@
1616

1717
package com.google.cloud.language.samples;
1818

19-
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
20-
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
21-
import com.google.api.client.http.HttpRequest;
22-
import com.google.api.client.http.HttpRequestInitializer;
23-
import com.google.api.client.json.JsonFactory;
24-
import com.google.api.client.json.jackson2.JacksonFactory;
25-
import com.google.api.services.language.v1.CloudNaturalLanguage;
26-
import com.google.api.services.language.v1.CloudNaturalLanguageScopes;
27-
import com.google.api.services.language.v1.model.AnalyzeEntitiesRequest;
28-
import com.google.api.services.language.v1.model.AnalyzeEntitiesResponse;
29-
import com.google.api.services.language.v1.model.AnalyzeSentimentRequest;
30-
import com.google.api.services.language.v1.model.AnalyzeSentimentResponse;
31-
import com.google.api.services.language.v1.model.AnalyzeSyntaxRequest;
32-
import com.google.api.services.language.v1.model.AnalyzeSyntaxResponse;
33-
import com.google.api.services.language.v1.model.AnnotateTextRequest;
34-
import com.google.api.services.language.v1.model.AnnotateTextResponse;
35-
import com.google.api.services.language.v1.model.Document;
36-
import com.google.api.services.language.v1.model.Entity;
37-
import com.google.api.services.language.v1.model.EntityMention;
38-
import com.google.api.services.language.v1.model.Features;
39-
import com.google.api.services.language.v1.model.Sentiment;
40-
import com.google.api.services.language.v1.model.Token;
19+
import com.google.cloud.language.spi.v1.LanguageServiceClient;
20+
21+
import com.google.cloud.language.v1.AnalyzeEntitiesRequest;
22+
import com.google.cloud.language.v1.AnalyzeEntitiesResponse;
23+
import com.google.cloud.language.v1.AnalyzeSentimentResponse;
24+
import com.google.cloud.language.v1.AnalyzeSyntaxRequest;
25+
import com.google.cloud.language.v1.AnalyzeSyntaxResponse;
26+
import com.google.cloud.language.v1.Document;
27+
import com.google.cloud.language.v1.Document.Type;
28+
import com.google.cloud.language.v1.EncodingType;
29+
import com.google.cloud.language.v1.Entity;
30+
import com.google.cloud.language.v1.EntityMention;
31+
import com.google.cloud.language.v1.Sentiment;
32+
import com.google.cloud.language.v1.Token;
33+
import com.google.protobuf.Descriptors;
4134

4235
import java.io.IOException;
4336
import java.io.PrintStream;
@@ -49,16 +42,7 @@
4942
* A sample application that uses the Natural Language API to perform
5043
* entity, sentiment and syntax analysis.
5144
*/
52-
@SuppressWarnings("serial")
5345
public class Analyze {
54-
/**
55-
* Be sure to specify the name of your application. If the application name is {@code null} or
56-
* blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0".
57-
*/
58-
private static final String APPLICATION_NAME = "Google-LanguagAPISample/1.0";
59-
60-
private static final int MAX_RESULTS = 4;
61-
6246
/**
6347
* Detects entities,sentiment and syntax in a document using the Natural Language API.
6448
*/
@@ -73,7 +57,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
7357
String command = args[0];
7458
String text = args[1];
7559

76-
Analyze app = new Analyze(getLanguageService());
60+
Analyze app = new Analyze(createLanguageService());
7761

7862
if (command.equals("entities")) {
7963
printEntities(System.out, app.analyzeEntities(text));
@@ -97,15 +81,17 @@ public static void printEntities(PrintStream out, List<Entity> entities) {
9781
out.printf("%s\n", entity.getName());
9882
out.printf("\tSalience: %.3f\n", entity.getSalience());
9983
out.printf("\tType: %s\n", entity.getType());
100-
if (entity.getMetadata() != null) {
101-
for (Map.Entry<String, String> metadata : entity.getMetadata().entrySet()) {
84+
if (entity.getMetadataMap() != null) {
85+
for (Map.Entry<String, String> metadata : entity.getMetadataMap().entrySet()) {
10286
out.printf("\tMetadata: %s = %s\n", metadata.getKey(), metadata.getValue());
10387
}
10488
}
105-
if (entity.getMentions() != null) {
106-
for (EntityMention mention : entity.getMentions()) {
107-
for (Map.Entry<String, Object> mentionSetMember : mention.entrySet()) {
108-
out.printf("\tMention: %s = %s\n", mentionSetMember.getKey(), mentionSetMember.getValue());
89+
if (entity.getMentionsList() != null) {
90+
for (EntityMention mention : entity.getMentionsList()) {
91+
for (Map.Entry<Descriptors.FieldDescriptor, Object> mentionSetMember :
92+
mention.getAllFields().entrySet()) {
93+
out.printf("\tMention: %s = %s\n", mentionSetMember.getKey(),
94+
mentionSetMember.getValue());
10995
}
11096
}
11197
}
@@ -157,29 +143,16 @@ public static void printSyntax(PrintStream out, List<Token> tokens) {
157143
/**
158144
* Connects to the Natural Language API using Application Default Credentials.
159145
*/
160-
public static CloudNaturalLanguage getLanguageService()
161-
throws IOException, GeneralSecurityException {
162-
GoogleCredential credential =
163-
GoogleCredential.getApplicationDefault().createScoped(CloudNaturalLanguageScopes.all());
164-
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
165-
return new CloudNaturalLanguage.Builder(
166-
GoogleNetHttpTransport.newTrustedTransport(),
167-
jsonFactory, new HttpRequestInitializer() {
168-
@Override
169-
public void initialize(HttpRequest request) throws IOException {
170-
credential.initialize(request);
171-
}
172-
})
173-
.setApplicationName(APPLICATION_NAME)
174-
.build();
146+
public static LanguageServiceClient createLanguageService() throws IOException{
147+
return LanguageServiceClient.create();
175148
}
176149

177-
private final CloudNaturalLanguage languageApi;
150+
private final LanguageServiceClient languageApi;
178151

179152
/**
180153
* Constructs a {@link Analyze} which connects to the Cloud Natural Language API.
181154
*/
182-
public Analyze(CloudNaturalLanguage languageApi) {
155+
public Analyze(LanguageServiceClient languageApi) {
183156
this.languageApi = languageApi;
184157
}
185158

@@ -188,41 +161,30 @@ public Analyze(CloudNaturalLanguage languageApi) {
188161
*/
189162
public List<Entity> analyzeEntities(String text) throws IOException {
190163
AnalyzeEntitiesRequest request =
191-
new AnalyzeEntitiesRequest()
192-
.setDocument(new Document().setContent(text).setType("PLAIN_TEXT"))
193-
.setEncodingType("UTF16");
194-
CloudNaturalLanguage.Documents.AnalyzeEntities analyze =
195-
languageApi.documents().analyzeEntities(request);
196-
197-
AnalyzeEntitiesResponse response = analyze.execute();
198-
return response.getEntities();
164+
AnalyzeEntitiesRequest.newBuilder()
165+
.setDocument(Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT))
166+
.setEncodingType(EncodingType.UTF16).build();
167+
AnalyzeEntitiesResponse response = languageApi.analyzeEntities(request);
168+
return response.getEntitiesList();
199169
}
200170

201171
/**
202172
* Gets {@link Sentiment} from the string {@code text}.
203173
*/
204174
public Sentiment analyzeSentiment(String text) throws IOException {
205-
AnalyzeSentimentRequest request =
206-
new AnalyzeSentimentRequest()
207-
.setDocument(new Document().setContent(text).setType("PLAIN_TEXT"));
208-
CloudNaturalLanguage.Documents.AnalyzeSentiment analyze =
209-
languageApi.documents().analyzeSentiment(request);
210-
211-
AnalyzeSentimentResponse response = analyze.execute();
175+
AnalyzeSentimentResponse response = languageApi.analyzeSentiment(
176+
Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build());
212177
return response.getDocumentSentiment();
213178
}
214179

215180
/**
216181
* Gets {@link Token}s from the string {@code text}.
217182
*/
218183
public List<Token> analyzeSyntax(String text) throws IOException {
219-
AnalyzeSyntaxRequest request =
220-
new AnalyzeSyntaxRequest()
221-
.setDocument(new Document().setContent(text).setType("PLAIN_TEXT"))
222-
.setEncodingType("UTF16");
223-
CloudNaturalLanguage.Documents.AnalyzeSyntax analyze =
224-
languageApi.documents().analyzeSyntax(request);
225-
AnalyzeSyntaxResponse response = analyze.execute();
226-
return response.getTokens();
184+
AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder()
185+
.setDocument(Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build())
186+
.setEncodingType(EncodingType.UTF16).build();
187+
AnalyzeSyntaxResponse response = languageApi.analyzeSyntax(request);
188+
return response.getTokensList();
227189
}
228190
}

language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21-
import com.google.api.services.language.v1.model.Entity;
22-
import com.google.api.services.language.v1.model.Sentiment;
23-
import com.google.api.services.language.v1.model.Token;
21+
import com.google.cloud.language.v1.Entity;
22+
import com.google.cloud.language.v1.Sentiment;
23+
import com.google.cloud.language.v1.Token;
2424

2525
import org.junit.Before;
2626
import org.junit.Test;
@@ -40,7 +40,7 @@ public class AnalyzeIT {
4040
private Analyze analyzeApp;
4141

4242
@Before public void setup() throws Exception {
43-
analyzeApp = new Analyze(Analyze.getLanguageService());
43+
analyzeApp = new Analyze(Analyze.createLanguageService());
4444
}
4545

4646
@Test public void analyzeEntities_withEntities_returnsLarryPage() throws Exception {

language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeTest.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21-
import com.google.api.services.language.v1.model.Entity;
21+
import com.google.cloud.language.v1.Entity;
22+
import com.google.cloud.language.v1.Entity.Builder;
23+
import com.google.cloud.language.v1.Entity.Type;
2224
import com.google.common.collect.ImmutableList;
2325
import com.google.common.collect.ImmutableMap;
2426

@@ -69,13 +71,22 @@ public class AnalyzeTest {
6971
PrintStream out = new PrintStream(bout);
7072
ImmutableList<Entity> entities =
7173
ImmutableList.of(
72-
new Entity().setName("Larry Page").setSalience(0.426f).setType("PERSON").setMetadata(
73-
ImmutableMap.<String, String>builder()
74-
.put("knowledge_graph_mid", "/m/0gjpq")
75-
.put("wikipedia_url", "http://en.wikipedia.org/wiki/index.html?curid=60903")
76-
.build()),
77-
new Entity().setName("search engine").setSalience(0.188f).setType("CONSUMER_GOOD"),
78-
new Entity().setName("something"));
74+
Entity.newBuilder().setName("Larry Page")
75+
.setSalience(0.426f)
76+
.setType(Type.PERSON)
77+
.putAllMetadata(
78+
ImmutableMap.<String, String>builder()
79+
.put("knowledge_graph_mid", "/m/0gjpq")
80+
.put("wikipedia_url",
81+
"http://en.wikipedia.org/wiki/index.html?curid=60903")
82+
.build())
83+
.build(),
84+
Entity.newBuilder()
85+
.setName("search engine")
86+
.setSalience(0.188f)
87+
.setType(Type.CONSUMER_GOOD)
88+
.build(),
89+
Entity.newBuilder().setName("something").build());
7990

8091
// Act
8192
Analyze.printEntities(out, entities);

0 commit comments

Comments
 (0)