Add collocatordb_query example
Change-Id: Icf15c3bf3914cded65d2b5116f88e10bdd79e215
diff --git a/examples/collocatordb_query.c b/examples/collocatordb_query.c
new file mode 100644
index 0000000..2336f85
--- /dev/null
+++ b/examples/collocatordb_query.c
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include "../src/collocatordb.h"
+
+int main(int argc, char* argv[]) {
+ if (argc < 3) {
+ fprintf(stderr, "Usage: %s <dbpath> <word1> [word2] ... [wordN]\n", argv[0]);
+ return 1;
+ }
+
+
+
+ char *dbpath = argv[1];
+ char *ext = strrchr(dbpath, '.');
+ if (ext && strcmp(ext, ".rocksdb") == 0) {
+ *ext = '\0';
+ }
+
+ COLLOCATORDB *cdb;
+
+ fprintf(stderr, "opening collocatordb for reading: %s ...\n", dbpath);
+ if (!(cdb = open_collocatordb(dbpath))) {
+ fprintf(stderr, "Error opening %s exiting.\n", dbpath);
+ return 1;
+ }
+ fprintf(stderr, "Successfully opened %s.\n", dbpath);
+
+ int i;
+ for (i = 2; i < argc; i++) {
+ uint32_t word_id = get_word_id(cdb, argv[i]);
+ if (word_id == 0) {
+ fprintf(stderr, "Word \"%s\" not found in the database.\n", argv[i]);
+ continue;
+ }
+
+ printf("printing collocators of \"%s\" as json:\n", argv[i]);
+ printf("%s\n", get_collocators_as_json(cdb, word_id));
+ }
+
+ return 0;
+}
\ No newline at end of file