antirez's picture
Upload 3 files
11a3bed verified
raw
history blame contribute delete
783 Bytes
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main(void) {
FILE *fp = fopen("word2vec.bin","rb");
struct {
uint32_t items;
uint32_t dim;
} hdr;
fread(&hdr,sizeof(hdr),1,fp);
printf("%u items of size %u\n", hdr.items, hdr.dim);
float *v = malloc(sizeof(float)*hdr.dim); // You should check for NULL.
for (int j = 0; j < 10; j++) {
uint16_t wlen;
fread(&wlen,sizeof(wlen),1,fp);
char *word = malloc(wlen+1); // Check for NULL.
fread(word,wlen,1,fp);
word[wlen] = 0; // Null term.
fread(v,hdr.dim*sizeof(float),1,fp); // Read the vector of floats.
printf("\"%s\" -> %f, %f, %f, ...\n",word,v[0],v[1],v[2]);
free(word);
}
free(v);
fclose(fp);
}