Skip to content

Commit

Permalink
Port Java to PHP using ChatGPT.
Browse files Browse the repository at this point in the history
  • Loading branch information
codecop committed Mar 8, 2024
1 parent 1daf4f2 commit d7597e8
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
80 changes: 80 additions & 0 deletions php/src/Ocr.php
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;
}
}
92 changes: 92 additions & 0 deletions php/test/OcrTest.php
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);
// }

}

0 comments on commit d7597e8

Please sign in to comment.