Add GCS examples for Natural Language by gguuss · Pull Request #432 · GoogleCloudPlatform/java-docs-samples · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions language/analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,23 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
Analyze app = new Analyze(LanguageServiceClient.create());

if (command.equals("entities")) {
printEntities(System.out, app.analyzeEntities(text));
if (text.startsWith("gs://")) {
printEntities(System.out, app.analyzeEntitiesFile(text));
} else {
printEntities(System.out, app.analyzeEntitiesText(text));
}
} else if (command.equals("sentiment")) {
printSentiment(System.out, app.analyzeSentiment(text));
if (text.startsWith("gs://")) {
printSentiment(System.out, app.analyzeSentimentFile(text));
} else {
printSentiment(System.out, app.analyzeSentimentText(text));
}
} else if (command.equals("syntax")) {
printSyntax(System.out, app.analyzeSyntax(text));
if (text.startsWith("gs://")) {
printSyntax(System.out, app.analyzeSyntaxFile(text));
} else {
printSyntax(System.out, app.analyzeSyntaxText(text));
}
}
}

Expand Down Expand Up @@ -111,6 +123,9 @@ public static void printSentiment(PrintStream out, Sentiment sentiment) {
out.printf("\tScore: %.3f\n", sentiment.getScore());
}

/**
* Prints the Syntax for the {@code tokens}.
*/
public static void printSyntax(PrintStream out, List<Token> tokens) {
if (tokens == null || tokens.size() == 0) {
out.println("No syntax found");
Expand Down Expand Up @@ -153,7 +168,7 @@ public Analyze(LanguageServiceClient languageApi) {
/**
* Gets {@link Entity}s from the string {@code text}.
*/
public List<Entity> analyzeEntities(String text) throws IOException {
public List<Entity> analyzeEntitiesText(String text) throws IOException {
Document doc = Document.newBuilder()
.setContent(text).setType(Type.PLAIN_TEXT).build();
AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder()
Expand All @@ -163,20 +178,43 @@ public List<Entity> analyzeEntities(String text) throws IOException {
return response.getEntitiesList();
}

/**
* Gets {@link Entity}s from the contents of the object at the given GCS {@code path}.
*/
public List<Entity> analyzeEntitiesFile(String path) throws IOException {
Document doc = Document.newBuilder()
.setGcsContentUri(path).setType(Type.PLAIN_TEXT).build();
AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest.newBuilder()
.setDocument(doc)
.setEncodingType(EncodingType.UTF16).build();
AnalyzeEntitiesResponse response = languageApi.analyzeEntities(request);
return response.getEntitiesList();
}

/**
* Gets {@link Sentiment} from the string {@code text}.
*/
public Sentiment analyzeSentiment(String text) throws IOException {
public Sentiment analyzeSentimentText(String text) throws IOException {
Document doc = Document.newBuilder()
.setContent(text).setType(Type.PLAIN_TEXT).build();
AnalyzeSentimentResponse response = languageApi.analyzeSentiment(doc);
return response.getDocumentSentiment();
}

/**
* Gets {@link Sentiment} from the contents of the object at the given GCS {@code path}.
*/
public Sentiment analyzeSentimentFile(String path) throws IOException {
Document doc = Document.newBuilder()
.setGcsContentUri(path).setType(Type.PLAIN_TEXT).build();
AnalyzeSentimentResponse response = languageApi.analyzeSentiment(doc);
return response.getDocumentSentiment();
}

/**
* Gets {@link Token}s from the string {@code text}.
*/
public List<Token> analyzeSyntax(String text) throws IOException {
public List<Token> analyzeSyntaxText(String text) throws IOException {
Document doc = Document.newBuilder()
.setContent(text).setType(Type.PLAIN_TEXT).build();
AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder()
Expand All @@ -185,4 +223,17 @@ public List<Token> analyzeSyntax(String text) throws IOException {
AnalyzeSyntaxResponse response = languageApi.analyzeSyntax(request);
return response.getTokensList();
}

/**
* Gets {@link Token}s from the contents of the object at the given GCS {@code path}.
*/
public List<Token> analyzeSyntaxFile(String path) throws IOException {
Document doc = Document.newBuilder()
.setGcsContentUri(path).setType(Type.PLAIN_TEXT).build();
AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder()
.setDocument(doc)
.setEncodingType(EncodingType.UTF16).build();
AnalyzeSyntaxResponse response = languageApi.analyzeSyntax(request);
return response.getTokensList();
}
}