Skip to content

Commit c8175dc

Browse files
committed
add classification classes
1 parent 981e2df commit c8175dc

File tree

9 files changed

+145
-16
lines changed

9 files changed

+145
-16
lines changed

src/main/java/com/mindee/parsing/v2/CommonResponse.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,4 @@ public abstract class CommonResponse {
1616
* This is not formatted in any way by the library and may contain newline and tab characters.
1717
*/
1818
private String rawResponse;
19-
20-
public void setRawResponse(String contents) {
21-
rawResponse = contents;
22-
}
2319
}

src/main/java/com/mindee/parsing/v2/Inference.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,17 @@
1515
@JsonIgnoreProperties(ignoreUnknown = true)
1616
@AllArgsConstructor
1717
@NoArgsConstructor
18-
public class Inference extends BaseInference {
18+
public class Inference extends BaseInference<InferenceResult> {
1919
/**
2020
* Active options for the inference.
2121
*/
2222
@JsonProperty("active_options")
2323
private InferenceActiveOptions activeOptions;
2424

25-
/**
26-
* Model result values.
27-
*/
28-
@JsonProperty("result")
29-
private InferenceResult result;
30-
3125
@Override
3226
public String toString() {
3327
StringJoiner joiner = new StringJoiner("\n");
34-
joiner.add(activeOptions.toString()).add("").add(result != null ? result.toString() : "");
35-
return super.toString() + "\n" + joiner.toString().trim() + "\n";
28+
joiner.add(toStringBase()).add(activeOptions.toString()).add("").add(result.toString());
29+
return joiner.toString().trim() + "\n";
3630
}
3731
}

src/main/java/com/mindee/v2/parsing/BaseInference.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@JsonIgnoreProperties(ignoreUnknown = true)
1818
@AllArgsConstructor
1919
@NoArgsConstructor
20-
public abstract class BaseInference {
20+
public abstract class BaseInference<TResult> {
2121
/**
2222
* Inference ID.
2323
*/
@@ -42,8 +42,13 @@ public abstract class BaseInference {
4242
@JsonProperty("file")
4343
protected InferenceFile file;
4444

45-
@Override
46-
public String toString() {
45+
/**
46+
* Result of the inference.
47+
*/
48+
@JsonProperty("result")
49+
protected TResult result;
50+
51+
protected String toStringBase() {
4752
StringJoiner joiner = new StringJoiner("\n");
4853
joiner
4954
.add("Inference")
@@ -55,4 +60,11 @@ public String toString() {
5560
.add(file.toString());
5661
return joiner.toString().trim() + "\n";
5762
}
63+
64+
@Override
65+
public String toString() {
66+
StringJoiner joiner = new StringJoiner("\n");
67+
joiner.add(toStringBase()).add("").add(result.toString());
68+
return joiner.toString().trim() + "\n";
69+
}
5870
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.mindee.v2.product.classification;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.Getter;
6+
7+
/**
8+
* Classification of the document type from the source file.
9+
*/
10+
@Getter
11+
@JsonIgnoreProperties(ignoreUnknown = true)
12+
public class ClassificationClassifier {
13+
/**
14+
* The document type, as identified on given classification values.
15+
*/
16+
@JsonProperty("document_type")
17+
private String documentType;
18+
19+
@Override
20+
public String toString() {
21+
return "Document Type: " + documentType;
22+
}
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.mindee.v2.product.classification;
2+
3+
import com.mindee.v2.parsing.BaseInference;
4+
5+
/**
6+
* The inference result for a classification utility request.
7+
*/
8+
public class ClassificationInference extends BaseInference<ClassificationResult> {
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.mindee.v2.product.classification;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.mindee.parsing.v2.CommonResponse;
6+
import lombok.Getter;
7+
8+
/**
9+
* Response for a classification utility inference.
10+
*/
11+
@Getter
12+
@JsonIgnoreProperties(ignoreUnknown = true)
13+
public class ClassificationResponse extends CommonResponse {
14+
15+
/**
16+
* The inference result for a classification utility request.
17+
*/
18+
@JsonProperty("inference")
19+
private ClassificationInference inference;
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.mindee.v2.product.classification;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import java.util.StringJoiner;
6+
import lombok.AllArgsConstructor;
7+
import lombok.EqualsAndHashCode;
8+
import lombok.Getter;
9+
import lombok.NoArgsConstructor;
10+
11+
/**
12+
* Result of the document classifier inference.
13+
*/
14+
@Getter
15+
@EqualsAndHashCode
16+
@JsonIgnoreProperties(ignoreUnknown = true)
17+
@AllArgsConstructor
18+
@NoArgsConstructor
19+
public final class ClassificationResult {
20+
/**
21+
* Classification of the document type from the source file.
22+
*/
23+
@JsonProperty("classification")
24+
private ClassificationClassifier classification;
25+
26+
@Override
27+
public String toString() {
28+
StringJoiner joiner = new StringJoiner("\n");
29+
joiner.add("Classification").add("==============");
30+
joiner.add(classification.toString());
31+
32+
return joiner.toString();
33+
}
34+
}

src/test/java/com/mindee/TestingUtilities.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static void assertStringEqualsFile(String expected, String filePath) thro
3939
Assertions.assertEquals(expectedSummary, actualSummary);
4040
}
4141

42+
public static void assertStringEqualsFile(String expected, Path filePath) throws IOException {
43+
assertStringEqualsFile(expected, filePath.toString());
44+
}
45+
4246
/**
4347
* Retrieves the version from an RST prediction output.
4448
*
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.mindee.v2.product;
2+
3+
import static com.mindee.TestingUtilities.getV2ResourcePath;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
7+
import com.mindee.input.LocalResponse;
8+
import com.mindee.v2.product.classification.ClassificationResponse;
9+
import java.io.IOException;
10+
import org.junit.jupiter.api.DisplayName;
11+
import org.junit.jupiter.api.Nested;
12+
import org.junit.jupiter.api.Test;
13+
14+
@DisplayName("MindeeV2 - Classification Model Tests")
15+
public class ClassificationTest {
16+
private ClassificationResponse loadResponse(String filePath) throws IOException {
17+
LocalResponse localResponse = new LocalResponse(getV2ResourcePath(filePath));
18+
return localResponse.deserializeResponse(ClassificationResponse.class);
19+
}
20+
21+
@Nested
22+
@DisplayName("Classification with single value")
23+
class SinglePredictionTest {
24+
@Test
25+
@DisplayName("all properties must be valid")
26+
void mustHaveValidProperties() throws IOException {
27+
ClassificationResponse response = loadResponse(
28+
"products/classification/classification_single.json"
29+
);
30+
assertNotNull(response.getInference());
31+
assertEquals(
32+
"invoice",
33+
response.getInference().getResult().getClassification().getDocumentType()
34+
);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)