Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 1 | #ifdef __cplusplus |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 2 | #include <typeinfo> |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 3 | #include "rocksdb/db.h" |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 4 | #endif |
| 5 | #include <stdint.h> |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 6 | |
| 7 | #define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100) |
| 8 | #define encodeCollocation(w1, w2, dist) (((uint64_t)dist << 56) | ((uint64_t)w2 << 24) | w1) |
| 9 | #define W1(key) (uint64_t)(key & 0xffffff) |
| 10 | #define W2(key) (uint64_t)((key >> 24) & 0xffffff) |
| 11 | #define DIST(key) (int8_t)((uint64_t)((key >> 56) & 0xff)) |
| 12 | |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 13 | #ifdef __cplusplus |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 14 | namespace rocksdb { |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 15 | class Collocator { |
| 16 | public: |
| 17 | uint64_t w2; |
Marc Kupietz | cc6c459 | 2019-01-23 10:11:23 +0100 | [diff] [blame] | 18 | uint64_t f2; |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 19 | uint64_t raw; |
| 20 | double pmi; |
| 21 | double npmi; |
| 22 | double llr; |
| 23 | double lfmd; |
Marc Kupietz | 4188045 | 2019-01-22 15:29:06 +0100 | [diff] [blame] | 24 | double md; |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 25 | double left_lfmd; |
| 26 | double right_lfmd; |
| 27 | double left_npmi; |
| 28 | double right_npmi; |
Marc Kupietz | 4188045 | 2019-01-22 15:29:06 +0100 | [diff] [blame] | 29 | double dice; |
| 30 | double logdice; |
Marc Kupietz | 3203e4c | 2019-02-04 12:42:45 +0100 | [diff] [blame] | 31 | double ldaf; |
Marc Kupietz | e9f5893 | 2019-01-24 15:12:59 +0100 | [diff] [blame] | 32 | int window; |
| 33 | int af_window; |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 34 | }; |
Marc Kupietz | 0421d09 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 35 | namespace rocksdb { |
| 36 | class Collocator { |
| 37 | public: |
| 38 | uint32_t w2; |
| 39 | uint64_t f2; |
| 40 | uint64_t raw; |
| 41 | double pmi; |
| 42 | double npmi; |
| 43 | double llr; |
| 44 | double lfmd; |
| 45 | double md; |
| 46 | uint64_t left_raw; |
| 47 | uint64_t right_raw; |
| 48 | double left_pmi; |
| 49 | double right_pmi; |
| 50 | double dice; |
| 51 | double logdice; |
| 52 | double ldaf; |
| 53 | int window; |
| 54 | int af_window; |
| 55 | }; |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 56 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 57 | class CollocatorIterator : public Iterator { |
| 58 | public: |
| 59 | CollocatorIterator(const Iterator& it); |
| 60 | void SeekToFirst(); |
| 61 | void SeekToLast(); |
| 62 | void Seek(const rocksdb::Slice&); |
| 63 | void Prev(); |
| 64 | bool isValid(); |
| 65 | uint64_t intValue(); |
| 66 | uint64_t intKey(); |
| 67 | }; |
| 68 | |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 69 | extern "C" { |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 70 | class CollocatorDB { |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 71 | public: |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 72 | std::string getWord(uint32_t w1); |
| 73 | std::vector<Collocator> get_collocators(uint32_t w1); |
Marc Kupietz | bd96619 | 2018-10-13 14:14:37 +0200 | [diff] [blame] | 74 | std::vector<Collocator> get_collocators(uint32_t w1, uint32_t max_w2); |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 75 | void dumpSparseLlr(uint32_t w1, uint32_t min_cooccur); |
| 76 | CollocatorDB(const char *db_name, const bool read_only); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 77 | void inc(const uint32_t w1, const uint32_t w2, const uint8_t dist); |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 78 | void dump(const uint32_t w1, const uint32_t w2, const uint8_t dist); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 79 | CollocatorIterator* SeekIterator(uint64_t w1, uint64_t w2, int8_t dist); |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 83 | } |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 84 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 85 | typedef rocksdb::CollocatorDB COLLOCATORDB; |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 86 | |
| 87 | #else |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 88 | typedef struct COLLOCATORDB COLLOCATORDB; |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame^] | 89 | typedef struct { |
| 90 | uint32_t w2; |
| 91 | uint64_t f2; |
| 92 | uint64_t raw; |
| 93 | double pmi; |
| 94 | double npmi; |
| 95 | double llr; |
| 96 | double lfmd; |
| 97 | double md; |
| 98 | uint64_t left_raw; |
| 99 | uint64_t right_raw; |
| 100 | double left_pmi; |
| 101 | double right_pmi; |
| 102 | double dice; |
| 103 | double logdice; |
| 104 | double ldaf; |
| 105 | int window; |
| 106 | int af_window; |
| 107 | } Collocator ; |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 108 | #endif |
| 109 | |
Marc Kupietz | 88d116b | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 110 | extern COLLOCATORDB *open_collocatordb(const char *s); |
| 111 | extern COLLOCATORDB *open_collocatordb_for_write(const char *s); |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 112 | extern void inc_collocator(COLLOCATORDB *db, uint64_t w1, uint64_t w2, int8_t dist); |
| 113 | extern void dump_collocators(COLLOCATORDB *db, uint32_t w1, uint32_t w2, int8_t dist); |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame^] | 114 | extern Collocator *get_collocators(COLLOCATORDB *db, uint32_t w1); |
| 115 | extern Collocator *get_collocation_scores(COLLOCATORDB *db, uint32_t w1, uint32_t w2); |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 116 | extern char *get_collocators_as_json(COLLOCATORDB *db, uint32_t w1); |
Marc Kupietz | 88d116b | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 117 | extern char *get_collocation_scores_as_json(COLLOCATORDB *db, uint32_t w1, uint32_t w2); |
Marc Kupietz | ca3a52e | 2018-06-05 14:16:23 +0200 | [diff] [blame] | 118 | extern char *get_word(COLLOCATORDB *db, uint32_t w1); |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame^] | 119 | extern void read_vocab(COLLOCATORDB *db, char *fname); |
| 120 | |