Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Parsing/V2/InferenceActiveOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class InferenceActiveOptions
{
/**
* @var boolean Whether the Retrieval-Augmented Generation feature was activated.
* When this feature is activated, the RAG pipeline is used to increase result accuracy.
*/
public bool $rag;

Expand All @@ -24,14 +25,21 @@ class InferenceActiveOptions
public bool $rawText;

/**
* @var boolean Whether the polygon feature was activated.
* @var boolean Whether the Raw Text feature was activated.
* When this feature is activated, the raw text extracted from the document is returned in the result.
*/
public bool $polygon;

/**
* @var boolean Whether the confidence feature was activated.
* When this feature is activated, a confidence score for each field is returned in the result.
*/
public bool $confidence;
/**
* @var boolean Whether the text context feature was activated.
* When this feature is activated, the provided context is used to improve the accuracy of the inference.
*/
public bool $textContext;

/**
* @param array $serverResponse Raw server response array.
Expand All @@ -42,6 +50,7 @@ public function __construct(array $serverResponse)
$this->rawText = $serverResponse['raw_text'];
$this->polygon = $serverResponse['polygon'];
$this->confidence = $serverResponse['confidence'];
$this->textContext = $serverResponse['text_context'];
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Input;
namespace V1\Input;

use Mindee\Input\LocalResponse;
use PHPUnit\Framework\TestCase;

class LocalResponseTest extends TestCase
class LocalResponseV1Test extends TestCase
{
private string $signature;
private string $dummyKey;
Expand Down
56 changes: 56 additions & 0 deletions tests/V2/Input/LocalResponseV2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace V2\Input;

use Mindee\Input\LocalResponse;
use Mindee\Parsing\V2\InferenceResponse;
use PHPUnit\Framework\TestCase;

class LocalResponseV2Test extends TestCase
{
private string $filePath;

protected function setUp(): void
{
$this->filePath = \TestingUtilities::getV2DataDir() . '/inference/standard_field_types.json';
}

protected function assertLocalResponse(LocalResponse $localResponse): void
{
$fakeHMACSigning = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH";
$signature = "b82a515c832fd2c4f4ce3a7e6f53c12e8d10e19223f6cf0e3a9809a7a3da26be";
$reflectedLocalResponse = new \ReflectionClass($localResponse);
$reflectedFile = $reflectedLocalResponse->getProperty('file');
$reflectedFile->setAccessible(true);
$this->assertNotNull($reflectedFile);
$this->assertFalse($localResponse->isValidHMACSignature($fakeHMACSigning, "fake HMAC signature"));
$this->assertEquals($signature, $localResponse->getHmacSignature($fakeHMACSigning));
$this->assertTrue($localResponse->isValidHMACSignature($fakeHMACSigning, $signature));
$response = $localResponse->deserializeResponse(InferenceResponse::class);
$this->assertInstanceOf(InferenceResponse::class, $response);
$this->assertNotNull($response->inference);
$this->assertNotNull($response->inference->result);
$this->assertNotNull($response->inference->result->fields);
}

public function testValidFileLocalResponse(){
$file = fopen($this->filePath, 'rb');
$localResponse = new LocalResponse($file);
fclose($file);
$this->assertLocalResponse($localResponse);
}

public function testValidPathLocalResponse(){
$localResponse = new LocalResponse($this->filePath);
$this->assertLocalResponse($localResponse);
}

public function testValidBytesLocalResponse(){
$raw = file_get_contents($this->filePath);
$encoding = mb_detect_encoding($raw, ['UTF-8','UTF-16','UTF-32','ISO-8859-1','Windows-1252'], true) ?: 'UTF-8';
$utf8 = ($encoding === 'UTF-8') ? $raw : mb_convert_encoding($raw, 'UTF-8', $encoding);
$utf8 = preg_replace('/^\xEF\xBB\xBF/', '', $utf8);
$localResponse = new LocalResponse($utf8);
$this->assertLocalResponse($localResponse);
}
}
33 changes: 33 additions & 0 deletions tests/V2/Parsing/InferenceResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,13 @@ public function testCoordinatesAndLocationDataMustBeAccessible(): void
$this->assertTrue(FieldConfidence::Low->lessThanOrEqual($dateField->confidence));
$this->assertTrue(FieldConfidence::Medium->lessThanOrEqual($dateField->confidence));
$this->assertEquals('Medium', $dateField->confidence->value);

$activeOptions = $inference->activeOptions;
$this->assertTrue($activeOptions->polygon);
$this->assertFalse($activeOptions->confidence);
$this->assertFalse($activeOptions->rag);
$this->assertFalse($activeOptions->rawText);
$this->assertFalse($activeOptions->textContext);
}

public function testRagMetadataWhenMatched()
Expand Down Expand Up @@ -394,4 +401,30 @@ public function testShouldLoadWith422Error()
$this->assertEquals(1, count($response->job->error->errors));
$this->assertInstanceOf(ErrorItem::class, $response->job->error->errors[0]);
}

public function testTextContextIsTrue(): void
{
$response = $this->loadFromResource('v2/inference/text_context_enabled.json');
$inference = $response->inference;
$this->assertNotNull($inference);
$activeOptions = $inference->activeOptions;
$this->assertFalse($activeOptions->polygon);
$this->assertFalse($activeOptions->confidence);
$this->assertFalse($activeOptions->rag);
$this->assertFalse($activeOptions->rawText);
$this->assertTrue($activeOptions->textContext);
}

public function testTextContextIsFalse(): void
{
$response = $this->loadFromResource('v2/products/financial_document/complete.json');
$inference = $response->inference;
$this->assertNotNull($inference);
$activeOptions = $inference->activeOptions;
$this->assertFalse($activeOptions->polygon);
$this->assertFalse($activeOptions->confidence);
$this->assertFalse($activeOptions->rag);
$this->assertFalse($activeOptions->rawText);
$this->assertFalse($activeOptions->textContext);
}
}