blob: c63ebd82785a02bfaa9fbee5423ed678331a426d [file] [log] [blame]
Marc Kupietzf08631e2024-11-21 18:05:07 +01001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <math.h>
5#include "../src/collocatordb.h"
6
Marc Kupietz62837a32024-11-22 19:03:07 +01007int main(int argc, char *argv[]) {
Marc Kupietzb9912e32024-11-23 10:11:06 +01008 if (argc < 3 || (argv > 0 && strcmp(argv[1], "-h") == 0)) {
Marc Kupietz62837a32024-11-22 19:03:07 +01009 fprintf(stderr,
Marc Kupietzb9912e32024-11-23 10:11:06 +010010 "Usage: %s <dbpath> <word1> [word2] ... [wordN]\n\n"
11 "Gets the collocates of the given words from a collocatordb and "
12 "prints them as json.\n\n"
13 "EXAMPLE\n../tests/data/wpd19_10000 geht auch\n",
Marc Kupietz62837a32024-11-22 19:03:07 +010014 argv[0], argv[0]);
15 return 1;
16 }
Marc Kupietzf08631e2024-11-21 18:05:07 +010017
Marc Kupietz62837a32024-11-22 19:03:07 +010018 char *dbpath = argv[1];
Marc Kupietzf08631e2024-11-21 18:05:07 +010019 char *ext = strrchr(dbpath, '.');
20 if (ext && strcmp(ext, ".rocksdb") == 0) {
21 *ext = '\0';
22 }
23
24 COLLOCATORDB *cdb;
25
26 fprintf(stderr, "opening collocatordb for reading: %s ...\n", dbpath);
27 if (!(cdb = open_collocatordb(dbpath))) {
28 fprintf(stderr, "Error opening %s exiting.\n", dbpath);
29 return 1;
30 }
31 fprintf(stderr, "Successfully opened %s.\n", dbpath);
32
33 int i;
Marc Kupietz62837a32024-11-22 19:03:07 +010034
35 printf("[\n");
Marc Kupietzf08631e2024-11-21 18:05:07 +010036 for (i = 2; i < argc; i++) {
37 uint32_t word_id = get_word_id(cdb, argv[i]);
38 if (word_id == 0) {
39 fprintf(stderr, "Word \"%s\" not found in the database.\n", argv[i]);
40 continue;
41 }
42
Marc Kupietzb9912e32024-11-23 10:11:06 +010043 fprintf(stderr, "printing collocates of \"%s\" as json:\n", argv[i]);
44 printf("%s%s", get_collocators_as_json(cdb, word_id), i < argc - 1 ? "," : "");
Marc Kupietzf08631e2024-11-21 18:05:07 +010045 }
Marc Kupietz62837a32024-11-22 19:03:07 +010046 printf("]\n");
Marc Kupietzf08631e2024-11-21 18:05:07 +010047 return 0;
48}