1010import com .mindee .parsing .common .Document ;
1111import com .mindee .parsing .common .Inference ;
1212import com .mindee .parsing .common .PredictResponse ;
13+ import com .mindee .parsing .common .ocr .Ocr ;
1314import com .mindee .product .custom .CustomV1 ;
1415import com .mindee .product .generated .GeneratedV1 ;
1516import java .io .File ;
1617import java .io .IOException ;
1718import java .lang .reflect .Method ;
1819import java .util .ArrayList ;
20+ import java .util .Arrays ;
1921import java .util .List ;
2022import java .util .Objects ;
2123import java .util .concurrent .Callable ;
24+ import java .util .stream .Collectors ;
25+
2226import picocli .CommandLine ;
2327import picocli .CommandLine .Command ;
2428import picocli .CommandLine .Model .CommandSpec ;
@@ -66,7 +70,8 @@ private enum OutputChoices { summary, full, raw }
6670 description = "Output type, one of:\n "
6771 + " summary - document predictions\n "
6872 + " full - all predictions\n "
69- + " raw - raw response from the server"
73+ + " raw - raw response from the server" ,
74+ defaultValue = "summary"
7075 )
7176 private OutputChoices outputType ;
7277
@@ -109,7 +114,6 @@ public static void main(String[] args) {
109114
110115 /**
111116 * Adds all commands from CommandLineInterfaceProducts automatically.
112- * Avoids using a Mixin, which I can't get to work.
113117 */
114118 @ CommandLine .Command (mixinStandardHelpOptions = true , description = "Auto-generated product command" )
115119 public static class ProductCommandHandler implements Callable <Integer > {
@@ -138,7 +142,10 @@ public Integer call() throws Exception {
138142 }
139143 }
140144
141- @ Command (name = "custom" , description = "Invokes a Custom API (API Builder only, use 'generated' for regular custom APIs)" )
145+ @ Command (
146+ name = "custom" ,
147+ description = "Invokes a Custom API (API Builder only, use 'generated' for regular custom APIs)"
148+ )
142149 void customMethod (
143150 @ Option (
144151 names = {"-a" , "--account" },
@@ -261,19 +268,30 @@ void generatedMethod(
261268 }
262269
263270 protected PageOptions getDefaultPageOptions () {
264-
265271 List <Integer > pageNumbers = new ArrayList <>();
266272 pageNumbers .add (0 );
267273 pageNumbers .add (1 );
268274 pageNumbers .add (2 );
269275 pageNumbers .add (3 );
270276 pageNumbers .add (4 );
271-
272277 return new PageOptions (pageNumbers , PageOptionsOperation .KEEP_ONLY );
273278 }
274279
280+ private String wordsOutput (Ocr ocr ) {
281+ StringBuilder output = new StringBuilder ();
282+ output .append ("\n #############\n Document Text\n #############\n ::\n " );
283+ output .append (
284+ Arrays .stream (ocr .toString ().split (String .format ("%n" )))
285+ .collect (Collectors .joining (String .format ("%n " )))
286+ );
287+ output .append ("\n " );
288+ return output .toString ();
289+ }
290+
275291 @ Override
276- public <T extends Inference <?, ?>> String standardProductOutput (Class <T > productClass , File file ) throws IOException {
292+ public <T extends Inference <?, ?>> String standardProductOutput (Class <T > productClass , File file )
293+ throws IOException
294+ {
277295 MindeeClient mindeeClient = new MindeeClient (apiKey );
278296 LocalInputSource inputSource = new LocalInputSource (file );
279297 PredictResponse <T > response ;
@@ -284,18 +302,29 @@ protected PageOptions getDefaultPageOptions() {
284302 response = mindeeClient .parse (productClass , inputSource , predictOptions );
285303 }
286304
287- if (outputType == OutputChoices .full ) {
288- return response .getDocument ().toString ();
305+ StringBuilder output = new StringBuilder ();
306+ switch (outputType ) {
307+ case full :
308+ output .append (response .getDocument ().toString ());
309+ break ;
310+ case raw :
311+ output .append (response .getRawResponse ());
312+ break ;
313+ default :
314+ output .append (
315+ response .getDocument ().getInference ().getPrediction ().toString ()
316+ );
289317 }
290- if (outputType == OutputChoices . raw ) {
291- return response .getRawResponse ( );
318+ if (words ) {
319+ output . append ( wordsOutput ( response .getDocument (). getOcr ()) );
292320 }
293- Document <T > document = response .getDocument ();
294- return document .getInference ().getPrediction ().toString ();
321+ return output .toString ();
295322 }
296323
297324 @ Override
298- public <T extends Inference <?, ?>> String standardProductAsyncOutput (Class <T > productClass , File file ) throws IOException , InterruptedException {
325+ public <T extends Inference <?, ?>> String standardProductAsyncOutput (Class <T > productClass , File file )
326+ throws IOException , InterruptedException
327+ {
299328 MindeeClient mindeeClient = new MindeeClient (apiKey );
300329 LocalInputSource inputSource = new LocalInputSource (file );
301330 AsyncPredictResponse <T > response ;
@@ -310,13 +339,22 @@ productClass, inputSource, predictOptions, getDefaultPageOptions(), null
310339 );
311340 }
312341
313- if (outputType == OutputChoices .full ) {
314- return response .getDocumentObj ().toString ();
342+ StringBuilder output = new StringBuilder ();
343+ switch (outputType ) {
344+ case full :
345+ output .append (response .getDocument ().toString ());
346+ break ;
347+ case raw :
348+ output .append (response .getRawResponse ());
349+ break ;
350+ default :
351+ output .append (
352+ response .getDocumentObj ().getInference ().getPrediction ().toString ()
353+ );
315354 }
316- if (outputType == OutputChoices . raw ) {
317- return response .getRawResponse ( );
355+ if (words ) {
356+ output . append ( wordsOutput ( response .getDocumentObj (). getOcr ()) );
318357 }
319- Document <T > document = response .getDocumentObj ();
320- return document .getInference ().getPrediction ().toString ();
358+ return output .toString ();
321359 }
322360}
0 commit comments