Skip to content

Commit 3c464c2

Browse files
aa
git-svn-id: http://word2vec.googlecode.com/svn/trunk@27 c84ef02e-58a5-4c83-e53e-41fc32d635eb
1 parent 00e86fa commit 3c464c2

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

compute-accuracy.c

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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 <malloc.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], ch;
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+
fscanf(f, "%s%c", &vocab[b * max_w], &ch);
57+
for (a = 0; a < max_w; a++) vocab[b * max_w + a] = toupper(vocab[b * max_w + a]);
58+
for (a = 0; a < size; a++) fread(&M[a + b * size], sizeof(float), 1, f);
59+
len = 0;
60+
for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
61+
len = sqrt(len);
62+
for (a = 0; a < size; a++) M[a + b * size] /= len;
63+
}
64+
fclose(f);
65+
TCN = 0;
66+
while (1) {
67+
for (a = 0; a < N; a++) bestd[a] = 0;
68+
for (a = 0; a < N; a++) bestw[a][0] = 0;
69+
scanf("%s", st1);
70+
for (a = 0; a < strlen(st1); a++) st1[a] = toupper(st1[a]);
71+
if ((!strcmp(st1, ":")) || (!strcmp(st1, "EXIT")) || feof(stdin)) {
72+
if (TCN == 0) TCN = 1;
73+
if (QID != 0) {
74+
printf("ACCURACY TOP1: %.2f %% (%d / %d)\n", CCN / (float)TCN * 100, CCN, TCN);
75+
printf("Total accuracy: %.2f %% Semantic accuracy: %.2f %% Syntactic accuracy: %.2f %% \n", CACN / (float)TACN * 100, SEAC / (float)SECN * 100, SYAC / (float)SYCN * 100);
76+
}
77+
QID++;
78+
scanf("%s", st1);
79+
if (feof(stdin)) break;
80+
printf("%s:\n", st1);
81+
TCN = 0;
82+
CCN = 0;
83+
continue;
84+
}
85+
if (!strcmp(st1, "EXIT")) break;
86+
scanf("%s", st2);
87+
for (a = 0; a < strlen(st2); a++) st2[a] = toupper(st2[a]);
88+
scanf("%s", st3);
89+
for (a = 0; a<strlen(st3); a++) st3[a] = toupper(st3[a]);
90+
scanf("%s", st4);
91+
for (a = 0; a < strlen(st4); a++) st4[a] = toupper(st4[a]);
92+
for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st1)) break;
93+
b1 = b;
94+
for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st2)) break;
95+
b2 = b;
96+
for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st3)) break;
97+
b3 = b;
98+
for (a = 0; a < N; a++) bestd[a] = 0;
99+
for (a = 0; a < N; a++) bestw[a][0] = 0;
100+
for (a = 0; a < size; a++) vec[a] = (M[a + b2 * size] - M[a + b1 * size]) + M[a + b3 * size];
101+
TQ++;
102+
if (b1 == words) continue;
103+
if (b2 == words) continue;
104+
if (b3 == words) continue;
105+
for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st4)) break;
106+
if (b == words) continue;
107+
TQS++;
108+
for (c = 0; c < words; c++) {
109+
if (c == b1) continue;
110+
if (c == b2) continue;
111+
if (c == b3) continue;
112+
dist = 0;
113+
for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
114+
for (a = 0; a < N; a++) {
115+
if (dist > bestd[a]) {
116+
for (d = N - 1; d > a; d--) {
117+
bestd[d] = bestd[d - 1];
118+
strcpy(bestw[d], bestw[d - 1]);
119+
}
120+
bestd[a] = dist;
121+
strcpy(bestw[a], &vocab[c * max_w]);
122+
break;
123+
}
124+
}
125+
}
126+
if (!strcmp(st4, bestw[0])) {
127+
CCN++;
128+
CACN++;
129+
if (QID <= 5) SEAC++; else SYAC++;
130+
}
131+
if (QID <= 5) SECN++; else SYCN++;
132+
TCN++;
133+
TACN++;
134+
}
135+
printf("Questions seen / total: %d %d %.2f %% \n", TQS, TQ, TQS/(float)TQ*100);
136+
return 0;
137+
}

0 commit comments

Comments
 (0)