Marc Kupietz | f08631e | 2024-11-21 18:05:07 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <math.h> |
| 5 | #include "../src/collocatordb.h" |
| 6 | |
Marc Kupietz | 62837a3 | 2024-11-22 19:03:07 +0100 | [diff] [blame^] | 7 | int 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 Kupietz | f08631e | 2024-11-21 18:05:07 +0100 | [diff] [blame] | 17 | |
Marc Kupietz | 62837a3 | 2024-11-22 19:03:07 +0100 | [diff] [blame^] | 18 | char *dbpath = argv[1]; |
Marc Kupietz | f08631e | 2024-11-21 18:05:07 +0100 | [diff] [blame] | 19 | 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 Kupietz | 62837a3 | 2024-11-22 19:03:07 +0100 | [diff] [blame^] | 34 | |
| 35 | printf("[\n"); |
Marc Kupietz | f08631e | 2024-11-21 18:05:07 +0100 | [diff] [blame] | 36 | 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 Kupietz | 62837a3 | 2024-11-22 19:03:07 +0100 | [diff] [blame^] | 43 | 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 Kupietz | f08631e | 2024-11-21 18:05:07 +0100 | [diff] [blame] | 45 | } |
Marc Kupietz | 62837a3 | 2024-11-22 19:03:07 +0100 | [diff] [blame^] | 46 | printf("]\n"); |
Marc Kupietz | f08631e | 2024-11-21 18:05:07 +0100 | [diff] [blame] | 47 | return 0; |
| 48 | } |