-
Notifications
You must be signed in to change notification settings - Fork 8
/
topk_binary.c
119 lines (104 loc) · 3.3 KB
/
topk_binary.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
/* Copyright (c) 2019-present, All rights reserved.
* Written by Julien Tissier <[email protected]>
*
* This file is part of the "Near-lossless Binarization of Word Embeddings"
* software (https://github.com/tca19/near-lossless-binarization).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License at the root of this repository for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h> /* fprintf() */
#include <stdlib.h> /* calloc() */
#include <time.h> /* clock() */
#include "utils.h"
struct neighbor
{
long index;
float similarity;
};
/* find_topk: return the k nearest neighbors of word */
struct neighbor *find_topk(const char *word, const int k, const long n_vecs,
const int n_long, unsigned long **vec)
{
long i, j, index;
struct neighbor *topk, tmp;
if ((topk = calloc(k + 1, sizeof *topk)) == NULL)
{
fprintf(stderr, "find_topk: can't allocate memory for heap\n");
exit(1);
}
/* word has no vector, can't find its neighbors */
if ((index = get_index(word)) < 0)
return NULL;
for (i = 0; i < n_vecs; ++i)
{
/* a word cannot be its nearest neighbor; skip it */
if (i == index)
continue;
/* values in topk are sorted by decreasing similarity. If the
* similarity with current vector is greater than minimal
* similarity in topk, insert current similarity into topk with
* bubble sort */
topk[k].similarity = binary_sim(vec[index], vec[i], n_long);
if (topk[k].similarity < topk[k-1].similarity)
continue;
for (topk[k].index = i, j = k;
j > 0 && topk[j].similarity > topk[j-1].similarity;
--j)
{
/* swap element j-1 with element j */
tmp = topk[j-1];
topk[j-1] = topk[j];
topk[j] = tmp;
}
}
return topk;
}
int main(int argc, char *argv[])
{
int n_bits, n_long; /* #bits per vector, #long per array */
long n_vecs; /* #vectors in embedding file */
unsigned long **embedding;
struct neighbor *topk;
int i, k;
clock_t start, end;
if (argc < 4)
{
printf("usage: ./topk_binary EMBEDDING K QUERY...\n");
exit(1);
}
embedding = load_vectors(*++argv, &n_vecs, &n_bits, &n_long, 1);
k = atoi(*++argv);
argc -= 2; /* because already used argument 0 and 1 */
while (--argc > 0)
{
start = clock();
topk = find_topk(*++argv, k, n_vecs, n_long, embedding);
end = clock();
if (topk == NULL)
{
printf("%s doesn't have a vector; can't find its"
" nearest neighbors.\n\n", *argv);
continue;
}
printf("Top %d closest words of %s\n", k, *argv);
for (i = 0; i < k; ++i)
printf(" %-15s %.3f\n", words[topk[i].index],
topk[i].similarity);
printf("> Query processed in %.3f ms.\n",
(double) (end - start) * 1000 / CLOCKS_PER_SEC);
printf("\n");
}
return 0;
}