blob: 2660346956ba44815f9948105eff257c99d7cbae [file] [log] [blame]
Marc Kupietz4b799e92018-01-02 11:04:56 +01001#define EXPORT __attribute__((visibility("visible")))
2#define IMPORT
Marc Kupietz12af0192021-03-13 18:05:14 +01003
Marc Kupietz28cc53e2017-12-23 17:24:55 +01004#include <assert.h>
5#include <memory>
6#include <iostream>
Marc Kupietzc8ddf452018-01-07 21:33:12 +01007#include <algorithm>
8#include <vector>
Marc Kupietz28cc53e2017-12-23 17:24:55 +01009#include <stdint.h>
Marc Kupietzc8ddf452018-01-07 21:33:12 +010010#include <string>
11#include <sstream> // for ostringstream
12#include <math.h>
Marc Kupietzd31254c2018-01-20 21:29:30 +010013#include <rocksdb/cache.h>
Marc Kupietz28cc53e2017-12-23 17:24:55 +010014#include "rocksdb/db.h"
15#include "rocksdb/env.h"
Marc Kupietzc8ddf452018-01-07 21:33:12 +010016#include "rocksdb/table.h"
Marc Kupietz28cc53e2017-12-23 17:24:55 +010017#include <rocksdb/merge_operator.h>
Marc Kupietzc8ddf452018-01-07 21:33:12 +010018#include <rocksdb/slice_transform.h>
Marc Kupietz28cc53e2017-12-23 17:24:55 +010019#include "merge_operators.h"
Marc Kupietz44229232024-08-05 15:00:20 +020020#include "export.h"
Marc Kupietz28cc53e2017-12-23 17:24:55 +010021
Marc Kupietz75af60f2019-01-22 22:34:29 +010022#define WINDOW_SIZE 5
Marc Kupietz98cbcdc2019-01-21 17:11:27 +010023#define FREQUENCY_THRESHOLD 5
Marc Kupietz28cc53e2017-12-23 17:24:55 +010024#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 Kupietz18375e12017-12-24 10:11:18 +010026#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 Kupietzc8ddf452018-01-07 21:33:12 +010029
30typedef struct {
31 uint64_t freq;
32 char *word;
Marc Kupietz12af0192021-03-13 18:05:14 +010033} vocab_entry;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010034
35// typedef struct Collocator {
36// uint64_t w2;
37// uint64_t sum;
38// };
39
Marc Kupietz28cc53e2017-12-23 17:24:55 +010040using namespace rocksdb;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010041using namespace std;
Marc Kupietz28cc53e2017-12-23 17:24:55 +010042
Marc Kupietz4b799e92018-01-02 11:04:56 +010043namespace rocksdb {
Marc Kupietz12af0192021-03-13 18:05:14 +010044 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 Kupietzc8ddf452018-01-07 21:33:12 +010063 };
64
Marc Kupietz28cc53e2017-12-23 17:24:55 +010065 size_t num_merge_operator_calls;
Marc Kupietz12af0192021-03-13 18:05:14 +010066
Marc Kupietz28cc53e2017-12-23 17:24:55 +010067 void resetNumMergeOperatorCalls() { num_merge_operator_calls = 0; }
Marc Kupietzc8ddf452018-01-07 21:33:12 +010068
Marc Kupietz28cc53e2017-12-23 17:24:55 +010069 size_t num_partial_merge_calls;
Marc Kupietz12af0192021-03-13 18:05:14 +010070
Marc Kupietz28cc53e2017-12-23 17:24:55 +010071 void resetNumPartialMergeCalls() { num_partial_merge_calls = 0; }
Marc Kupietz28cc53e2017-12-23 17:24:55 +010072
73
Marc Kupietz12af0192021-03-13 18:05:14 +010074 inline void EncodeFixed64(char *buf, uint64_t value) {
75 if (!IS_BIG_ENDIAN) {
Marc Kupietz4b799e92018-01-02 11:04:56 +010076 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 Kupietz28cc53e2017-12-23 17:24:55 +010087 }
88
Marc Kupietz12af0192021-03-13 18:05:14 +010089 inline uint32_t DecodeFixed32(const char *ptr) {
90 if (!IS_BIG_ENDIAN) {
Marc Kupietz4b799e92018-01-02 11:04:56 +010091 // 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 Kupietz12af0192021-03-13 18:05:14 +0100103 inline uint64_t DecodeFixed64(const char *ptr) {
104 if (!IS_BIG_ENDIAN) {
Marc Kupietz4b799e92018-01-02 11:04:56 +0100105 // 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 Kupietz8e0ebea2018-01-24 09:53:26 +0100116 static inline double ca_pmi(uint64_t f1, uint64_t f2, uint64_t f12, uint64_t total, double window_size) {
Marc Kupietz1335dd72019-01-22 15:35:21 +0100117 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100118 r1 = f1 * window_size,
119 c1 = f2,
120 e = r1 * c1 / total,
121 o = f12;
122 if (f12 < FREQUENCY_THRESHOLD)
Marc Kupietzf4a649a2021-02-26 09:18:01 +0100123 return -1.0;
124 else
Marc Kupietz12af0192021-03-13 18:05:14 +0100125 return log2(o / e);
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100126 }
127
Marc Kupietzce0b8b02018-06-05 11:06:39 +0200128 // 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 Kupietz8e0ebea2018-01-24 09:53:26 +0100130 static inline double ca_npmi(uint64_t f1, uint64_t f2, uint64_t f12, uint64_t total, double window_size) {
Marc Kupietz1335dd72019-01-22 15:35:21 +0100131 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100132 r1 = f1 * window_size,
133 c1 = f2,
134 e = r1 * c1 / total,
135 o = f12;
136 if (f12 < FREQUENCY_THRESHOLD)
Marc Kupietz8caf9912018-06-05 10:51:18 +0200137 return -1.0;
138 else
Marc Kupietz12af0192021-03-13 18:05:14 +0100139 return log2(o / e) / (-log2(o / total / window_size));
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100140 }
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 Kupietz1335dd72019-01-22 15:35:21 +0100146 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100147 r1 = f1 * window_size,
148 c1 = f2,
149 e = r1 * c1 / total,
150 o = f12;
151 return log2(o * o / e);
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100152 }
153
154 static inline double ca_lfmd(uint64_t f1, uint64_t f2, uint64_t f12, uint64_t total, double window_size) {
Marc Kupietz1335dd72019-01-22 15:35:21 +0100155 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100156 r1 = f1 * window_size,
157 c1 = f2,
158 e = r1 * c1 / total,
159 o = f12;
160 if (f12 == 0)
Marc Kupietz8caf9912018-06-05 10:51:18 +0200161 return 0;
162 else
Marc Kupietz12af0192021-03-13 18:05:14 +0100163 return log2(o * o * o / e);
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100164 }
165
Marc Kupietzbbd236e2019-01-21 16:50:19 +0100166 // 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 Kupietz12af0192021-03-13 18:05:14 +0100170 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 Kupietzbbd236e2019-01-21 16:50:19 +0100180 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100181
Marc Kupietz41880452019-01-22 15:29:06 +0100182
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 Kupietz12af0192021-03-13 18:05:14 +0100185 r1 = (double) w1 * window_size,
186 c1 = w2;
187 return 2 * w12 / (c1 + r1);
Marc Kupietz41880452019-01-22 15:29:06 +0100188 }
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 Kupietz12af0192021-03-13 18:05:14 +0100193 r1 = (double) w1 * window_size,
194 c1 = w2;
195 return 14 + log2(2 * w12 / (c1 + r1));
Marc Kupietz41880452019-01-22 15:29:06 +0100196 }
197
Marc Kupietz4b799e92018-01-02 11:04:56 +0100198 class CountMergeOperator : public AssociativeMergeOperator {
199 public:
Marc Kupietz12af0192021-03-13 18:05:14 +0100200 CountMergeOperator() {
201 mergeOperator_ = MergeOperators::CreateUInt64AddOperator();
202 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100203
Marc Kupietz12af0192021-03-13 18:05:14 +0100204 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 Kupietz28cc53e2017-12-23 17:24:55 +0100215
Marc Kupietz12af0192021-03-13 18:05:14 +0100216 return mergeOperator_->PartialMerge(
217 key,
218 *existing_value,
219 value,
220 new_value,
221 logger);
222 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100223
Marc Kupietz12af0192021-03-13 18:05:14 +0100224 virtual const char *Name() const override {
225 return "UInt64AddOperator";
226 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100227
Marc Kupietz4b799e92018-01-02 11:04:56 +0100228 private:
Marc Kupietz12af0192021-03-13 18:05:14 +0100229 std::shared_ptr<MergeOperator> mergeOperator_;
Marc Kupietz4b799e92018-01-02 11:04:56 +0100230 };
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100231
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100232
Marc Kupietz4b799e92018-01-02 11:04:56 +0100233 class CollocatorIterator : public Iterator {
234 private:
Marc Kupietz12af0192021-03-13 18:05:14 +0100235 char prefixc[sizeof(uint64_t)];
236 Iterator *base_iterator_;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100237
238
Marc Kupietz4b799e92018-01-02 11:04:56 +0100239 public:
Marc Kupietz12af0192021-03-13 18:05:14 +0100240 CollocatorIterator(Iterator *base_iterator)
241 : base_iterator_(base_iterator) {}
Marc Kupietz4b799e92018-01-02 11:04:56 +0100242
Marc Kupietz12af0192021-03-13 18:05:14 +0100243 void setPrefix(char *prefix) {
244 memcpy(prefixc, prefix, sizeof(uint64_t));
245 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100246
Marc Kupietz12af0192021-03-13 18:05:14 +0100247 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 Kupietzc8ddf452018-01-07 21:33:12 +0100273
Marc Kupietz4b799e92018-01-02 11:04:56 +0100274 };
Marc Kupietz18375e12017-12-24 10:11:18 +0100275
Marc Kupietz4b799e92018-01-02 11:04:56 +0100276 // rocksdb::CollocatorIterator::CollocatorIterator(Iterator* base_iterator) {}
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100277
Marc Kupietz4b799e92018-01-02 11:04:56 +0100278 bool rocksdb::CollocatorIterator::Valid() const {
Marc Kupietz12af0192021-03-13 18:05:14 +0100279 return base_iterator_->Valid() && key().starts_with(std::string(prefixc, 3));
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100280 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100281
Marc Kupietz4b799e92018-01-02 11:04:56 +0100282 bool rocksdb::CollocatorIterator::isValid() {
Marc Kupietz12af0192021-03-13 18:05:14 +0100283 return base_iterator_->Valid() && key().starts_with(std::string(prefixc, 3));
Marc Kupietzd31254c2018-01-20 21:29:30 +0100284 // return key().starts_with(std::string(prefixc,3));
Marc Kupietz4b799e92018-01-02 11:04:56 +0100285 }
Marc Kupietz18375e12017-12-24 10:11:18 +0100286
Marc Kupietz4b799e92018-01-02 11:04:56 +0100287 uint64_t rocksdb::CollocatorIterator::intKey() {
288 return DecodeFixed64(base_iterator_->key().data());
289 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100290
Marc Kupietz4b799e92018-01-02 11:04:56 +0100291 uint64_t rocksdb::CollocatorIterator::intValue() {
292 return DecodeFixed64(base_iterator_->value().data());
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100293 }
294
Marc Kupietz37359b12018-01-09 21:11:37 +0100295 class VocabEntry {
296 public:
Marc Kupietz12af0192021-03-13 18:05:14 +0100297 string word;
298 uint64_t freq;
Marc Kupietz37359b12018-01-09 21:11:37 +0100299 };
300
Marc Kupietz6aec7682018-01-10 09:47:48 +0100301 class CollocatorDB {
Marc Kupietz4b799e92018-01-02 11:04:56 +0100302 private:
Marc Kupietz12af0192021-03-13 18:05:14 +0100303 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 Kupietz4b799e92018-01-02 11:04:56 +0100311 protected:
Marc Kupietz12af0192021-03-13 18:05:14 +0100312 std::shared_ptr<DB> db_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100313
Marc Kupietz12af0192021-03-13 18:05:14 +0100314 WriteOptions put_option_;
315 ReadOptions get_option_;
316 WriteOptions delete_option_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100317
Marc Kupietz12af0192021-03-13 18:05:14 +0100318 uint64_t default_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100319
Marc Kupietz12af0192021-03-13 18:05:14 +0100320 std::shared_ptr<DB> OpenDb(const char *dbname);
321
322 std::shared_ptr<DB> OpenDbForRead(const char *dbname);
323
Marc Kupietz12af0192021-03-13 18:05:14 +0100324
Marc Kupietz4b799e92018-01-02 11:04:56 +0100325 public:
Marc Kupietzb4a683c2021-03-14 09:19:44 +0100326 void readVocab(string fname);
Marc Kupietz12af0192021-03-13 18:05:14 +0100327 string getWord(uint32_t w1);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100328
Marc Kupietz12af0192021-03-13 18:05:14 +0100329 CollocatorDB(const char *db_name, bool read_only);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100330
Marc Kupietz12af0192021-03-13 18:05:14 +0100331 // public interface of CollocatorDB.
332 // All four functions return false
333 // if the underlying level db operation failed.
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100334
Marc Kupietz12af0192021-03-13 18:05:14 +0100335 // 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 Kupietz4b799e92018-01-02 11:04:56 +0100342
Marc Kupietz12af0192021-03-13 18:05:14 +0100343 if (s.ok()) {
344 return true;
345 } else {
346 std::cerr << s.ToString() << std::endl;
347 return false;
348 }
349 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100350
Marc Kupietz12af0192021-03-13 18:05:14 +0100351 DB *getDb() {
352 return db_.get();
353 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100354
Marc Kupietz12af0192021-03-13 18:05:14 +0100355 // mapped to a rocksdb Delete
356 bool remove(const std::string &key) {
357 auto s = db_->Delete(delete_option_, key);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100358
Marc Kupietz12af0192021-03-13 18:05:14 +0100359 if (s.ok()) {
360 return true;
361 } else {
362 std::cerr << s.ToString() << std::endl;
363 return false;
364 }
365 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100366
Marc Kupietz12af0192021-03-13 18:05:14 +0100367 // 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 Kupietz4b799e92018-01-02 11:04:56 +0100389
390
Marc Kupietz12af0192021-03-13 18:05:14 +0100391 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 Kupietz4b799e92018-01-02 11:04:56 +0100398
Marc Kupietz12af0192021-03-13 18:05:14 +0100399 virtual void inc(const std::string &key) {
400 db_->Merge(merge_option_, key, _one_slice);
401 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100402
Marc Kupietz12af0192021-03-13 18:05:14 +0100403 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 Kupietz4b799e92018-01-02 11:04:56 +0100408
Marc Kupietz12af0192021-03-13 18:05:14 +0100409 virtual void inc(const uint32_t w1, const uint32_t w2, const uint8_t dist);
Marc Kupietz8c62c372019-01-31 12:21:01 +0100410
Marc Kupietz12af0192021-03-13 18:05:14 +0100411 void dump(uint32_t w1, uint32_t w2, int8_t dist);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100412
Marc Kupietz12af0192021-03-13 18:05:14 +0100413 vector<Collocator> get_collocators(uint32_t w1);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100414
Marc Kupietz12af0192021-03-13 18:05:14 +0100415 vector<Collocator> get_collocators(uint32_t w1, uint32_t max_w2);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100416
Marc Kupietz12af0192021-03-13 18:05:14 +0100417 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 Kupietz4b799e92018-01-02 11:04:56 +0100447 };
448
Marc Kupietz6aec7682018-01-10 09:47:48 +0100449 rocksdb::CollocatorDB::CollocatorDB(const char *db_name, bool read_only = false) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100450 // merge_option_.sync = true;
451 if (read_only)
Marc Kupietz88d116b2021-03-13 18:05:14 +0100452 db_ = OpenDbForRead(strdup(db_name));
Marc Kupietz6bb27762018-01-09 17:53:01 +0100453 else
454 db_ = OpenDb(db_name);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100455 assert(db_);
456 uint64_t one = 1;
457 EncodeFixed64(_one, one);
458 _one_slice = Slice(_one, sizeof(uint64_t));
459 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100460
Marc Kupietz6aec7682018-01-10 09:47:48 +0100461 void rocksdb::CollocatorDB::inc(const uint32_t w1, const uint32_t w2, const uint8_t dist) {
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100462 inc(encodeCollocation(w1, w2, dist));
463 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100464
Marc Kupietzb4a683c2021-03-14 09:19:44 +0100465 void rocksdb::CollocatorDB::readVocab(string fname) {
Marc Kupietz37359b12018-01-09 21:11:37 +0100466 char strbuf[2048];
467 uint64_t freq;
468 FILE *fin = fopen(fname.c_str(), "rb");
469 if (fin == NULL) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100470 cout << "Vocabulary file " << fname << " not found\n";
Marc Kupietz37359b12018-01-09 21:11:37 +0100471 exit(1);
472 }
473 uint64_t i = 0;
Marc Kupietz12af0192021-03-13 18:05:14 +0100474 while (!feof(fin)) {
Marc Kupietzd31254c2018-01-20 21:29:30 +0100475 fscanf(fin, "%s %lu", strbuf, &freq);
Marc Kupietz37359b12018-01-09 21:11:37 +0100476 _vocab.push_back({strbuf, freq});
477 total += freq;
478 i++;
479 }
480 fclose(fin);
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100481
482 char size_fname[256];
483 strcpy(size_fname, fname.c_str());
484 char *pos = strstr(size_fname, ".vocab");
Marc Kupietz12af0192021-03-13 18:05:14 +0100485 if (pos) {
486 *pos = 0;
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100487 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 Kupietz12af0192021-03-13 18:05:14 +0100492 float sl = (float) total / (float) sentences;
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100493 float w = WINDOW_SIZE;
Marc Kupietz12af0192021-03-13 18:05:14 +0100494 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 Kupietz4ec51c12019-01-21 11:06:39 +0100498 fclose(fp);
499 } else {
Marc Kupietzb4a683c2021-03-14 09:19:44 +0100500 // std::cout << "size file " << size_fname << " not found\n";
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100501 }
502 } else {
Marc Kupietz12af0192021-03-13 18:05:14 +0100503 std::cout << "cannot determine size file " << size_fname << "\n";
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100504 }
Marc Kupietz37359b12018-01-09 21:11:37 +0100505 }
506
Marc Kupietz6aec7682018-01-10 09:47:48 +0100507 std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDbForRead(const char *name) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100508 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 Kupietz0dd86ef2018-01-11 22:23:17 +0100514 // options.prefix_extractor.reset(NewFixedPrefixTransform(8));
Marc Kupietz12af0192021-03-13 18:05:14 +0100515 options.IncreaseParallelism();
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100516 options.OptimizeLevelStyleCompaction();
517 options.prefix_extractor.reset(NewFixedPrefixTransform(3));
Marc Kupietz37359b12018-01-09 21:11:37 +0100518 ostringstream dbname, vocabname;
Marc Kupietz6bb27762018-01-09 17:53:01 +0100519 dbname << name << ".rocksdb";
Marc Kupietz12af0192021-03-13 18:05:14 +0100520 auto s = DB::OpenForReadOnly(options, dbname.str(), &db);
521 if (!s.ok()) {
522 std::cerr << s.ToString() << std::endl;
523 assert(false);
524 }
Marc Kupietz37359b12018-01-09 21:11:37 +0100525 vocabname << name << ".vocab";
Marc Kupietzb4a683c2021-03-14 09:19:44 +0100526 readVocab(vocabname.str());
Marc Kupietz12af0192021-03-13 18:05:14 +0100527 return std::shared_ptr<DB>(db);
Marc Kupietz6bb27762018-01-09 17:53:01 +0100528 }
529
Marc Kupietz6aec7682018-01-10 09:47:48 +0100530 std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDb(const char *dbname) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100531 DB *db;
532 Options options;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100533
534
Marc Kupietz12af0192021-03-13 18:05:14 +0100535 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 Kupietz0dd86ef2018-01-11 22:23:17 +0100539 // options.prefix_extractor.reset(NewFixedPrefixTransform(8));
Marc Kupietz12af0192021-03-13 18:05:14 +0100540 options.IncreaseParallelism();
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100541 options.OptimizeLevelStyleCompaction();
542 // options.max_write_buffer_number = 48;
543 // options.max_background_jobs = 48;
544 // options.allow_concurrent_memtable_write=true;
Marc Kupietz12af0192021-03-13 18:05:14 +0100545 // 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 Kupietzc8ddf452018-01-07 21:33:12 +0100553 // BlockBasedTableOptions table_options;
554 // table_options.filter_policy.reset(NewBloomFilterPolicy(24, false));
Marc Kupietz12af0192021-03-13 18:05:14 +0100555 // options.bloom_locality = 1;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100556 // std::shared_ptr<Cache> cache = NewLRUCache(512 * 1024 * 1024);
557 // table_options.block_cache = cache;
Marc Kupietz12af0192021-03-13 18:05:14 +0100558 // 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 Kupietzb4a683c2021-03-14 09:19:44 +0100566 total = 1000;
Marc Kupietz12af0192021-03-13 18:05:14 +0100567 return std::shared_ptr<DB>(db);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100568 }
569
Marc Kupietz12af0192021-03-13 18:05:14 +0100570 CollocatorIterator *rocksdb::CollocatorDB::SeekIterator(uint64_t w1, uint64_t w2, int8_t dist) {
Marc Kupietz18375e12017-12-24 10:11:18 +0100571 ReadOptions options;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100572 options.prefix_same_as_start = true;
Marc Kupietz18375e12017-12-24 10:11:18 +0100573 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 Kupietz88d116b2021-03-13 18:05:14 +0100577 if (w2 > 0)
578 cit->Seek(std::string(prefixc, 6));
579 else
580 cit->Seek(std::string(prefixc, 3));
Marc Kupietz18375e12017-12-24 10:11:18 +0100581 cit->setPrefix(prefixc);
582 return cit;
583 }
584
Marc Kupietz12af0192021-03-13 18:05:14 +0100585 void rocksdb::CollocatorDB::dump(uint32_t w1, uint32_t w2, int8_t dist) {
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100586 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 Kupietz12af0192021-03-13 18:05:14 +0100590 std::cout << "w1:" << W1(key) << ", w2:" << W2(key) << ", dist:" << (int32_t) DIST(key) << " - count:" << value
591 << std::endl;
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100592 }
593 std::cout << "ready dumping\n";
594 }
595
Marc Kupietz12af0192021-03-13 18:05:14 +0100596 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 Kupietz3203e4c2019-02-04 12:42:45 +0100604 bool sortByLogDiceAF(const Collocator &lhs, const Collocator &rhs) { return lhs.ldaf > rhs.ldaf; }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100605
Marc Kupietz8c62c372019-01-31 12:21:01 +0100606
Marc Kupietz12af0192021-03-13 18:05:14 +0100607 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 Kupietz8c62c372019-01-31 12:21:01 +0100610 uint64_t f1 = _vocab[w1].freq, f2 = _vocab[w2].freq;
611 double o = sum,
Marc Kupietz12af0192021-03-13 18:05:14 +0100612 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 Kupietz8c62c372019-01-31 12:21:01 +0100620
621 int bestWindow = usedPositions;
622 double bestAF = ld;
623 double currentAF;
624 // if(f1<75000000)
625 //#pragma omp parallel for reduction(max:bestAF)
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100626 // #pragma omp target teams distribute parallel for reduction(max:bestAF) map(tofrom:bestAF,currentAF,bestWindow,usedPositions)
Marc Kupietz12af0192021-03-13 18:05:14 +0100627 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 Kupietz6d0fa542021-02-26 09:24:35 +0100630 // #pragma omp target teams distribute parallel for reduction(+:currentWindowSum) map(tofrom:bitmask,usedPositions)
Marc Kupietz12af0192021-03-13 18:05:14 +0100631 for (int pos = 0; pos < 2 * WINDOW_SIZE; pos++) {
632 if (((1 << pos) & bitmask & usedPositions) != 0)
633 currentWindowSum += sumWindow[pos];
Marc Kupietz8c62c372019-01-31 12:21:01 +0100634 }
635 currentAF = ca_logdice(f1, f2, currentWindowSum, total, __builtin_popcount(bitmask));
Marc Kupietz12af0192021-03-13 18:05:14 +0100636 if (currentAF > bestAF) {
Marc Kupietz8c62c372019-01-31 12:21:01 +0100637 bestAF = currentAF;
638 bestWindow = bitmask;
639 }
640 }
641
Marc Kupietz0421d092021-03-13 18:05:14 +0100642 *result = {w2,
643 f2,
Marc Kupietz12af0192021-03-13 18:05:14 +0100644 sum,
Marc Kupietz0421d092021-03-13 18:05:14 +0100645 pmi,
Marc Kupietz12af0192021-03-13 18:05:14 +0100646 pmi / (-log2(o / total / true_window_size)),
Marc Kupietz0421d092021-03-13 18:05:14 +0100647 llr,
Marc Kupietz12af0192021-03-13 18:05:14 +0100648 lfmd,
649 md,
Marc Kupietz0421d092021-03-13 18:05:14 +0100650 sumWindow[WINDOW_SIZE],
Marc Kupietz12af0192021-03-13 18:05:14 +0100651 sumWindow[WINDOW_SIZE - 1],
Marc Kupietz6d9221d2021-02-26 09:34:40 +0100652 ca_pmi(f1, f2, sumWindow[WINDOW_SIZE], total, 1),
Marc Kupietz12af0192021-03-13 18:05:14 +0100653 ca_pmi(f1, f2, sumWindow[WINDOW_SIZE - 1], total, 1),
Marc Kupietz8c62c372019-01-31 12:21:01 +0100654 ca_dice(f1, f2, sum, total, true_window_size),
655 ld,
656 bestAF,
657 usedPositions,
658 bestWindow
659 };
660
661 }
662
Marc Kupietz88d116b2021-03-13 18:05:14 +0100663 std::vector<Collocator> rocksdb::CollocatorDB::get_collocators(uint32_t w1, uint32_t min_w2, uint32_t max_w2) {
Marc Kupietz75af60f2019-01-22 22:34:29 +0100664 std::vector<Collocator> collocators;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100665 uint64_t w2, last_w2 = 0xffffffffffffffff;
Marc Kupietz8c62c372019-01-31 12:21:01 +0100666 uint64_t maxv = 0, sum = 0;
Marc Kupietz12af0192021-03-13 18:05:14 +0100667 uint64_t *sumWindow = (uint64_t *) malloc(sizeof(uint64_t) * 2 * WINDOW_SIZE);
668 memset(sumWindow, 0, sizeof(uint64_t) * 2 * WINDOW_SIZE);
Marc Kupietzade33222019-01-22 22:52:44 +0100669 int true_window_size = 1;
Marc Kupietz12af0192021-03-13 18:05:14 +0100670 int usedPositions = 0;
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100671
Marc Kupietz12af0192021-03-13 18:05:14 +0100672 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 Kupietzd31254c2018-01-20 21:29:30 +0100682 uint64_t value = it->intValue(),
Marc Kupietz12af0192021-03-13 18:05:14 +0100683 key = it->intKey();
684 if ((w2 = W2(key)) > max_w2)
Marc Kupietzbd966192018-10-13 14:14:37 +0200685 continue;
Marc Kupietz12af0192021-03-13 18:05:14 +0100686 if (last_w2 == 0xffffffffffffffff) last_w2 = w2;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100687 if (w2 != last_w2) {
Marc Kupietz75af60f2019-01-22 22:34:29 +0100688 if (sum >= FREQUENCY_THRESHOLD) {
Marc Kupietz8c62c372019-01-31 12:21:01 +0100689 collocators.push_back({});
Marc Kupietz12af0192021-03-13 18:05:14 +0100690 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 Kupietz8c62c372019-01-31 12:21:01 +0100692 {
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 Kupietz75af60f2019-01-22 22:34:29 +0100697 }
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100698 }
Marc Kupietz12af0192021-03-13 18:05:14 +0100699 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 Kupietzd31254c2018-01-20 21:29:30 +0100702 last_w2 = w2;
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100703 maxv = value;
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100704 sum = value;
Marc Kupietzade33222019-01-22 22:52:44 +0100705 true_window_size = 1;
Marc Kupietz12af0192021-03-13 18:05:14 +0100706 if (min_w2 == max_w2 && w2 != min_w2)
707 break;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100708 } else {
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100709 sum += value;
Marc Kupietz12af0192021-03-13 18:05:14 +0100710 if (value > maxv)
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100711 maxv = value;
Marc Kupietz12af0192021-03-13 18:05:14 +0100712 usedPositions |= 1 << (-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0));
713 sumWindow[-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0)] = value;
Marc Kupietzade33222019-01-22 22:52:44 +0100714 true_window_size++;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100715 }
716 }
717
Marc Kupietz12af0192021-03-13 18:05:14 +0100718 // #pragma omp taskwait
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100719 sort(collocators.begin(), collocators.end(), sortByLogDiceAF);
Marc Kupietz8c62c372019-01-31 12:21:01 +0100720
Marc Kupietz12af0192021-03-13 18:05:14 +0100721#ifdef DEBUG
Marc Kupietzd31254c2018-01-20 21:29:30 +0100722 int i=0;
723 for (Collocator c : collocators) {
724 if(i++>10) break;
Marc Kupietz8c62c372019-01-31 12:21:01 +0100725 std::cout << "w1:" << _vocab[w1].word << ", w2: *" << _vocab[c.w2].word << "*"
Marc Kupietzd31254c2018-01-20 21:29:30 +0100726 << "\t f(w1):" << _vocab[w1].freq
727 << "\t f(w2):" << _vocab[c.w2].freq
Marc Kupietz51f93792018-01-25 08:51:01 +0100728 << "\t f(w1, w2):" << c.raw
Marc Kupietzd31254c2018-01-20 21:29:30 +0100729 << "\t pmi:" << c.pmi
730 << "\t npmi:" << c.npmi
731 << "\t llr:" << c.llr
Marc Kupietz8c62c372019-01-31 12:21:01 +0100732 << "\t md:" << c.md
Marc Kupietzd31254c2018-01-20 21:29:30 +0100733 << "\t lfmd:" << c.lfmd
Marc Kupietzd31254c2018-01-20 21:29:30 +0100734 << "\t total:" << total
735 << std::endl;
736 }
Marc Kupietz12af0192021-03-13 18:05:14 +0100737#endif
Marc Kupietz8c62c372019-01-31 12:21:01 +0100738
739 return collocators;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100740 }
741
Marc Kupietz88d116b2021-03-13 18:05:14 +0100742
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 Kupietz8c62c372019-01-31 12:21:01 +0100747 std::vector<Collocator> rocksdb::CollocatorDB::get_collocators(uint32_t w1) {
Marc Kupietz88d116b2021-03-13 18:05:14 +0100748 return get_collocators(w1, 0, UINT32_MAX);
Marc Kupietzbd966192018-10-13 14:14:37 +0200749 }
750
Marc Kupietz3400aa52018-06-05 10:28:55 +0200751 void rocksdb::CollocatorDB::dumpSparseLlr(uint32_t w1, uint32_t min_cooccur) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100752 std::vector<Collocator> collocators;
Marc Kupietz3400aa52018-06-05 10:28:55 +0200753 std::stringstream stream;
754 uint64_t w2, last_w2 = 0xffffffffffffffff;
755 uint64_t maxv = 0, total_w1 = 0;
756 bool first = true;
Marc Kupietz12af0192021-03-13 18:05:14 +0100757 for (auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) {
Marc Kupietz3400aa52018-06-05 10:28:55 +0200758 uint64_t value = it->intValue(),
Marc Kupietz12af0192021-03-13 18:05:14 +0100759 key = it->intKey();
Marc Kupietz3400aa52018-06-05 10:28:55 +0200760 w2 = W2(key);
761 total_w1 += value;
Marc Kupietz12af0192021-03-13 18:05:14 +0100762 if (last_w2 == 0xffffffffffffffff) last_w2 = w2;
Marc Kupietz3400aa52018-06-05 10:28:55 +0200763 if (w2 != last_w2) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100764 if (maxv >= min_cooccur) {
765 double llr = ca_ll(_vocab[w1].freq, _vocab[last_w2].freq, maxv, total, 1);
766 if (first)
Marc Kupietz3400aa52018-06-05 10:28:55 +0200767 first = false;
768 else
Marc Kupietz12af0192021-03-13 18:05:14 +0100769 stream << " ";
770 stream << w2 << " " << llr;
Marc Kupietz3400aa52018-06-05 10:28:55 +0200771 }
772 last_w2 = w2;
773 maxv = value;
774 } else {
Marc Kupietz12af0192021-03-13 18:05:14 +0100775 if (value > maxv)
Marc Kupietz3400aa52018-06-05 10:28:55 +0200776 maxv = value;
777 }
778 }
Marc Kupietz12af0192021-03-13 18:05:14 +0100779 if (first)
780 stream << "1 0.0";
781 stream << "\n";
Marc Kupietz3400aa52018-06-05 10:28:55 +0200782 std::cout << stream.str();
783 }
784
Marc Kupietz4b799e92018-01-02 11:04:56 +0100785 rocksdb::Slice rocksdb::CollocatorIterator::key() const { return base_iterator_->key(); }
Marc Kupietz12af0192021-03-13 18:05:14 +0100786
Marc Kupietz4b799e92018-01-02 11:04:56 +0100787 rocksdb::Slice rocksdb::CollocatorIterator::value() const { return base_iterator_->value(); }
Marc Kupietz12af0192021-03-13 18:05:14 +0100788
Marc Kupietz4b799e92018-01-02 11:04:56 +0100789 rocksdb::Status rocksdb::CollocatorIterator::status() const { return base_iterator_->status(); }
790
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100791};
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100792
Marc Kupietz4a5e08a2018-06-05 11:07:11 +0200793string rocksdb::CollocatorDB::getWord(uint32_t w1) {
794 return _vocab[w1].word;
795}
796
Marc Kupietze9627152019-02-04 12:32:12 +0100797string rocksdb::CollocatorDB::collocators2json(uint32_t w1, vector<Collocator> collocators) {
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100798 ostringstream s;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100799 int i = 0;
Marc Kupietze9627152019-02-04 12:32:12 +0100800 s << " { \"f1\": " << _vocab[w1].freq << "," <<
801 "\"w1\":\"" << string(_vocab[w1].word) << "\", " <<
802 "\"N\": " << total << ", " <<
803 "\"collocates\": [";
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100804 bool first = true;
805 for (Collocator c : collocators) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100806 if (strncmp(_vocab[c.w2].word.c_str(), "quot", 4) == 0) continue;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100807 if (i++ > 200)
808 break;
Marc Kupietz12af0192021-03-13 18:05:14 +0100809 if (!first)
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100810 s << ",\n";
811 else
812 first = false;
813 s << "{"
Marc Kupietz12af0192021-03-13 18:05:14 +0100814 "\"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 Kupietz97f433b2021-03-13 18:10:52 +0100825 "\"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 Kupietze9f58932019-01-24 15:12:59 +0100830 "\"win\":" << c.window << "," <<
Marc Kupietz12af0192021-03-13 18:05:14 +0100831 "\"afwin\":" << c.af_window <<
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100832 "}";
833 }
Marc Kupietze9627152019-02-04 12:32:12 +0100834 s << "]}\n";
Marc Kupietz0421d092021-03-13 18:05:14 +0100835 // std::cout << s.str();
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100836 return s.str();
837}
838
Marc Kupietz6aec7682018-01-10 09:47:48 +0100839typedef rocksdb::CollocatorDB COLLOCATORS;
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100840
841extern "C" {
Marc Kupietz12af0192021-03-13 18:05:14 +0100842#ifdef __clang__
843#pragma clang diagnostic push
844#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
845#endif
Marc Kupietz44229232024-08-05 15:00:20 +0200846 DLL_EXPORT COLLOCATORS *open_collocatordb_for_write(char *dbname) {
Marc Kupietz6aec7682018-01-10 09:47:48 +0100847 return new rocksdb::CollocatorDB(dbname, false);
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100848 }
Marc Kupietz12af0192021-03-13 18:05:14 +0100849
Marc Kupietz44229232024-08-05 15:00:20 +0200850 DLL_EXPORT COLLOCATORS *open_collocatordb(char *dbname) {
Marc Kupietz6aec7682018-01-10 09:47:48 +0100851 return new rocksdb::CollocatorDB(dbname, true);
Marc Kupietz6bb27762018-01-09 17:53:01 +0100852 }
853
Marc Kupietz44229232024-08-05 15:00:20 +0200854 DLL_EXPORT void inc_collocator(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100855 db->inc(w1, w2, dist);
856 }
857
Marc Kupietz44229232024-08-05 15:00:20 +0200858 DLL_EXPORT void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100859 db->dump(w1, w2, dist);
860 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100861
Marc Kupietz44229232024-08-05 15:00:20 +0200862 DLL_EXPORT COLLOCATORS *get_collocators(COLLOCATORS *db, uint32_t w1) {
Marc Kupietz6663f112021-03-14 09:20:59 +0100863 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 Kupietz12af0192021-03-13 18:05:14 +0100870 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100871
Marc Kupietz44229232024-08-05 15:00:20 +0200872 DLL_EXPORT COLLOCATORS *get_collocation_scores(COLLOCATORS *db, uint32_t w1, uint32_t w2) {
Marc Kupietz6663f112021-03-14 09:20:59 +0100873 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 Kupietz12af0192021-03-13 18:05:14 +0100880 }
Marc Kupietz88d116b2021-03-13 18:05:14 +0100881
Marc Kupietz44229232024-08-05 15:00:20 +0200882 DLL_EXPORT char *get_word(COLLOCATORS *db, uint32_t w) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100883 return strdup(db->getWord(w).c_str());
884 }
Marc Kupietzca3a52e2018-06-05 14:16:23 +0200885
Marc Kupietz44229232024-08-05 15:00:20 +0200886 DLL_EXPORT void read_vocab(COLLOCATORS *db, char *fname) {
Marc Kupietzb4a683c2021-03-14 09:19:44 +0100887 std::string fName(fname);
888 db->readVocab(fName);
889 }
890
Marc Kupietz44229232024-08-05 15:00:20 +0200891 DLL_EXPORT const char *get_collocators_as_json(COLLOCATORS *db, uint32_t w1) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100892 return strdup(db->collocators2json(w1, db->get_collocators(w1)).c_str());
893 }
Marc Kupietz88d116b2021-03-13 18:05:14 +0100894
Marc Kupietz44229232024-08-05 15:00:20 +0200895 DLL_EXPORT const char *get_collocation_scores_as_json(COLLOCATORS *db, uint32_t w1, uint32_t w2) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100896 return strdup(db->collocators2json(w1, db->get_collocation_scores(w1, w2)).c_str());
897 }
Marc Kupietzb4a683c2021-03-14 09:19:44 +0100898
Marc Kupietz12af0192021-03-13 18:05:14 +0100899#ifdef __clang__
900#pragma clang diagnostic push
901#endif
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100902}