7
7
import com .mindee .input .PageOptions ;
8
8
import com .mindee .input .PageOptionsOperation ;
9
9
import com .mindee .parsing .common .AsyncPredictResponse ;
10
- import com .mindee .parsing .common .Document ;
11
10
import com .mindee .parsing .common .Inference ;
12
11
import com .mindee .parsing .common .PredictResponse ;
12
+ import com .mindee .parsing .common .ocr .Ocr ;
13
13
import com .mindee .product .custom .CustomV1 ;
14
14
import com .mindee .product .generated .GeneratedV1 ;
15
15
import java .io .File ;
16
16
import java .io .IOException ;
17
17
import java .lang .reflect .Method ;
18
18
import java .util .ArrayList ;
19
+ import java .util .Arrays ;
19
20
import java .util .List ;
20
21
import java .util .Objects ;
21
22
import java .util .concurrent .Callable ;
23
+ import java .util .stream .Collectors ;
22
24
import picocli .CommandLine ;
23
25
import picocli .CommandLine .Command ;
24
26
import picocli .CommandLine .Model .CommandSpec ;
@@ -66,7 +68,8 @@ private enum OutputChoices { summary, full, raw }
66
68
description = "Output type, one of:\n "
67
69
+ " summary - document predictions\n "
68
70
+ " full - all predictions\n "
69
- + " raw - raw response from the server"
71
+ + " raw - raw response from the server" ,
72
+ defaultValue = "summary"
70
73
)
71
74
private OutputChoices outputType ;
72
75
@@ -110,7 +113,6 @@ public static void main(String[] args) {
110
113
111
114
/**
112
115
* Adds all commands from CommandLineInterfaceProducts automatically.
113
- * Avoids using a Mixin, which I can't get to work.
114
116
*/
115
117
@ CommandLine .Command (mixinStandardHelpOptions = true , description = "Auto-generated product command" )
116
118
public static class ProductCommandHandler implements Callable <Integer > {
@@ -139,7 +141,10 @@ public Integer call() throws Exception {
139
141
}
140
142
}
141
143
142
- @ Command (name = "custom" , description = "Invokes a Custom API (API Builder only, use 'generated' for regular custom APIs)" )
144
+ @ Command (
145
+ name = "custom" ,
146
+ description = "Invokes a Custom API (API Builder only, use 'generated' for regular custom APIs)"
147
+ )
143
148
void customMethod (
144
149
@ Option (
145
150
names = {"-a" , "--account" },
@@ -262,19 +267,29 @@ void generatedMethod(
262
267
}
263
268
264
269
protected PageOptions getDefaultPageOptions () {
265
-
266
270
List <Integer > pageNumbers = new ArrayList <>();
267
271
pageNumbers .add (0 );
268
272
pageNumbers .add (1 );
269
273
pageNumbers .add (2 );
270
274
pageNumbers .add (3 );
271
275
pageNumbers .add (4 );
272
-
273
276
return new PageOptions (pageNumbers , PageOptionsOperation .KEEP_ONLY );
274
277
}
275
278
279
+ private String wordsOutput (Ocr ocr ) {
280
+ StringBuilder output = new StringBuilder ();
281
+ output .append ("\n #############\n Document Text\n #############\n ::\n " );
282
+ output .append (
283
+ Arrays .stream (ocr .toString ().split (String .format ("%n" )))
284
+ .collect (Collectors .joining (String .format ("%n " )))
285
+ );
286
+ output .append ("\n " );
287
+ return output .toString ();
288
+ }
289
+
276
290
@ Override
277
- public <T extends Inference <?, ?>> String standardProductOutput (Class <T > productClass , File file ) throws IOException {
291
+ public <T extends Inference <?, ?>> String standardProductOutput (Class <T > productClass , File file )
292
+ throws IOException {
278
293
MindeeClient mindeeClient = new MindeeClient (apiKey );
279
294
LocalInputSource inputSource = new LocalInputSource (file );
280
295
PredictResponse <T > response ;
@@ -285,18 +300,28 @@ protected PageOptions getDefaultPageOptions() {
285
300
response = mindeeClient .parse (productClass , inputSource , predictOptions );
286
301
}
287
302
288
- if (outputType == OutputChoices .full ) {
289
- return response .getDocument ().toString ();
303
+ StringBuilder output = new StringBuilder ();
304
+ switch (outputType ) {
305
+ case full :
306
+ output .append (response .getDocument ().toString ());
307
+ break ;
308
+ case raw :
309
+ output .append (response .getRawResponse ());
310
+ break ;
311
+ default :
312
+ output .append (
313
+ response .getDocument ().getInference ().getPrediction ().toString ()
314
+ );
290
315
}
291
- if (outputType == OutputChoices . raw ) {
292
- return response .getRawResponse ( );
316
+ if (words ) {
317
+ output . append ( wordsOutput ( response .getDocument (). getOcr ()) );
293
318
}
294
- Document <T > document = response .getDocument ();
295
- return document .getInference ().getPrediction ().toString ();
319
+ return output .toString ();
296
320
}
297
321
298
322
@ Override
299
- public <T extends Inference <?, ?>> String standardProductAsyncOutput (Class <T > productClass , File file ) throws IOException , InterruptedException {
323
+ public <T extends Inference <?, ?>> String standardProductAsyncOutput (Class <T > productClass , File file )
324
+ throws IOException , InterruptedException {
300
325
MindeeClient mindeeClient = new MindeeClient (apiKey );
301
326
LocalInputSource inputSource = new LocalInputSource (file );
302
327
AsyncPredictResponse <T > response ;
@@ -311,13 +336,22 @@ productClass, inputSource, predictOptions, getDefaultPageOptions(), null
311
336
);
312
337
}
313
338
314
- if (outputType == OutputChoices .full ) {
315
- return response .getDocumentObj ().toString ();
339
+ StringBuilder output = new StringBuilder ();
340
+ switch (outputType ) {
341
+ case full :
342
+ output .append (response .getDocument ().toString ());
343
+ break ;
344
+ case raw :
345
+ output .append (response .getRawResponse ());
346
+ break ;
347
+ default :
348
+ output .append (
349
+ response .getDocumentObj ().getInference ().getPrediction ().toString ()
350
+ );
316
351
}
317
- if (outputType == OutputChoices . raw ) {
318
- return response .getRawResponse ( );
352
+ if (words ) {
353
+ output . append ( wordsOutput ( response .getDocumentObj (). getOcr ()) );
319
354
}
320
- Document <T > document = response .getDocumentObj ();
321
- return document .getInference ().getPrediction ().toString ();
355
+ return output .toString ();
322
356
}
323
357
}
0 commit comments