blob: c611994a06485b25984525a8342ffec757bf1af0 [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"
20
Marc Kupietz75af60f2019-01-22 22:34:29 +010021#define WINDOW_SIZE 5
Marc Kupietz98cbcdc2019-01-21 17:11:27 +010022#define FREQUENCY_THRESHOLD 5
Marc Kupietz28cc53e2017-12-23 17:24:55 +010023#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
24#define encodeCollocation(w1, w2, dist) (((uint64_t)dist << 56) | ((uint64_t)w2 << 24) | w1)
Marc Kupietz18375e12017-12-24 10:11:18 +010025#define W1(key) (uint64_t)(key & 0xffffff)
26#define W2(key) (uint64_t)((key >> 24) & 0xffffff)
27#define DIST(key) (int8_t)((uint64_t)((key >> 56) & 0xff))
Marc Kupietzc8ddf452018-01-07 21:33:12 +010028
29typedef struct {
30 uint64_t freq;
31 char *word;
Marc Kupietz12af0192021-03-13 18:05:14 +010032} vocab_entry;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010033
34// typedef struct Collocator {
35// uint64_t w2;
36// uint64_t sum;
37// };
38
Marc Kupietz28cc53e2017-12-23 17:24:55 +010039using namespace rocksdb;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010040using namespace std;
Marc Kupietz28cc53e2017-12-23 17:24:55 +010041
Marc Kupietz4b799e92018-01-02 11:04:56 +010042namespace rocksdb {
Marc Kupietz12af0192021-03-13 18:05:14 +010043 class Collocator {
44 public:
45 uint32_t w2;
46 uint64_t f2;
47 uint64_t raw;
48 double pmi;
49 double npmi;
50 double llr;
51 double lfmd;
52 double md;
53 uint64_t left_raw;
54 uint64_t right_raw;
55 double left_pmi;
56 double right_pmi;
57 double dice;
58 double logdice;
59 double ldaf;
60 int window;
61 int af_window;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010062 };
63
Marc Kupietz28cc53e2017-12-23 17:24:55 +010064 size_t num_merge_operator_calls;
Marc Kupietz12af0192021-03-13 18:05:14 +010065
Marc Kupietz28cc53e2017-12-23 17:24:55 +010066 void resetNumMergeOperatorCalls() { num_merge_operator_calls = 0; }
Marc Kupietzc8ddf452018-01-07 21:33:12 +010067
Marc Kupietz28cc53e2017-12-23 17:24:55 +010068 size_t num_partial_merge_calls;
Marc Kupietz12af0192021-03-13 18:05:14 +010069
Marc Kupietz28cc53e2017-12-23 17:24:55 +010070 void resetNumPartialMergeCalls() { num_partial_merge_calls = 0; }
Marc Kupietz28cc53e2017-12-23 17:24:55 +010071
72
Marc Kupietz12af0192021-03-13 18:05:14 +010073 inline void EncodeFixed64(char *buf, uint64_t value) {
74 if (!IS_BIG_ENDIAN) {
Marc Kupietz4b799e92018-01-02 11:04:56 +010075 memcpy(buf, &value, sizeof(value));
76 } else {
77 buf[0] = value & 0xff;
78 buf[1] = (value >> 8) & 0xff;
79 buf[2] = (value >> 16) & 0xff;
80 buf[3] = (value >> 24) & 0xff;
81 buf[4] = (value >> 32) & 0xff;
82 buf[5] = (value >> 40) & 0xff;
83 buf[6] = (value >> 48) & 0xff;
84 buf[7] = (value >> 56) & 0xff;
85 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +010086 }
87
Marc Kupietz12af0192021-03-13 18:05:14 +010088 inline uint32_t DecodeFixed32(const char *ptr) {
89 if (!IS_BIG_ENDIAN) {
Marc Kupietz4b799e92018-01-02 11:04:56 +010090 // Load the raw bytes
91 uint32_t result;
92 memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
93 return result;
94 } else {
95 return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0])))
96 | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8)
97 | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16)
98 | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24));
99 }
100 }
101
Marc Kupietz12af0192021-03-13 18:05:14 +0100102 inline uint64_t DecodeFixed64(const char *ptr) {
103 if (!IS_BIG_ENDIAN) {
Marc Kupietz4b799e92018-01-02 11:04:56 +0100104 // Load the raw bytes
105 uint64_t result;
106 memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
107 return result;
108 } else {
109 uint64_t lo = DecodeFixed32(ptr);
110 uint64_t hi = DecodeFixed32(ptr + 4);
111 return (hi << 32) | lo;
112 }
113 }
114
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100115 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 +0100116 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100117 r1 = f1 * window_size,
118 c1 = f2,
119 e = r1 * c1 / total,
120 o = f12;
121 if (f12 < FREQUENCY_THRESHOLD)
Marc Kupietzf4a649a2021-02-26 09:18:01 +0100122 return -1.0;
123 else
Marc Kupietz12af0192021-03-13 18:05:14 +0100124 return log2(o / e);
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100125 }
126
Marc Kupietzce0b8b02018-06-05 11:06:39 +0200127 // Bouma, Gerlof (2009): <a href="https://svn.spraakdata.gu.se/repos/gerlof/pub/www/Docs/npmi-pfd.pdf">
128 // Normalized (pointwise) mutual information in collocation extraction</a>. In Proceedings of GSCL.
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100129 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 +0100130 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100131 r1 = f1 * window_size,
132 c1 = f2,
133 e = r1 * c1 / total,
134 o = f12;
135 if (f12 < FREQUENCY_THRESHOLD)
Marc Kupietz8caf9912018-06-05 10:51:18 +0200136 return -1.0;
137 else
Marc Kupietz12af0192021-03-13 18:05:14 +0100138 return log2(o / e) / (-log2(o / total / window_size));
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100139 }
140
141 // Thanopoulos, A., Fakotakis, N., Kokkinakis, G.: Comparative evaluation of collocation extraction metrics.
142 // In: International Conference on Language Resources and Evaluation (LREC-2002). (2002) 620–625
143 // double md = log2(pow((double)max * window_size / total, 2) / (window_size * ((double)_vocab[w1].freq/total) * ((double)_vocab[last_w2].freq/total)));
144 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 +0100145 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100146 r1 = f1 * window_size,
147 c1 = f2,
148 e = r1 * c1 / total,
149 o = f12;
150 return log2(o * o / e);
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100151 }
152
153 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 +0100154 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100155 r1 = f1 * window_size,
156 c1 = f2,
157 e = r1 * c1 / total,
158 o = f12;
159 if (f12 == 0)
Marc Kupietz8caf9912018-06-05 10:51:18 +0200160 return 0;
161 else
Marc Kupietz12af0192021-03-13 18:05:14 +0100162 return log2(o * o * o / e);
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100163 }
164
Marc Kupietzbbd236e2019-01-21 16:50:19 +0100165 // 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.
166 // Free PDF available from http://purl.org/stefan.evert/PUB/Evert2004phd.pdf
167 static inline double ca_ll(uint64_t w1, uint64_t w2, uint64_t w12, uint64_t n, uint64_t window_size) {
168 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100169 r1 = (double) w1 * window_size,
170 r2 = (double) n - r1,
171 c1 = w2,
172 c2 = n - c1,
173 o11 = w12, o12 = r1 - o11,
174 o21 = c1 - w12, o22 = r2 - o21,
175 e11 = r1 * c1 / n, e12 = r1 * c2 / n,
176 e21 = r2 * c1 / n, e22 = r2 * c2 / n;
177 return (2 * ((o11 > 0 ? o11 * log(o11 / e11) : 0) + (o12 > 0 ? o12 * log(o12 / e12) : 0) +
178 (o21 > 0 ? o21 * log(o21 / e21) : 0) + (o22 > 0 ? o22 * log(o22 / e22) : 0)));
Marc Kupietzbbd236e2019-01-21 16:50:19 +0100179 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100180
Marc Kupietz41880452019-01-22 15:29:06 +0100181
182 static inline double ca_dice(uint64_t w1, uint64_t w2, uint64_t w12, uint64_t n, uint64_t window_size) {
183 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100184 r1 = (double) w1 * window_size,
185 c1 = w2;
186 return 2 * w12 / (c1 + r1);
Marc Kupietz41880452019-01-22 15:29:06 +0100187 }
188
189 // 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.
190 static inline double ca_logdice(uint64_t w1, uint64_t w2, uint64_t w12, uint64_t n, uint64_t window_size) {
191 double
Marc Kupietz12af0192021-03-13 18:05:14 +0100192 r1 = (double) w1 * window_size,
193 c1 = w2;
194 return 14 + log2(2 * w12 / (c1 + r1));
Marc Kupietz41880452019-01-22 15:29:06 +0100195 }
196
Marc Kupietz4b799e92018-01-02 11:04:56 +0100197 class CountMergeOperator : public AssociativeMergeOperator {
198 public:
Marc Kupietz12af0192021-03-13 18:05:14 +0100199 CountMergeOperator() {
200 mergeOperator_ = MergeOperators::CreateUInt64AddOperator();
201 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100202
Marc Kupietz12af0192021-03-13 18:05:14 +0100203 virtual bool Merge(const Slice &key,
204 const Slice *existing_value,
205 const Slice &value,
206 std::string *new_value,
207 Logger *logger) const override {
208 assert(new_value->empty());
209 ++num_merge_operator_calls;
210 if (existing_value == nullptr) {
211 new_value->assign(value.data(), value.size());
212 return true;
213 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100214
Marc Kupietz12af0192021-03-13 18:05:14 +0100215 return mergeOperator_->PartialMerge(
216 key,
217 *existing_value,
218 value,
219 new_value,
220 logger);
221 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100222
Marc Kupietz12af0192021-03-13 18:05:14 +0100223 virtual const char *Name() const override {
224 return "UInt64AddOperator";
225 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100226
Marc Kupietz4b799e92018-01-02 11:04:56 +0100227 private:
Marc Kupietz12af0192021-03-13 18:05:14 +0100228 std::shared_ptr<MergeOperator> mergeOperator_;
Marc Kupietz4b799e92018-01-02 11:04:56 +0100229 };
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100230
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100231
Marc Kupietz4b799e92018-01-02 11:04:56 +0100232 class CollocatorIterator : public Iterator {
233 private:
Marc Kupietz12af0192021-03-13 18:05:14 +0100234 char prefixc[sizeof(uint64_t)];
235 Iterator *base_iterator_;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100236
237
Marc Kupietz4b799e92018-01-02 11:04:56 +0100238 public:
Marc Kupietz12af0192021-03-13 18:05:14 +0100239 CollocatorIterator(Iterator *base_iterator)
240 : base_iterator_(base_iterator) {}
Marc Kupietz4b799e92018-01-02 11:04:56 +0100241
Marc Kupietz12af0192021-03-13 18:05:14 +0100242 void setPrefix(char *prefix) {
243 memcpy(prefixc, prefix, sizeof(uint64_t));
244 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100245
Marc Kupietz12af0192021-03-13 18:05:14 +0100246 virtual void SeekToFirst() { base_iterator_->SeekToFirst(); }
247
248 virtual void SeekToLast() { base_iterator_->SeekToLast(); }
249
250 virtual void Seek(const rocksdb::Slice &s) { base_iterator_->Seek(s); }
251
252 virtual void
253 SeekForPrev(const rocksdb::Slice &s) { base_iterator_->SeekForPrev(s); }
254
255 virtual void Prev() { base_iterator_->Prev(); }
256
257 virtual void Next() { base_iterator_->Next(); }
258
259 virtual Slice key() const;
260
261 virtual Slice value() const;
262
263 virtual Status status() const;
264
265 virtual bool Valid() const;
266
267 bool isValid();
268
269 uint64_t intValue();
270
271 uint64_t intKey();
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100272
Marc Kupietz4b799e92018-01-02 11:04:56 +0100273 };
Marc Kupietz18375e12017-12-24 10:11:18 +0100274
Marc Kupietz4b799e92018-01-02 11:04:56 +0100275 // rocksdb::CollocatorIterator::CollocatorIterator(Iterator* base_iterator) {}
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100276
Marc Kupietz4b799e92018-01-02 11:04:56 +0100277 bool rocksdb::CollocatorIterator::Valid() const {
Marc Kupietz12af0192021-03-13 18:05:14 +0100278 return base_iterator_->Valid() && key().starts_with(std::string(prefixc, 3));
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100279 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100280
Marc Kupietz4b799e92018-01-02 11:04:56 +0100281 bool rocksdb::CollocatorIterator::isValid() {
Marc Kupietz12af0192021-03-13 18:05:14 +0100282 return base_iterator_->Valid() && key().starts_with(std::string(prefixc, 3));
Marc Kupietzd31254c2018-01-20 21:29:30 +0100283 // return key().starts_with(std::string(prefixc,3));
Marc Kupietz4b799e92018-01-02 11:04:56 +0100284 }
Marc Kupietz18375e12017-12-24 10:11:18 +0100285
Marc Kupietz4b799e92018-01-02 11:04:56 +0100286 uint64_t rocksdb::CollocatorIterator::intKey() {
287 return DecodeFixed64(base_iterator_->key().data());
288 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100289
Marc Kupietz4b799e92018-01-02 11:04:56 +0100290 uint64_t rocksdb::CollocatorIterator::intValue() {
291 return DecodeFixed64(base_iterator_->value().data());
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100292 }
293
Marc Kupietz37359b12018-01-09 21:11:37 +0100294 class VocabEntry {
295 public:
Marc Kupietz12af0192021-03-13 18:05:14 +0100296 string word;
297 uint64_t freq;
Marc Kupietz37359b12018-01-09 21:11:37 +0100298 };
299
Marc Kupietz6aec7682018-01-10 09:47:48 +0100300 class CollocatorDB {
Marc Kupietz4b799e92018-01-02 11:04:56 +0100301 private:
Marc Kupietz12af0192021-03-13 18:05:14 +0100302 WriteOptions merge_option_; // for merge
303 char _one[sizeof(uint64_t)];
304 Slice _one_slice;
305 vector<VocabEntry> _vocab;
306 uint64_t total = 0;
307 uint64_t sentences = 0;
308 float avg_window_size = 8.0;
309
Marc Kupietz4b799e92018-01-02 11:04:56 +0100310 protected:
Marc Kupietz12af0192021-03-13 18:05:14 +0100311 std::shared_ptr<DB> db_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100312
Marc Kupietz12af0192021-03-13 18:05:14 +0100313 WriteOptions put_option_;
314 ReadOptions get_option_;
315 WriteOptions delete_option_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100316
Marc Kupietz12af0192021-03-13 18:05:14 +0100317 uint64_t default_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100318
Marc Kupietz12af0192021-03-13 18:05:14 +0100319 std::shared_ptr<DB> OpenDb(const char *dbname);
320
321 std::shared_ptr<DB> OpenDbForRead(const char *dbname);
322
323 void read_vocab(string fname);
324
Marc Kupietz4b799e92018-01-02 11:04:56 +0100325 public:
Marc Kupietz12af0192021-03-13 18:05:14 +0100326 string getWord(uint32_t w1);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100327
Marc Kupietz12af0192021-03-13 18:05:14 +0100328 CollocatorDB(const char *db_name, bool read_only);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100329
Marc Kupietz12af0192021-03-13 18:05:14 +0100330 // public interface of CollocatorDB.
331 // All four functions return false
332 // if the underlying level db operation failed.
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100333
Marc Kupietz12af0192021-03-13 18:05:14 +0100334 // mapped to a levedb Put
335 bool set(const std::string &key, uint64_t value) {
336 // just treat the internal rep of int64 as the string
337 char buf[sizeof(value)];
338 EncodeFixed64(buf, value);
339 Slice slice(buf, sizeof(value));
340 auto s = db_->Put(put_option_, key, slice);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100341
Marc Kupietz12af0192021-03-13 18:05:14 +0100342 if (s.ok()) {
343 return true;
344 } else {
345 std::cerr << s.ToString() << std::endl;
346 return false;
347 }
348 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100349
Marc Kupietz12af0192021-03-13 18:05:14 +0100350 DB *getDb() {
351 return db_.get();
352 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100353
Marc Kupietz12af0192021-03-13 18:05:14 +0100354 // mapped to a rocksdb Delete
355 bool remove(const std::string &key) {
356 auto s = db_->Delete(delete_option_, key);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100357
Marc Kupietz12af0192021-03-13 18:05:14 +0100358 if (s.ok()) {
359 return true;
360 } else {
361 std::cerr << s.ToString() << std::endl;
362 return false;
363 }
364 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100365
Marc Kupietz12af0192021-03-13 18:05:14 +0100366 // mapped to a rocksdb Get
367 bool get(const std::string &key, uint64_t *value) {
368 std::string str;
369 auto s = db_->Get(get_option_, key, &str);
370
371 if (s.IsNotFound()) {
372 // return default value if not found;
373 *value = default_;
374 return true;
375 } else if (s.ok()) {
376 // deserialization
377 if (str.size() != sizeof(uint64_t)) {
378 std::cerr << "value corruption\n";
379 return false;
380 }
381 *value = DecodeFixed64(&str[0]);
382 return true;
383 } else {
384 std::cerr << s.ToString() << std::endl;
385 return false;
386 }
387 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100388
389
Marc Kupietz12af0192021-03-13 18:05:14 +0100390 uint64_t get(const uint32_t w1, const uint32_t w2, const int8_t dist) {
391 char encoded_key[sizeof(uint64_t)];
392 EncodeFixed64(encoded_key, encodeCollocation(w1, w2, dist));
393 uint64_t value = default_;
394 get(std::string(encoded_key, 8), &value);
395 return value;
396 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100397
Marc Kupietz12af0192021-03-13 18:05:14 +0100398 virtual void inc(const std::string &key) {
399 db_->Merge(merge_option_, key, _one_slice);
400 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100401
Marc Kupietz12af0192021-03-13 18:05:14 +0100402 void inc(const uint64_t key) {
403 char encoded_key[sizeof(uint64_t)];
404 EncodeFixed64(encoded_key, key);
405 db_->Merge(merge_option_, std::string(encoded_key, 8), _one_slice);
406 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100407
Marc Kupietz12af0192021-03-13 18:05:14 +0100408 virtual void inc(const uint32_t w1, const uint32_t w2, const uint8_t dist);
Marc Kupietz8c62c372019-01-31 12:21:01 +0100409
Marc Kupietz12af0192021-03-13 18:05:14 +0100410 void dump(uint32_t w1, uint32_t w2, int8_t dist);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100411
Marc Kupietz12af0192021-03-13 18:05:14 +0100412 vector<Collocator> get_collocators(uint32_t w1);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100413
Marc Kupietz12af0192021-03-13 18:05:14 +0100414 vector<Collocator> get_collocators(uint32_t w1, uint32_t max_w2);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100415
Marc Kupietz12af0192021-03-13 18:05:14 +0100416 vector<Collocator> get_collocation_scores(uint32_t w1, uint32_t w2);
417
418 vector<Collocator>
419 get_collocators(uint32_t w1, uint32_t min_w2, uint32_t max_w2);
420
421 void
422 applyCAMeasures(const uint32_t w1, const uint32_t w2, uint64_t *sumWindow,
423 const uint64_t sum, const int usedPositions,
424 int true_window_size, rocksdb::Collocator *result);
425
426 void dumpSparseLlr(uint32_t w1, uint32_t min_cooccur);
427
428 string collocators2json(uint32_t w1, vector<Collocator> collocators);
429
430 // mapped to a rocksdb Merge operation
431 virtual bool add(const std::string &key, uint64_t value) {
432 char encoded[sizeof(uint64_t)];
433 EncodeFixed64(encoded, value);
434 Slice slice(encoded, sizeof(uint64_t));
435 auto s = db_->Merge(merge_option_, key, slice);
436
437 if (s.ok()) {
438 return true;
439 } else {
440 std::cerr << s.ToString() << std::endl;
441 return false;
442 }
443 }
444
445 CollocatorIterator *SeekIterator(uint64_t w1, uint64_t w2, int8_t dist);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100446 };
447
Marc Kupietz6aec7682018-01-10 09:47:48 +0100448 rocksdb::CollocatorDB::CollocatorDB(const char *db_name, bool read_only = false) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100449 // merge_option_.sync = true;
450 if (read_only)
Marc Kupietz88d116b2021-03-13 18:05:14 +0100451 db_ = OpenDbForRead(strdup(db_name));
Marc Kupietz6bb27762018-01-09 17:53:01 +0100452 else
453 db_ = OpenDb(db_name);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100454 assert(db_);
455 uint64_t one = 1;
456 EncodeFixed64(_one, one);
457 _one_slice = Slice(_one, sizeof(uint64_t));
458 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100459
Marc Kupietz6aec7682018-01-10 09:47:48 +0100460 void rocksdb::CollocatorDB::inc(const uint32_t w1, const uint32_t w2, const uint8_t dist) {
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100461 inc(encodeCollocation(w1, w2, dist));
462 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100463
Marc Kupietz6aec7682018-01-10 09:47:48 +0100464 void rocksdb::CollocatorDB::read_vocab(string fname) {
Marc Kupietz37359b12018-01-09 21:11:37 +0100465 char strbuf[2048];
466 uint64_t freq;
467 FILE *fin = fopen(fname.c_str(), "rb");
468 if (fin == NULL) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100469 cout << "Vocabulary file " << fname << " not found\n";
Marc Kupietz37359b12018-01-09 21:11:37 +0100470 exit(1);
471 }
472 uint64_t i = 0;
Marc Kupietz12af0192021-03-13 18:05:14 +0100473 while (!feof(fin)) {
Marc Kupietzd31254c2018-01-20 21:29:30 +0100474 fscanf(fin, "%s %lu", strbuf, &freq);
Marc Kupietz37359b12018-01-09 21:11:37 +0100475 _vocab.push_back({strbuf, freq});
476 total += freq;
477 i++;
478 }
479 fclose(fin);
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100480
481 char size_fname[256];
482 strcpy(size_fname, fname.c_str());
483 char *pos = strstr(size_fname, ".vocab");
Marc Kupietz12af0192021-03-13 18:05:14 +0100484 if (pos) {
485 *pos = 0;
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100486 strcat(size_fname, ".size");
487 FILE *fp = fopen(size_fname, "r");
488 if (fp != NULL) {
489 fscanf(fp, "%lu", &sentences);
490 fscanf(fp, "%lu", &total);
Marc Kupietz12af0192021-03-13 18:05:14 +0100491 float sl = (float) total / (float) sentences;
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100492 float w = WINDOW_SIZE;
Marc Kupietz12af0192021-03-13 18:05:14 +0100493 avg_window_size = ((sl > 2 * w ? (sl - 2 * w) * 2 * w : 0) + (double) w * (3 * w - 1)) / sl;
494 fprintf(stdout,
495 "Size corrections found: corpus size: %lu tokens in %lu sentences, avg. sentence size: %f, avg. window size: %f\n",
496 total, sentences, sl, avg_window_size);
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100497 fclose(fp);
498 } else {
Marc Kupietz12af0192021-03-13 18:05:14 +0100499 std::cout << "size file " << size_fname << " not found\n";
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100500 }
501 } else {
Marc Kupietz12af0192021-03-13 18:05:14 +0100502 std::cout << "cannot determine size file " << size_fname << "\n";
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100503 }
Marc Kupietz37359b12018-01-09 21:11:37 +0100504 }
505
Marc Kupietz6aec7682018-01-10 09:47:48 +0100506 std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDbForRead(const char *name) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100507 DB *db;
508 Options options;
509 options.env->SetBackgroundThreads(4);
510 options.create_if_missing = true;
511 options.merge_operator = std::make_shared<CountMergeOperator>();
512 options.max_successive_merges = 0;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100513 // options.prefix_extractor.reset(NewFixedPrefixTransform(8));
Marc Kupietz12af0192021-03-13 18:05:14 +0100514 options.IncreaseParallelism();
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100515 options.OptimizeLevelStyleCompaction();
516 options.prefix_extractor.reset(NewFixedPrefixTransform(3));
Marc Kupietz37359b12018-01-09 21:11:37 +0100517 ostringstream dbname, vocabname;
Marc Kupietz6bb27762018-01-09 17:53:01 +0100518 dbname << name << ".rocksdb";
Marc Kupietz12af0192021-03-13 18:05:14 +0100519 auto s = DB::OpenForReadOnly(options, dbname.str(), &db);
520 if (!s.ok()) {
521 std::cerr << s.ToString() << std::endl;
522 assert(false);
523 }
Marc Kupietz37359b12018-01-09 21:11:37 +0100524 vocabname << name << ".vocab";
525 read_vocab(vocabname.str());
Marc Kupietz12af0192021-03-13 18:05:14 +0100526 return std::shared_ptr<DB>(db);
Marc Kupietz6bb27762018-01-09 17:53:01 +0100527 }
528
Marc Kupietz6aec7682018-01-10 09:47:48 +0100529 std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDb(const char *dbname) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100530 DB *db;
531 Options options;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100532
533
Marc Kupietz12af0192021-03-13 18:05:14 +0100534 options.env->SetBackgroundThreads(4);
535 options.create_if_missing = true;
536 options.merge_operator = std::make_shared<CountMergeOperator>();
537 options.max_successive_merges = 0;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100538 // options.prefix_extractor.reset(NewFixedPrefixTransform(8));
Marc Kupietz12af0192021-03-13 18:05:14 +0100539 options.IncreaseParallelism();
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100540 options.OptimizeLevelStyleCompaction();
541 // options.max_write_buffer_number = 48;
542 // options.max_background_jobs = 48;
543 // options.allow_concurrent_memtable_write=true;
Marc Kupietz12af0192021-03-13 18:05:14 +0100544 // options.memtable_factory.reset(rocksdb::NewHashLinkListRepFactory(200000));
545 // options.enable_write_thread_adaptive_yield = 1;
546 // options.allow_concurrent_memtable_write = 1;
547 // options.memtable_factory.reset(new rocksdb::SkipListFactory);
548 // options.write_buffer_size = 1 << 22;
549 // options.allow_mmap_reads = true;
550 // options.allow_mmap_writes = true;
551 // options.max_background_compactions = 40;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100552 // BlockBasedTableOptions table_options;
553 // table_options.filter_policy.reset(NewBloomFilterPolicy(24, false));
Marc Kupietz12af0192021-03-13 18:05:14 +0100554 // options.bloom_locality = 1;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100555 // std::shared_ptr<Cache> cache = NewLRUCache(512 * 1024 * 1024);
556 // table_options.block_cache = cache;
Marc Kupietz12af0192021-03-13 18:05:14 +0100557 // options.table_factory.reset(NewBlockBasedTableFactory(table_options));
558 Status s;
559 // DestroyDB(dbname, Options());
560 s = DB::Open(options, dbname, &db);
561 if (!s.ok()) {
562 std::cerr << s.ToString() << std::endl;
563 assert(false);
564 }
565 return std::shared_ptr<DB>(db);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100566 }
567
Marc Kupietz12af0192021-03-13 18:05:14 +0100568 CollocatorIterator *rocksdb::CollocatorDB::SeekIterator(uint64_t w1, uint64_t w2, int8_t dist) {
Marc Kupietz18375e12017-12-24 10:11:18 +0100569 ReadOptions options;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100570 options.prefix_same_as_start = true;
Marc Kupietz18375e12017-12-24 10:11:18 +0100571 char prefixc[sizeof(uint64_t)];
572 EncodeFixed64(prefixc, encodeCollocation(w1, w2, dist));
573 Iterator *it = db_->NewIterator(options);
574 CollocatorIterator *cit = new CollocatorIterator(it);
Marc Kupietz88d116b2021-03-13 18:05:14 +0100575 if (w2 > 0)
576 cit->Seek(std::string(prefixc, 6));
577 else
578 cit->Seek(std::string(prefixc, 3));
Marc Kupietz18375e12017-12-24 10:11:18 +0100579 cit->setPrefix(prefixc);
580 return cit;
581 }
582
Marc Kupietz12af0192021-03-13 18:05:14 +0100583 void rocksdb::CollocatorDB::dump(uint32_t w1, uint32_t w2, int8_t dist) {
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100584 auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, w2, dist));
585 for (; it->isValid(); it->Next()) {
586 uint64_t value = it->intValue();
587 uint64_t key = it->intKey();
Marc Kupietz12af0192021-03-13 18:05:14 +0100588 std::cout << "w1:" << W1(key) << ", w2:" << W2(key) << ", dist:" << (int32_t) DIST(key) << " - count:" << value
589 << std::endl;
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100590 }
591 std::cout << "ready dumping\n";
592 }
593
Marc Kupietz12af0192021-03-13 18:05:14 +0100594 bool sortByNpmi(const Collocator &lhs, const Collocator &rhs) { return lhs.npmi > rhs.npmi; }
595
596 bool sortByLfmd(const Collocator &lhs, const Collocator &rhs) { return lhs.lfmd > rhs.lfmd; }
597
598 bool sortByLlr(const Collocator &lhs, const Collocator &rhs) { return lhs.llr > rhs.llr; }
599
600 bool sortByLogDice(const Collocator &lhs, const Collocator &rhs) { return lhs.logdice > rhs.logdice; }
601
Marc Kupietz3203e4c2019-02-04 12:42:45 +0100602 bool sortByLogDiceAF(const Collocator &lhs, const Collocator &rhs) { return lhs.ldaf > rhs.ldaf; }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100603
Marc Kupietz8c62c372019-01-31 12:21:01 +0100604
Marc Kupietz12af0192021-03-13 18:05:14 +0100605 void rocksdb::CollocatorDB::applyCAMeasures(const uint32_t w1, const uint32_t w2, uint64_t *sumWindow,
606 const uint64_t sum, const int usedPositions, int true_window_size,
607 rocksdb::Collocator *result) {
Marc Kupietz8c62c372019-01-31 12:21:01 +0100608 uint64_t f1 = _vocab[w1].freq, f2 = _vocab[w2].freq;
609 double o = sum,
Marc Kupietz12af0192021-03-13 18:05:14 +0100610 r1 = f1 * true_window_size,
611 c1 = f2,
612 e = r1 * c1 / total,
613 pmi = log2(o / e),
614 md = log2(o * o / e),
615 lfmd = log2(o * o * o / e),
616 llr = ca_ll(f1, f2, sum, total, true_window_size);
617 double ld = ca_logdice(f1, f2, sum, total, true_window_size);
Marc Kupietz8c62c372019-01-31 12:21:01 +0100618
619 int bestWindow = usedPositions;
620 double bestAF = ld;
621 double currentAF;
622 // if(f1<75000000)
623 //#pragma omp parallel for reduction(max:bestAF)
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100624 // #pragma omp target teams distribute parallel for reduction(max:bestAF) map(tofrom:bestAF,currentAF,bestWindow,usedPositions)
Marc Kupietz12af0192021-03-13 18:05:14 +0100625 for (int bitmask = 1; bitmask < (1 << (2 * WINDOW_SIZE)); bitmask++) {
626 if ((bitmask & usedPositions) == 0 || (bitmask & ~usedPositions) > 0) continue;
627 uint64_t currentWindowSum = 0;
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100628 // #pragma omp target teams distribute parallel for reduction(+:currentWindowSum) map(tofrom:bitmask,usedPositions)
Marc Kupietz12af0192021-03-13 18:05:14 +0100629 for (int pos = 0; pos < 2 * WINDOW_SIZE; pos++) {
630 if (((1 << pos) & bitmask & usedPositions) != 0)
631 currentWindowSum += sumWindow[pos];
Marc Kupietz8c62c372019-01-31 12:21:01 +0100632 }
633 currentAF = ca_logdice(f1, f2, currentWindowSum, total, __builtin_popcount(bitmask));
Marc Kupietz12af0192021-03-13 18:05:14 +0100634 if (currentAF > bestAF) {
Marc Kupietz8c62c372019-01-31 12:21:01 +0100635 bestAF = currentAF;
636 bestWindow = bitmask;
637 }
638 }
639
Marc Kupietz0421d092021-03-13 18:05:14 +0100640 *result = {w2,
641 f2,
Marc Kupietz12af0192021-03-13 18:05:14 +0100642 sum,
Marc Kupietz0421d092021-03-13 18:05:14 +0100643 pmi,
Marc Kupietz12af0192021-03-13 18:05:14 +0100644 pmi / (-log2(o / total / true_window_size)),
Marc Kupietz0421d092021-03-13 18:05:14 +0100645 llr,
Marc Kupietz12af0192021-03-13 18:05:14 +0100646 lfmd,
647 md,
Marc Kupietz0421d092021-03-13 18:05:14 +0100648 sumWindow[WINDOW_SIZE],
Marc Kupietz12af0192021-03-13 18:05:14 +0100649 sumWindow[WINDOW_SIZE - 1],
Marc Kupietz6d9221d2021-02-26 09:34:40 +0100650 ca_pmi(f1, f2, sumWindow[WINDOW_SIZE], total, 1),
Marc Kupietz12af0192021-03-13 18:05:14 +0100651 ca_pmi(f1, f2, sumWindow[WINDOW_SIZE - 1], total, 1),
Marc Kupietz8c62c372019-01-31 12:21:01 +0100652 ca_dice(f1, f2, sum, total, true_window_size),
653 ld,
654 bestAF,
655 usedPositions,
656 bestWindow
657 };
658
659 }
660
Marc Kupietz88d116b2021-03-13 18:05:14 +0100661 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 +0100662 std::vector<Collocator> collocators;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100663 uint64_t w2, last_w2 = 0xffffffffffffffff;
Marc Kupietz8c62c372019-01-31 12:21:01 +0100664 uint64_t maxv = 0, sum = 0;
Marc Kupietz12af0192021-03-13 18:05:14 +0100665 uint64_t *sumWindow = (uint64_t *) malloc(sizeof(uint64_t) * 2 * WINDOW_SIZE);
666 memset(sumWindow, 0, sizeof(uint64_t) * 2 * WINDOW_SIZE);
Marc Kupietzade33222019-01-22 22:52:44 +0100667 int true_window_size = 1;
Marc Kupietz12af0192021-03-13 18:05:14 +0100668 int usedPositions = 0;
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100669
Marc Kupietz12af0192021-03-13 18:05:14 +0100670 if (w1 > _vocab.size()) {
671 std::cout << w1 << "> vocabulary size " << _vocab.size() << "\n";
672 w1 -= _vocab.size();
673 }
674#ifdef DEBUG
675 std::cout << "Searching for collocates of " << _vocab[w1].word << "\n";
676#endif
677 // #pragma omp parallel num_threads(40)
678 // #pragma omp single
679 for (auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, min_w2, 0)); it->isValid(); it->Next()) {
Marc Kupietzd31254c2018-01-20 21:29:30 +0100680 uint64_t value = it->intValue(),
Marc Kupietz12af0192021-03-13 18:05:14 +0100681 key = it->intKey();
682 if ((w2 = W2(key)) > max_w2)
Marc Kupietzbd966192018-10-13 14:14:37 +0200683 continue;
Marc Kupietz12af0192021-03-13 18:05:14 +0100684 if (last_w2 == 0xffffffffffffffff) last_w2 = w2;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100685 if (w2 != last_w2) {
Marc Kupietz75af60f2019-01-22 22:34:29 +0100686 if (sum >= FREQUENCY_THRESHOLD) {
Marc Kupietz8c62c372019-01-31 12:21:01 +0100687 collocators.push_back({});
Marc Kupietz12af0192021-03-13 18:05:14 +0100688 rocksdb::Collocator *result = &(collocators[collocators.size() - 1]);
689 // #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 +0100690 {
691 // uint64_t *nsw = (uint64_t *)malloc(sizeof(uint64_t) * 2 *WINDOW_SIZE);
692 // memcpy(nsw, sumWindow, sizeof(uint64_t) * 2 *WINDOW_SIZE);
693 applyCAMeasures(w1, last_w2, sumWindow, sum, usedPositions, true_window_size, result);
694 // free(nsw);
Marc Kupietz75af60f2019-01-22 22:34:29 +0100695 }
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100696 }
Marc Kupietz12af0192021-03-13 18:05:14 +0100697 memset(sumWindow, 0, 2 * WINDOW_SIZE * sizeof(uint64_t));
698 usedPositions = 1 << (-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0));
699 sumWindow[-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0)] = value;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100700 last_w2 = w2;
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100701 maxv = value;
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100702 sum = value;
Marc Kupietzade33222019-01-22 22:52:44 +0100703 true_window_size = 1;
Marc Kupietz12af0192021-03-13 18:05:14 +0100704 if (min_w2 == max_w2 && w2 != min_w2)
705 break;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100706 } else {
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100707 sum += value;
Marc Kupietz12af0192021-03-13 18:05:14 +0100708 if (value > maxv)
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100709 maxv = value;
Marc Kupietz12af0192021-03-13 18:05:14 +0100710 usedPositions |= 1 << (-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0));
711 sumWindow[-DIST(key) + WINDOW_SIZE - (DIST(key) < 0 ? 1 : 0)] = value;
Marc Kupietzade33222019-01-22 22:52:44 +0100712 true_window_size++;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100713 }
714 }
715
Marc Kupietz12af0192021-03-13 18:05:14 +0100716 // #pragma omp taskwait
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100717 sort(collocators.begin(), collocators.end(), sortByLogDiceAF);
Marc Kupietz8c62c372019-01-31 12:21:01 +0100718
Marc Kupietz12af0192021-03-13 18:05:14 +0100719#ifdef DEBUG
Marc Kupietzd31254c2018-01-20 21:29:30 +0100720 int i=0;
721 for (Collocator c : collocators) {
722 if(i++>10) break;
Marc Kupietz8c62c372019-01-31 12:21:01 +0100723 std::cout << "w1:" << _vocab[w1].word << ", w2: *" << _vocab[c.w2].word << "*"
Marc Kupietzd31254c2018-01-20 21:29:30 +0100724 << "\t f(w1):" << _vocab[w1].freq
725 << "\t f(w2):" << _vocab[c.w2].freq
Marc Kupietz51f93792018-01-25 08:51:01 +0100726 << "\t f(w1, w2):" << c.raw
Marc Kupietzd31254c2018-01-20 21:29:30 +0100727 << "\t pmi:" << c.pmi
728 << "\t npmi:" << c.npmi
729 << "\t llr:" << c.llr
Marc Kupietz8c62c372019-01-31 12:21:01 +0100730 << "\t md:" << c.md
Marc Kupietzd31254c2018-01-20 21:29:30 +0100731 << "\t lfmd:" << c.lfmd
Marc Kupietzd31254c2018-01-20 21:29:30 +0100732 << "\t total:" << total
733 << std::endl;
734 }
Marc Kupietz12af0192021-03-13 18:05:14 +0100735#endif
Marc Kupietz8c62c372019-01-31 12:21:01 +0100736
737 return collocators;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100738 }
739
Marc Kupietz88d116b2021-03-13 18:05:14 +0100740
741 std::vector<Collocator> rocksdb::CollocatorDB::get_collocation_scores(uint32_t w1, uint32_t w2) {
742 return get_collocators(w1, w2, w2);
743 }
744
Marc Kupietz8c62c372019-01-31 12:21:01 +0100745 std::vector<Collocator> rocksdb::CollocatorDB::get_collocators(uint32_t w1) {
Marc Kupietz88d116b2021-03-13 18:05:14 +0100746 return get_collocators(w1, 0, UINT32_MAX);
Marc Kupietzbd966192018-10-13 14:14:37 +0200747 }
748
Marc Kupietz3400aa52018-06-05 10:28:55 +0200749 void rocksdb::CollocatorDB::dumpSparseLlr(uint32_t w1, uint32_t min_cooccur) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100750 std::vector<Collocator> collocators;
Marc Kupietz3400aa52018-06-05 10:28:55 +0200751 std::stringstream stream;
752 uint64_t w2, last_w2 = 0xffffffffffffffff;
753 uint64_t maxv = 0, total_w1 = 0;
754 bool first = true;
Marc Kupietz12af0192021-03-13 18:05:14 +0100755 for (auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) {
Marc Kupietz3400aa52018-06-05 10:28:55 +0200756 uint64_t value = it->intValue(),
Marc Kupietz12af0192021-03-13 18:05:14 +0100757 key = it->intKey();
Marc Kupietz3400aa52018-06-05 10:28:55 +0200758 w2 = W2(key);
759 total_w1 += value;
Marc Kupietz12af0192021-03-13 18:05:14 +0100760 if (last_w2 == 0xffffffffffffffff) last_w2 = w2;
Marc Kupietz3400aa52018-06-05 10:28:55 +0200761 if (w2 != last_w2) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100762 if (maxv >= min_cooccur) {
763 double llr = ca_ll(_vocab[w1].freq, _vocab[last_w2].freq, maxv, total, 1);
764 if (first)
Marc Kupietz3400aa52018-06-05 10:28:55 +0200765 first = false;
766 else
Marc Kupietz12af0192021-03-13 18:05:14 +0100767 stream << " ";
768 stream << w2 << " " << llr;
Marc Kupietz3400aa52018-06-05 10:28:55 +0200769 }
770 last_w2 = w2;
771 maxv = value;
772 } else {
Marc Kupietz12af0192021-03-13 18:05:14 +0100773 if (value > maxv)
Marc Kupietz3400aa52018-06-05 10:28:55 +0200774 maxv = value;
775 }
776 }
Marc Kupietz12af0192021-03-13 18:05:14 +0100777 if (first)
778 stream << "1 0.0";
779 stream << "\n";
Marc Kupietz3400aa52018-06-05 10:28:55 +0200780 std::cout << stream.str();
781 }
782
Marc Kupietz4b799e92018-01-02 11:04:56 +0100783 rocksdb::Slice rocksdb::CollocatorIterator::key() const { return base_iterator_->key(); }
Marc Kupietz12af0192021-03-13 18:05:14 +0100784
Marc Kupietz4b799e92018-01-02 11:04:56 +0100785 rocksdb::Slice rocksdb::CollocatorIterator::value() const { return base_iterator_->value(); }
Marc Kupietz12af0192021-03-13 18:05:14 +0100786
Marc Kupietz4b799e92018-01-02 11:04:56 +0100787 rocksdb::Status rocksdb::CollocatorIterator::status() const { return base_iterator_->status(); }
788
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100789};
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100790
Marc Kupietz4a5e08a2018-06-05 11:07:11 +0200791string rocksdb::CollocatorDB::getWord(uint32_t w1) {
792 return _vocab[w1].word;
793}
794
Marc Kupietze9627152019-02-04 12:32:12 +0100795string rocksdb::CollocatorDB::collocators2json(uint32_t w1, vector<Collocator> collocators) {
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100796 ostringstream s;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100797 int i = 0;
Marc Kupietze9627152019-02-04 12:32:12 +0100798 s << " { \"f1\": " << _vocab[w1].freq << "," <<
799 "\"w1\":\"" << string(_vocab[w1].word) << "\", " <<
800 "\"N\": " << total << ", " <<
801 "\"collocates\": [";
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100802 bool first = true;
803 for (Collocator c : collocators) {
Marc Kupietz12af0192021-03-13 18:05:14 +0100804 if (strncmp(_vocab[c.w2].word.c_str(), "quot", 4) == 0) continue;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100805 if (i++ > 200)
806 break;
Marc Kupietz12af0192021-03-13 18:05:14 +0100807 if (!first)
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100808 s << ",\n";
809 else
810 first = false;
811 s << "{"
Marc Kupietz12af0192021-03-13 18:05:14 +0100812 "\"word\":\"" << (string(_vocab[c.w2].word).compare("<num>") == 0 ? string("###") : string(_vocab[c.w2].word))
813 << "\"," <<
814 "\"f2\":" << c.f2 << "," <<
815 "\"f\":" << c.raw << "," <<
816 "\"npmi\":" << c.npmi << "," <<
817 "\"pmi\":" << c.pmi << "," <<
818 "\"llr\":" << c.llr << "," <<
819 "\"lfmd\":" << c.lfmd << "," <<
820 "\"md\":" << c.md << "," <<
821 "\"dice\":" << c.dice << "," <<
822 "\"ld\":" << c.logdice << "," <<
Marc Kupietz97f433b2021-03-13 18:10:52 +0100823 "\"ln_count\":" << c.left_raw << "," <<
824 "\"rn_count\":" << c.right_raw << "," <<
825 "\"ln_pmi\":" << c.left_pmi << "," <<
826 "\"rn_pmi\":" << c.right_pmi << "," <<
827 "\"ldaf\":" << c.ldaf << "," <<
Marc Kupietze9f58932019-01-24 15:12:59 +0100828 "\"win\":" << c.window << "," <<
Marc Kupietz12af0192021-03-13 18:05:14 +0100829 "\"afwin\":" << c.af_window <<
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100830 "}";
831 }
Marc Kupietze9627152019-02-04 12:32:12 +0100832 s << "]}\n";
Marc Kupietz0421d092021-03-13 18:05:14 +0100833 // std::cout << s.str();
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100834 return s.str();
835}
836
Marc Kupietz6aec7682018-01-10 09:47:48 +0100837typedef rocksdb::CollocatorDB COLLOCATORS;
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100838
839extern "C" {
Marc Kupietz12af0192021-03-13 18:05:14 +0100840#ifdef __clang__
841#pragma clang diagnostic push
842#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
843#endif
844 COLLOCATORS *open_collocatordb_for_write(char *dbname) {
Marc Kupietz6aec7682018-01-10 09:47:48 +0100845 return new rocksdb::CollocatorDB(dbname, false);
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100846 }
Marc Kupietz12af0192021-03-13 18:05:14 +0100847
Marc Kupietz6aec7682018-01-10 09:47:48 +0100848 COLLOCATORS *open_collocatordb(char *dbname) {
849 return new rocksdb::CollocatorDB(dbname, true);
Marc Kupietz6bb27762018-01-09 17:53:01 +0100850 }
851
Marc Kupietz6aec7682018-01-10 09:47:48 +0100852 void inc_collocator(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100853 db->inc(w1, w2, dist);
854 }
855
Marc Kupietz12af0192021-03-13 18:05:14 +0100856 void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
857 db->dump(w1, w2, dist);
858 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100859
Marc Kupietz12af0192021-03-13 18:05:14 +0100860 void get_collocators(COLLOCATORS *db, uint32_t w1) {
861 db->get_collocators(w1);
862 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100863
Marc Kupietz12af0192021-03-13 18:05:14 +0100864 void get_collocation_scores(COLLOCATORS *db, uint32_t w1, uint32_t w2) {
865 db->get_collocation_scores(w1, w2);
866 }
Marc Kupietz88d116b2021-03-13 18:05:14 +0100867
Marc Kupietz12af0192021-03-13 18:05:14 +0100868 const char *get_word(COLLOCATORS *db, uint32_t w) {
869 return strdup(db->getWord(w).c_str());
870 }
Marc Kupietzca3a52e2018-06-05 14:16:23 +0200871
Marc Kupietz12af0192021-03-13 18:05:14 +0100872 const char *get_collocators_as_json(COLLOCATORS *db, uint32_t w1) {
873 return strdup(db->collocators2json(w1, db->get_collocators(w1)).c_str());
874 }
Marc Kupietz88d116b2021-03-13 18:05:14 +0100875
Marc Kupietz12af0192021-03-13 18:05:14 +0100876 const char *get_collocation_scores_as_json(COLLOCATORS *db, uint32_t w1, uint32_t w2) {
877 return strdup(db->collocators2json(w1, db->get_collocation_scores(w1, w2)).c_str());
878 }
879#ifdef __clang__
880#pragma clang diagnostic push
881#endif
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100882}