Skip to content

Commit 2b15150

Browse files
tswastJerjou Cheng
authored and
Jerjou Cheng
committed
Add Cloud Natural Language API Java sample.
This sample shows how to use the Cloud Natural Language API to do entity recognition. The client libraries are vendored in while we are in alpha. Change-Id: I037901017d0ffb7ffc73cc78c43badaff1dffd3c
1 parent f7741fa commit 2b15150

File tree

7 files changed

+614
-0
lines changed

7 files changed

+614
-0
lines changed

language/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Google Cloud Natural Language API Samples
2+
3+
These samples demonstrate the use of the [Google Cloud Natural Language API][NL-Docs].
4+
5+
[NL-Docs]: https://cloud.google.com/language/docs/
6+
7+
## Prerequisites
8+
9+
### Download Maven
10+
11+
This sample uses the [Apache Maven][maven] build system. Before getting started, be
12+
sure to [download][maven-download] and [install][maven-install] it. When you use
13+
Maven as described here, it will automatically download the needed client
14+
libraries.
15+
16+
[maven]: https://maven.apache.org
17+
[maven-download]: https://maven.apache.org/download.cgi
18+
[maven-install]: https://maven.apache.org/install.html
19+
20+
### Set Up to Authenticate With Your Project's Credentials
21+
22+
Please follow the [Set Up Your Project](https://cloud.google.com/natural-language/docs/getting-started#set_up_your_project)
23+
steps in the Quickstart doc to create a project and enable the
24+
Cloud Natural Language API. Following those steps, make sure that you
25+
[Set Up a Service Account](https://cloud.google.com/natural-language/docs/common/auth#set_up_a_service_account),
26+
and export the following environment variable:
27+
28+
```
29+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
30+
```
31+
32+
[cloud-console]: https://console.cloud.google.com
33+
[language-api]: https://console.cloud.google.com/apis/api/language.googleapis.com/overview?project=_
34+
[adc]: https://cloud.google.com/docs/authentication#developer_workflow
35+
36+
## Samples
37+
38+
- [Analyze](analysis) is a command line tool to show case the features of the API.
39+

language/analysis/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Google Cloud Natural Language API Entity Recognition Sample
2+
3+
This sample demonstrates the use of the [Google Cloud Natural Language API][NL-Docs]
4+
for entity recognition.
5+
6+
[NL-Docs]: https://cloud.google.com/language/docs/
7+
8+
## Java Version
9+
10+
This sample requires you to have
11+
[Java8](https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html).
12+
13+
## Download Maven
14+
15+
This sample uses the [Apache Maven][maven] build system. Before getting started, be
16+
sure to [download][maven-download] and [install][maven-install] it. When you use
17+
Maven as described here, it will automatically download the needed client
18+
libraries.
19+
20+
[maven]: https://maven.apache.org
21+
[maven-download]: https://maven.apache.org/download.cgi
22+
[maven-install]: https://maven.apache.org/install.html
23+
24+
## Run the sample
25+
26+
To build the sample, we use Maven.
27+
28+
```bash
29+
mvn clean compile assembly:single
30+
```
31+
32+
We can then run the assembled JAR file with the `java` command. The variable $COMMAND takes
33+
three values `entities`, `sentiment` or `syntax`.
34+
35+
```
36+
MAIN_CLASS=com.google.cloud.language.samples.Analyze
37+
JAR_FILE=target/entities-1.0-SNAPSHOT-jar-with-dependencies.jar
38+
java -cp $JAR_FILE $MAIN_CLASS <sentiment|entities|syntax> <text>
39+
```
40+
41+
Example usage:
42+
43+
```
44+
QUOTE="Larry Page, Google's co-founder, once described the 'perfect search
45+
engine' as something that 'understands exactly what you mean and gives you
46+
back exactly what you want.' Since he spoke those words Google has grown to
47+
offer products beyond search, but the spirit of what he said remains."
48+
49+
java -cp $JAR_FILE $MAIN_CLASS entities "$QUOTE"
50+
java -cp $JAR_FILE $MAIN_CLASS sentiment "$QUOTE"
51+
java -cp $JAR_FILE $MAIN_CLASS syntax "$QUOTE"
52+
```
53+

language/analysis/pom.xml

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<!--
2+
Copyright 2016 Google Inc. All Rights Reserved.
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+
http://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+
<project>
17+
<modelVersion>4.0.0</modelVersion>
18+
<packaging>jar</packaging>
19+
<version>1.0-SNAPSHOT</version>
20+
<groupId>com.google.cloud.language.samples</groupId>
21+
<artifactId>entities</artifactId>
22+
23+
<dependencies>
24+
<!-- [START dependencies] -->
25+
<dependency>
26+
<groupId>com.google.apis</groupId>
27+
<artifactId>google-api-services-language</artifactId>
28+
<version>v1beta1-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.21.0</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>com.google.guava</groupId>
37+
<artifactId>guava</artifactId>
38+
<version>19.0</version>
39+
</dependency>
40+
<!-- [END dependencies] -->
41+
42+
<!-- Test Dependencies -->
43+
<dependency>
44+
<groupId>junit</groupId>
45+
<artifactId>junit</artifactId>
46+
<version>4.12</version>
47+
</dependency>
48+
<dependency>
49+
<!-- For checking HTTP response codes. -->
50+
<groupId>javax.servlet</groupId>
51+
<artifactId>javax.servlet-api</artifactId>
52+
<version>3.1.0</version>
53+
</dependency>
54+
</dependencies>
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<artifactId>maven-assembly-plugin</artifactId>
59+
<configuration>
60+
<archive>
61+
<manifest>
62+
<mainClass>com.google.cloud.language.samples.entities.AnalyzeEntitiesApp</mainClass>
63+
</manifest>
64+
</archive>
65+
<descriptorRefs>
66+
<descriptorRef>jar-with-dependencies</descriptorRef>
67+
</descriptorRefs>
68+
</configuration>
69+
</plugin>
70+
<plugin>
71+
<groupId>org.apache.maven.plugins</groupId>
72+
<artifactId>maven-failsafe-plugin</artifactId>
73+
<version>2.18.1</version>
74+
<executions>
75+
<execution>
76+
<goals>
77+
<goal>integration-test</goal>
78+
<goal>verify</goal>
79+
</goals>
80+
</execution>
81+
</executions>
82+
</plugin>
83+
<plugin>
84+
<groupId>org.apache.maven.plugins</groupId>
85+
<version>3.3</version>
86+
<artifactId>maven-compiler-plugin</artifactId>
87+
<configuration>
88+
<source>1.8</source>
89+
<target>1.8</target>
90+
</configuration>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
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+
* http://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+
17+
package com.google.cloud.language.samples;
18+
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.v1beta1.CloudNaturalLanguageAPI;
26+
import com.google.api.services.language.v1beta1.CloudNaturalLanguageAPIScopes;
27+
import com.google.api.services.language.v1beta1.model.AnalyzeEntitiesRequest;
28+
import com.google.api.services.language.v1beta1.model.AnalyzeEntitiesResponse;
29+
import com.google.api.services.language.v1beta1.model.AnalyzeSentimentRequest;
30+
import com.google.api.services.language.v1beta1.model.AnalyzeSentimentResponse;
31+
import com.google.api.services.language.v1beta1.model.AnnotateTextRequest;
32+
import com.google.api.services.language.v1beta1.model.AnnotateTextResponse;
33+
import com.google.api.services.language.v1beta1.model.Document;
34+
import com.google.api.services.language.v1beta1.model.Entity;
35+
import com.google.api.services.language.v1beta1.model.Features;
36+
import com.google.api.services.language.v1beta1.model.Sentiment;
37+
import com.google.api.services.language.v1beta1.model.Token;
38+
39+
import java.io.IOException;
40+
import java.io.PrintStream;
41+
import java.security.GeneralSecurityException;
42+
import java.util.List;
43+
import java.util.Map;
44+
45+
/**
46+
* A sample application that uses the Natural Language API to perform
47+
* entity, sentiment and syntax analysis.
48+
*/
49+
@SuppressWarnings("serial")
50+
public class Analyze {
51+
/**
52+
* Be sure to specify the name of your application. If the application name is {@code null} or
53+
* blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0".
54+
*/
55+
private static final String APPLICATION_NAME = "Google-LanguagAPISample/1.0";
56+
57+
private static final int MAX_RESULTS = 4;
58+
59+
/**
60+
* Detects entities,sentiment and syntax in a document using the Natural Language API.
61+
*/
62+
public static void main(String[] args) throws IOException, GeneralSecurityException {
63+
if (args.length != 2) {
64+
System.err.println("Usage:");
65+
System.err.printf(
66+
"\tjava %s \"command\" \"text to analyze\"\n",
67+
Analyze.class.getCanonicalName());
68+
System.exit(1);
69+
}
70+
String command = args[0];
71+
String text = args[1];
72+
73+
Analyze app = new Analyze(getLanguageService());
74+
75+
if (command.equals("entities")) {
76+
printEntities(System.out, app.analyzeEntities(text));
77+
} else if (command.equals("sentiment")) {
78+
printSentiment(System.out, app.analyzeSentiment(text));
79+
} else if (command.equals("syntax")) {
80+
printSyntax(System.out, app.analyzeSyntax(text));
81+
}
82+
}
83+
84+
/**
85+
* Print a list of {@code entities}.
86+
*/
87+
public static void printEntities(PrintStream out, List<Entity> entities) {
88+
if (entities == null || entities.size() == 0) {
89+
out.println("No entities found.");
90+
return;
91+
}
92+
out.printf("Found %d entit%s.\n", entities.size(), entities.size() == 1 ? "y" : "ies");
93+
for (Entity entity : entities) {
94+
out.printf("%s\n", entity.getName());
95+
out.printf("\tSalience: %.3f\n", entity.getSalience());
96+
out.printf("\tType: %s\n", entity.getType());
97+
if (entity.getMetadata() != null) {
98+
for (Map.Entry<String, String> metadata : entity.getMetadata().entrySet()) {
99+
out.printf("\tMetadata: %s = %s\n", metadata.getKey(), metadata.getValue());
100+
}
101+
}
102+
}
103+
}
104+
105+
/**
106+
* Print the Sentiment {@code sentiment}.
107+
*/
108+
public static void printSentiment(PrintStream out, Sentiment sentiment) {
109+
if (sentiment == null) {
110+
out.println("No sentiment found");
111+
return;
112+
}
113+
out.println("Found sentiment.");
114+
out.printf("\tMagnitude: %.3f\n", sentiment.getMagnitude());
115+
out.printf("\tPolarity: %.3f\n", sentiment.getPolarity());
116+
}
117+
118+
public static void printSyntax(PrintStream out, List<Token> tokens) {
119+
if (tokens == null || tokens.size() == 0) {
120+
out.println("No syntax found");
121+
return;
122+
}
123+
out.printf("Found %d token%s.\n", tokens.size(), tokens.size() == 1 ? "" : "s");
124+
for (Token token : tokens) {
125+
out.println("TextSpan");
126+
out.printf("\tText: %s\n", token.getText().getContent());
127+
out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset());
128+
out.printf("Lemma: %s\n", token.getLemma());
129+
out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag());
130+
out.println("DependencyEdge");
131+
out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex());
132+
out.printf("\tLabel: %s\n", token.getDependencyEdge().getLabel());
133+
}
134+
}
135+
136+
/**
137+
* Connects to the Natural Language API using Application Default Credentials.
138+
*/
139+
public static CloudNaturalLanguageAPI getLanguageService()
140+
throws IOException, GeneralSecurityException {
141+
GoogleCredential credential =
142+
GoogleCredential.getApplicationDefault().createScoped(CloudNaturalLanguageAPIScopes.all());
143+
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
144+
return new CloudNaturalLanguageAPI.Builder(
145+
GoogleNetHttpTransport.newTrustedTransport(),
146+
jsonFactory, new HttpRequestInitializer() {
147+
@Override
148+
public void initialize(HttpRequest request) throws IOException {
149+
credential.initialize(request);
150+
}
151+
})
152+
.setApplicationName(APPLICATION_NAME)
153+
.build();
154+
}
155+
156+
private final CloudNaturalLanguageAPI languageApi;
157+
158+
/**
159+
* Constructs a {@link Analyze} which connects to the Cloud Natural Language API.
160+
*/
161+
public Analyze(CloudNaturalLanguageAPI languageApi) {
162+
this.languageApi = languageApi;
163+
}
164+
165+
/**
166+
* Gets {@link Entity}s from the string {@code text}.
167+
*/
168+
public List<Entity> analyzeEntities(String text) throws IOException {
169+
AnalyzeEntitiesRequest request =
170+
new AnalyzeEntitiesRequest()
171+
.setDocument(new Document().setContent(text).setType("PLAIN_TEXT"))
172+
.setEncodingType("UTF16");
173+
CloudNaturalLanguageAPI.Documents.AnalyzeEntities analyze =
174+
languageApi.documents().analyzeEntities(request);
175+
176+
AnalyzeEntitiesResponse response = analyze.execute();
177+
return response.getEntities();
178+
}
179+
180+
/**
181+
* Gets {@link Sentiment} from the string {@code text}.
182+
*/
183+
public Sentiment analyzeSentiment(String text) throws IOException {
184+
AnalyzeSentimentRequest request =
185+
new AnalyzeSentimentRequest()
186+
.setDocument(new Document().setContent(text).setType("PLAIN_TEXT"));
187+
CloudNaturalLanguageAPI.Documents.AnalyzeSentiment analyze =
188+
languageApi.documents().analyzeSentiment(request);
189+
190+
AnalyzeSentimentResponse response = analyze.execute();
191+
return response.getDocumentSentiment();
192+
}
193+
194+
/**
195+
* Gets {@link Token}s from the string {@code text}.
196+
*/
197+
public List<Token> analyzeSyntax(String text) throws IOException {
198+
AnnotateTextRequest request =
199+
new AnnotateTextRequest()
200+
.setDocument(new Document().setContent(text).setType("PLAIN_TEXT"))
201+
.setFeatures(new Features().setExtractSyntax(true))
202+
.setEncodingType("UTF16");
203+
CloudNaturalLanguageAPI.Documents.AnnotateText analyze =
204+
languageApi.documents().annotateText(request);
205+
206+
AnnotateTextResponse response = analyze.execute();
207+
return response.getTokens();
208+
}
209+
}

0 commit comments

Comments
 (0)