OCR / XOR / NN / NN_Tools / load.c
load.c
Raw
#include "load.h"

float *goalArray(char letter)
{
	float *goal = calloc(52, sizeof(float));

  	if(letter >= 'A' && letter <= 'Z')
		goal[(int)(letter) - 65] = 1;

	else if(letter >= 'a' && letter <= 'z')
		goal[((int)(letter) - 97) + 26] = 1;

	return goal;
}

float **goalMatrix()
{
	float **goalMatrix = malloc(sizeof(float*) * 52);
	char maj = 'A';
	char min = 'a';
	for(int i = 0; i < 52; i++)
	{
		if(i < 26)
    	{
			goalMatrix[i] = goalArray(maj);
			maj++;
		}
		else
		{
			goalMatrix[i] = goalArray(min);
			min++;
		}
	}
	return goalMatrix;
}

float *matrixFromFile(char *filename)
{
	float *matrix = malloc(sizeof(float) * 28 * 28);
	FILE *file = fopen(filename, "r");

	if(file == NULL)
		printf("File is NULL \n");

	for(int i = 0; i <= 28; i++)
	{
		for(int j = 0; j <= 28; j++)
		{
			int c = fgetc(file);
			if(c == 49)
				matrix[j+i*28] = 1;
			if(c == 48)
				matrix[j+i*28] = 0;
		}
	}
	fclose(file);
	return matrix;
}

float **lettersMatrix()
{
	char majs_path[22] = "Dataset/majs/0/00.txt\0";
	char mins_path[22] = "Dataset/mins/0/00.txt\0";
	float **lettersMatrix = malloc(sizeof(float *) * 52);
	char maj = 'A';
	char min = 'a';
	char count = '4';

	for(int i = 0; i < 52; i++)
	{

		if(i < 26 * 1)
		{
			majs_path[13] = maj;
			majs_path[15] = maj;
			majs_path[16] = count;
			printf("%s\n",majs_path);
			lettersMatrix[i] = matrixFromFile(majs_path);
			maj++;
		}
		else if(i >= 26*1)
		{
			count = '3';
			mins_path[13] = min;
			mins_path[15] = min;
			mins_path[16] = count;
			printf("%s\n",mins_path);
			lettersMatrix[i] = matrixFromFile(mins_path);
			min++;
		}
	}
	return lettersMatrix;
}