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