@@ -57,7 +57,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
57
57
String command = args [0 ];
58
58
String text = args [1 ];
59
59
60
- Analyze app = new Analyze (createLanguageService ());
60
+ Analyze app = new Analyze (LanguageServiceClient . create ());
61
61
62
62
if (command .equals ("entities" )) {
63
63
printEntities (System .out , app .analyzeEntities (text ));
@@ -140,12 +140,6 @@ public static void printSyntax(PrintStream out, List<Token> tokens) {
140
140
}
141
141
}
142
142
143
- /**
144
- * Connects to the Natural Language API using Application Default Credentials.
145
- */
146
- public static LanguageServiceClient createLanguageService () throws IOException {
147
- return LanguageServiceClient .create ();
148
- }
149
143
150
144
private final LanguageServiceClient languageApi ;
151
145
@@ -160,10 +154,11 @@ public Analyze(LanguageServiceClient languageApi) {
160
154
* Gets {@link Entity}s from the string {@code text}.
161
155
*/
162
156
public List <Entity > analyzeEntities (String text ) throws IOException {
163
- AnalyzeEntitiesRequest request =
164
- AnalyzeEntitiesRequest .newBuilder ()
165
- .setDocument (Document .newBuilder ().setContent (text ).setType (Type .PLAIN_TEXT ))
166
- .setEncodingType (EncodingType .UTF16 ).build ();
157
+ Document doc = Document .newBuilder ()
158
+ .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
159
+ AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest .newBuilder ()
160
+ .setDocument (doc )
161
+ .setEncodingType (EncodingType .UTF16 ).build ();
167
162
AnalyzeEntitiesResponse response = languageApi .analyzeEntities (request );
168
163
return response .getEntitiesList ();
169
164
}
@@ -172,18 +167,21 @@ public List<Entity> analyzeEntities(String text) throws IOException {
172
167
* Gets {@link Sentiment} from the string {@code text}.
173
168
*/
174
169
public Sentiment analyzeSentiment (String text ) throws IOException {
175
- AnalyzeSentimentResponse response = languageApi .analyzeSentiment (
176
- Document .newBuilder ().setContent (text ).setType (Type .PLAIN_TEXT ).build ());
170
+ Document doc = Document .newBuilder ()
171
+ .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
172
+ AnalyzeSentimentResponse response = languageApi .analyzeSentiment (doc );
177
173
return response .getDocumentSentiment ();
178
174
}
179
175
180
176
/**
181
177
* Gets {@link Token}s from the string {@code text}.
182
178
*/
183
179
public List <Token > analyzeSyntax (String text ) throws IOException {
180
+ Document doc = Document .newBuilder ()
181
+ .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
184
182
AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest .newBuilder ()
185
- .setDocument (Document . newBuilder (). setContent ( text ). setType ( Type . PLAIN_TEXT ). build () )
186
- .setEncodingType (EncodingType .UTF16 ).build ();
183
+ .setDocument (doc )
184
+ .setEncodingType (EncodingType .UTF16 ).build ();
187
185
AnalyzeSyntaxResponse response = languageApi .analyzeSyntax (request );
188
186
return response .getTokensList ();
189
187
}
0 commit comments