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