-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_maker.c
More file actions
256 lines (218 loc) · 7.54 KB
/
query_maker.c
File metadata and controls
256 lines (218 loc) · 7.54 KB
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
//
// Created by jorge on 03-12-17.
//
//Librerias
#include <stdlib.h>
#include <stdio.h>
#include "similitud.h"
#include <time.h>
//Macros
#define DEBUG 0
#define ITERS 5
#define N_QUERY 8606// Largo query
#define N_DIC 77455 // Largo diccionario
#define N_WORD 64 // Largo palabras
//Variables globales
int long cuentaeditdist = 0;
char QUERY_PATH[128];
char PIVOTS_PATH[128];
int N_PIVOTS;
char DICTIONARY_PATH[128];
char INDEX_PATH[128];
int RANGE;
char RESULT_PATH[128];
// ./query_maker query.txt pivotes.txt N_PIVOTES diccionario.txt indice.txt RANGO resultado.txt
int main(int argc, char *(argv[])) {
int i, j, k; //Iteradores
FILE *dic, *p, *index, *q, *results; //Archivos
int D_ip, D_qp; //Variables formula candidatos
double time_search = 0; //Tiempo demora promedio
struct timespec ts1, ts2; //Variables toma de tiempos
long int n_candidates = 0, n_solutions = 0; //N cadidatos y soluciones
if (argc != 8) {
printf("Error en el numero de parametros\n");
return -1;
}
strcpy(QUERY_PATH, argv[1]);
strcpy(PIVOTS_PATH, argv[2]);
N_PIVOTS = atoi(argv[3]);
strcpy(DICTIONARY_PATH, argv[4]);
strcpy(INDEX_PATH, argv[5]);
RANGE = atoi(argv[6]);
strcpy(RESULT_PATH, argv[7]);
//Abrir archivos
if ((dic = fopen(DICTIONARY_PATH, "r")) == NULL) {
printf("Error load [DIC]\n");
return -1;
}
if ((p = fopen(PIVOTS_PATH, "r")) == NULL) {
printf("Error load [PIVOT]\n");
return -1;
}
if ((index = fopen(INDEX_PATH, "r")) == NULL) {
printf("Error load [INDEX]\n");
return -1;
}
if ((q = fopen(QUERY_PATH, "r")) == NULL) {
printf("Error load [QUERY]\n");
return -1;
}
if ((results = fopen(RESULT_PATH, "w")) == NULL) {
printf("Error open [RESULTADOS]\n");
return -1;
}
printf("Open files OK...\n");
//CARGAR INDICE EN MEMORIA
int **dist_word_pivot = malloc(N_DIC * sizeof(int *));
for (i = 0; i < N_DIC; i++)
dist_word_pivot[i] = malloc(N_PIVOTS * sizeof(int));
for (i = 0; i < N_DIC; i++) {
for (j = 0; j < N_PIVOTS; j++) {
fscanf(index, "%d", &dist_word_pivot[i][j]);
}
}
//CARGAR QUERY EN MEMORIA
char **querys = malloc(N_QUERY * sizeof(char *));
for (i = 0; i < N_QUERY; i++) {
querys[i] = malloc(N_WORD * sizeof(char));
fscanf(q, "%s", querys[i]);
}
//CARGAR DICCIONARIO EN MEMORIA
char **words = malloc(N_DIC * sizeof(char *));
for (i = 0; i < N_DIC; i++) {
words[i] = malloc(N_WORD * sizeof(char));
fscanf(dic, "%s", words[i]);
}
//CARGAR PIVOTES EN MEMORIA
char **pivots = malloc(N_PIVOTS * sizeof(char *));
for (i = 0; i < N_PIVOTS; i++) {
pivots[i] = malloc(N_WORD * sizeof(char));
fscanf(p, "%s", pivots[i]);
}
//printf("%d\n", edit("aino", "ino",0,0));
printf("---------------------------------------------------------------------\n");
printf("PIVOTES:%d\tRANGO:%d\n", N_PIVOTS, RANGE);
printf("Analizar query...\n");
//Matriz precalculos
int **dist_query_pivot = malloc(N_QUERY * sizeof(int *));
for (i = 0; i < N_QUERY; i++) {
dist_query_pivot[i] = malloc(N_PIVOTS * sizeof(int));
}
//INICIO PRUEBAS
for (int l = 0; l < ITERS; ++l) {
printf("Iter..%d\n", l);
n_candidates = 0;
n_solutions = 0;
cuentaeditdist = 0;
//INICIO TOMA DE TIEMPO
clock_gettime(CLOCK_REALTIME, &ts1);
//Precalcular distancias query-pivote
for (i = 0; i < N_QUERY; i++) {
for (j = 0; j < N_PIVOTS; j++) {
dist_query_pivot[i][j] = edit(querys[i], pivots[j], 0, 0);
}
}
#if DEBUG
printf("Precalculo distancia pivote-query OK..\n");
#endif
//Ciclo lectura querys
for (k = 0; k < N_QUERY; k++) {
#if DEBUG
printf("\nQUERY = %s, range = %d\n", querys[k], RANGE);
#else
//printf("Q = %d...\n", k);
#endif
//Ciclo lectura palabras diccionario
for (i = 0; i < N_DIC; i++) {
//Ciclo lectura pivotes
for (j = 0; j < N_PIVOTS; j++) {
D_ip = dist_word_pivot[i][j];
D_qp = dist_query_pivot[k][j];
if (abs(D_ip - D_qp) > RANGE) {
#if DEBUG
printf("fuera rango |\t");
#endif
break;
}
#if DEBUG
printf("candidato |\t");
#endif
}
if (j == N_PIVOTS) {
#if DEBUG
printf("POSIBLE CANDIDATO!!!! |");
#endif
n_candidates++;
if (edit(querys[k], words[i], 0, 0) <= RANGE) {
#if DEBUG
printf("!!!SOLUCION!!!");
#endif
n_solutions++;
//fprintf(results, "%s %s\n", querys[k], words[i]);
}
}
#if DEBUG
printf("/// %s", words[i]);
printf("\n");
#endif
}
#if DEBUG
printf("----------------------------------------------------------------------------------------------\n");
#endif
}
clock_gettime(CLOCK_REALTIME, &ts2); //FIN TOMA DE TIEMPO
time_search += (float) (1.0 * (1.0 * ts2.tv_nsec - ts1.tv_nsec * 1.0) * 1e-9 + 1.0 * ts2.tv_sec -
1.0 * ts1.tv_sec);
}
/*
n_solutions = 0;
clock_gettime(CLOCK_REALTIME, &ts1);
for(i = 0; i < N_QUERY; i++){
printf("%d...\n", i);
for(j = 0; j < N_DIC; j++){
if (edit(querys[i], words[j],0,0) <= RANGE){
n_solutions++;
}
}
}
clock_gettime(CLOCK_REALTIME, &ts2);
time_search += (float) (1.0 * (1.0 * ts2.tv_nsec - ts1.tv_nsec * 1.0) * 1e-9 + 1.0 * ts2.tv_sec -
1.0 * ts1.tv_sec);
*/
time_search = time_search / ITERS;
printf("Iteraciones:%d\nTiempo Promedio:=%.4g segundos\nCandidatos:%ld\nSoluciones:%ld\nEvaluaciones totales:%ld\n", ITERS,
time_search, n_candidates, n_solutions, cuentaeditdist);
printf("---------------------------------------------------------------------\n");
fprintf(results, "Tiempo promedio:\t%.4g\n", time_search);
fprintf(results, "Rango\t%d\n", RANGE);
fprintf(results, "Numero pivotes:\t%d\n", N_PIVOTS);
fprintf(results, "Evaluaciones\t%ld\n", cuentaeditdist);
fprintf(results, "Candidatos:\t%ld\n", n_candidates);
fprintf(results, "Soluciones:\t%ld\n", n_solutions);
fclose(results);
fclose(index);
fclose(dic);
fclose(q);
fclose(p);
for( i = 0; i < N_QUERY; i++){
free(dist_query_pivot[i]);
}
free(dist_query_pivot);
for( i = 0; i < N_DIC; i++){
free(dist_word_pivot[i]);
}
free(dist_word_pivot);
for( i = 0; i < N_DIC; i++){
free(words[i]);
}
free(words);
for( i = 0; i < N_QUERY; i++){
free(querys[i]);
}
free(querys);
for( i = 0; i < N_PIVOTS; i++){
free(pivots[i]);
}
free(pivots);
return 0;
}