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