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[]) { |
Marc Kupietz | b9912e3 | 2024-11-23 10:11:06 +0100 | [diff] [blame^] | 8 | if (argc < 3 || (argv > 0 && strcmp(argv[1], "-h") == 0)) { |
Marc Kupietz | 62837a3 | 2024-11-22 19:03:07 +0100 | [diff] [blame] | 9 | fprintf(stderr, |
Marc Kupietz | b9912e3 | 2024-11-23 10:11:06 +0100 | [diff] [blame^] | 10 | "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 Kupietz | 62837a3 | 2024-11-22 19:03:07 +0100 | [diff] [blame] | 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 | b9912e3 | 2024-11-23 10:11:06 +0100 | [diff] [blame^] | 43 | 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 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 | } |