Marc Kupietz | d6f9c71 | 2016-03-16 11:50:56 +0100 | [diff] [blame] | 1 | // Copyright 2013 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include <math.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <ctype.h> |
| 21 | |
| 22 | const long long max_size = 2000; // max length of strings |
| 23 | const long long N = 1; // number of closest words |
| 24 | const long long max_w = 50; // max length of vocabulary entries |
| 25 | |
| 26 | int main(int argc, char **argv) |
| 27 | { |
| 28 | FILE *f; |
| 29 | char st1[max_size], st2[max_size], st3[max_size], st4[max_size], bestw[N][max_size], file_name[max_size]; |
| 30 | float dist, len, bestd[N], vec[max_size]; |
| 31 | long long words, size, a, b, c, d, b1, b2, b3, threshold = 0; |
| 32 | float *M; |
| 33 | char *vocab; |
| 34 | int TCN, CCN = 0, TACN = 0, CACN = 0, SECN = 0, SYCN = 0, SEAC = 0, SYAC = 0, QID = 0, TQ = 0, TQS = 0; |
| 35 | if (argc < 2) { |
| 36 | printf("Usage: ./compute-accuracy <FILE> <threshold>\nwhere FILE contains word projections, and threshold is used to reduce vocabulary of the model for fast approximate evaluation (0 = off, otherwise typical value is 30000)\n"); |
| 37 | return 0; |
| 38 | } |
| 39 | strcpy(file_name, argv[1]); |
| 40 | if (argc > 2) threshold = atoi(argv[2]); |
| 41 | f = fopen(file_name, "rb"); |
| 42 | if (f == NULL) { |
| 43 | printf("Input file not found\n"); |
| 44 | return -1; |
| 45 | } |
| 46 | fscanf(f, "%lld", &words); |
| 47 | if (threshold) if (words > threshold) words = threshold; |
| 48 | fscanf(f, "%lld", &size); |
| 49 | vocab = (char *)malloc(words * max_w * sizeof(char)); |
| 50 | M = (float *)malloc(words * size * sizeof(float)); |
| 51 | if (M == NULL) { |
| 52 | printf("Cannot allocate memory: %lld MB\n", words * size * sizeof(float) / 1048576); |
| 53 | return -1; |
| 54 | } |
| 55 | for (b = 0; b < words; b++) { |
| 56 | a = 0; |
| 57 | while (1) { |
| 58 | vocab[b * max_w + a] = fgetc(f); |
| 59 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 60 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 61 | } |
| 62 | vocab[b * max_w + a] = 0; |
| 63 | for (a = 0; a < max_w; a++) vocab[b * max_w + a] = toupper(vocab[b * max_w + a]); |
| 64 | for (a = 0; a < size; a++) fread(&M[a + b * size], sizeof(float), 1, f); |
| 65 | len = 0; |
| 66 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 67 | len = sqrt(len); |
| 68 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 69 | } |
| 70 | fclose(f); |
| 71 | TCN = 0; |
| 72 | while (1) { |
| 73 | for (a = 0; a < N; a++) bestd[a] = 0; |
| 74 | for (a = 0; a < N; a++) bestw[a][0] = 0; |
| 75 | scanf("%s", st1); |
| 76 | for (a = 0; a < strlen(st1); a++) st1[a] = toupper(st1[a]); |
| 77 | if ((!strcmp(st1, ":")) || (!strcmp(st1, "EXIT")) || feof(stdin)) { |
| 78 | if (TCN == 0) TCN = 1; |
| 79 | if (QID != 0) { |
| 80 | printf("ACCURACY TOP1: %.2f %% (%d / %d)\n", CCN / (float)TCN * 100, CCN, TCN); |
| 81 | printf("Total accuracy: %.2f %% Semantic accuracy: %.2f %% Syntactic accuracy: %.2f %% \n", CACN / (float)TACN * 100, SEAC / (float)SECN * 100, SYAC / (float)SYCN * 100); |
| 82 | } |
| 83 | QID++; |
| 84 | scanf("%s", st1); |
| 85 | if (feof(stdin)) break; |
| 86 | printf("%s:\n", st1); |
| 87 | TCN = 0; |
| 88 | CCN = 0; |
| 89 | continue; |
| 90 | } |
| 91 | if (!strcmp(st1, "EXIT")) break; |
| 92 | scanf("%s", st2); |
| 93 | for (a = 0; a < strlen(st2); a++) st2[a] = toupper(st2[a]); |
| 94 | scanf("%s", st3); |
| 95 | for (a = 0; a<strlen(st3); a++) st3[a] = toupper(st3[a]); |
| 96 | scanf("%s", st4); |
| 97 | for (a = 0; a < strlen(st4); a++) st4[a] = toupper(st4[a]); |
| 98 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st1)) break; |
| 99 | b1 = b; |
| 100 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st2)) break; |
| 101 | b2 = b; |
| 102 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st3)) break; |
| 103 | b3 = b; |
| 104 | for (a = 0; a < N; a++) bestd[a] = 0; |
| 105 | for (a = 0; a < N; a++) bestw[a][0] = 0; |
| 106 | TQ++; |
| 107 | if (b1 == words) continue; |
| 108 | if (b2 == words) continue; |
| 109 | if (b3 == words) continue; |
| 110 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st4)) break; |
| 111 | if (b == words) continue; |
| 112 | for (a = 0; a < size; a++) vec[a] = (M[a + b2 * size] - M[a + b1 * size]) + M[a + b3 * size]; |
| 113 | TQS++; |
| 114 | for (c = 0; c < words; c++) { |
| 115 | if (c == b1) continue; |
| 116 | if (c == b2) continue; |
| 117 | if (c == b3) continue; |
| 118 | dist = 0; |
| 119 | for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
| 120 | for (a = 0; a < N; a++) { |
| 121 | if (dist > bestd[a]) { |
| 122 | for (d = N - 1; d > a; d--) { |
| 123 | bestd[d] = bestd[d - 1]; |
| 124 | strcpy(bestw[d], bestw[d - 1]); |
| 125 | } |
| 126 | bestd[a] = dist; |
| 127 | strcpy(bestw[a], &vocab[c * max_w]); |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | if (!strcmp(st4, bestw[0])) { |
| 133 | CCN++; |
| 134 | CACN++; |
| 135 | if (QID <= 5) SEAC++; else SYAC++; |
| 136 | } |
| 137 | if (QID <= 5) SECN++; else SYCN++; |
| 138 | TCN++; |
| 139 | TACN++; |
| 140 | } |
| 141 | printf("Questions seen / total: %d %d %.2f %% \n", TQS, TQ, TQS/(float)TQ*100); |
| 142 | return 0; |
| 143 | } |