public static matrixItem getItem(List <string> lines, int l, int x) { //Console.WriteLine("l = " + l); string line0, line1, line2; int index = x * 3; line0 = lines[l].Substring(index, 3); line1 = lines[l + 1].Substring(index, 3); line2 = lines[l + 2].Substring(index, 3); matrixItem ocrDigit = new matrixItem(line0, line1, line2, 0); return(ocrDigit); }
public static int toDigit(matrixItem ocrDigit) { int value = -1; // all the parentheses seem to be needed - otherwise errors if ((ocrDigit.Row1.Equals(" ")) & (ocrDigit.Row2.Equals(" |")) & (ocrDigit.Row3.Equals(" |"))) { value = 1; } else if ((ocrDigit.Row1.Equals(" _ ")) & (ocrDigit.Row2.Equals(" _|")) & (ocrDigit.Row3.Equals("|_ "))) { value = 2; } else if ((ocrDigit.Row1.Equals(" _ ")) & (ocrDigit.Row2.Equals(" _|")) & (ocrDigit.Row3.Equals(" _|"))) { value = 3; } else if ((ocrDigit.Row1.Equals(" ")) & (ocrDigit.Row2.Equals("|_|")) & (ocrDigit.Row3.Equals(" |"))) { value = 4; } else if ((ocrDigit.Row1.Equals(" _ ")) & (ocrDigit.Row2.Equals("|_ ")) & (ocrDigit.Row3.Equals(" _|"))) { value = 5; } else if ((ocrDigit.Row1.Equals(" _ ")) & (ocrDigit.Row2.Equals("|_ ")) & (ocrDigit.Row3.Equals("|_|"))) { value = 6; } else if ((ocrDigit.Row1.Equals(" _ ")) & (ocrDigit.Row2.Equals(" |")) & (ocrDigit.Row3.Equals(" |"))) { value = 7; } else if ((ocrDigit.Row1.Equals(" _ ")) & (ocrDigit.Row2.Equals("|_|")) & (ocrDigit.Row3.Equals("|_|"))) { value = 8; } else if ((ocrDigit.Row1.Equals(" _ ")) & (ocrDigit.Row2.Equals("|_|")) & (ocrDigit.Row3.Equals(" _|"))) { value = 9; } else if ((ocrDigit.Row1.Equals(" _ ")) & (ocrDigit.Row2.Equals("| |")) & (ocrDigit.Row3.Equals("|_|"))) { value = 0; } else { ocrDigit.ErrorCode = 1; Debug.WriteLine("Error in data: digit not recognized"); } return(value); }
static void Main(string[] args) { /* * Imperfect to hard-code the input file, but here * overriding it with user input is possible. * Using built-in functions to obtain the local project path could be better. */ string path = @"/Users/alcurry/Projects/bankOCR/OCRinput.txt"; Console.WriteLine("default input file is : " + path); Console.WriteLine("to use it, press enter, or enter your full path and filename"); string newPath = Console.ReadLine(); if (!String.IsNullOrWhiteSpace(newPath)) { path = newPath; } if (!File.Exists(path)) { Debug.WriteLine("Where you at ?"); } List <string> lines = File.ReadLines(path).Where(line => line != "").ToList(); Debug.WriteLine(lines.Count + " lines in input file"); for (int i = 0; i <= lines.Count - 1; i++) { Debug.Write(lines[i]); Debug.WriteLine(lines[i].Length); } matrixItem digit = new matrixItem(); for (int l = 0; l < lines.Count; l += 3) { int x = 0; int value = 0; string strResult = ""; /* * Assume no blank columns between characters * requirements inconsistent (written description vs. sample input file), * this approach probably makes more sense for a real application. * * Calculating "charCount" this way, diving by 3, since the matrix is 3 * characters wide. Problem description states errors are limited - * "the characters inside the 3 x 3 matrix may not be valid" - this is checked * in the toDigit method in the matrixItem class. */ int charCount = lines[l].Length / 3; do { digit = matrixItem.getItem(lines, l, x); value = matrixItem.toDigit(digit); x++; // convert each digit returned to a string, final output for each line // is a string, check for errors from each 3x3 matrix strResult = strResult + value.ToString(); if (value == -1) { Console.WriteLine("Error in data"); break; } } while (x < charCount); // No errors from "toDigit" above if (value > -1) { Console.WriteLine(strResult); } } }