/// <summary> /// Calculates the distance between two collections /// </summary> /// <param name="first"></param> /// <param name="second"></param> /// <returns></returns> private ConfidenceResult CalcDistance(char[][] first, char[][] second) { var res = new ConfidenceResult(); var string1 = CharArrayHelper.ArrToString(first); var string2 = CharArrayHelper.ArrToString(second); res.DiceCoeff = (int)Math.Round(DiceMatch(string1, string2) * 100, 0); res.Hamming = GetHammingDistance(string1, string2); return(res); }
/// <summary> /// </summary> /// <param name="num"></param> /// <returns></returns> private char GetMatch(char[][] num) { var res = '?'; //Return Question mark if none found if (_ocrDict.Values.Any(o => CharArrayHelper.EqualityCheck(num, o))) { res = _ocrDict.First(o => CharArrayHelper.EqualityCheck(num, o.Value)).Key; } return(res); }
private static void Main(string[] args) { ReadFile(args.Length == 1 ? args[0] : @".\TestData.txt"); while (_currRecord != _recordCount) { ReadRecord(); CharArrayHelper.DisplayRaw(BufferArr); //Show the current Buffer var result = OcrEngine.Decode(BufferArr); ConsoleHelper.Write($"{result}", MessageType.Info); } Console.ReadKey(); }
/// <summary> /// Read the next record into BufferArr /// </summary> private static void ReadRecord() { var j = 0; var x = _currRecord == 0 ? 0 : _currRecord * 4; for (var i = x; i < x + 4; i++) //will always be 4 lines { BufferArr[j] = _lineStrings[i].Length == 27 ? _lineStrings[i].ToCharArray() : CharArrayHelper.PadArray(27); //Convert line to Character array in BufferArr j++; } _currRecord++; //Increment the Record Count }
/// <summary> /// Decodes a line of characters /// </summary> /// <param name="record">The line to decode</param> /// <returns><see cref="string" />The decoded String</returns> public string Decode(char[][] record) { var res = new StringBuilder(); for (var i = 0; i < record[0].Length / 3; i++) { var temp = CharArrayHelper.GetCharAtIndex(record, i); var match = GetMatch(temp); res.Append(match); } var decoded = res.ToString(); res.Clear(); if (decoded.Contains("?")) { var misreadCount = decoded.ToCharArray().Count(o => o == '?'); if (misreadCount >= _misreadThreshold) { res.AppendLine($"Decoded string misread count reached misread threshold: {decoded}"); } res.AppendLine( $"Decoded string contains {misreadCount} misread character(s): {decoded} \r\n Attempting prediction"); var possibleStrings = GetPossibleStrings(record, decoded); foreach (var possibleString in possibleStrings) { res.AppendLine($"Possible Decoded String: {possibleString}"); res.AppendLine($"Check sum Result: {(Checksum(possibleString) ? " Pass" : " Fail")}"); } } else { res.AppendLine($"Result: {decoded}"); res.AppendLine($"Check sum Result: {(Checksum(decoded) ? " Pass" : " Fail")}"); } return(res.ToString()); }
/// <summary> /// Gets all possible strings from a string with a misread character /// </summary> /// <param name="record"></param> /// <param name="decoded"></param> /// <returns></returns> private List <string> GetPossibleStrings(char[][] record, string decoded) { var res = new List <string>(); var initialPostion = decoded.IndexOf("?", StringComparison.Ordinal); var tempString = decoded; var checkChar = CharArrayHelper.GetCharAtIndex(record, initialPostion); var possibleChars = GetPossibleMatches(checkChar); foreach (var possibleChar in possibleChars) { tempString = tempString.Remove(initialPostion, 1).Insert(initialPostion, possibleChar.ToString()); if (tempString.Contains("?")) { GetPossibleStrings(record, tempString).ForEach(o => res.Add(o)); } else { res.Add(tempString); } } return(res); }