blob: da5b67e674c7153e2092fbf2c63d40abf5128f3b [file] [log] [blame]
Marc Kupietzd6f9c712016-03-16 11:50:56 +01001// 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 <string.h>
17#include <math.h>
18#include <stdlib.h>
19#include <time.h>
20
21const long long max_size = 2000; // max length of strings
22const long long N = 40; // number of closest words that will be shown
23const long long max_w = 50; // max length of vocabulary entries
24
25#define MAX_STRING 100
26void ReadWord(char *word, FILE *fin) {
27 int a = 0, ch;
28 while (!feof(fin)) {
29 ch = fgetc(fin);
30 if (ch == 13) continue;
31 if ((ch == ' ') || (ch == '\t') || (ch == '\n')) {
32 if (a > 0) {
33 if (ch == '\n') ungetc(ch, fin);
34 break;
35 }
36 if (ch == '\n') {
37 strcpy(word, (char *)"</s>");
38 return;
39 } else continue;
40 }
41 word[a] = ch;
42 a++;
43 if (a >= MAX_STRING - 1) a--; // Truncate too long words
44 }
45 word[a] = 0;
46}
47
48int main(int argc, char **argv) {
49 FILE *f;
50 char st1[max_size];
51 char *bestw[N];
52 char file_name[max_size], st[100][max_size];
53 float dist, len, bestd[N], vec[max_size];
54 long long words, size, a, b, c, d, cn, bi[100];
55 float *M;
56 char *vocab;
57 char word[MAX_STRING];
58 clock_t begin;
59 if (argc < 2) {
60 printf("Usage: ./distance <FILE>\nwhere FILE contains word projections in the BINARY FORMAT\n");
61 return 0;
62 }
63 strcpy(file_name, argv[1]);
64 f = fopen(file_name, "rb");
65 if (f == NULL) {
66 printf("Input file not found\n");
67 return -1;
68 }
69 ReadWord(word, f);
70 words = atoi(word);
71 ReadWord(word, f);
72 size = atoi(word);
73 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
74 for (a = 0; a < N; a++) bestw[a] = (char *)malloc(max_size * sizeof(char));
75 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
76 if (M == NULL) {
77 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
78 return -1;
79 }
80 for (b = 0; b < words; b++) {
81 a = 0;
82 while (1) {
83 vocab[b * max_w + a] = fgetc(f);
84 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
85 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
86 }
87 vocab[b * max_w + a] = 0;
88 for (a = 0; a < size; a++) {
89 ReadWord(word,f);
90 M[a + b * size] = atof(word);
91 }
92 len = 0;
93 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
94 len = sqrt(len);
95 for (a = 0; a < size; a++) M[a + b * size] /= len;
96 }
97 fclose(f);
98 while (1) {
99 for (a = 0; a < N; a++) bestd[a] = 0;
100 for (a = 0; a < N; a++) bestw[a][0] = 0;
101 printf("Enter word or sentence (EXIT to break): ");
102 a = 0;
103 while (1) {
104 st1[a] = fgetc(stdin);
105 if ((st1[a] == '\n') || (a >= max_size - 1)) {
106 st1[a] = 0;
107 break;
108 }
109 a++;
110 }
111 if (!strcmp(st1, "EXIT")) break;
112 cn = 0;
113 b = 0;
114 c = 0;
115 while (1) {
116 st[cn][b] = st1[c];
117 b++;
118 c++;
119 st[cn][b] = 0;
120 if (st1[c] == 0) break;
121 if (st1[c] == ' ') {
122 cn++;
123 b = 0;
124 c++;
125 }
126 }
127 cn++;
128 for (a = 0; a < cn; a++) {
129 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
130 if (b == words) b = -1;
131 bi[a] = b;
132 printf("\nWord: %s Position in vocabulary: %lld\n", st[a], bi[a]);
133 if (b == -1) {
134 printf("Out of dictionary word!\n");
135 break;
136 }
137 }
138 if (b == -1) continue;
139 begin = clock();
140
141 printf("\n Word Cosine distance\n------------------------------------------------------------------------\n");
142 for (a = 0; a < size; a++) vec[a] = 0;
143 for (b = 0; b < cn; b++) {
144 if (bi[b] == -1) continue;
145 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
146 }
147 len = 0;
148 for (a = 0; a < size; a++) len += vec[a] * vec[a];
149 len = sqrt(len);
150 for (a = 0; a < size; a++) vec[a] /= len;
151 for (a = 0; a < N; a++) bestd[a] = -1;
152 for (a = 0; a < N; a++) bestw[a][0] = 0;
153 for (c = 0; c < words; c++) {
154 a = 0;
155 for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
156 if (a == 1) continue;
157 dist = 0;
158 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
159 for (a = 0; a < N; a++) {
160 if (dist > bestd[a]) {
161 for (d = N - 1; d > a; d--) {
162 bestd[d] = bestd[d - 1];
163 strcpy(bestw[d], bestw[d - 1]);
164 }
165 bestd[a] = dist;
166 strcpy(bestw[a], &vocab[c * max_w]);
167 break;
168 }
169 }
170 }
171 for (a = 0; a < N; a++) printf("%50s\t\t%f\n", bestw[a], bestd[a]);
172 printf("time spent = %f seconds\n", (double)(clock() - begin) / CLOCKS_PER_SEC);
173 }
174 return 0;
175}