This sample demonstrates the use of the Google Cloud Natural Language API for entity recognition.
This sample requires you to have Java8.
Note The Natural Language client is not supported by App Engine Standard.
This sample uses the Apache Maven build system. Before getting started, be sure to download and install it. When you use Maven as described here, it will automatically download the needed client libraries.
Please follow the Set Up Your Project steps in the Quickstart doc to create a project and enable the Cloud Natural Language API. Following those steps, make sure that you Set Up a Service Account, and export the following environment variable:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
To build the sample, we use Maven.
mvn clean compile assembly:single
We can then run the assembled JAR file with the java
command. The variable $COMMAND takes
three values entities
, entities-sentiment
, sentiment
, or syntax
.
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.Analyze \
<entities | entities-sentiment | sentiment | syntax> \
<text | GCS path>
Analyze entities
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.Analyze \
entities \
"The quick brown fox jumped over the lazy dog."
Analyze sentiment
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.Analyze \
sentiment \
"The quick brown fox jumped over the lazy dog."
Analyze entity sentiment
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.Analyze entities-sentiment \
"There's nothing better than searching for ice cream on Google."
Analyze syntax
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.Analyze \
syntax \
"The quick brown fox jumped over the lazy dog."
Analyze categories in text
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.Analyze classify \
"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets."
Analyze categories in GCS file
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.Analyze classify \
"gs://cloud-samples-tests/natural-language/android-text.txt"
Included with the sample are demo.sh
and demo.bat
which show additional
examples of usage.
Run demo from *nix or OSX
demo.sh
Run demo from Windows
demo
Analyze sentiment beta
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.AnalyzeBeta \
sentiment \
"Der schnelle braune Fuchs sprang über den faulen Hund."
Analyze categories in text Beta
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.AnalyzeBeta classify \
"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets."
Analyze categories in GCS file Beta
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.AnalyzeBeta classify \
"gs://cloud-samples-tests/natural-language/android-text.txt"
Run beta demo from *nix or OSX
demo-beta.sh