@@ -60,11 +60,23 @@ public static void main(String[] args) throws IOException, GeneralSecurityExcept
6060 Analyze app = new Analyze (LanguageServiceClient .create ());
6161
6262 if (command .equals ("entities" )) {
63- printEntities (System .out , app .analyzeEntities (text ));
63+ if (text .startsWith ("gs://" )) {
64+ printEntities (System .out , app .analyzeEntitiesFile (text ));
65+ } else {
66+ printEntities (System .out , app .analyzeEntitiesText (text ));
67+ }
6468 } else if (command .equals ("sentiment" )) {
65- printSentiment (System .out , app .analyzeSentiment (text ));
69+ if (text .startsWith ("gs://" )) {
70+ printSentiment (System .out , app .analyzeSentimentFile (text ));
71+ } else {
72+ printSentiment (System .out , app .analyzeSentimentText (text ));
73+ }
6674 } else if (command .equals ("syntax" )) {
67- printSyntax (System .out , app .analyzeSyntax (text ));
75+ if (text .startsWith ("gs://" )) {
76+ printSyntax (System .out , app .analyzeSyntaxFile (text ));
77+ } else {
78+ printSyntax (System .out , app .analyzeSyntaxText (text ));
79+ }
6880 }
6981 }
7082
@@ -111,6 +123,9 @@ public static void printSentiment(PrintStream out, Sentiment sentiment) {
111123 out .printf ("\t Score: %.3f\n " , sentiment .getScore ());
112124 }
113125
126+ /**
127+ * Prints the Syntax for the {@code tokens}.
128+ */
114129 public static void printSyntax (PrintStream out , List <Token > tokens ) {
115130 if (tokens == null || tokens .size () == 0 ) {
116131 out .println ("No syntax found" );
@@ -153,7 +168,7 @@ public Analyze(LanguageServiceClient languageApi) {
153168 /**
154169 * Gets {@link Entity}s from the string {@code text}.
155170 */
156- public List <Entity > analyzeEntities (String text ) throws IOException {
171+ public List <Entity > analyzeEntitiesText (String text ) throws IOException {
157172 Document doc = Document .newBuilder ()
158173 .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
159174 AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest .newBuilder ()
@@ -163,20 +178,43 @@ public List<Entity> analyzeEntities(String text) throws IOException {
163178 return response .getEntitiesList ();
164179 }
165180
181+ /**
182+ * Gets {@link Entity}s from the contents of the object at the given GCS {@code path}.
183+ */
184+ public List <Entity > analyzeEntitiesFile (String path ) throws IOException {
185+ Document doc = Document .newBuilder ()
186+ .setGcsContentUri (path ).setType (Type .PLAIN_TEXT ).build ();
187+ AnalyzeEntitiesRequest request = AnalyzeEntitiesRequest .newBuilder ()
188+ .setDocument (doc )
189+ .setEncodingType (EncodingType .UTF16 ).build ();
190+ AnalyzeEntitiesResponse response = languageApi .analyzeEntities (request );
191+ return response .getEntitiesList ();
192+ }
193+
166194 /**
167195 * Gets {@link Sentiment} from the string {@code text}.
168196 */
169- public Sentiment analyzeSentiment (String text ) throws IOException {
197+ public Sentiment analyzeSentimentText (String text ) throws IOException {
170198 Document doc = Document .newBuilder ()
171199 .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
172200 AnalyzeSentimentResponse response = languageApi .analyzeSentiment (doc );
173201 return response .getDocumentSentiment ();
174202 }
175203
204+ /**
205+ * Gets {@link Sentiment} from the contents of the object at the given GCS {@code path}.
206+ */
207+ public Sentiment analyzeSentimentFile (String path ) throws IOException {
208+ Document doc = Document .newBuilder ()
209+ .setGcsContentUri (path ).setType (Type .PLAIN_TEXT ).build ();
210+ AnalyzeSentimentResponse response = languageApi .analyzeSentiment (doc );
211+ return response .getDocumentSentiment ();
212+ }
213+
176214 /**
177215 * Gets {@link Token}s from the string {@code text}.
178216 */
179- public List <Token > analyzeSyntax (String text ) throws IOException {
217+ public List <Token > analyzeSyntaxText (String text ) throws IOException {
180218 Document doc = Document .newBuilder ()
181219 .setContent (text ).setType (Type .PLAIN_TEXT ).build ();
182220 AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest .newBuilder ()
@@ -185,4 +223,17 @@ public List<Token> analyzeSyntax(String text) throws IOException {
185223 AnalyzeSyntaxResponse response = languageApi .analyzeSyntax (request );
186224 return response .getTokensList ();
187225 }
226+
227+ /**
228+ * Gets {@link Token}s from the contents of the object at the given GCS {@code path}.
229+ */
230+ public List <Token > analyzeSyntaxFile (String path ) throws IOException {
231+ Document doc = Document .newBuilder ()
232+ .setGcsContentUri (path ).setType (Type .PLAIN_TEXT ).build ();
233+ AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest .newBuilder ()
234+ .setDocument (doc )
235+ .setEncodingType (EncodingType .UTF16 ).build ();
236+ AnalyzeSyntaxResponse response = languageApi .analyzeSyntax (request );
237+ return response .getTokensList ();
238+ }
188239}
0 commit comments