Avatar of jdvr

jdvr's solution

to OCR Numbers in the Java Track

Published at Aug 22 2019 · 0 comments
Instructions
Test suite
Solution

Given a 3 x 4 grid of pipes, underscores, and spaces, determine which number is represented, or whether it is garbled.

Step One

To begin with, convert a simple binary font to a string containing 0 or 1.

The binary font uses pipes and underscores, four rows high and three columns wide.

     _   #
    | |  # zero.
    |_|  #
         # the fourth row is always blank

Is converted to "0"

         #
      |  # one.
      |  #
         # (blank fourth row)

Is converted to "1"

If the input is the correct size, but not recognizable, your program should return '?'

If the input is the incorrect size, your program should return an error.

Step Two

Update your program to recognize multi-character binary strings, replacing garbled numbers with ?

Step Three

Update your program to recognize all numbers 0 through 9, both individually and as part of a larger string.

 _ 
 _|
|_ 
   

Is converted to "2"

      _  _     _  _  _  _  _  _  #
    | _| _||_||_ |_   ||_||_|| | # decimal numbers.
    ||_  _|  | _||_|  ||_| _||_| #
                                 # fourth line is always blank

Is converted to "1234567890"

Step Four

Update your program to handle multiple numbers, one per line. When converting several lines, join the lines with commas.

    _  _ 
  | _| _|
  ||_  _|
         
    _  _ 
|_||_ |_ 
  | _||_|
         
 _  _  _ 
  ||_||_|
  ||_| _|
         

Is converted to "123,456,789"

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Source

Inspired by the Bank OCR kata http://codingdojo.org/cgi-bin/wiki.pl?KataBankOCR

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

OpticalCharacterReaderTest.java

import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;

public class OpticalCharacterReaderTest {

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Test
    public void testReaderRecognizesSingle0() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "| |",
                "|_|",
                "   "
        ));

        assertEquals("0", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle1() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "   ",
                "  |",
                "  |",
                "   "
        ));

        assertEquals("1", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderReturnsQuestionMarkForUnreadableButCorrectlySizedInput() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "   ",
                "  _",
                "  |",
                "   "
        ));

        assertEquals("?", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderThrowsExceptionWhenNumberOfInputLinesIsNotAMultipleOf4() {
        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage("Number of input rows must be a positive multiple of 4");

        new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "| |",
                "   "
        ));
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderThrowsExceptionWhenNumberOfInputColumnsIsNotAMultipleOf3() {
        expectedException.expect(IllegalArgumentException.class);
        expectedException.expectMessage("Number of input columns must be a positive multiple of 3");

        new OpticalCharacterReader().parse(Arrays.asList(
                "    ",
                "   |",
                "   |",
                "    "
        ));
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesBinarySequence110101100() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "       _     _        _  _ ",
                "  |  || |  || |  |  || || |",
                "  |  ||_|  ||_|  |  ||_||_|",
                "                           "
        ));

        assertEquals("110101100", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderReplacesUnreadableDigitsWithQuestionMarksWithinSequence() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "       _     _           _ ",
                "  |  || |  || |     || || |",
                "  |  | _|  ||_|  |  ||_||_|",
                "                           "
        ));

        assertEquals("11?10?1?0", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle2() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                " _|",
                "|_ ",
                "   "
        ));

        assertEquals("2", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle3() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                " _|",
                " _|",
                "   "
        ));

        assertEquals("3", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle4() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "   ",
                "|_|",
                "  |",
                "   "
        ));

        assertEquals("4", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle5() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "|_ ",
                " _|",
                "   "
        ));

        assertEquals("5", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle6() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "|_ ",
                "|_|",
                "   "
        ));

        assertEquals("6", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle7() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "  |",
                "  |",
                "   "
        ));

        assertEquals("7", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle8() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "|_|",
                "|_|",
                "   "
        ));

        assertEquals("8", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSingle9() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                " _ ",
                "|_|",
                " _|",
                "   "
        ));

        assertEquals("9", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesSequence1234567890() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "    _  _     _  _  _  _  _  _ ",
                "  | _| _||_||_ |_   ||_||_|| |",
                "  ||_  _|  | _||_|  ||_| _||_|",
                "                              "
        ));

        assertEquals("1234567890", parsedInput);
    }

    @Ignore("Remove to run test")
    @Test
    public void testReaderRecognizesAndCorrectlyFormatsMultiRowInput() {
        String parsedInput = new OpticalCharacterReader().parse(Arrays.asList(
                "    _  _ ",
                "  | _| _|",
                "  ||_  _|",
                "         ",
                "    _  _ ",
                "|_||_ |_ ",
                "  | _||_|",
                "         ",
                " _  _  _ ",
                "  ||_||_|",
                "  ||_| _|",
                "         "
        ));

        assertEquals("123,456,789", parsedInput);
    }

}

src/main/java/jdvr/Token.java

package jdvr;

enum Token {
	Underscore('_'),
	Pipe('|'),
	Space(' ');

	private final char value;

	Token(char value) {
		this.value = value;
	}

	public char getValue() {
		return value;
	}

	static Token parse(String character) {
		for (Token c : values()) {
			if (String.valueOf(c.getValue()).equals(character)) {
				return c;
			}
		}
		return null;
	}
}

src/main/java/jdvr/NumberPattern.java

package jdvr;

import java.util.List;
import java.util.stream.IntStream;

import static jdvr.Token.Pipe;
import static jdvr.Token.Space;
import static jdvr.Token.Underscore;

public enum NumberPattern {

	Zero(List.of(Space, Underscore, Space, Pipe, Space, Pipe, Pipe, Underscore, Pipe, Space, Space, Space), "0"),

	One(List.of(Space, Space, Space, Space, Space, Pipe, Space, Space, Pipe, Space, Space, Space), "1"),

	Two(List.of(Space, Underscore, Space, Space, Underscore, Pipe, Pipe, Underscore, Space, Space, Space, Space), "2"),

	Three(List.of(Space, Underscore, Space, Space, Underscore, Pipe, Space, Underscore, Pipe, Space, Space, Space), "3"),

	Four(List.of(Space, Space, Space, Pipe, Underscore, Pipe, Space, Space, Pipe, Space, Space, Space), "4"),

	Five(List.of(Space, Underscore, Space, Pipe, Underscore, Space, Space, Underscore, Pipe, Space, Space, Space), "5"),

	Six(List.of(Space, Underscore, Space, Pipe, Underscore, Space, Pipe, Underscore, Pipe, Space, Space, Space), "6"),

	Seven(List.of(Space, Underscore, Space, Space, Space, Pipe, Space, Space, Pipe, Space, Space, Space), "7"),

	Eight(List.of(Space, Underscore, Space, Pipe, Underscore, Pipe, Pipe, Underscore, Pipe, Space, Space, Space), "8"),

	Nine(List.of(Space, Underscore, Space, Pipe, Underscore, Pipe, Space, Underscore, Pipe, Space, Space, Space), "9");

	public List<Token> pattern;
	private String representation;

	NumberPattern(List<Token> pattern, String representation) {
		this.pattern = pattern;
		this.representation = representation;
	}

	public boolean match(List<Token> pattern) {
		return IntStream.range(0, pattern.size())
				.allMatch(i -> this.pattern.get(i) == pattern.get(i));
	}

	@Override
	public String toString() {
		return representation;
	}
}

src/main/java/jdvr/OpticalCharacterReader.java

package jdvr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class OpticalCharacterReader {

	private static final InputType isOneRowWithOneNumber = text -> text.size() == 4 && text.get(0).length() == 3;
	private static final InputType isOneRowMultipleNumbers = text -> text.size() == 4 && text.get(0).length() > 3;
	private static final InputType isMoreThanOneRow = text -> text.size() > 4;

	private static final Map<InputType, Parser> ParseTextRules = Map.of(
			isOneRowWithOneNumber, input -> parseNumber(singleNumberTokens(input)),
			isOneRowMultipleNumbers, OpticalCharacterReader::parseRow,
			isMoreThanOneRow, input -> splitRows(input).stream().map(OpticalCharacterReader::parseRow).collect(Collectors.joining(","))
	);

	public String parse(List<String> input) {
		check(input);

		return ParseTextRules.entrySet()
				.stream()
				.filter(es -> es.getKey().test(input))
				.findFirst()
				.map(es -> es.getValue().parse(input))
				.orElse("");
	}

	private static String parseRow(List<String> row) {
		return split(row).stream()
				.map(OpticalCharacterReader::singleNumberTokens)
				.map(OpticalCharacterReader::parseNumber)
				.collect(Collectors.joining());
	}

	private static List<List<String>> splitRows(List<String> input) {
		List<List<String>> numbers = new ArrayList<>();
		for (int i = 0; i < input.size() / 4; i++) {
			var from = i * 4;
			var to = i * 4 + 4;
			numbers.add(input.subList(from, to));
		}
		return numbers;
	}

	private static List<List<String>> split(List<String> input) {
		List<List<String>> numbers = new ArrayList<>();
		for (int i = 0; i < input.get(0).length() / 3; i++) {
			var from = i * 3;
			var to = i * 3 + 3;
			var number = new ArrayList<String>();
			for (String row : input) {
				number.add(row.substring(from, to));
			}
			numbers.add(number);
		}
		return numbers;
	}

	private static List<Token> singleNumberTokens(List<String> input) {
		return input.stream()
				.map(s -> s.split(""))
				.flatMap(Stream::of)
				.map(Token::parse)
				.collect(Collectors.toList());
	}

	private static String parseNumber(List<Token> tokens) {
		return Stream.of(NumberPattern.values())
				.filter(n -> n.match(tokens))
				.findFirst()
				.map(NumberPattern::toString)
				.orElse("?");
	}

	private static void check(List<String> input) {
		if (input.size() % 4 != 0) {
			throw new IllegalArgumentException("Number of input rows must be a positive multiple of 4");
		}
		boolean inValidColumnSize = input.stream().anyMatch(s -> s.length() % 3 != 0);
		if (inValidColumnSize) {
			throw new IllegalArgumentException("Number of input columns must be a positive multiple of 3");
		}
	}

	@FunctionalInterface
	interface InputType extends Predicate<List<String>> {
		@Override
		boolean test(List<String> strings);
	}

	@FunctionalInterface
	interface Parser {
		String parse(List<String> input);
	}

}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?