Skip to content

Commit d7f1d7f

Browse files
authored
Merge pull request GoogleCloudPlatform#275 from GoogleCloudPlatform/nl
Add Cloud Natural Language API Java sample.
2 parents f7741fa + baf7df5 commit d7f1d7f

File tree

7 files changed

+613
-0
lines changed

7 files changed

+613
-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

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
<groupId>com.google.truth</groupId>
50+
<artifactId>truth</artifactId>
51+
<version>0.28</version>
52+
</dependency>
53+
</dependencies>
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<artifactId>maven-assembly-plugin</artifactId>
58+
<configuration>
59+
<archive>
60+
<manifest>
61+
<mainClass>com.google.cloud.language.samples.entities.AnalyzeEntitiesApp</mainClass>
62+
</manifest>
63+
</archive>
64+
<descriptorRefs>
65+
<descriptorRef>jar-with-dependencies</descriptorRef>
66+
</descriptorRefs>
67+
</configuration>
68+
</plugin>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-failsafe-plugin</artifactId>
72+
<version>2.18.1</version>
73+
<executions>
74+
<execution>
75+
<goals>
76+
<goal>integration-test</goal>
77+
<goal>verify</goal>
78+
</goals>
79+
</execution>
80+
</executions>
81+
</plugin>
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<version>3.3</version>
85+
<artifactId>maven-compiler-plugin</artifactId>
86+
<configuration>
87+
<source>1.8</source>
88+
<target>1.8</target>
89+
</configuration>
90+
</plugin>
91+
</plugins>
92+
</build>
93+
</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)