-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Ocr; | ||
|
||
class Ocr { | ||
|
||
private const NUMERALS = [ | ||
[" _ ", | ||
"| | ", | ||
"|_| ", | ||
" "], | ||
[" ", | ||
" | ", | ||
" | ", | ||
" "], | ||
[" _ ", | ||
" _| ", | ||
"|_ ", | ||
" "], | ||
[" _ ", | ||
" _| ", | ||
" _| ", | ||
" "], | ||
[" ", | ||
"|_| ", | ||
" | ", | ||
" "], | ||
[" _ ", | ||
"|_ ", | ||
" _| ", | ||
" "], | ||
[" _ ", | ||
"|_ ", | ||
"|_| ", | ||
" "], | ||
[" _ ", | ||
" | ", | ||
" | ", | ||
" "], | ||
[" _ ", | ||
"|_| ", | ||
"|_| ", | ||
" "], | ||
[" _ ", | ||
"|_| ", | ||
" _| ", | ||
" "] | ||
]; | ||
|
||
public static function parse(...$lines): array { | ||
$result = []; | ||
for ($i = 0; $i < count($lines); $i += 4) { | ||
$work = " "; | ||
for ($pos = 0; $pos < 9; ++$pos) { | ||
$work[$pos] = '?'; | ||
$got1 = false; | ||
for ($numeral = 0; $numeral <= 9; ++$numeral) { | ||
$ok = true; | ||
for ($row = 0; $row < 4; ++$row) { | ||
for ($col = 0; $col < 4; ++$col) { | ||
if (self::NUMERALS[$numeral][$row][$col] !== $lines[$i + $row][4 * $pos + $col]) | ||
$ok = false; | ||
} | ||
} | ||
if ($ok) { | ||
$work[$pos] = chr($numeral + ord('0')); | ||
$got1 = true; | ||
break; | ||
} | ||
} | ||
if (!$got1) { | ||
$work[10] = 'I'; | ||
$work[11] = $work[12] = 'L'; | ||
} | ||
} | ||
$result[] = $work; | ||
} | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Ocr\Ocr; | ||
|
||
class OcrTest extends TestCase { | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function parses_one_record_with_888888888() { | ||
$expected = ["888888888 "]; | ||
$actual = Ocr::parse(" _ _ _ _ _ _ _ _ _ ", | ||
"|_| |_| |_| |_| |_| |_| |_| |_| |_| ", | ||
"|_| |_| |_| |_| |_| |_| |_| |_| |_| ", | ||
" "); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function parses_two_records_with_888888888() { | ||
$expected = ["888888888 ", | ||
"888888888 "]; | ||
$actual = Ocr::parse(" _ _ _ _ _ _ _ _ _ ", | ||
"|_| |_| |_| |_| |_| |_| |_| |_| |_| ", | ||
"|_| |_| |_| |_| |_| |_| |_| |_| |_| ", | ||
" ", | ||
" _ _ _ _ _ _ _ _ _ ", | ||
"|_| |_| |_| |_| |_| |_| |_| |_| |_| ", | ||
"|_| |_| |_| |_| |_| |_| |_| |_| |_| ", | ||
" "); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function parses_one_record_with_123456790() { | ||
$expected = ["123456790 "]; | ||
$actual = Ocr::parse(" _ _ _ _ _ _ _ ", | ||
" | _| _| |_| |_ |_ | |_| | | ", | ||
" | |_ _| | _| |_| | _| |_| ", | ||
" "); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function parses_two_records() { | ||
$expected = ["123456790 ", | ||
"947874222 "]; | ||
$actual = Ocr::parse(" _ _ _ _ _ _ _ ", | ||
" | _| _| |_| |_ |_ | |_| | | ", | ||
" | |_ _| | _| |_| | _| |_| ", | ||
" ", | ||
" _ _ _ _ _ _ _ ", | ||
"|_| |_| | |_| | |_| _| _| _| ", | ||
" _| | | |_| | | |_ |_ |_ ", | ||
" "); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function parses_illegal_digit() { | ||
$expected = ["12??56790 ILL"]; | ||
$actual = Ocr::parse(" _ _ _ _ _ _ _ ", | ||
" | _| |_| _| |_ |_ | |_| | | ", | ||
" | |_ _ | _| |_| | _| |_| ", | ||
" "); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
// /** | ||
// * @test | ||
// */ | ||
// public function checksum_fail_returns_ERR() { | ||
// $expected = ["664371495 ERR"]; | ||
// $actual = Ocr::parse(" _ _ _ _ _ _ ", | ||
// "|_ |_ |_| _| | | |_| |_| |_ ", | ||
// "|_| |_| | _| | | | _| _| ", | ||
// " "); | ||
// $this->assertEquals($expected, $actual); | ||
// } | ||
|
||
} |