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