Skip to content

Commit

Permalink
Modified word_distance to work with paragraph file names instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Seymour committed Aug 6, 2014
1 parent de60427 commit 3657330
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions phrase_distance.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
#include <string.h>
#include <math.h>
#include <malloc.h>
#include <limits.h>

const long long max_size = 2000; // max length of strings
const long long N = 40; // number of closest words that will be shown
const long long max_w = 50; // max length of vocabulary entries
const long long max_w = PATH_MAX; // max length of file names

int main(int argc, char **argv) {
FILE *f;
Expand All @@ -30,9 +31,9 @@ int main(int argc, char **argv) {
long long words, size, a, b, c, d, cn, bi[100];
char ch;
float *M;
char *vocab;
char *files;
if (argc < 2) {
printf("Usage: ./distance <FILE>\nwhere FILE contains word projections in the BINARY FORMAT\n");
printf("Usage: ./distance <FILE>\nwhere FILE contains phrase projections in the BINARY FORMAT\n");
return 0;
}
strcpy(file_name, argv[1]);
Expand All @@ -43,15 +44,15 @@ int main(int argc, char **argv) {
}
fscanf(f, "%lld", &words);
fscanf(f, "%lld", &size);
vocab = (char *)malloc((long long)words * max_w * sizeof(char));
files = (char *)malloc((long long)words * max_w * sizeof(char));
for (a = 0; a < N; a++) bestw[a] = (char *)malloc(max_size * sizeof(char));
M = (float *)malloc((long long)words * (long long)size * sizeof(float));
if (M == NULL) {
printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
return -1;
}
for (b = 0; b < words; b++) {
fscanf(f, "%s%c", &vocab[b * max_w], &ch);
fscanf(f, "%s%c", &files[b * max_w], &ch);
for (a = 0; a < size; a++) fread(&M[a + b * size], sizeof(float), 1, f);
len = 0;
for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
Expand All @@ -62,7 +63,7 @@ int main(int argc, char **argv) {
while (1) {
for (a = 0; a < N; a++) bestd[a] = 0;
for (a = 0; a < N; a++) bestw[a][0] = 0;
printf("Enter word or sentence (EXIT to break): ");
printf("Enter file name (EXIT to break): ");
a = 0;
while (1) {
st1[a] = fgetc(stdin);
Expand Down Expand Up @@ -90,7 +91,7 @@ int main(int argc, char **argv) {
}
cn++;
for (a = 0; a < cn; a++) {
for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
for (b = 0; b < words; b++) if (!strcmp(&files[b * max_w], st[a])) break;
if (b == words) b = -1;
bi[a] = b;
printf("\nWord: %s Position in vocabulary: %lld\n", st[a], bi[a]);
Expand Down Expand Up @@ -125,7 +126,7 @@ int main(int argc, char **argv) {
strcpy(bestw[d], bestw[d - 1]);
}
bestd[a] = dist;
strcpy(bestw[a], &vocab[c * max_w]);
strcpy(bestw[a], &files[c * max_w]);
break;
}
}
Expand Down

0 comments on commit 3657330

Please sign in to comment.