Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 1 | #define EXPORT __attribute__((visibility("visible"))) |
| 2 | #define IMPORT |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 3 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 4 | #include <assert.h> |
| 5 | #include <memory> |
| 6 | #include <iostream> |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <vector> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 9 | #include <stdint.h> |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <sstream> // for ostringstream |
| 12 | #include <math.h> |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 13 | #include <rocksdb/cache.h> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 14 | #include "rocksdb/db.h" |
| 15 | #include "rocksdb/env.h" |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 16 | #include "rocksdb/table.h" |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 17 | #include <rocksdb/merge_operator.h> |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 18 | #include <rocksdb/slice_transform.h> |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 19 | #include "merge_operators.h" |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 20 | #include "export.h" |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 21 | |
Marc Kupietz | 75af60f | 2019-01-22 22:34:29 +0100 | [diff] [blame] | 22 | #define WINDOW_SIZE 5 |
Marc Kupietz | 98cbcdc | 2019-01-21 17:11:27 +0100 | [diff] [blame] | 23 | #define FREQUENCY_THRESHOLD 5 |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 24 | #define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100) |
| 25 | #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] | 26 | #define W1(key) (uint64_t)(key & 0xffffff) |
| 27 | #define W2(key) (uint64_t)((key >> 24) & 0xffffff) |
| 28 | #define DIST(key) (int8_t)((uint64_t)((key >> 56) & 0xff)) |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 29 | |
| 30 | typedef struct { |
| 31 | uint64_t freq; |
| 32 | char *word; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 33 | } vocab_entry; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 34 | |
| 35 | // typedef struct Collocator { |
| 36 | // uint64_t w2; |
| 37 | // uint64_t sum; |
| 38 | // }; |
| 39 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 40 | using namespace rocksdb; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 41 | using namespace std; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 42 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 43 | namespace rocksdb { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 44 | class Collocator { |
| 45 | public: |
| 46 | uint32_t w2; |
| 47 | uint64_t f2; |
| 48 | uint64_t raw; |
| 49 | double pmi; |
| 50 | double npmi; |
| 51 | double llr; |
| 52 | double lfmd; |
| 53 | double md; |
| 54 | uint64_t left_raw; |
| 55 | uint64_t right_raw; |
| 56 | double left_pmi; |
| 57 | double right_pmi; |
| 58 | double dice; |
| 59 | double logdice; |
| 60 | double ldaf; |
| 61 | int window; |
| 62 | int af_window; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 65 | size_t num_merge_operator_calls; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 66 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 67 | void resetNumMergeOperatorCalls() { num_merge_operator_calls = 0; } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 68 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 69 | size_t num_partial_merge_calls; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 70 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 71 | void resetNumPartialMergeCalls() { num_partial_merge_calls = 0; } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 72 | |
| 73 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 74 | inline void EncodeFixed64(char *buf, uint64_t value) { |
| 75 | if (!IS_BIG_ENDIAN) { |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 76 | memcpy(buf, &value, sizeof(value)); |
| 77 | } else { |
| 78 | buf[0] = value & 0xff; |
| 79 | buf[1] = (value >> 8) & 0xff; |
| 80 | buf[2] = (value >> 16) & 0xff; |
| 81 | buf[3] = (value >> 24) & 0xff; |
| 82 | buf[4] = (value >> 32) & 0xff; |
| 83 | buf[5] = (value >> 40) & 0xff; |
| 84 | buf[6] = (value >> 48) & 0xff; |
| 85 | buf[7] = (value >> 56) & 0xff; |
| 86 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 89 | inline uint32_t DecodeFixed32(const char *ptr) { |
| 90 | if (!IS_BIG_ENDIAN) { |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 91 | // Load the raw bytes |
| 92 | uint32_t result; |
| 93 | memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load |
| 94 | return result; |
| 95 | } else { |
| 96 | return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0]))) |
| 97 | | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8) |
| 98 | | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16) |
| 99 | | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24)); |
| 100 | } |
| 101 | } |
| 102 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 103 | inline uint64_t DecodeFixed64(const char *ptr) { |
| 104 | if (!IS_BIG_ENDIAN) { |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 105 | // Load the raw bytes |
| 106 | uint64_t result; |
| 107 | memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load |
| 108 | return result; |
| 109 | } else { |
| 110 | uint64_t lo = DecodeFixed32(ptr); |
| 111 | uint64_t hi = DecodeFixed32(ptr + 4); |
| 112 | return (hi << 32) | lo; |
| 113 | } |
| 114 | } |
| 115 | |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 116 | 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] | 117 | double |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 118 | r1 = f1 * window_size, |
| 119 | c1 = f2, |
| 120 | e = r1 * c1 / total, |
| 121 | o = f12; |
| 122 | if (f12 < FREQUENCY_THRESHOLD) |
Marc Kupietz | f4a649a | 2021-02-26 09:18:01 +0100 | [diff] [blame] | 123 | return -1.0; |
| 124 | else |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 125 | return log2(o / e); |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Marc Kupietz | ce0b8b0 | 2018-06-05 11:06:39 +0200 | [diff] [blame] | 128 | // Bouma, Gerlof (2009): <a href="https://svn.spraakdata.gu.se/repos/gerlof/pub/www/Docs/npmi-pfd.pdf"> |
| 129 | // Normalized (pointwise) mutual information in collocation extraction</a>. In Proceedings of GSCL. |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 130 | 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] | 131 | double |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 132 | r1 = f1 * window_size, |
| 133 | c1 = f2, |
| 134 | e = r1 * c1 / total, |
| 135 | o = f12; |
| 136 | if (f12 < FREQUENCY_THRESHOLD) |
Marc Kupietz | 8caf991 | 2018-06-05 10:51:18 +0200 | [diff] [blame] | 137 | return -1.0; |
| 138 | else |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 139 | return log2(o / e) / (-log2(o / total / window_size)); |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Thanopoulos, A., Fakotakis, N., Kokkinakis, G.: Comparative evaluation of collocation extraction metrics. |
| 143 | // In: International Conference on Language Resources and Evaluation (LREC-2002). (2002) 620–625 |
| 144 | // double md = log2(pow((double)max * window_size / total, 2) / (window_size * ((double)_vocab[w1].freq/total) * ((double)_vocab[last_w2].freq/total))); |
| 145 | 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] | 146 | double |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 147 | r1 = f1 * window_size, |
| 148 | c1 = f2, |
| 149 | e = r1 * c1 / total, |
| 150 | o = f12; |
| 151 | return log2(o * o / e); |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | 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] | 155 | double |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 156 | r1 = f1 * window_size, |
| 157 | c1 = f2, |
| 158 | e = r1 * c1 / total, |
| 159 | o = f12; |
| 160 | if (f12 == 0) |
Marc Kupietz | 8caf991 | 2018-06-05 10:51:18 +0200 | [diff] [blame] | 161 | return 0; |
| 162 | else |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 163 | return log2(o * o * o / e); |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Marc Kupietz | bbd236e | 2019-01-21 16:50:19 +0100 | [diff] [blame] | 166 | // 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. |
| 167 | // Free PDF available from http://purl.org/stefan.evert/PUB/Evert2004phd.pdf |
| 168 | static inline double ca_ll(uint64_t w1, uint64_t w2, uint64_t w12, uint64_t n, uint64_t window_size) { |
| 169 | double |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 170 | r1 = (double) w1 * window_size, |
| 171 | r2 = (double) n - r1, |
| 172 | c1 = w2, |
| 173 | c2 = n - c1, |
| 174 | o11 = w12, o12 = r1 - o11, |
| 175 | o21 = c1 - w12, o22 = r2 - o21, |
| 176 | e11 = r1 * c1 / n, e12 = r1 * c2 / n, |
| 177 | e21 = r2 * c1 / n, e22 = r2 * c2 / n; |
| 178 | return (2 * ((o11 > 0 ? o11 * log(o11 / e11) : 0) + (o12 > 0 ? o12 * log(o12 / e12) : 0) + |
| 179 | (o21 > 0 ? o21 * log(o21 / e21) : 0) + (o22 > 0 ? o22 * log(o22 / e22) : 0))); |
Marc Kupietz | bbd236e | 2019-01-21 16:50:19 +0100 | [diff] [blame] | 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 |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 185 | r1 = (double) w1 * window_size, |
| 186 | c1 = w2; |
| 187 | return 2 * w12 / (c1 + r1); |
Marc Kupietz | 4188045 | 2019-01-22 15:29:06 +0100 | [diff] [blame] | 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 | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 193 | r1 = (double) w1 * window_size, |
| 194 | c1 = w2; |
| 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: |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 200 | CountMergeOperator() { |
| 201 | mergeOperator_ = MergeOperators::CreateUInt64AddOperator(); |
| 202 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 203 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +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 | 12af019 | 2021-03-13 18:05:14 +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 | 12af019 | 2021-03-13 18:05:14 +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: |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 229 | std::shared_ptr<MergeOperator> mergeOperator_; |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 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: |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 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: |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 240 | CollocatorIterator(Iterator *base_iterator) |
| 241 | : base_iterator_(base_iterator) {} |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 242 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 243 | void setPrefix(char *prefix) { |
| 244 | memcpy(prefixc, prefix, sizeof(uint64_t)); |
| 245 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 246 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 247 | virtual void SeekToFirst() { base_iterator_->SeekToFirst(); } |
| 248 | |
| 249 | virtual void SeekToLast() { base_iterator_->SeekToLast(); } |
| 250 | |
| 251 | virtual void Seek(const rocksdb::Slice &s) { base_iterator_->Seek(s); } |
| 252 | |
| 253 | virtual void |
| 254 | SeekForPrev(const rocksdb::Slice &s) { base_iterator_->SeekForPrev(s); } |
| 255 | |
| 256 | virtual void Prev() { base_iterator_->Prev(); } |
| 257 | |
| 258 | virtual void Next() { base_iterator_->Next(); } |
| 259 | |
| 260 | virtual Slice key() const; |
| 261 | |
| 262 | virtual Slice value() const; |
| 263 | |
| 264 | virtual Status status() const; |
| 265 | |
| 266 | virtual bool Valid() const; |
| 267 | |
| 268 | bool isValid(); |
| 269 | |
| 270 | uint64_t intValue(); |
| 271 | |
| 272 | uint64_t intKey(); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 273 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 274 | }; |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 275 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 276 | // rocksdb::CollocatorIterator::CollocatorIterator(Iterator* base_iterator) {} |
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 | bool rocksdb::CollocatorIterator::Valid() const { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 279 | return base_iterator_->Valid() && key().starts_with(std::string(prefixc, 3)); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 280 | } |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 281 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 282 | bool rocksdb::CollocatorIterator::isValid() { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 283 | return base_iterator_->Valid() && key().starts_with(std::string(prefixc, 3)); |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 284 | // return key().starts_with(std::string(prefixc,3)); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 285 | } |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 286 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 287 | uint64_t rocksdb::CollocatorIterator::intKey() { |
| 288 | return DecodeFixed64(base_iterator_->key().data()); |
| 289 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 290 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 291 | uint64_t rocksdb::CollocatorIterator::intValue() { |
| 292 | return DecodeFixed64(base_iterator_->value().data()); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 295 | class VocabEntry { |
| 296 | public: |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 297 | string word; |
| 298 | uint64_t freq; |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 299 | }; |
| 300 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 301 | class CollocatorDB { |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 302 | private: |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 303 | WriteOptions merge_option_; // for merge |
| 304 | char _one[sizeof(uint64_t)]; |
| 305 | Slice _one_slice; |
| 306 | vector<VocabEntry> _vocab; |
| 307 | uint64_t total = 0; |
| 308 | uint64_t sentences = 0; |
| 309 | float avg_window_size = 8.0; |
| 310 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 311 | protected: |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 312 | std::shared_ptr<DB> db_; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 313 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 314 | WriteOptions put_option_; |
| 315 | ReadOptions get_option_; |
| 316 | WriteOptions delete_option_; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 317 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 318 | uint64_t default_; |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 319 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 320 | std::shared_ptr<DB> OpenDb(const char *dbname); |
| 321 | |
| 322 | std::shared_ptr<DB> OpenDbForRead(const char *dbname); |
| 323 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 324 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 325 | public: |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame] | 326 | void readVocab(string fname); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 327 | string getWord(uint32_t w1); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 328 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 329 | CollocatorDB(const char *db_name, bool read_only); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 330 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 331 | // public interface of CollocatorDB. |
| 332 | // All four functions return false |
| 333 | // if the underlying level db operation failed. |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 334 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 335 | // mapped to a levedb Put |
| 336 | bool set(const std::string &key, uint64_t value) { |
| 337 | // just treat the internal rep of int64 as the string |
| 338 | char buf[sizeof(value)]; |
| 339 | EncodeFixed64(buf, value); |
| 340 | Slice slice(buf, sizeof(value)); |
| 341 | auto s = db_->Put(put_option_, key, slice); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 342 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 343 | if (s.ok()) { |
| 344 | return true; |
| 345 | } else { |
| 346 | std::cerr << s.ToString() << std::endl; |
| 347 | return false; |
| 348 | } |
| 349 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 350 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 351 | DB *getDb() { |
| 352 | return db_.get(); |
| 353 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 354 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 355 | // mapped to a rocksdb Delete |
| 356 | bool remove(const std::string &key) { |
| 357 | auto s = db_->Delete(delete_option_, key); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 358 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 359 | if (s.ok()) { |
| 360 | return true; |
| 361 | } else { |
| 362 | std::cerr << s.ToString() << std::endl; |
| 363 | return false; |
| 364 | } |
| 365 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 366 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 367 | // mapped to a rocksdb Get |
| 368 | bool get(const std::string &key, uint64_t *value) { |
| 369 | std::string str; |
| 370 | auto s = db_->Get(get_option_, key, &str); |
| 371 | |
| 372 | if (s.IsNotFound()) { |
| 373 | // return default value if not found; |
| 374 | *value = default_; |
| 375 | return true; |
| 376 | } else if (s.ok()) { |
| 377 | // deserialization |
| 378 | if (str.size() != sizeof(uint64_t)) { |
| 379 | std::cerr << "value corruption\n"; |
| 380 | return false; |
| 381 | } |
| 382 | *value = DecodeFixed64(&str[0]); |
| 383 | return true; |
| 384 | } else { |
| 385 | std::cerr << s.ToString() << std::endl; |
| 386 | return false; |
| 387 | } |
| 388 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 389 | |
| 390 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 391 | uint64_t get(const uint32_t w1, const uint32_t w2, const int8_t dist) { |
| 392 | char encoded_key[sizeof(uint64_t)]; |
| 393 | EncodeFixed64(encoded_key, encodeCollocation(w1, w2, dist)); |
| 394 | uint64_t value = default_; |
| 395 | get(std::string(encoded_key, 8), &value); |
| 396 | return value; |
| 397 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 398 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 399 | virtual void inc(const std::string &key) { |
| 400 | db_->Merge(merge_option_, key, _one_slice); |
| 401 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 402 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 403 | void inc(const uint64_t key) { |
| 404 | char encoded_key[sizeof(uint64_t)]; |
| 405 | EncodeFixed64(encoded_key, key); |
| 406 | db_->Merge(merge_option_, std::string(encoded_key, 8), _one_slice); |
| 407 | } |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 408 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 409 | virtual void inc(const uint32_t w1, const uint32_t w2, const uint8_t dist); |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 410 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 411 | void dump(uint32_t w1, uint32_t w2, int8_t dist); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 412 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 413 | vector<Collocator> get_collocators(uint32_t w1); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 414 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 415 | vector<Collocator> get_collocators(uint32_t w1, uint32_t max_w2); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 416 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 417 | vector<Collocator> get_collocation_scores(uint32_t w1, uint32_t w2); |
| 418 | |
| 419 | vector<Collocator> |
| 420 | get_collocators(uint32_t w1, uint32_t min_w2, uint32_t max_w2); |
| 421 | |
| 422 | void |
| 423 | applyCAMeasures(const uint32_t w1, const uint32_t w2, uint64_t *sumWindow, |
| 424 | const uint64_t sum, const int usedPositions, |
| 425 | int true_window_size, rocksdb::Collocator *result); |
| 426 | |
| 427 | void dumpSparseLlr(uint32_t w1, uint32_t min_cooccur); |
| 428 | |
| 429 | string collocators2json(uint32_t w1, vector<Collocator> collocators); |
| 430 | |
| 431 | // mapped to a rocksdb Merge operation |
| 432 | virtual bool add(const std::string &key, uint64_t value) { |
| 433 | char encoded[sizeof(uint64_t)]; |
| 434 | EncodeFixed64(encoded, value); |
| 435 | Slice slice(encoded, sizeof(uint64_t)); |
| 436 | auto s = db_->Merge(merge_option_, key, slice); |
| 437 | |
| 438 | if (s.ok()) { |
| 439 | return true; |
| 440 | } else { |
| 441 | std::cerr << s.ToString() << std::endl; |
| 442 | return false; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | CollocatorIterator *SeekIterator(uint64_t w1, uint64_t w2, int8_t dist); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 447 | }; |
| 448 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 449 | rocksdb::CollocatorDB::CollocatorDB(const char *db_name, bool read_only = false) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 450 | // merge_option_.sync = true; |
| 451 | if (read_only) |
Marc Kupietz | 88d116b | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 452 | db_ = OpenDbForRead(strdup(db_name)); |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 453 | else |
| 454 | db_ = OpenDb(db_name); |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 455 | assert(db_); |
| 456 | uint64_t one = 1; |
| 457 | EncodeFixed64(_one, one); |
| 458 | _one_slice = Slice(_one, sizeof(uint64_t)); |
| 459 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 460 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 461 | 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] | 462 | inc(encodeCollocation(w1, w2, dist)); |
| 463 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 464 | |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame] | 465 | void rocksdb::CollocatorDB::readVocab(string fname) { |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 466 | char strbuf[2048]; |
| 467 | uint64_t freq; |
| 468 | FILE *fin = fopen(fname.c_str(), "rb"); |
| 469 | if (fin == NULL) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 470 | cout << "Vocabulary file " << fname << " not found\n"; |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 471 | exit(1); |
| 472 | } |
| 473 | uint64_t i = 0; |
„feldmueller“ | 2441f7c | 2024-11-14 16:31:30 +0100 | [diff] [blame] | 474 | while (fscanf(fin, "%s %lu", strbuf, &freq) == 2) { |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 475 | _vocab.push_back({strbuf, freq}); |
| 476 | total += freq; |
| 477 | i++; |
| 478 | } |
| 479 | fclose(fin); |
Marc Kupietz | 4ec51c1 | 2019-01-21 11:06:39 +0100 | [diff] [blame] | 480 | |
| 481 | char size_fname[256]; |
| 482 | strcpy(size_fname, fname.c_str()); |
| 483 | char *pos = strstr(size_fname, ".vocab"); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 484 | if (pos) { |
| 485 | *pos = 0; |
Marc Kupietz | 4ec51c1 | 2019-01-21 11:06:39 +0100 | [diff] [blame] | 486 | strcat(size_fname, ".size"); |
| 487 | FILE *fp = fopen(size_fname, "r"); |
| 488 | if (fp != NULL) { |
| 489 | fscanf(fp, "%lu", &sentences); |
| 490 | fscanf(fp, "%lu", &total); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 491 | float sl = (float) total / (float) sentences; |
Marc Kupietz | 4ec51c1 | 2019-01-21 11:06:39 +0100 | [diff] [blame] | 492 | float w = WINDOW_SIZE; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 493 | avg_window_size = ((sl > 2 * w ? (sl - 2 * w) * 2 * w : 0) + (double) w * (3 * w - 1)) / sl; |
| 494 | fprintf(stdout, |
| 495 | "Size corrections found: corpus size: %lu tokens in %lu sentences, avg. sentence size: %f, avg. window size: %f\n", |
| 496 | total, sentences, sl, avg_window_size); |
Marc Kupietz | 4ec51c1 | 2019-01-21 11:06:39 +0100 | [diff] [blame] | 497 | fclose(fp); |
| 498 | } else { |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame] | 499 | // std::cout << "size file " << size_fname << " not found\n"; |
Marc Kupietz | 4ec51c1 | 2019-01-21 11:06:39 +0100 | [diff] [blame] | 500 | } |
| 501 | } else { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 502 | std::cout << "cannot determine size file " << size_fname << "\n"; |
Marc Kupietz | 4ec51c1 | 2019-01-21 11:06:39 +0100 | [diff] [blame] | 503 | } |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 506 | std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDbForRead(const char *name) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 507 | DB *db; |
| 508 | Options options; |
| 509 | options.env->SetBackgroundThreads(4); |
| 510 | options.create_if_missing = true; |
| 511 | options.merge_operator = std::make_shared<CountMergeOperator>(); |
| 512 | options.max_successive_merges = 0; |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 513 | // options.prefix_extractor.reset(NewFixedPrefixTransform(8)); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 514 | options.IncreaseParallelism(); |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 515 | options.OptimizeLevelStyleCompaction(); |
| 516 | options.prefix_extractor.reset(NewFixedPrefixTransform(3)); |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 517 | ostringstream dbname, vocabname; |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 518 | dbname << name << ".rocksdb"; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 519 | auto s = DB::OpenForReadOnly(options, dbname.str(), &db); |
| 520 | if (!s.ok()) { |
| 521 | std::cerr << s.ToString() << std::endl; |
| 522 | assert(false); |
| 523 | } |
Marc Kupietz | 37359b1 | 2018-01-09 21:11:37 +0100 | [diff] [blame] | 524 | vocabname << name << ".vocab"; |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame] | 525 | readVocab(vocabname.str()); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 526 | return std::shared_ptr<DB>(db); |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 529 | std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDb(const char *dbname) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 530 | DB *db; |
| 531 | Options options; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 532 | |
| 533 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 534 | options.env->SetBackgroundThreads(4); |
| 535 | options.create_if_missing = true; |
| 536 | options.merge_operator = std::make_shared<CountMergeOperator>(); |
| 537 | options.max_successive_merges = 0; |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 538 | // options.prefix_extractor.reset(NewFixedPrefixTransform(8)); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 539 | options.IncreaseParallelism(); |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 540 | options.OptimizeLevelStyleCompaction(); |
| 541 | // options.max_write_buffer_number = 48; |
| 542 | // options.max_background_jobs = 48; |
| 543 | // options.allow_concurrent_memtable_write=true; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 544 | // options.memtable_factory.reset(rocksdb::NewHashLinkListRepFactory(200000)); |
| 545 | // options.enable_write_thread_adaptive_yield = 1; |
| 546 | // options.allow_concurrent_memtable_write = 1; |
| 547 | // options.memtable_factory.reset(new rocksdb::SkipListFactory); |
| 548 | // options.write_buffer_size = 1 << 22; |
| 549 | // options.allow_mmap_reads = true; |
| 550 | // options.allow_mmap_writes = true; |
| 551 | // options.max_background_compactions = 40; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 552 | // BlockBasedTableOptions table_options; |
| 553 | // table_options.filter_policy.reset(NewBloomFilterPolicy(24, false)); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 554 | // options.bloom_locality = 1; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 555 | // std::shared_ptr<Cache> cache = NewLRUCache(512 * 1024 * 1024); |
| 556 | // table_options.block_cache = cache; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 557 | // options.table_factory.reset(NewBlockBasedTableFactory(table_options)); |
| 558 | Status s; |
| 559 | // DestroyDB(dbname, Options()); |
| 560 | s = DB::Open(options, dbname, &db); |
| 561 | if (!s.ok()) { |
| 562 | std::cerr << s.ToString() << std::endl; |
| 563 | assert(false); |
| 564 | } |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame] | 565 | total = 1000; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 566 | return std::shared_ptr<DB>(db); |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 567 | } |
| 568 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 569 | 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] | 570 | ReadOptions options; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 571 | options.prefix_same_as_start = true; |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 572 | char prefixc[sizeof(uint64_t)]; |
| 573 | EncodeFixed64(prefixc, encodeCollocation(w1, w2, dist)); |
| 574 | Iterator *it = db_->NewIterator(options); |
| 575 | CollocatorIterator *cit = new CollocatorIterator(it); |
Marc Kupietz | 88d116b | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 576 | if (w2 > 0) |
| 577 | cit->Seek(std::string(prefixc, 6)); |
| 578 | else |
| 579 | cit->Seek(std::string(prefixc, 3)); |
Marc Kupietz | 18375e1 | 2017-12-24 10:11:18 +0100 | [diff] [blame] | 580 | cit->setPrefix(prefixc); |
| 581 | return cit; |
| 582 | } |
| 583 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 584 | 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] | 585 | auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, w2, dist)); |
| 586 | for (; it->isValid(); it->Next()) { |
| 587 | uint64_t value = it->intValue(); |
| 588 | uint64_t key = it->intKey(); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 589 | std::cout << "w1:" << W1(key) << ", w2:" << W2(key) << ", dist:" << (int32_t) DIST(key) << " - count:" << value |
| 590 | << std::endl; |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 591 | } |
| 592 | std::cout << "ready dumping\n"; |
| 593 | } |
| 594 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 595 | bool sortByNpmi(const Collocator &lhs, const Collocator &rhs) { return lhs.npmi > rhs.npmi; } |
| 596 | |
| 597 | bool sortByLfmd(const Collocator &lhs, const Collocator &rhs) { return lhs.lfmd > rhs.lfmd; } |
| 598 | |
| 599 | bool sortByLlr(const Collocator &lhs, const Collocator &rhs) { return lhs.llr > rhs.llr; } |
| 600 | |
| 601 | bool sortByLogDice(const Collocator &lhs, const Collocator &rhs) { return lhs.logdice > rhs.logdice; } |
| 602 | |
Marc Kupietz | 3203e4c | 2019-02-04 12:42:45 +0100 | [diff] [blame] | 603 | 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] | 604 | |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 605 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 606 | void rocksdb::CollocatorDB::applyCAMeasures(const uint32_t w1, const uint32_t w2, uint64_t *sumWindow, |
| 607 | const uint64_t sum, const int usedPositions, int true_window_size, |
| 608 | rocksdb::Collocator *result) { |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 609 | uint64_t f1 = _vocab[w1].freq, f2 = _vocab[w2].freq; |
| 610 | double o = sum, |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 611 | r1 = f1 * true_window_size, |
| 612 | c1 = f2, |
| 613 | e = r1 * c1 / total, |
| 614 | pmi = log2(o / e), |
| 615 | md = log2(o * o / e), |
| 616 | lfmd = log2(o * o * o / e), |
| 617 | llr = ca_ll(f1, f2, sum, total, true_window_size); |
| 618 | double ld = ca_logdice(f1, f2, sum, total, true_window_size); |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 619 | |
| 620 | int bestWindow = usedPositions; |
| 621 | double bestAF = ld; |
| 622 | double currentAF; |
| 623 | // if(f1<75000000) |
| 624 | //#pragma omp parallel for reduction(max:bestAF) |
Marc Kupietz | 6d0fa54 | 2021-02-26 09:24:35 +0100 | [diff] [blame] | 625 | // #pragma omp target teams distribute parallel for reduction(max:bestAF) map(tofrom:bestAF,currentAF,bestWindow,usedPositions) |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 626 | for (int bitmask = 1; bitmask < (1 << (2 * WINDOW_SIZE)); bitmask++) { |
| 627 | if ((bitmask & usedPositions) == 0 || (bitmask & ~usedPositions) > 0) continue; |
| 628 | uint64_t currentWindowSum = 0; |
Marc Kupietz | 6d0fa54 | 2021-02-26 09:24:35 +0100 | [diff] [blame] | 629 | // #pragma omp target teams distribute parallel for reduction(+:currentWindowSum) map(tofrom:bitmask,usedPositions) |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 630 | for (int pos = 0; pos < 2 * WINDOW_SIZE; pos++) { |
| 631 | if (((1 << pos) & bitmask & usedPositions) != 0) |
| 632 | currentWindowSum += sumWindow[pos]; |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 633 | } |
| 634 | currentAF = ca_logdice(f1, f2, currentWindowSum, total, __builtin_popcount(bitmask)); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 635 | if (currentAF > bestAF) { |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 636 | bestAF = currentAF; |
| 637 | bestWindow = bitmask; |
| 638 | } |
| 639 | } |
| 640 | |
Marc Kupietz | 0421d09 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 641 | *result = {w2, |
| 642 | f2, |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 643 | sum, |
Marc Kupietz | 0421d09 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 644 | pmi, |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 645 | pmi / (-log2(o / total / true_window_size)), |
Marc Kupietz | 0421d09 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 646 | llr, |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 647 | lfmd, |
| 648 | md, |
Marc Kupietz | 0421d09 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 649 | sumWindow[WINDOW_SIZE], |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 650 | sumWindow[WINDOW_SIZE - 1], |
Marc Kupietz | 6d9221d | 2021-02-26 09:34:40 +0100 | [diff] [blame] | 651 | ca_pmi(f1, f2, sumWindow[WINDOW_SIZE], total, 1), |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 652 | ca_pmi(f1, f2, sumWindow[WINDOW_SIZE - 1], total, 1), |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 653 | ca_dice(f1, f2, sum, total, true_window_size), |
| 654 | ld, |
| 655 | bestAF, |
| 656 | usedPositions, |
| 657 | bestWindow |
| 658 | }; |
| 659 | |
| 660 | } |
| 661 | |
Marc Kupietz | 88d116b | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 662 | std::vector<Collocator> rocksdb::CollocatorDB::get_collocators(uint32_t w1, uint32_t min_w2, uint32_t max_w2) { |
Marc Kupietz | 75af60f | 2019-01-22 22:34:29 +0100 | [diff] [blame] | 663 | std::vector<Collocator> collocators; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 664 | uint64_t w2, last_w2 = 0xffffffffffffffff; |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 665 | uint64_t maxv = 0, sum = 0; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 666 | uint64_t *sumWindow = (uint64_t *) malloc(sizeof(uint64_t) * 2 * WINDOW_SIZE); |
| 667 | memset(sumWindow, 0, sizeof(uint64_t) * 2 * WINDOW_SIZE); |
Marc Kupietz | ade3322 | 2019-01-22 22:52:44 +0100 | [diff] [blame] | 668 | int true_window_size = 1; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 669 | int usedPositions = 0; |
Marc Kupietz | 98cbcdc | 2019-01-21 17:11:27 +0100 | [diff] [blame] | 670 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 671 | if (w1 > _vocab.size()) { |
| 672 | std::cout << w1 << "> vocabulary size " << _vocab.size() << "\n"; |
| 673 | w1 -= _vocab.size(); |
| 674 | } |
| 675 | #ifdef DEBUG |
| 676 | std::cout << "Searching for collocates of " << _vocab[w1].word << "\n"; |
| 677 | #endif |
| 678 | // #pragma omp parallel num_threads(40) |
| 679 | // #pragma omp single |
| 680 | for (auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, min_w2, 0)); it->isValid(); it->Next()) { |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 681 | uint64_t value = it->intValue(), |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 682 | key = it->intKey(); |
| 683 | if ((w2 = W2(key)) > max_w2) |
Marc Kupietz | bd96619 | 2018-10-13 14:14:37 +0200 | [diff] [blame] | 684 | continue; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 685 | if (last_w2 == 0xffffffffffffffff) last_w2 = w2; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 686 | if (w2 != last_w2) { |
Marc Kupietz | 75af60f | 2019-01-22 22:34:29 +0100 | [diff] [blame] | 687 | if (sum >= FREQUENCY_THRESHOLD) { |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 688 | collocators.push_back({}); |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 689 | rocksdb::Collocator *result = &(collocators[collocators.size() - 1]); |
| 690 | // #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] | 691 | { |
| 692 | // uint64_t *nsw = (uint64_t *)malloc(sizeof(uint64_t) * 2 *WINDOW_SIZE); |
| 693 | // memcpy(nsw, sumWindow, sizeof(uint64_t) * 2 *WINDOW_SIZE); |
| 694 | applyCAMeasures(w1, last_w2, sumWindow, sum, usedPositions, true_window_size, result); |
| 695 | // free(nsw); |
Marc Kupietz | 75af60f | 2019-01-22 22:34:29 +0100 | [diff] [blame] | 696 | } |
Marc Kupietz | 98cbcdc | 2019-01-21 17:11:27 +0100 | [diff] [blame] | 697 | } |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 698 | memset(sumWindow, 0, 2 * WINDOW_SIZE * sizeof(uint64_t)); |
| 699 | usedPositions = 1 << (-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0)); |
| 700 | sumWindow[-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0)] = value; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 701 | last_w2 = w2; |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 702 | maxv = value; |
Marc Kupietz | 98cbcdc | 2019-01-21 17:11:27 +0100 | [diff] [blame] | 703 | sum = value; |
Marc Kupietz | ade3322 | 2019-01-22 22:52:44 +0100 | [diff] [blame] | 704 | true_window_size = 1; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 705 | if (min_w2 == max_w2 && w2 != min_w2) |
| 706 | break; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 707 | } else { |
Marc Kupietz | 98cbcdc | 2019-01-21 17:11:27 +0100 | [diff] [blame] | 708 | sum += value; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 709 | if (value > maxv) |
Marc Kupietz | 8e0ebea | 2018-01-24 09:53:26 +0100 | [diff] [blame] | 710 | maxv = value; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 711 | usedPositions |= 1 << (-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0)); |
| 712 | sumWindow[-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0)] = value; |
Marc Kupietz | ade3322 | 2019-01-22 22:52:44 +0100 | [diff] [blame] | 713 | true_window_size++; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 717 | // #pragma omp taskwait |
Marc Kupietz | 6d0fa54 | 2021-02-26 09:24:35 +0100 | [diff] [blame] | 718 | sort(collocators.begin(), collocators.end(), sortByLogDiceAF); |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 719 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 720 | #ifdef DEBUG |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 721 | int i=0; |
| 722 | for (Collocator c : collocators) { |
| 723 | if(i++>10) break; |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 724 | std::cout << "w1:" << _vocab[w1].word << ", w2: *" << _vocab[c.w2].word << "*" |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 725 | << "\t f(w1):" << _vocab[w1].freq |
| 726 | << "\t f(w2):" << _vocab[c.w2].freq |
Marc Kupietz | 51f9379 | 2018-01-25 08:51:01 +0100 | [diff] [blame] | 727 | << "\t f(w1, w2):" << c.raw |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 728 | << "\t pmi:" << c.pmi |
| 729 | << "\t npmi:" << c.npmi |
| 730 | << "\t llr:" << c.llr |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 731 | << "\t md:" << c.md |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 732 | << "\t lfmd:" << c.lfmd |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 733 | << "\t total:" << total |
| 734 | << std::endl; |
| 735 | } |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 736 | #endif |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 737 | |
| 738 | return collocators; |
Marc Kupietz | d31254c | 2018-01-20 21:29:30 +0100 | [diff] [blame] | 739 | } |
| 740 | |
Marc Kupietz | 88d116b | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 741 | |
| 742 | std::vector<Collocator> rocksdb::CollocatorDB::get_collocation_scores(uint32_t w1, uint32_t w2) { |
| 743 | return get_collocators(w1, w2, w2); |
| 744 | } |
| 745 | |
Marc Kupietz | 8c62c37 | 2019-01-31 12:21:01 +0100 | [diff] [blame] | 746 | std::vector<Collocator> rocksdb::CollocatorDB::get_collocators(uint32_t w1) { |
Marc Kupietz | 88d116b | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 747 | return get_collocators(w1, 0, UINT32_MAX); |
Marc Kupietz | bd96619 | 2018-10-13 14:14:37 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 750 | void rocksdb::CollocatorDB::dumpSparseLlr(uint32_t w1, uint32_t min_cooccur) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 751 | std::vector<Collocator> collocators; |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 752 | std::stringstream stream; |
| 753 | uint64_t w2, last_w2 = 0xffffffffffffffff; |
| 754 | uint64_t maxv = 0, total_w1 = 0; |
| 755 | bool first = true; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 756 | for (auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) { |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 757 | uint64_t value = it->intValue(), |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 758 | key = it->intKey(); |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 759 | w2 = W2(key); |
| 760 | total_w1 += value; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 761 | if (last_w2 == 0xffffffffffffffff) last_w2 = w2; |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 762 | if (w2 != last_w2) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 763 | if (maxv >= min_cooccur) { |
| 764 | double llr = ca_ll(_vocab[w1].freq, _vocab[last_w2].freq, maxv, total, 1); |
| 765 | if (first) |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 766 | first = false; |
| 767 | else |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 768 | stream << " "; |
| 769 | stream << w2 << " " << llr; |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 770 | } |
| 771 | last_w2 = w2; |
| 772 | maxv = value; |
| 773 | } else { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 774 | if (value > maxv) |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 775 | maxv = value; |
| 776 | } |
| 777 | } |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 778 | if (first) |
| 779 | stream << "1 0.0"; |
| 780 | stream << "\n"; |
Marc Kupietz | 3400aa5 | 2018-06-05 10:28:55 +0200 | [diff] [blame] | 781 | std::cout << stream.str(); |
| 782 | } |
| 783 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 784 | rocksdb::Slice rocksdb::CollocatorIterator::key() const { return base_iterator_->key(); } |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 785 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 786 | rocksdb::Slice rocksdb::CollocatorIterator::value() const { return base_iterator_->value(); } |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 787 | |
Marc Kupietz | 4b799e9 | 2018-01-02 11:04:56 +0100 | [diff] [blame] | 788 | rocksdb::Status rocksdb::CollocatorIterator::status() const { return base_iterator_->status(); } |
| 789 | |
Marc Kupietz | 28cc53e | 2017-12-23 17:24:55 +0100 | [diff] [blame] | 790 | }; |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 791 | |
Marc Kupietz | 4a5e08a | 2018-06-05 11:07:11 +0200 | [diff] [blame] | 792 | string rocksdb::CollocatorDB::getWord(uint32_t w1) { |
| 793 | return _vocab[w1].word; |
| 794 | } |
| 795 | |
Marc Kupietz | e962715 | 2019-02-04 12:32:12 +0100 | [diff] [blame] | 796 | string rocksdb::CollocatorDB::collocators2json(uint32_t w1, vector<Collocator> collocators) { |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 797 | ostringstream s; |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 798 | int i = 0; |
Marc Kupietz | e962715 | 2019-02-04 12:32:12 +0100 | [diff] [blame] | 799 | s << " { \"f1\": " << _vocab[w1].freq << "," << |
| 800 | "\"w1\":\"" << string(_vocab[w1].word) << "\", " << |
| 801 | "\"N\": " << total << ", " << |
| 802 | "\"collocates\": ["; |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 803 | bool first = true; |
| 804 | for (Collocator c : collocators) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 805 | if (strncmp(_vocab[c.w2].word.c_str(), "quot", 4) == 0) continue; |
Marc Kupietz | 0dd86ef | 2018-01-11 22:23:17 +0100 | [diff] [blame] | 806 | if (i++ > 200) |
| 807 | break; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 808 | if (!first) |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 809 | s << ",\n"; |
| 810 | else |
| 811 | first = false; |
| 812 | s << "{" |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 813 | "\"word\":\"" << (string(_vocab[c.w2].word).compare("<num>") == 0 ? string("###") : string(_vocab[c.w2].word)) |
| 814 | << "\"," << |
| 815 | "\"f2\":" << c.f2 << "," << |
| 816 | "\"f\":" << c.raw << "," << |
| 817 | "\"npmi\":" << c.npmi << "," << |
| 818 | "\"pmi\":" << c.pmi << "," << |
| 819 | "\"llr\":" << c.llr << "," << |
| 820 | "\"lfmd\":" << c.lfmd << "," << |
| 821 | "\"md\":" << c.md << "," << |
| 822 | "\"dice\":" << c.dice << "," << |
| 823 | "\"ld\":" << c.logdice << "," << |
Marc Kupietz | 97f433b | 2021-03-13 18:10:52 +0100 | [diff] [blame] | 824 | "\"ln_count\":" << c.left_raw << "," << |
| 825 | "\"rn_count\":" << c.right_raw << "," << |
| 826 | "\"ln_pmi\":" << c.left_pmi << "," << |
| 827 | "\"rn_pmi\":" << c.right_pmi << "," << |
| 828 | "\"ldaf\":" << c.ldaf << "," << |
Marc Kupietz | e9f5893 | 2019-01-24 15:12:59 +0100 | [diff] [blame] | 829 | "\"win\":" << c.window << "," << |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 830 | "\"afwin\":" << c.af_window << |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 831 | "}"; |
| 832 | } |
Marc Kupietz | e962715 | 2019-02-04 12:32:12 +0100 | [diff] [blame] | 833 | s << "]}\n"; |
Marc Kupietz | 0421d09 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 834 | // std::cout << s.str(); |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 835 | return s.str(); |
| 836 | } |
| 837 | |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 838 | typedef rocksdb::CollocatorDB COLLOCATORS; |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 839 | |
| 840 | extern "C" { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 841 | #ifdef __clang__ |
| 842 | #pragma clang diagnostic push |
| 843 | #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection" |
| 844 | #endif |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 845 | DLL_EXPORT COLLOCATORS *open_collocatordb_for_write(char *dbname) { |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 846 | return new rocksdb::CollocatorDB(dbname, false); |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 847 | } |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 848 | |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 849 | DLL_EXPORT COLLOCATORS *open_collocatordb(char *dbname) { |
Marc Kupietz | 6aec768 | 2018-01-10 09:47:48 +0100 | [diff] [blame] | 850 | return new rocksdb::CollocatorDB(dbname, true); |
Marc Kupietz | 6bb2776 | 2018-01-09 17:53:01 +0100 | [diff] [blame] | 851 | } |
| 852 | |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 853 | DLL_EXPORT 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] | 854 | db->inc(w1, w2, dist); |
| 855 | } |
| 856 | |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 857 | DLL_EXPORT void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 858 | db->dump(w1, w2, dist); |
| 859 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 860 | |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 861 | DLL_EXPORT COLLOCATORS *get_collocators(COLLOCATORS *db, uint32_t w1) { |
Marc Kupietz | 6663f11 | 2021-03-14 09:20:59 +0100 | [diff] [blame] | 862 | std::vector<Collocator> c = db->get_collocators(w1); |
| 863 | if (c.empty()) |
| 864 | return NULL; |
| 865 | uint64_t size = c.size() + sizeof c[0]; |
| 866 | COLLOCATORS *p = (COLLOCATORS *) malloc(size); |
| 867 | memcpy(p, c.data(), size); |
| 868 | return p; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 869 | } |
Marc Kupietz | c8ddf45 | 2018-01-07 21:33:12 +0100 | [diff] [blame] | 870 | |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 871 | DLL_EXPORT COLLOCATORS *get_collocation_scores(COLLOCATORS *db, uint32_t w1, uint32_t w2) { |
Marc Kupietz | 6663f11 | 2021-03-14 09:20:59 +0100 | [diff] [blame] | 872 | std::vector<Collocator> c = db->get_collocation_scores(w1, w2); |
| 873 | if (c.empty()) |
| 874 | return NULL; |
| 875 | uint64_t size = c.size() + sizeof c[0]; |
| 876 | COLLOCATORS *p = (COLLOCATORS *) malloc(size); |
| 877 | memcpy(p, c.data(), size); |
| 878 | return p; |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 879 | } |
Marc Kupietz | 88d116b | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 880 | |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 881 | DLL_EXPORT char *get_word(COLLOCATORS *db, uint32_t w) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 882 | return strdup(db->getWord(w).c_str()); |
| 883 | } |
Marc Kupietz | ca3a52e | 2018-06-05 14:16:23 +0200 | [diff] [blame] | 884 | |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 885 | DLL_EXPORT void read_vocab(COLLOCATORS *db, char *fname) { |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame] | 886 | std::string fName(fname); |
| 887 | db->readVocab(fName); |
| 888 | } |
| 889 | |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 890 | DLL_EXPORT const char *get_collocators_as_json(COLLOCATORS *db, uint32_t w1) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 891 | return strdup(db->collocators2json(w1, db->get_collocators(w1)).c_str()); |
| 892 | } |
Marc Kupietz | 88d116b | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 893 | |
Marc Kupietz | 4422923 | 2024-08-05 15:00:20 +0200 | [diff] [blame] | 894 | DLL_EXPORT const char *get_collocation_scores_as_json(COLLOCATORS *db, uint32_t w1, uint32_t w2) { |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 895 | return strdup(db->collocators2json(w1, db->get_collocation_scores(w1, w2)).c_str()); |
| 896 | } |
Marc Kupietz | b4a683c | 2021-03-14 09:19:44 +0100 | [diff] [blame] | 897 | |
Marc Kupietz | 12af019 | 2021-03-13 18:05:14 +0100 | [diff] [blame] | 898 | #ifdef __clang__ |
| 899 | #pragma clang diagnostic push |
| 900 | #endif |
Marc Kupietz | 06c9a9f | 2018-01-02 16:56:43 +0100 | [diff] [blame] | 901 | } |