/** * Given the 64 bit OCR data for binarised image and the * expected outcome value, tests the network for a correct output. * * Runs the 64 bit inputs through the network and checks * which output it gets. * * @param character: OCRCharacter obj with 64 bit numeric inputs * representing the OCR data & identifier. * @return good or bad (true or false). */ public bool recallNetwork(OCRCharacter character) { // Populate the input neurons. // int answer = character.getIdentifier(); int index = recallNetworkGuess(character); bool correct = index == answer ? true : false; // Output feedback Console.WriteLine("Input data for: " + answer + " / output: " + index + " (" + answer + ")" + " correct: " + correct); return(correct); }
/** * Given a a list of neighbouring characters, checks which character kind has * the most frequent occurrence (majority) * @param neighbors * @param useDistanceScore: True use scoring based on euclidean distance for * classification, False uses majority votes. * @return charIdentifier: character which has highest majority. */ private static int classify(List <OCRCharacter> neighbors, bool useDistanceScore) { Dictionary <int, double> charOccurenceList = new Dictionary <int, double>(); int num = neighbors.Count; for (int index = 0; index < num; index++) { OCRCharacter temp = neighbors[index]; int key = temp.getIdentifier(); // If character kind has not been added yet. if (!charOccurenceList.ContainsKey(key)) { // Best practise is to add the total distance of the neighbour char type as score value. if (useDistanceScore) { charOccurenceList.Add(key, 1 / temp.distance); } else { charOccurenceList.Add(key, 1.0); } } // If already added, add occurence majority vote else { double value = charOccurenceList[key]; // Best practise is to add the total distance of the neighbour char type as score value. if (useDistanceScore) { value += 1 / temp.distance; } else { value += 1.0; } charOccurenceList[key] = value; // update existing value + 1 } } // Check which kind of identifiers have the most of the same type double majorityScore = 0; int charIdentifier = -1; // Loop and determine which char identifier scored the highest foreach (KeyValuePair <int, double> entry in charOccurenceList) { int identifier = entry.Key; double value = entry.Value; if (value > majorityScore) { majorityScore = value; charIdentifier = identifier; } } return(charIdentifier); }