File size: 783 Bytes
11a3bed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#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);
}