-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoletorDeInfoemacoesComMemoria.c
282 lines (247 loc) · 8.89 KB
/
coletorDeInfoemacoesComMemoria.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Titulo: coletor de informacoes com memoria
* Descricao: algoritmo que extrai a matriz de transicao de uma cadeia considerando contextos
*
* Autor: Pedro Vaz
* Data: marco de 2021
*
* Software relacionado aa IC "Estimacao de Cadeias de Markov com
* Alcance Variavel e suas Aplicacoes"
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
char* elementosDoAlfabeto;
int tamanhoDoAlfabeto = 0;
int profundidade = 0;
typedef struct{
char *contexto;
unsigned int *ocorrenciasDosSubsequentes;
unsigned int somatorioDasOcorrenciasDosSubsequentes;
double *razaoDeTransicao;
}Contexto;
void imprimirIntro(){
#ifdef __unix__
system("clear");
#elif defined(_WIN32) || defined(WIN32)
system("cls");
#endif
printf("\nColetor de informacoes de uma cadeia de Markov");
printf("\nDescricao: algoritmo que extrai a matriz de transicao de uma cadeia considerando contextos;");
printf("\nAutor: Pedro Henrique Estevam Vaz de Melo;");
printf("\nData: marco de 2020.\n\n");
}
void entrada_caminhoParaArquivo(char* caminhoParaArquivo){
bool arquivoExiste = false;
do{
printf("\tDigite o nome do arquivo em que esta a cadeia a ser lida: ");
scanf("%s", caminhoParaArquivo);
FILE* arquivo = fopen(caminhoParaArquivo, "r");
if(arquivo != NULL){
arquivoExiste = true;
}else{
printf("\n\tERRO! O arquivo nao existe. Tente novamente.\n");
}
}while(!arquivoExiste);
caminhoParaArquivo = (char*) realloc(caminhoParaArquivo, strlen(caminhoParaArquivo));
}
bool charEhAlgarismo(char c){
if(c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9'){
return true;
}
return false;
}
int charParaAlgarismo(char c){
if(c == '0')
return 0;
else if(c == '1')
return 1;
else if(c == '2')
return 2;
else if(c == '3')
return 3;
else if(c == '4')
return 4;
else if(c == '5')
return 5;
else if(c == '6')
return 6;
else if(c == '7')
return 7;
else if(c == '8')
return 8;
else if(c == '9')
return 9;
else
return -1;
}
char algarismoParaChar(int algarismo){
if(algarismo == 1){
return '1';
}else if(algarismo == 2){
return '2';
}else if(algarismo == 3){
return '3';
}else if(algarismo == 4){
return '4';
}else if(algarismo == 5){
return '5';
}else if(algarismo == 6){
return '6';
}else if(algarismo == 7){
return '7';
}else if(algarismo == 8){
return '8';
}else if(algarismo == 9){
return '9';
}else if(algarismo == 0){
return '0';
}else{
return ' ';
}
}
void criaContextos(Contexto *contextos, int indiceContextosInicio, int indiceContextosFim, int indiceIdContexto){
if(indiceIdContexto < profundidade){
int faixaDeTrabalho = indiceContextosFim - indiceContextosInicio;
int faixaDeRepeticao = faixaDeTrabalho / tamanhoDoAlfabeto;
int indiceVetorAlfabeto = 0, indiceContextos = indiceContextosInicio;
while(indiceVetorAlfabeto < tamanhoDoAlfabeto){
for(int i = 0; i < faixaDeRepeticao; i++){
contextos[indiceContextos].contexto[indiceIdContexto] = elementosDoAlfabeto[indiceVetorAlfabeto];
indiceContextos++;
}
indiceVetorAlfabeto++;
}
// Condicao de parada da recurcao
if(faixaDeTrabalho == 1){
return;
}else{
for(int i = indiceContextosInicio; i < indiceContextosFim; i += faixaDeRepeticao){
criaContextos(contextos, i, i + faixaDeRepeticao, indiceIdContexto + 1);
}
}
}
}
bool charPertenceAoAlfabeto(char c){
for(int i = 0; i < tamanhoDoAlfabeto; i++){
if(elementosDoAlfabeto[i] == c){
return true;
}
}
return false;
}
int main(){
imprimirIntro();
// Entrada para o caminho do arquivo
char* caminhoParaArquivo = (char*) malloc(4096 * sizeof(char));
entrada_caminhoParaArquivo(caminhoParaArquivo);
// Entrada para o alfabeto da cadeia
bool entradaRealizadaComSucesso = false;
while(!entradaRealizadaComSucesso){
printf("\tDigite o tamanho do alfabeto da cadeia a ser analisada: ");
scanf("%d", &tamanhoDoAlfabeto);
if(tamanhoDoAlfabeto < 1 || tamanhoDoAlfabeto > 10){
printf("Erro! O tamanho do alfabeto deve ser entre 1 e 10.\nPor favor, tente novamente!\n");
}else{
entradaRealizadaComSucesso = true;
}
}
// Entrada para a profundidade da cadeia
entradaRealizadaComSucesso = false;
while(!entradaRealizadaComSucesso){
printf("\tDigite a profundidade a ser analisada: ");
scanf("%d", &profundidade);
if(profundidade < 1){
printf("Erro! A profundidade da cadeia deve smaior que 1.\nPor favor, tente novamente!\n");
}else{
entradaRealizadaComSucesso = true;
}
}
// Definicao do vetor de elementos do alfabeto
elementosDoAlfabeto = (char*) malloc(tamanhoDoAlfabeto * sizeof(char));
for(int i = 0; i < tamanhoDoAlfabeto; i++){
elementosDoAlfabeto[i] = algarismoParaChar(i);
}
// Cria contextos
int quantidadeDeContextos = (int)pow(tamanhoDoAlfabeto, profundidade);
Contexto* contextos = (Contexto*) malloc(quantidadeDeContextos * sizeof(Contexto));
for(int i = 0; i < quantidadeDeContextos; i++){
contextos[i].contexto = (char*) malloc(profundidade * sizeof(char));
contextos[i].ocorrenciasDosSubsequentes = (unsigned int*) malloc(tamanhoDoAlfabeto * sizeof(unsigned int));
contextos[i].razaoDeTransicao = (double*) malloc(tamanhoDoAlfabeto * sizeof(double));
for(int j = 0; j < tamanhoDoAlfabeto; j++){
contextos[i].ocorrenciasDosSubsequentes[j] = 0;
contextos[i].somatorioDasOcorrenciasDosSubsequentes = 0;
contextos[i].razaoDeTransicao[j] = 0.0;
}
}
criaContextos(contextos, 0, quantidadeDeContextos, 0);
// Ocorrencia de contextos e subsequentes
char memoriaDaCadeia[profundidade];
char elementoAtual;
FILE *arquivo = fopen(caminhoParaArquivo, "r");
for(int i = profundidade - 1; i <= 0; i--){
memoriaDaCadeia[i] = fgetc(arquivo); // Carregando vetor memoria da cadeia
}
do{
elementoAtual = fgetc(arquivo);
// Verificando se o contexto atual
if(charEhAlgarismo(elementoAtual)){
for(int i = 0; i < quantidadeDeContextos; i++){
bool contextoEncontrado = false;
for(int j = 0; j < profundidade; j++){
if(memoriaDaCadeia[j] != contextos[i].contexto[j]){
break;
}else if(memoriaDaCadeia[j] == contextos[i].contexto[j] && j == profundidade - 1){
contextoEncontrado = true;
break;
}
}
if(contextoEncontrado && charPertenceAoAlfabeto(elementoAtual)){
contextos[i].ocorrenciasDosSubsequentes[charParaAlgarismo(elementoAtual)]++;
contextos[i].somatorioDasOcorrenciasDosSubsequentes++;
break;
}
}
}
// Reorganizando elementos da memoria da cadeia
int indiceMemoriaDaCadeia = profundidade - 2;
while(indiceMemoriaDaCadeia >= 0){
memoriaDaCadeia[indiceMemoriaDaCadeia + 1] = memoriaDaCadeia[indiceMemoriaDaCadeia];
indiceMemoriaDaCadeia--;
}
memoriaDaCadeia[0] = elementoAtual;
}while(elementoAtual != EOF);
fclose(arquivo);
// Calculo de estatisticas de transicao
for(int i = 0; i < quantidadeDeContextos; i++){
for(int j = 0; j < tamanhoDoAlfabeto; j++){
if(contextos[i].somatorioDasOcorrenciasDosSubsequentes == 0){
contextos[i].razaoDeTransicao[j] = 0.0;
}else{
contextos[i].razaoDeTransicao[j] = (double) contextos[i].ocorrenciasDosSubsequentes[j] / (double) contextos[i].somatorioDasOcorrenciasDosSubsequentes;
}
}
}
// Imprimindo Resultados
printf("\nResultados:");
for(int i = 0; i < quantidadeDeContextos; i++){
printf("\n\tContexto: ");
for(int j = 0; j < profundidade; j++){
printf("%c", contextos[i].contexto[j]);
}
printf("\t");
for(int j = 0; j < tamanhoDoAlfabeto; j++){
if(contextos[i].razaoDeTransicao[j] == 0.0){
printf("subsequente %d : -- \t", j);
}else{
printf("subsequente %d : %lf\t", j, contextos[i].razaoDeTransicao[j]);
}
}
}
printf("\n");
return 0;
}