Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 1 | #include <typeinfo> |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 2 | #define EXPORT __attribute__((visibility("visible"))) |
| 3 | #define IMPORT |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 4 | #include <assert.h> |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 5 | #include <inttypes.h> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 6 | #include <memory> |
| 7 | #include <iostream> |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 8 | #include <algorithm> |
| 9 | #include <vector> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 10 | #include <stdint.h> |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <sstream> // for ostringstream |
| 13 | #include <math.h> |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 14 | #include <rocksdb/cache.h> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 15 | #include "rocksdb/comparator.h" |
| 16 | #include "rocksdb/db.h" |
| 17 | #include "rocksdb/env.h" |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 18 | #include "rocksdb/table.h" |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 19 | #include <rocksdb/merge_operator.h> |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 20 | #include <rocksdb/slice_transform.h> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 21 | #include "rocksdb/utilities/db_ttl.h" |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 22 | #include "rocksdb/filter_policy.h" |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 23 | #include "merge_operators.h" |
| 24 | |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 25 | #define AVG_WINDOW_SIZE 7 |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 26 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 27 | #define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100) |
| 28 | #define encodeCollocation(w1, w2, dist) (((uint64_t)dist << 56) | ((uint64_t)w2 << 24) | w1) |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 29 | #define W1(key) (uint64_t)(key & 0xffffff) |
| 30 | #define W2(key) (uint64_t)((key >> 24) & 0xffffff) |
| 31 | #define DIST(key) (int8_t)((uint64_t)((key >> 56) & 0xff)) |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 32 | |
| 33 | typedef struct { |
| 34 | uint64_t freq; |
| 35 | char *word; |
| 36 | } vocab_entry; |
| 37 | |
| 38 | // typedef struct Collocator { |
| 39 | // uint64_t w2; |
| 40 | // uint64_t sum; |
| 41 | // }; |
| 42 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 43 | using namespace rocksdb; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 44 | using namespace std; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 45 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 46 | namespace rocksdb { |
Marc Kupietz | 4a5e08a | 2018-06-05 11:07:11 +0200 | [diff] [blame] | 47 | class Collocator { |
| 48 | public: |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 49 | uint64_t w2; |
Marc Kupietz | 51f9379 | 2018-01-25 08:51:01 +0100 | [diff] [blame] | 50 | uint64_t raw; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 51 | double pmi; |
| 52 | double npmi; |
| 53 | double llr; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 54 | double lfmd; |
| 55 | double fpmi; |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 56 | double left_lfmd; |
| 57 | double right_lfmd; |
| 58 | double left_npmi; |
| 59 | double right_npmi; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 62 | size_t num_merge_operator_calls; |
| 63 | void resetNumMergeOperatorCalls() { num_merge_operator_calls = 0; } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 64 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 65 | size_t num_partial_merge_calls; |
| 66 | void resetNumPartialMergeCalls() { num_partial_merge_calls = 0; } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 67 | |
| 68 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 69 | inline void EncodeFixed64(char* buf, uint64_t value) { |
| 70 | if (! IS_BIG_ENDIAN) { |
| 71 | memcpy(buf, &value, sizeof(value)); |
| 72 | } else { |
| 73 | buf[0] = value & 0xff; |
| 74 | buf[1] = (value >> 8) & 0xff; |
| 75 | buf[2] = (value >> 16) & 0xff; |
| 76 | buf[3] = (value >> 24) & 0xff; |
| 77 | buf[4] = (value >> 32) & 0xff; |
| 78 | buf[5] = (value >> 40) & 0xff; |
| 79 | buf[6] = (value >> 48) & 0xff; |
| 80 | buf[7] = (value >> 56) & 0xff; |
| 81 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 84 | inline uint32_t DecodeFixed32(const char* ptr) { |
| 85 | if (! IS_BIG_ENDIAN) { |
| 86 | // Load the raw bytes |
| 87 | uint32_t result; |
| 88 | memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load |
| 89 | return result; |
| 90 | } else { |
| 91 | return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0]))) |
| 92 | | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8) |
| 93 | | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16) |
| 94 | | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | inline uint64_t DecodeFixed64(const char* ptr) { |
| 99 | if (! IS_BIG_ENDIAN) { |
| 100 | // Load the raw bytes |
| 101 | uint64_t result; |
| 102 | memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load |
| 103 | return result; |
| 104 | } else { |
| 105 | uint64_t lo = DecodeFixed32(ptr); |
| 106 | uint64_t hi = DecodeFixed32(ptr + 4); |
| 107 | return (hi << 32) | lo; |
| 108 | } |
| 109 | } |
| 110 | |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 111 | static inline double ca_pmi(uint64_t f1, uint64_t f2, uint64_t f12, uint64_t total, double window_size) { |
| 112 | return log2( total * ((double) f12) / (window_size * ((double) f1) * ((double)f2) )); |
| 113 | } |
| 114 | |
Marc Kupietz | ce0b8b0 | 2018-06-05 11:06:39 +0200 | [diff] [blame] | 115 | // Bouma, Gerlof (2009): <a href="https://svn.spraakdata.gu.se/repos/gerlof/pub/www/Docs/npmi-pfd.pdf"> |
| 116 | // Normalized (pointwise) mutual information in collocation extraction</a>. In Proceedings of GSCL. |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 117 | static inline double ca_npmi(uint64_t f1, uint64_t f2, uint64_t f12, uint64_t total, double window_size) { |
Marc Kupietz | 8caf991 | 2018-06-05 10:51:18 +0200 | [diff] [blame] | 118 | if(f12 == 0) |
| 119 | return -1.0; |
| 120 | else |
| 121 | return log2( total * ((double) f12) / (window_size * ((double) f1) * ((double)f2) )) / (-log2(((double) f12 / window_size / total))); |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Thanopoulos, A., Fakotakis, N., Kokkinakis, G.: Comparative evaluation of collocation extraction metrics. |
| 125 | // In: International Conference on Language Resources and Evaluation (LREC-2002). (2002) 620–625 |
| 126 | // double md = log2(pow((double)max * window_size / total, 2) / (window_size * ((double)_vocab[w1].freq/total) * ((double)_vocab[last_w2].freq/total))); |
| 127 | static inline double ca_md(uint64_t f1, uint64_t f2, uint64_t f12, uint64_t total, double window_size) { |
| 128 | return log2((double)f12 * f12 / ((double) total * window_size * window_size * f1 * f2)); |
| 129 | } |
| 130 | |
| 131 | static inline double ca_lfmd(uint64_t f1, uint64_t f2, uint64_t f12, uint64_t total, double window_size) { |
Marc Kupietz | 8caf991 | 2018-06-05 10:51:18 +0200 | [diff] [blame] | 132 | if(f12 == 0) |
| 133 | return 0; |
| 134 | else |
| 135 | return log2((double)f12 * f12 / ((double) total * window_size * window_size * f1 * f2)) + log2((double) f12 / window_size / total); |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 138 | |
| 139 | class CountMergeOperator : public AssociativeMergeOperator { |
| 140 | public: |
| 141 | CountMergeOperator() { |
| 142 | mergeOperator_ = MergeOperators::CreateUInt64AddOperator(); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 145 | virtual bool Merge(const Slice& key, |
| 146 | const Slice* existing_value, |
| 147 | const Slice& value, |
| 148 | std::string* new_value, |
| 149 | Logger* logger) const override { |
| 150 | assert(new_value->empty()); |
| 151 | ++num_merge_operator_calls; |
| 152 | if (existing_value == nullptr) { |
| 153 | new_value->assign(value.data(), value.size()); |
| 154 | return true; |
| 155 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 156 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 157 | return mergeOperator_->PartialMerge( |
| 158 | key, |
| 159 | *existing_value, |
| 160 | value, |
| 161 | new_value, |
| 162 | logger); |
| 163 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 164 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 165 | virtual const char* Name() const override { |
| 166 | return "UInt64AddOperator"; |
| 167 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 168 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 169 | private: |
| 170 | std::shared_ptr<MergeOperator> mergeOperator_; |
| 171 | }; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 172 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 173 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 174 | class CollocatorIterator : public Iterator { |
| 175 | private: |
| 176 | char prefixc[sizeof(uint64_t)]; |
| 177 | Iterator *base_iterator_; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 178 | |
| 179 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 180 | public: |
| 181 | CollocatorIterator(Iterator* base_iterator) |
| 182 | : base_iterator_(base_iterator) |
| 183 | {} |
| 184 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 185 | void setPrefix(char *prefix) { |
| 186 | memcpy(prefixc, prefix, sizeof(uint64_t)); |
| 187 | } |
| 188 | |
| 189 | virtual void SeekToFirst() { base_iterator_->SeekToFirst(); } |
| 190 | virtual void SeekToLast() { base_iterator_->SeekToLast(); } |
| 191 | virtual void Seek(const rocksdb::Slice& s) { base_iterator_->Seek(s); } |
| 192 | virtual void Prev() { base_iterator_->Prev(); } |
| 193 | virtual void Next() { base_iterator_->Next(); } |
| 194 | virtual Slice key() const; |
| 195 | virtual Slice value() const; |
| 196 | virtual Status status() const; |
| 197 | virtual bool Valid() const; |
| 198 | bool isValid(); |
| 199 | uint64_t intValue(); |
| 200 | uint64_t intKey(); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 201 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 202 | }; |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 203 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 204 | // rocksdb::CollocatorIterator::CollocatorIterator(Iterator* base_iterator) {} |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 205 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 206 | bool rocksdb::CollocatorIterator::Valid() const { |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 207 | return base_iterator_->Valid() && key().starts_with(std::string(prefixc,3)); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 208 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 209 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 210 | bool rocksdb::CollocatorIterator::isValid() { |
| 211 | return base_iterator_->Valid() && key().starts_with(std::string(prefixc,3)); |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 212 | // return key().starts_with(std::string(prefixc,3)); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 213 | } |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 214 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 215 | uint64_t rocksdb::CollocatorIterator::intKey() { |
| 216 | return DecodeFixed64(base_iterator_->key().data()); |
| 217 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 218 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 219 | uint64_t rocksdb::CollocatorIterator::intValue() { |
| 220 | return DecodeFixed64(base_iterator_->value().data()); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 223 | class VocabEntry { |
| 224 | public: |
| 225 | string word; |
| 226 | uint64_t freq; |
| 227 | }; |
| 228 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 229 | class CollocatorDB { |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 230 | private: |
| 231 | WriteOptions merge_option_; // for merge |
| 232 | char _one[sizeof(uint64_t)]; |
| 233 | Slice _one_slice; |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 234 | vector<VocabEntry> _vocab; |
| 235 | uint64_t total; |
| 236 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 237 | protected: |
| 238 | std::shared_ptr<DB> db_; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 239 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 240 | WriteOptions put_option_; |
| 241 | ReadOptions get_option_; |
| 242 | WriteOptions delete_option_; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 243 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 244 | uint64_t default_; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 245 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 246 | std::shared_ptr<DB> OpenDb(const char *dbname); |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 247 | std::shared_ptr<DB> OpenDbForRead(const char *dbname); |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 248 | void read_vocab(string fname); |
| 249 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 250 | public: |
Marc Kupietz | 4a5e08a | 2018-06-05 11:07:11 +0200 | [diff] [blame] | 251 | string getWord(uint32_t w1); |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 252 | CollocatorDB(const char *db_name, bool read_only); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 253 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 254 | // public interface of CollocatorDB. |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 255 | // All four functions return false |
| 256 | // if the underlying level db operation failed. |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 257 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 258 | // mapped to a levedb Put |
| 259 | bool set(const std::string& key, uint64_t value) { |
| 260 | // just treat the internal rep of int64 as the string |
| 261 | char buf[sizeof(value)]; |
| 262 | EncodeFixed64(buf, value); |
| 263 | Slice slice(buf, sizeof(value)); |
| 264 | auto s = db_->Put(put_option_, key, slice); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 265 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 266 | if (s.ok()) { |
| 267 | return true; |
| 268 | } else { |
| 269 | std::cerr << s.ToString() << std::endl; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 270 | return false; |
| 271 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 272 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 273 | |
| 274 | DB *getDb() { |
| 275 | return db_.get(); |
| 276 | } |
| 277 | |
| 278 | // mapped to a rocksdb Delete |
| 279 | bool remove(const std::string& key) { |
| 280 | auto s = db_->Delete(delete_option_, key); |
| 281 | |
| 282 | if (s.ok()) { |
| 283 | return true; |
| 284 | } else { |
| 285 | std::cerr << s.ToString() << std::endl; |
| 286 | return false; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // mapped to a rocksdb Get |
| 291 | bool get(const std::string& key, uint64_t* value) { |
| 292 | std::string str; |
| 293 | auto s = db_->Get(get_option_, key, &str); |
| 294 | |
| 295 | if (s.IsNotFound()) { |
| 296 | // return default value if not found; |
| 297 | *value = default_; |
| 298 | return true; |
| 299 | } else if (s.ok()) { |
| 300 | // deserialization |
| 301 | if (str.size() != sizeof(uint64_t)) { |
| 302 | std::cerr << "value corruption\n"; |
| 303 | return false; |
| 304 | } |
| 305 | *value = DecodeFixed64(&str[0]); |
| 306 | return true; |
| 307 | } else { |
| 308 | std::cerr << s.ToString() << std::endl; |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | uint64_t get(const uint32_t w1, const uint32_t w2, const int8_t dist) { |
| 315 | char encoded_key[sizeof(uint64_t)]; |
| 316 | EncodeFixed64(encoded_key, encodeCollocation(w1,w2,dist)); |
| 317 | uint64_t value = default_; |
| 318 | get(std::string(encoded_key, 8), &value); |
| 319 | return value; |
| 320 | } |
| 321 | |
| 322 | virtual void inc(const std::string& key) { |
| 323 | db_->Merge(merge_option_, key, _one_slice); |
| 324 | } |
| 325 | |
| 326 | void inc(const uint64_t key) { |
| 327 | char encoded_key[sizeof(uint64_t)]; |
| 328 | EncodeFixed64(encoded_key, key); |
| 329 | db_->Merge(merge_option_, std::string(encoded_key, 8), _one_slice); |
| 330 | } |
| 331 | |
| 332 | virtual 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] | 333 | void dump(uint32_t w1, uint32_t w2, int8_t dist); |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 334 | vector<Collocator> get_collocators(uint32_t w1); |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 335 | void dumpSparseLlr(uint32_t w1, uint32_t min_cooccur); |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 336 | vector<Collocator> get_collocators_avg(uint32_t w1); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 337 | string collocators2json(vector<Collocator> collocators); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 338 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 339 | // mapped to a rocksdb Merge operation |
| 340 | virtual bool add(const std::string& key, uint64_t value) { |
| 341 | char encoded[sizeof(uint64_t)]; |
| 342 | EncodeFixed64(encoded, value); |
| 343 | Slice slice(encoded, sizeof(uint64_t)); |
| 344 | auto s = db_->Merge(merge_option_, key, slice); |
| 345 | |
| 346 | if (s.ok()) { |
| 347 | return true; |
| 348 | } else { |
| 349 | std::cerr << s.ToString() << std::endl; |
| 350 | return false; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | CollocatorIterator* SeekIterator(uint64_t w1, uint64_t w2, int8_t dist); |
| 355 | }; |
| 356 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 357 | rocksdb::CollocatorDB::CollocatorDB(const char *db_name, bool read_only = false) { |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 358 | // merge_option_.sync = true; |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 359 | if(read_only) |
| 360 | db_ = OpenDbForRead(db_name); |
| 361 | else |
| 362 | db_ = OpenDb(db_name); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 363 | assert(db_); |
| 364 | uint64_t one = 1; |
| 365 | EncodeFixed64(_one, one); |
| 366 | _one_slice = Slice(_one, sizeof(uint64_t)); |
| 367 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 368 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 369 | void rocksdb::CollocatorDB::inc(const uint32_t w1, const uint32_t w2, const uint8_t dist) { |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 370 | inc(encodeCollocation(w1, w2, dist)); |
| 371 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 372 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 373 | void rocksdb::CollocatorDB::read_vocab(string fname) { |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 374 | char strbuf[2048]; |
| 375 | uint64_t freq; |
| 376 | FILE *fin = fopen(fname.c_str(), "rb"); |
| 377 | if (fin == NULL) { |
| 378 | cout << "Vocabulary file " << fname <<" not found\n"; |
| 379 | exit(1); |
| 380 | } |
| 381 | uint64_t i = 0; |
| 382 | while(!feof(fin)) { |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 383 | fscanf(fin, "%s %lu", strbuf, &freq); |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 384 | _vocab.push_back({strbuf, freq}); |
| 385 | total += freq; |
| 386 | i++; |
| 387 | } |
| 388 | fclose(fin); |
| 389 | } |
| 390 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 391 | std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDbForRead(const char *name) { |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 392 | DB* db; |
| 393 | Options options; |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 394 | options.env->SetBackgroundThreads(4); |
| 395 | options.create_if_missing = true; |
| 396 | options.merge_operator = std::make_shared<CountMergeOperator>(); |
| 397 | options.max_successive_merges = 0; |
| 398 | // options.prefix_extractor.reset(NewFixedPrefixTransform(8)); |
| 399 | options.IncreaseParallelism(); |
| 400 | options.OptimizeLevelStyleCompaction(); |
| 401 | options.prefix_extractor.reset(NewFixedPrefixTransform(3)); |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 402 | ostringstream dbname, vocabname; |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 403 | dbname << name << ".rocksdb"; |
| 404 | auto s = DB::OpenForReadOnly(options, dbname.str(), &db); |
| 405 | if (!s.ok()) { |
| 406 | std::cerr << s.ToString() << std::endl; |
| 407 | assert(false); |
| 408 | } |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 409 | vocabname << name << ".vocab"; |
| 410 | read_vocab(vocabname.str()); |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 411 | return std::shared_ptr<DB>(db); |
| 412 | } |
| 413 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 414 | std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDb(const char *dbname) { |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 415 | DB* db; |
| 416 | Options options; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 417 | |
| 418 | |
| 419 | options.env->SetBackgroundThreads(4); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 420 | options.create_if_missing = true; |
| 421 | options.merge_operator = std::make_shared<CountMergeOperator>(); |
| 422 | options.max_successive_merges = 0; |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 423 | // options.prefix_extractor.reset(NewFixedPrefixTransform(8)); |
| 424 | options.IncreaseParallelism(); |
| 425 | options.OptimizeLevelStyleCompaction(); |
| 426 | // options.max_write_buffer_number = 48; |
| 427 | // options.max_background_jobs = 48; |
| 428 | // options.allow_concurrent_memtable_write=true; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 429 | // options.memtable_factory.reset(rocksdb::NewHashLinkListRepFactory(200000)); |
| 430 | // options.enable_write_thread_adaptive_yield = 1; |
| 431 | // options.allow_concurrent_memtable_write = 1; |
| 432 | // options.memtable_factory.reset(new rocksdb::SkipListFactory); |
| 433 | // options.write_buffer_size = 1 << 22; |
| 434 | // options.allow_mmap_reads = true; |
| 435 | // options.allow_mmap_writes = true; |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 436 | // options.max_background_compactions = 40; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 437 | // BlockBasedTableOptions table_options; |
| 438 | // table_options.filter_policy.reset(NewBloomFilterPolicy(24, false)); |
| 439 | // options.bloom_locality = 1; |
| 440 | // std::shared_ptr<Cache> cache = NewLRUCache(512 * 1024 * 1024); |
| 441 | // table_options.block_cache = cache; |
| 442 | // options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 443 | Status s; |
| 444 | // DestroyDB(dbname, Options()); |
| 445 | s = DB::Open(options, dbname, &db); |
| 446 | if (!s.ok()) { |
| 447 | std::cerr << s.ToString() << std::endl; |
| 448 | assert(false); |
| 449 | } |
| 450 | return std::shared_ptr<DB>(db); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 451 | } |
| 452 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 453 | CollocatorIterator* rocksdb::CollocatorDB::SeekIterator(uint64_t w1, uint64_t w2, int8_t dist) { |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 454 | ReadOptions options; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 455 | options.prefix_same_as_start = true; |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 456 | char prefixc[sizeof(uint64_t)]; |
| 457 | EncodeFixed64(prefixc, encodeCollocation(w1, w2, dist)); |
| 458 | Iterator *it = db_->NewIterator(options); |
| 459 | CollocatorIterator *cit = new CollocatorIterator(it); |
| 460 | cit->Seek(std::string(prefixc,3));// it->Valid() && it->key().starts_with(std::string(prefixc,3)); it->Next()) { |
| 461 | cit->setPrefix(prefixc); |
| 462 | return cit; |
| 463 | } |
| 464 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 465 | void rocksdb::CollocatorDB::dump(uint32_t w1, uint32_t w2, int8_t dist) { |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 466 | auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, w2, dist)); |
| 467 | for (; it->isValid(); it->Next()) { |
| 468 | uint64_t value = it->intValue(); |
| 469 | uint64_t key = it->intKey(); |
| 470 | std::cout << "w1:" << W1(key) << ", w2:" << W2(key) << ", dist:" << (int32_t) DIST(key) << " - count:" << value << std::endl; |
| 471 | } |
| 472 | std::cout << "ready dumping\n"; |
| 473 | } |
| 474 | |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 475 | double calculateLLR(uint64_t f_X_, uint64_t uintN, uint64_t f_X_Y_, uint64_t f_Y_) { |
| 476 | double f_e_, f_o_; |
| 477 | double A=0.0, B=0.0, C=0.0, D=0.0, N=0.0; |
| 478 | double LLR=0.0, statVal=0.0, minusDiffCoeff=0.0; |
| 479 | double BlogB=0.0, ClogC=0.0; |
| 480 | |
| 481 | N = (double)uintN; |
| 482 | A = (double)f_X_Y_; |
| 483 | B = (double)f_X_ -A; |
| 484 | C = (double)f_Y_ -A; |
| 485 | D = (double)N -A-B-C;; |
| 486 | |
| 487 | if (B > 0.) BlogB = B*log(B); |
| 488 | if (C > 0.) ClogC = C*log(C); |
| 489 | |
| 490 | if ((A>0.) && (D>0.) && (N>0.)) { |
| 491 | f_e_ = (double)f_X_ /(double)N; |
| 492 | f_o_ = (double)f_X_Y_/(double)f_Y_; |
| 493 | |
| 494 | minusDiffCoeff = |
| 495 | ( f_X_==0 ? (double)((-1)*f_X_Y_) : |
| 496 | ( f_X_Y_==0 ? (double)((+1)*f_X_) : |
| 497 | (f_e_-f_o_)/(f_e_+f_o_) |
| 498 | ) |
| 499 | ); |
| 500 | |
| 501 | /* log likelihood ratio */ |
| 502 | LLR = 2*( A*log(A) |
| 503 | +BlogB |
| 504 | +ClogC |
| 505 | +D*log(D) |
| 506 | -(A+B)*log(A+B) |
| 507 | -(A+C)*log(A+C) |
| 508 | -(B+D)*log(B+D) |
| 509 | -(C+D)*log(C+D) |
| 510 | +N*log(N) |
| 511 | ); |
| 512 | } |
| 513 | return(minusDiffCoeff > 0 ? 0 : (statVal=LLR)); |
| 514 | } |
| 515 | |
| 516 | |
| 517 | bool sortByNpmi(const Collocator &lhs, const Collocator &rhs) { return lhs.npmi > rhs.npmi; } |
| 518 | bool sortByLfmd(const Collocator &lhs, const Collocator &rhs) { return lhs.lfmd > rhs.lfmd; } |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 519 | bool sortByLlr(const Collocator &lhs, const Collocator &rhs) { return lhs.llr > rhs.llr; } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 520 | |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 521 | std::vector<Collocator> rocksdb::CollocatorDB::get_collocators_avg(uint32_t w1) { |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 522 | std::vector<Collocator> collocators; |
| 523 | uint64_t w2, last_w2 = 0xffffffffffffffff; |
| 524 | uint64_t sum = 0, total_w1 = 0; |
| 525 | for ( auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) { |
| 526 | uint64_t value = it->intValue(), |
| 527 | key = it->intKey(); |
| 528 | w2 = W2(key); |
| 529 | total_w1 += value; |
| 530 | if(last_w2 == 0xffffffffffffffff) last_w2 = w2; |
| 531 | if (w2 != last_w2) { |
| 532 | double pmi = log2( total * ((double) sum) / |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 533 | (AVG_WINDOW_SIZE * ((double)_vocab[w1].freq) * ((double)_vocab[last_w2].freq) )); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 534 | // Thanopoulos, A., Fakotakis, N., Kokkinakis, G.: Comparative evaluation of collocation extraction metrics. In: International Conference on Language Resources and Evaluation (LREC-2002). (2002) 620–625 |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 535 | // double md = log2(pow((double)sum * AVG_WINDOW_SIZE / total, 2) / (AVG_WINDOW_SIZE * ((double)_vocab[w1].freq/total) * ((double)_vocab[last_w2].freq/total))); |
| 536 | double md = log2((double)sum * sum / ((double) total * AVG_WINDOW_SIZE * AVG_WINDOW_SIZE * _vocab[w1].freq * _vocab[last_w2].freq)); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 537 | collocators.push_back ( {last_w2, sum, pmi, pmi / (-log2(((double) sum / AVG_WINDOW_SIZE / total))), /* normalize to [-1,1] */ |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 538 | calculateLLR(_vocab[w1].freq, total, sum, _vocab[last_w2].freq), md, md + log2((double)sum / AVG_WINDOW_SIZE / total), pmi*sum/total/AVG_WINDOW_SIZE} ); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 539 | last_w2 = w2; |
| 540 | sum = value; |
| 541 | } else { |
| 542 | sum += value; |
| 543 | } |
| 544 | } |
| 545 | |
Marc Kupietz | ea616ca | 2018-06-05 11:12:43 +0200 | [diff] [blame^] | 546 | sort(collocators.begin(), collocators.end(), sortByNpmi); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 547 | |
| 548 | int i=0; |
| 549 | for (Collocator c : collocators) { |
| 550 | if(i++>10) break; |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 551 | std::cout << "dont call me w1:" << _vocab[w1].word << ", w2:" << _vocab[c.w2].word |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 552 | << "\t f(w1):" << _vocab[w1].freq |
| 553 | << "\t f(w2):" << _vocab[c.w2].freq |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 554 | << "\t f(w1, x):" << total_w1 |
Marc Kupietz | 51f9379 | 2018-01-25 08:51:01 +0100 | [diff] [blame] | 555 | << "\t f(w1, w2):" << c.raw |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 556 | << "\t pmi:" << c.pmi |
| 557 | << "\t npmi:" << c.npmi |
| 558 | << "\t llr:" << c.llr |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 559 | << "\t lfmd:" << c.lfmd |
| 560 | << "\t fpmi:" << c.fpmi |
| 561 | << "\t total:" << total |
| 562 | << std::endl; |
| 563 | } |
| 564 | return collocators; |
| 565 | } |
| 566 | |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 567 | std::vector<Collocator> rocksdb::CollocatorDB::get_collocators(uint32_t w1) { |
| 568 | std::vector<Collocator> collocators; |
| 569 | uint64_t w2, last_w2 = 0xffffffffffffffff; |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 570 | uint64_t maxv = 0, left = 0, right = 0, total_w1 = 0; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 571 | const double window_size = 1; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 572 | for ( auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) { |
| 573 | uint64_t value = it->intValue(), |
| 574 | key = it->intKey(); |
| 575 | w2 = W2(key); |
| 576 | total_w1 += value; |
| 577 | if(last_w2 == 0xffffffffffffffff) last_w2 = w2; |
| 578 | if (w2 != last_w2) { |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 579 | double pmi = ca_pmi(_vocab[w1].freq, _vocab[last_w2].freq, maxv, total, window_size); |
| 580 | double lfmd = ca_lfmd(_vocab[w1].freq, _vocab[last_w2].freq, maxv, total, window_size); |
| 581 | double left_lfmd = ca_lfmd(_vocab[w1].freq, _vocab[last_w2].freq, left, total, 1); |
| 582 | double right_lfmd = ca_lfmd(_vocab[w1].freq, _vocab[last_w2].freq, right, total, 1); |
| 583 | double left_npmi = ca_npmi(_vocab[w1].freq, _vocab[last_w2].freq, left, total, 1); |
| 584 | double right_npmi = ca_npmi(_vocab[w1].freq, _vocab[last_w2].freq, right, total, 1); |
| 585 | collocators.push_back ( {last_w2, maxv, pmi, pmi / (-log2(((double) maxv / window_size / total))), /* normalize to [-1,1] */ |
| 586 | calculateLLR(_vocab[w1].freq, total, maxv, _vocab[last_w2].freq), lfmd, pmi*maxv/total/window_size, |
| 587 | left_lfmd, |
| 588 | right_lfmd, |
| 589 | left_npmi, |
| 590 | right_npmi} |
| 591 | ); |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 592 | last_w2 = w2; |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 593 | maxv = value; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 594 | } else { |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 595 | if(value > maxv) |
| 596 | maxv = value; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 597 | } |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 598 | if(DIST(key) == -1) |
| 599 | left = value; |
| 600 | else if(DIST(key) == 1) |
| 601 | right = value; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | sort(collocators.begin(), collocators.end(), sortByLfmd); |
| 605 | |
| 606 | int i=0; |
| 607 | for (Collocator c : collocators) { |
| 608 | if(i++>10) break; |
| 609 | std::cout << "w1:" << _vocab[w1].word << ", w2:" << _vocab[c.w2].word |
| 610 | << "\t f(w1):" << _vocab[w1].freq |
| 611 | << "\t f(w2):" << _vocab[c.w2].freq |
| 612 | << "\t f(w1, x):" << total_w1 |
Marc Kupietz | 51f9379 | 2018-01-25 08:51:01 +0100 | [diff] [blame] | 613 | << "\t f(w1, w2):" << c.raw |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 614 | << "\t pmi:" << c.pmi |
| 615 | << "\t npmi:" << c.npmi |
| 616 | << "\t llr:" << c.llr |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 617 | << "\t lfmd:" << c.lfmd |
| 618 | << "\t fpmi:" << c.fpmi |
| 619 | << "\t total:" << total |
| 620 | << std::endl; |
| 621 | } |
| 622 | return collocators; |
| 623 | } |
| 624 | |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 625 | void rocksdb::CollocatorDB::dumpSparseLlr(uint32_t w1, uint32_t min_cooccur) { |
| 626 | std::vector<Collocator> collocators; |
| 627 | std::stringstream stream; |
| 628 | uint64_t w2, last_w2 = 0xffffffffffffffff; |
| 629 | uint64_t maxv = 0, total_w1 = 0; |
| 630 | bool first = true; |
| 631 | for ( auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) { |
| 632 | uint64_t value = it->intValue(), |
| 633 | key = it->intKey(); |
| 634 | w2 = W2(key); |
| 635 | total_w1 += value; |
| 636 | if(last_w2 == 0xffffffffffffffff) last_w2 = w2; |
| 637 | if (w2 != last_w2) { |
| 638 | if(maxv >= min_cooccur) { |
| 639 | double llr = calculateLLR(_vocab[w1].freq, total, maxv, _vocab[last_w2].freq); |
| 640 | if(first) |
| 641 | first = false; |
| 642 | else |
| 643 | stream << " "; |
| 644 | stream << w2 << " " << llr; |
| 645 | } |
| 646 | last_w2 = w2; |
| 647 | maxv = value; |
| 648 | } else { |
| 649 | if(value > maxv) |
| 650 | maxv = value; |
| 651 | } |
| 652 | } |
| 653 | if(first) |
| 654 | stream << "1 0.0"; |
| 655 | stream << "\n"; |
| 656 | std::cout << stream.str(); |
| 657 | } |
| 658 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 659 | rocksdb::Slice rocksdb::CollocatorIterator::key() const { return base_iterator_->key(); } |
| 660 | rocksdb::Slice rocksdb::CollocatorIterator::value() const { return base_iterator_->value(); } |
| 661 | rocksdb::Status rocksdb::CollocatorIterator::status() const { return base_iterator_->status(); } |
| 662 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 663 | }; |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 664 | |
Marc Kupietz | 4a5e08a | 2018-06-05 11:07:11 +0200 | [diff] [blame] | 665 | string rocksdb::CollocatorDB::getWord(uint32_t w1) { |
| 666 | return _vocab[w1].word; |
| 667 | } |
| 668 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 669 | string rocksdb::CollocatorDB::collocators2json(vector<Collocator> collocators) { |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 670 | ostringstream s; |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 671 | int i = 0; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 672 | s << "["; |
| 673 | bool first = true; |
| 674 | for (Collocator c : collocators) { |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 675 | if (i++ > 200) |
| 676 | break; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 677 | if(!first) |
| 678 | s << ",\n"; |
| 679 | else |
| 680 | first = false; |
| 681 | s << "{" |
| 682 | "\"word\":\"" << string(_vocab[c.w2].word) << "\"," << |
| 683 | "\"rank\":" << c.w2 << "," << |
Marc Kupietz | 51f9379 | 2018-01-25 08:51:01 +0100 | [diff] [blame] | 684 | "\"f\":" << c.raw << "," << |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 685 | "\"npmi\":" << c.npmi << "," << |
| 686 | "\"llr\":" << c.llr << "," << |
| 687 | "\"lfmd\":" << c.lfmd << "," << |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 688 | "\"fpmi\":" << c.fpmi << "," << |
| 689 | "\"llfmd\":" << c.left_lfmd << "," << |
| 690 | "\"rlfmd\":" << c.right_lfmd << "," << |
| 691 | "\"lnpmi\":" << c.left_npmi << "," << |
| 692 | "\"rnpmi\":" << c.right_npmi << |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 693 | "}"; |
| 694 | } |
| 695 | s << "]\n"; |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 696 | // cout << s.str(); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 697 | return s.str(); |
| 698 | } |
| 699 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 700 | typedef rocksdb::CollocatorDB COLLOCATORS; |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 701 | |
| 702 | extern "C" { |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 703 | COLLOCATORS *open_collocatordb_for_write(char *dbname) { |
| 704 | return new rocksdb::CollocatorDB(dbname, false); |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 707 | COLLOCATORS *open_collocatordb(char *dbname) { |
| 708 | return new rocksdb::CollocatorDB(dbname, true); |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 709 | } |
| 710 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 711 | void inc_collocator(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) { |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 712 | db->inc(w1, w2, dist); |
| 713 | } |
| 714 | |
| 715 | void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) { |
| 716 | db->dump(w1, w2, dist); |
| 717 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 718 | |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 719 | void get_collocators(COLLOCATORS *db, uint32_t w1) { |
| 720 | db->get_collocators(w1); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 721 | } |
| 722 | |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 723 | const char *get_collocators_as_json(COLLOCATORS *db, uint32_t w1) { |
| 724 | return strdup(db->collocators2json(db->get_collocators(w1)).c_str()); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 725 | } |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 726 | } |