@@ -57,7 +57,7 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
5757 String command = args [0 ];
5858 String text = args [1 ];
5959
60- Analyze app = new Analyze (createLanguageService ());
60+ Analyze app = new Analyze (LanguageServiceClient . create ());
6161
6262 if (command .equals ("entities" )) {
6363 printEntities (System .out , app .analyzeEntities (text ));
@@ -140,12 +140,6 @@ public static void printSyntax(PrintStream out, List<Token> tokens) {
140140 }
141141 }
142142
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- }
149143
150144 private final LanguageServiceClient languageApi ;
151145
@@ -160,10 +154,11 @@ public Analyze(LanguageServiceClient languageApi) {
160154 * Gets {@link Entity}s from the string {@code text}.
161155 */
162156 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 ();
167162 AnalyzeEntitiesResponse response = languageApi .analyzeEntities (request );
168163 return response .getEntitiesList ();
169164 }
@@ -172,18 +167,21 @@ public List<Entity> analyzeEntities(String text) throws IOException {
172167 * Gets {@link Sentiment} from the string {@code text}.
173168 */
174169 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 );
177173 return response .getDocumentSentiment ();
178174 }
179175
180176 /**
181177 * Gets {@link Token}s from the string {@code text}.
182178 */
183179 public List <Token > analyzeSyntax (String text ) throws IOException {
180+ Document doc = Document .newBuilder ()
181+ .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
184182 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 ();
187185 AnalyzeSyntaxResponse response = languageApi .analyzeSyntax (request );
188186 return response .getTokensList ();
189187 }
0 commit comments