blob: f75f0c7f631318163e93b5ab749465cce55fda8a [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[]) {
8 if (argc < 3) {
9 fprintf(stderr,
10 "Usage: %s <dbpath> <word1> [word2] ... [wordN]\n"
11 "Gets the collocators of the given words from a collocatordb and "
12 "prints them as json.\n"
13 "Example: %s ../tests/data/wpd19_10000 geht auch\n",
14 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 Kupietz62837a32024-11-22 19:03:07 +010043 fprintf(stderr, "printing collocators of \"%s\" as json:\n", argv[i]);
44 printf("%s%s\n", 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}