diff --git a/src/main/java/com/mindee/CommandLineInterface.java b/src/main/java/com/mindee/CommandLineInterface.java index e7cd4943e..0fc0fd1f4 100644 --- a/src/main/java/com/mindee/CommandLineInterface.java +++ b/src/main/java/com/mindee/CommandLineInterface.java @@ -7,18 +7,20 @@ import com.mindee.input.PageOptions; import com.mindee.input.PageOptionsOperation; import com.mindee.parsing.common.AsyncPredictResponse; -import com.mindee.parsing.common.Document; import com.mindee.parsing.common.Inference; import com.mindee.parsing.common.PredictResponse; +import com.mindee.parsing.common.ocr.Ocr; import com.mindee.product.custom.CustomV1; import com.mindee.product.generated.GeneratedV1; import java.io.File; import java.io.IOException; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.concurrent.Callable; +import java.util.stream.Collectors; import picocli.CommandLine; import picocli.CommandLine.Command; import picocli.CommandLine.Model.CommandSpec; @@ -66,7 +68,8 @@ private enum OutputChoices { summary, full, raw } description = "Output type, one of:\n" + " summary - document predictions\n" + " full - all predictions\n" - + " raw - raw response from the server" + + " raw - raw response from the server", + defaultValue = "summary" ) private OutputChoices outputType; @@ -109,7 +112,6 @@ public static void main(String[] args) { /** * Adds all commands from CommandLineInterfaceProducts automatically. - * Avoids using a Mixin, which I can't get to work. */ @CommandLine.Command(mixinStandardHelpOptions = true, description = "Auto-generated product command") public static class ProductCommandHandler implements Callable { @@ -138,7 +140,10 @@ public Integer call() throws Exception { } } - @Command(name = "custom", description = "Invokes a Custom API (API Builder only, use 'generated' for regular custom APIs)") + @Command( + name = "custom", + description = "Invokes a Custom API (API Builder only, use 'generated' for regular custom APIs)" + ) void customMethod( @Option( names = {"-a", "--account"}, @@ -261,19 +266,29 @@ void generatedMethod( } protected PageOptions getDefaultPageOptions() { - List pageNumbers = new ArrayList<>(); pageNumbers.add(0); pageNumbers.add(1); pageNumbers.add(2); pageNumbers.add(3); pageNumbers.add(4); - return new PageOptions(pageNumbers, PageOptionsOperation.KEEP_ONLY); } + private String wordsOutput(Ocr ocr) { + StringBuilder output = new StringBuilder(); + output.append("\n#############\nDocument Text\n#############\n::\n "); + output.append( + Arrays.stream(ocr.toString().split(String.format("%n"))) + .collect(Collectors.joining(String.format("%n "))) + ); + output.append("\n"); + return output.toString(); + } + @Override - public > String standardProductOutput(Class productClass, File file) throws IOException { + public > String standardProductOutput(Class productClass, File file) + throws IOException { MindeeClient mindeeClient = new MindeeClient(apiKey); LocalInputSource inputSource = new LocalInputSource(file); PredictResponse response; @@ -284,18 +299,28 @@ protected PageOptions getDefaultPageOptions() { response = mindeeClient.parse(productClass, inputSource, predictOptions); } - if (outputType == OutputChoices.full) { - return response.getDocument().toString(); + StringBuilder output = new StringBuilder(); + switch (outputType) { + case full: + output.append(response.getDocument().toString()); + break; + case raw: + output.append(response.getRawResponse()); + break; + default: + output.append( + response.getDocument().getInference().getPrediction().toString() + ); } - if (outputType == OutputChoices.raw) { - return response.getRawResponse(); + if (words) { + output.append(wordsOutput(response.getDocument().getOcr())); } - Document document = response.getDocument(); - return document.getInference().getPrediction().toString(); + return output.toString(); } @Override - public > String standardProductAsyncOutput(Class productClass, File file) throws IOException, InterruptedException { + public > String standardProductAsyncOutput(Class productClass, File file) + throws IOException, InterruptedException { MindeeClient mindeeClient = new MindeeClient(apiKey); LocalInputSource inputSource = new LocalInputSource(file); AsyncPredictResponse response; @@ -310,13 +335,22 @@ productClass, inputSource, predictOptions, getDefaultPageOptions(), null ); } - if (outputType == OutputChoices.full) { - return response.getDocumentObj().toString(); + StringBuilder output = new StringBuilder(); + switch (outputType) { + case full: + output.append(response.getDocument().toString()); + break; + case raw: + output.append(response.getRawResponse()); + break; + default: + output.append( + response.getDocumentObj().getInference().getPrediction().toString() + ); } - if (outputType == OutputChoices.raw) { - return response.getRawResponse(); + if (words) { + output.append(wordsOutput(response.getDocumentObj().getOcr())); } - Document document = response.getDocumentObj(); - return document.getInference().getPrediction().toString(); + return output.toString(); } }