Skip to content

Commit 337e361

Browse files
authored
Merge branch 'master' into storagetransfer
2 parents 9618191 + 669d6a6 commit 337e361

File tree

5 files changed

+80
-110
lines changed

5 files changed

+80
-110
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

+43-83
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(LanguageServiceClient.create());
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
}
@@ -154,75 +140,49 @@ public static void printSyntax(PrintStream out, List<Token> tokens) {
154140
}
155141
}
156142

157-
/**
158-
* Connects to the Natural Language API using Application Default Credentials.
159-
*/
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();
175-
}
176143

177-
private final CloudNaturalLanguage languageApi;
144+
private final LanguageServiceClient languageApi;
178145

179146
/**
180147
* Constructs a {@link Analyze} which connects to the Cloud Natural Language API.
181148
*/
182-
public Analyze(CloudNaturalLanguage languageApi) {
149+
public Analyze(LanguageServiceClient languageApi) {
183150
this.languageApi = languageApi;
184151
}
185152

186153
/**
187154
* Gets {@link Entity}s from the string {@code text}.
188155
*/
189156
public List<Entity> analyzeEntities(String text) throws IOException {
190-
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();
157+
Document doc = Document.newBuilder()
158+
.setContent(text).setType(Type.PLAIN_TEXT).build();
159+
AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder()
160+
.setDocument(doc)
161+
.setEncodingType(EncodingType.UTF16).build();
162+
AnalyzeEntitiesResponse response = languageApi.analyzeEntities(request);
163+
return response.getEntitiesList();
199164
}
200165

201166
/**
202167
* Gets {@link Sentiment} from the string {@code text}.
203168
*/
204169
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();
170+
Document doc = Document.newBuilder()
171+
.setContent(text).setType(Type.PLAIN_TEXT).build();
172+
AnalyzeSentimentResponse response = languageApi.analyzeSentiment(doc);
212173
return response.getDocumentSentiment();
213174
}
214175

215176
/**
216177
* Gets {@link Token}s from the string {@code text}.
217178
*/
218179
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();
180+
Document doc = Document.newBuilder()
181+
.setContent(text).setType(Type.PLAIN_TEXT).build();
182+
AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder()
183+
.setDocument(doc)
184+
.setEncodingType(EncodingType.UTF16).build();
185+
AnalyzeSyntaxResponse response = languageApi.analyzeSyntax(request);
186+
return response.getTokensList();
227187
}
228188
}

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
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.spi.v1.LanguageServiceClient;
22+
import com.google.cloud.language.v1.Entity;
23+
import com.google.cloud.language.v1.PartOfSpeech.Tag;
24+
import com.google.cloud.language.v1.Sentiment;
25+
import com.google.cloud.language.v1.Token;
2426

2527
import org.junit.Before;
2628
import org.junit.Test;
@@ -40,7 +42,7 @@ public class AnalyzeIT {
4042
private Analyze analyzeApp;
4143

4244
@Before public void setup() throws Exception {
43-
analyzeApp = new Analyze(Analyze.getLanguageService());
45+
analyzeApp = new Analyze(LanguageServiceClient.create());
4446
}
4547

4648
@Test public void analyzeEntities_withEntities_returnsLarryPage() throws Exception {
@@ -85,11 +87,11 @@ public class AnalyzeIT {
8587
analyzeApp.analyzeSyntax(
8688
"President Obama was elected for the second term");
8789

88-
List<String> got = token.stream().map(e -> e.getPartOfSpeech().getTag())
90+
List<Tag> got = token.stream().map(e -> e.getPartOfSpeech().getTag())
8991
.collect(Collectors.toList());
9092

9193
// Assert
92-
assertThat(got).containsExactly("NOUN", "NOUN", "VERB",
93-
"VERB", "ADP", "DET", "ADJ", "NOUN").inOrder();
94+
assertThat(got).containsExactly(Tag.NOUN, Tag.NOUN, Tag.VERB,
95+
Tag.VERB, Tag.ADP, Tag.DET, Tag.ADJ, Tag.NOUN).inOrder();
9496
}
9597
}

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

+24-11
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

@@ -67,20 +69,31 @@ public class AnalyzeTest {
6769
// Arrange
6870
ByteArrayOutputStream bout = new ByteArrayOutputStream();
6971
PrintStream out = new PrintStream(bout);
72+
73+
// Mock natural-language entities based on actual data.
7074
ImmutableList<Entity> entities =
7175
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"));
79-
80-
// Act
76+
Entity.newBuilder().setName("Larry Page")
77+
.setSalience(0.426f)
78+
.setType(Type.PERSON)
79+
.putAllMetadata(
80+
ImmutableMap.<String, String>builder()
81+
.put("knowledge_graph_mid", "/m/0gjpq")
82+
.put("wikipedia_url",
83+
"http://en.wikipedia.org/wiki/index.html?curid=60903")
84+
.build())
85+
.build(),
86+
Entity.newBuilder()
87+
.setName("search engine")
88+
.setSalience(0.188f)
89+
.setType(Type.CONSUMER_GOOD)
90+
.build(),
91+
Entity.newBuilder().setName("something").build());
92+
93+
// Act on sample code with mock data.
8194
Analyze.printEntities(out, entities);
8295

83-
// Assert
96+
// Assert output from sample matches expected output.
8497
String got = bout.toString();
8598
assertThat(got).contains("Found 3 entities.");
8699
assertThat(got).contains("Larry Page");

0 commit comments

Comments
 (0)