Skip to content

Commit cfe5f86

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.
1 parent f7741fa commit cfe5f86

File tree

7 files changed

+619
-0
lines changed

7 files changed

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

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

0 commit comments

Comments
 (0)