blob: ce266932d537e06972bd877b1726cdffb28b5b05 [file] [log] [blame]
Marc Kupietz4b799e92018-01-02 11:04:56 +01001#define EXPORT __attribute__((visibility("visible")))
2#define IMPORT
Marc Kupietz28cc53e2017-12-23 17:24:55 +01003#include <assert.h>
4#include <memory>
5#include <iostream>
Marc Kupietzc8ddf452018-01-07 21:33:12 +01006#include <algorithm>
7#include <vector>
Marc Kupietz28cc53e2017-12-23 17:24:55 +01008#include <stdint.h>
Marc Kupietzc8ddf452018-01-07 21:33:12 +01009#include <string>
10#include <sstream> // for ostringstream
11#include <math.h>
Marc Kupietzd31254c2018-01-20 21:29:30 +010012#include <rocksdb/cache.h>
Marc Kupietz28cc53e2017-12-23 17:24:55 +010013#include "rocksdb/db.h"
14#include "rocksdb/env.h"
Marc Kupietzc8ddf452018-01-07 21:33:12 +010015#include "rocksdb/table.h"
Marc Kupietz28cc53e2017-12-23 17:24:55 +010016#include <rocksdb/merge_operator.h>
Marc Kupietzc8ddf452018-01-07 21:33:12 +010017#include <rocksdb/slice_transform.h>
Marc Kupietz28cc53e2017-12-23 17:24:55 +010018#include "merge_operators.h"
19
Marc Kupietz75af60f2019-01-22 22:34:29 +010020#define WINDOW_SIZE 5
Marc Kupietz98cbcdc2019-01-21 17:11:27 +010021#define FREQUENCY_THRESHOLD 5
Marc Kupietz28cc53e2017-12-23 17:24:55 +010022#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
23#define encodeCollocation(w1, w2, dist) (((uint64_t)dist << 56) | ((uint64_t)w2 << 24) | w1)
Marc Kupietz18375e12017-12-24 10:11:18 +010024#define W1(key) (uint64_t)(key & 0xffffff)
25#define W2(key) (uint64_t)((key >> 24) & 0xffffff)
26#define DIST(key) (int8_t)((uint64_t)((key >> 56) & 0xff))
Marc Kupietzc8ddf452018-01-07 21:33:12 +010027
28typedef struct {
29 uint64_t freq;
30 char *word;
31} vocab_entry;
32
33// typedef struct Collocator {
34// uint64_t w2;
35// uint64_t sum;
36// };
37
Marc Kupietz28cc53e2017-12-23 17:24:55 +010038using namespace rocksdb;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010039using namespace std;
Marc Kupietz28cc53e2017-12-23 17:24:55 +010040
Marc Kupietz4b799e92018-01-02 11:04:56 +010041namespace rocksdb {
Marc Kupietz4a5e08a2018-06-05 11:07:11 +020042 class Collocator {
43 public:
Marc Kupietz8c62c372019-01-31 12:21:01 +010044 uint32_t w2;
Marc Kupietzcc6c4592019-01-23 10:11:23 +010045 uint64_t f2;
Marc Kupietz51f93792018-01-25 08:51:01 +010046 uint64_t raw;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010047 double pmi;
48 double npmi;
49 double llr;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010050 double lfmd;
Marc Kupietz41880452019-01-22 15:29:06 +010051 double md;
Marc Kupietz8e0ebea2018-01-24 09:53:26 +010052 double left_lfmd;
53 double right_lfmd;
54 double left_npmi;
55 double right_npmi;
Marc Kupietz41880452019-01-22 15:29:06 +010056 double dice;
57 double logdice;
Marc Kupietz3203e4c2019-02-04 12:42:45 +010058 double ldaf;
Marc Kupietz75af60f2019-01-22 22:34:29 +010059 int window;
Marc Kupietze9f58932019-01-24 15:12:59 +010060 int af_window;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010061 };
62
Marc Kupietz28cc53e2017-12-23 17:24:55 +010063 size_t num_merge_operator_calls;
64 void resetNumMergeOperatorCalls() { num_merge_operator_calls = 0; }
Marc Kupietzc8ddf452018-01-07 21:33:12 +010065
Marc Kupietz28cc53e2017-12-23 17:24:55 +010066 size_t num_partial_merge_calls;
67 void resetNumPartialMergeCalls() { num_partial_merge_calls = 0; }
Marc Kupietz28cc53e2017-12-23 17:24:55 +010068
69
Marc Kupietz4b799e92018-01-02 11:04:56 +010070 inline void EncodeFixed64(char* buf, uint64_t value) {
71 if (! IS_BIG_ENDIAN) {
72 memcpy(buf, &value, sizeof(value));
73 } else {
74 buf[0] = value & 0xff;
75 buf[1] = (value >> 8) & 0xff;
76 buf[2] = (value >> 16) & 0xff;
77 buf[3] = (value >> 24) & 0xff;
78 buf[4] = (value >> 32) & 0xff;
79 buf[5] = (value >> 40) & 0xff;
80 buf[6] = (value >> 48) & 0xff;
81 buf[7] = (value >> 56) & 0xff;
82 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +010083 }
84
Marc Kupietz4b799e92018-01-02 11:04:56 +010085 inline uint32_t DecodeFixed32(const char* ptr) {
86 if (! IS_BIG_ENDIAN) {
87 // Load the raw bytes
88 uint32_t result;
89 memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
90 return result;
91 } else {
92 return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0])))
93 | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8)
94 | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16)
95 | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24));
96 }
97 }
98
99 inline uint64_t DecodeFixed64(const char* ptr) {
100 if (! IS_BIG_ENDIAN) {
101 // Load the raw bytes
102 uint64_t result;
103 memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
104 return result;
105 } else {
106 uint64_t lo = DecodeFixed32(ptr);
107 uint64_t hi = DecodeFixed32(ptr + 4);
108 return (hi << 32) | lo;
109 }
110 }
111
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100112 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 +0100113 double
114 r1 = f1 * window_size,
115 c1 = f2,
116 e = r1 * c1 / total,
117 o = f12;
Marc Kupietzf4a649a2021-02-26 09:18:01 +0100118 if(f12 < FREQUENCY_THRESHOLD)
119 return -1.0;
120 else
121 return log2(o/e);
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100122 }
123
Marc Kupietzce0b8b02018-06-05 11:06:39 +0200124 // Bouma, Gerlof (2009): <a href="https://svn.spraakdata.gu.se/repos/gerlof/pub/www/Docs/npmi-pfd.pdf">
125 // Normalized (pointwise) mutual information in collocation extraction</a>. In Proceedings of GSCL.
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100126 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 +0100127 double
128 r1 = f1 * window_size,
129 c1 = f2,
130 e = r1 * c1 / total,
131 o = f12;
132 if(f12 < FREQUENCY_THRESHOLD)
Marc Kupietz8caf9912018-06-05 10:51:18 +0200133 return -1.0;
134 else
Marc Kupietz1335dd72019-01-22 15:35:21 +0100135 return log2(o/e) / (-log2(o/total/window_size));
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100136 }
137
138 // Thanopoulos, A., Fakotakis, N., Kokkinakis, G.: Comparative evaluation of collocation extraction metrics.
139 // In: International Conference on Language Resources and Evaluation (LREC-2002). (2002) 620–625
140 // double md = log2(pow((double)max * window_size / total, 2) / (window_size * ((double)_vocab[w1].freq/total) * ((double)_vocab[last_w2].freq/total)));
141 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 +0100142 double
143 r1 = f1 * window_size,
144 c1 = f2,
145 e = r1 * c1 / total,
146 o = f12;
147 return log2(o*o/e);
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100148 }
149
150 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 +0100151 double
152 r1 = f1 * window_size,
153 c1 = f2,
154 e = r1 * c1 / total,
155 o = f12;
Marc Kupietz8caf9912018-06-05 10:51:18 +0200156 if(f12 == 0)
157 return 0;
158 else
Marc Kupietz1335dd72019-01-22 15:35:21 +0100159 return log2(o*o*o/e);
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100160 }
161
Marc Kupietzbbd236e2019-01-21 16:50:19 +0100162 // 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.
163 // Free PDF available from http://purl.org/stefan.evert/PUB/Evert2004phd.pdf
164 static inline double ca_ll(uint64_t w1, uint64_t w2, uint64_t w12, uint64_t n, uint64_t window_size) {
165 double
166 r1 = (double) w1 * window_size,
167 r2 = (double) n - r1,
168 c1 = w2,
169 c2 = n - c1,
170 o11 = w12, o12 = r1 - o11,
171 o21 = c1 - w12, o22 = r2 - o21,
172 e11 = r1 * c1 / n, e12 = r1 * c2 / n,
173 e21 = r2 * c1 / n, e22 = r2 * c2 / n;
174 return (2 * ( (o11>0? o11 * log(o11/e11):0) + (o12>0? o12 * log(o12/e12):0) + (o21>0? o21 * log(o21/e21):0) + (o22>0? o22 * log(o22/e22):0)));
175 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100176
Marc Kupietz41880452019-01-22 15:29:06 +0100177
178 static inline double ca_dice(uint64_t w1, uint64_t w2, uint64_t w12, uint64_t n, uint64_t window_size) {
179 double
180 r1 = (double) w1 * window_size,
181 c1 = w2;
182 return 2 * w12 / (c1+r1);
183 }
184
185 // 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.
186 static inline double ca_logdice(uint64_t w1, uint64_t w2, uint64_t w12, uint64_t n, uint64_t window_size) {
187 double
Marc Kupietz41880452019-01-22 15:29:06 +0100188 r1 = (double) w1 * window_size,
189 c1 = w2;
Marc Kupietzfdc0acf2019-01-31 12:42:58 +0100190 return 14 + log2(2 * w12 / (c1+r1));
Marc Kupietz41880452019-01-22 15:29:06 +0100191 }
192
Marc Kupietz4b799e92018-01-02 11:04:56 +0100193 class CountMergeOperator : public AssociativeMergeOperator {
194 public:
195 CountMergeOperator() {
196 mergeOperator_ = MergeOperators::CreateUInt64AddOperator();
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100197 }
198
Marc Kupietz4b799e92018-01-02 11:04:56 +0100199 virtual bool Merge(const Slice& key,
200 const Slice* existing_value,
201 const Slice& value,
202 std::string* new_value,
203 Logger* logger) const override {
204 assert(new_value->empty());
205 ++num_merge_operator_calls;
206 if (existing_value == nullptr) {
207 new_value->assign(value.data(), value.size());
208 return true;
209 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100210
Marc Kupietz4b799e92018-01-02 11:04:56 +0100211 return mergeOperator_->PartialMerge(
212 key,
213 *existing_value,
214 value,
215 new_value,
216 logger);
217 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100218
Marc Kupietz4b799e92018-01-02 11:04:56 +0100219 virtual const char* Name() const override {
220 return "UInt64AddOperator";
221 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100222
Marc Kupietz4b799e92018-01-02 11:04:56 +0100223 private:
224 std::shared_ptr<MergeOperator> mergeOperator_;
225 };
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100226
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100227
Marc Kupietz4b799e92018-01-02 11:04:56 +0100228 class CollocatorIterator : public Iterator {
229 private:
230 char prefixc[sizeof(uint64_t)];
231 Iterator *base_iterator_;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100232
233
Marc Kupietz4b799e92018-01-02 11:04:56 +0100234 public:
235 CollocatorIterator(Iterator* base_iterator)
236 : base_iterator_(base_iterator)
237 {}
238
Marc Kupietz4b799e92018-01-02 11:04:56 +0100239 void setPrefix(char *prefix) {
240 memcpy(prefixc, prefix, sizeof(uint64_t));
241 }
242
243 virtual void SeekToFirst() { base_iterator_->SeekToFirst(); }
244 virtual void SeekToLast() { base_iterator_->SeekToLast(); }
245 virtual void Seek(const rocksdb::Slice& s) { base_iterator_->Seek(s); }
246 virtual void Prev() { base_iterator_->Prev(); }
247 virtual void Next() { base_iterator_->Next(); }
248 virtual Slice key() const;
249 virtual Slice value() const;
250 virtual Status status() const;
251 virtual bool Valid() const;
252 bool isValid();
253 uint64_t intValue();
254 uint64_t intKey();
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100255
Marc Kupietz4b799e92018-01-02 11:04:56 +0100256 };
Marc Kupietz18375e12017-12-24 10:11:18 +0100257
Marc Kupietz4b799e92018-01-02 11:04:56 +0100258 // rocksdb::CollocatorIterator::CollocatorIterator(Iterator* base_iterator) {}
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100259
Marc Kupietz4b799e92018-01-02 11:04:56 +0100260 bool rocksdb::CollocatorIterator::Valid() const {
Marc Kupietz18375e12017-12-24 10:11:18 +0100261 return base_iterator_->Valid() && key().starts_with(std::string(prefixc,3));
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100262 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100263
Marc Kupietz4b799e92018-01-02 11:04:56 +0100264 bool rocksdb::CollocatorIterator::isValid() {
265 return base_iterator_->Valid() && key().starts_with(std::string(prefixc,3));
Marc Kupietzd31254c2018-01-20 21:29:30 +0100266 // return key().starts_with(std::string(prefixc,3));
Marc Kupietz4b799e92018-01-02 11:04:56 +0100267 }
Marc Kupietz18375e12017-12-24 10:11:18 +0100268
Marc Kupietz4b799e92018-01-02 11:04:56 +0100269 uint64_t rocksdb::CollocatorIterator::intKey() {
270 return DecodeFixed64(base_iterator_->key().data());
271 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100272
Marc Kupietz4b799e92018-01-02 11:04:56 +0100273 uint64_t rocksdb::CollocatorIterator::intValue() {
274 return DecodeFixed64(base_iterator_->value().data());
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100275 }
276
Marc Kupietz37359b12018-01-09 21:11:37 +0100277 class VocabEntry {
278 public:
279 string word;
280 uint64_t freq;
281 };
282
Marc Kupietz6aec7682018-01-10 09:47:48 +0100283 class CollocatorDB {
Marc Kupietz4b799e92018-01-02 11:04:56 +0100284 private:
285 WriteOptions merge_option_; // for merge
286 char _one[sizeof(uint64_t)];
287 Slice _one_slice;
Marc Kupietz37359b12018-01-09 21:11:37 +0100288 vector<VocabEntry> _vocab;
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100289 uint64_t total = 0;
290 uint64_t sentences = 0;
Marc Kupietz8cf7e912019-01-21 17:05:23 +0100291 float avg_window_size = 8.0;
Marc Kupietz37359b12018-01-09 21:11:37 +0100292
Marc Kupietz4b799e92018-01-02 11:04:56 +0100293 protected:
294 std::shared_ptr<DB> db_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100295
Marc Kupietz4b799e92018-01-02 11:04:56 +0100296 WriteOptions put_option_;
297 ReadOptions get_option_;
298 WriteOptions delete_option_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100299
Marc Kupietz4b799e92018-01-02 11:04:56 +0100300 uint64_t default_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100301
Marc Kupietz4b799e92018-01-02 11:04:56 +0100302 std::shared_ptr<DB> OpenDb(const char *dbname);
Marc Kupietz6bb27762018-01-09 17:53:01 +0100303 std::shared_ptr<DB> OpenDbForRead(const char *dbname);
Marc Kupietz37359b12018-01-09 21:11:37 +0100304 void read_vocab(string fname);
305
Marc Kupietz4b799e92018-01-02 11:04:56 +0100306 public:
Marc Kupietz4a5e08a2018-06-05 11:07:11 +0200307 string getWord(uint32_t w1);
Marc Kupietz6aec7682018-01-10 09:47:48 +0100308 CollocatorDB(const char *db_name, bool read_only);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100309
Marc Kupietz6aec7682018-01-10 09:47:48 +0100310 // public interface of CollocatorDB.
Marc Kupietz4b799e92018-01-02 11:04:56 +0100311 // All four functions return false
312 // if the underlying level db operation failed.
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100313
Marc Kupietz4b799e92018-01-02 11:04:56 +0100314 // mapped to a levedb Put
315 bool set(const std::string& key, uint64_t value) {
316 // just treat the internal rep of int64 as the string
317 char buf[sizeof(value)];
318 EncodeFixed64(buf, value);
319 Slice slice(buf, sizeof(value));
320 auto s = db_->Put(put_option_, key, slice);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100321
Marc Kupietz4b799e92018-01-02 11:04:56 +0100322 if (s.ok()) {
323 return true;
324 } else {
325 std::cerr << s.ToString() << std::endl;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100326 return false;
327 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100328 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100329
330 DB *getDb() {
331 return db_.get();
332 }
333
334 // mapped to a rocksdb Delete
335 bool remove(const std::string& key) {
336 auto s = db_->Delete(delete_option_, key);
337
338 if (s.ok()) {
339 return true;
340 } else {
341 std::cerr << s.ToString() << std::endl;
342 return false;
343 }
344 }
345
346 // mapped to a rocksdb Get
347 bool get(const std::string& key, uint64_t* value) {
348 std::string str;
349 auto s = db_->Get(get_option_, key, &str);
350
351 if (s.IsNotFound()) {
352 // return default value if not found;
353 *value = default_;
354 return true;
355 } else if (s.ok()) {
356 // deserialization
357 if (str.size() != sizeof(uint64_t)) {
358 std::cerr << "value corruption\n";
359 return false;
360 }
361 *value = DecodeFixed64(&str[0]);
362 return true;
363 } else {
364 std::cerr << s.ToString() << std::endl;
365 return false;
366 }
367 }
368
369
370 uint64_t get(const uint32_t w1, const uint32_t w2, const int8_t dist) {
371 char encoded_key[sizeof(uint64_t)];
372 EncodeFixed64(encoded_key, encodeCollocation(w1,w2,dist));
373 uint64_t value = default_;
374 get(std::string(encoded_key, 8), &value);
375 return value;
376 }
377
378 virtual void inc(const std::string& key) {
379 db_->Merge(merge_option_, key, _one_slice);
380 }
381
382 void inc(const uint64_t key) {
383 char encoded_key[sizeof(uint64_t)];
384 EncodeFixed64(encoded_key, key);
385 db_->Merge(merge_option_, std::string(encoded_key, 8), _one_slice);
386 }
387
388 virtual void inc(const uint32_t w1, const uint32_t w2, const uint8_t dist);
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100389 void dump(uint32_t w1, uint32_t w2, int8_t dist);
Marc Kupietz37359b12018-01-09 21:11:37 +0100390 vector<Collocator> get_collocators(uint32_t w1);
Marc Kupietzbd966192018-10-13 14:14:37 +0200391 vector<Collocator> get_collocators(uint32_t w1, uint32_t max_w2);
Marc Kupietz8c62c372019-01-31 12:21:01 +0100392 void applyCAMeasures(const uint32_t w1, const uint32_t w2, uint64_t *sumWindow, const uint64_t sum, const int usedPositions, int true_window_size, rocksdb::Collocator *result);
393
Marc Kupietz3400aa52018-06-05 10:28:55 +0200394 void dumpSparseLlr(uint32_t w1, uint32_t min_cooccur);
Marc Kupietze9627152019-02-04 12:32:12 +0100395 string collocators2json(uint32_t w1, vector<Collocator> collocators);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100396
Marc Kupietz4b799e92018-01-02 11:04:56 +0100397 // mapped to a rocksdb Merge operation
398 virtual bool add(const std::string& key, uint64_t value) {
399 char encoded[sizeof(uint64_t)];
400 EncodeFixed64(encoded, value);
401 Slice slice(encoded, sizeof(uint64_t));
402 auto s = db_->Merge(merge_option_, key, slice);
403
404 if (s.ok()) {
405 return true;
406 } else {
407 std::cerr << s.ToString() << std::endl;
408 return false;
409 }
410 }
411
412 CollocatorIterator* SeekIterator(uint64_t w1, uint64_t w2, int8_t dist);
413 };
414
Marc Kupietz6aec7682018-01-10 09:47:48 +0100415 rocksdb::CollocatorDB::CollocatorDB(const char *db_name, bool read_only = false) {
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100416 // merge_option_.sync = true;
Marc Kupietz6bb27762018-01-09 17:53:01 +0100417 if(read_only)
418 db_ = OpenDbForRead(db_name);
419 else
420 db_ = OpenDb(db_name);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100421 assert(db_);
422 uint64_t one = 1;
423 EncodeFixed64(_one, one);
424 _one_slice = Slice(_one, sizeof(uint64_t));
425 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100426
Marc Kupietz6aec7682018-01-10 09:47:48 +0100427 void rocksdb::CollocatorDB::inc(const uint32_t w1, const uint32_t w2, const uint8_t dist) {
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100428 inc(encodeCollocation(w1, w2, dist));
429 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100430
Marc Kupietz6aec7682018-01-10 09:47:48 +0100431 void rocksdb::CollocatorDB::read_vocab(string fname) {
Marc Kupietz37359b12018-01-09 21:11:37 +0100432 char strbuf[2048];
433 uint64_t freq;
434 FILE *fin = fopen(fname.c_str(), "rb");
435 if (fin == NULL) {
436 cout << "Vocabulary file " << fname <<" not found\n";
437 exit(1);
438 }
439 uint64_t i = 0;
440 while(!feof(fin)) {
Marc Kupietzd31254c2018-01-20 21:29:30 +0100441 fscanf(fin, "%s %lu", strbuf, &freq);
Marc Kupietz37359b12018-01-09 21:11:37 +0100442 _vocab.push_back({strbuf, freq});
443 total += freq;
444 i++;
445 }
446 fclose(fin);
Marc Kupietz4ec51c12019-01-21 11:06:39 +0100447
448 char size_fname[256];
449 strcpy(size_fname, fname.c_str());
450 char *pos = strstr(size_fname, ".vocab");
451 if(pos) {
452 *pos=0;
453 strcat(size_fname, ".size");
454 FILE *fp = fopen(size_fname, "r");
455 if (fp != NULL) {
456 fscanf(fp, "%lu", &sentences);
457 fscanf(fp, "%lu", &total);
458 float sl = (float)total/(float)sentences;
459 float w = WINDOW_SIZE;
460 avg_window_size = ((sl > 2*w? (sl-2*w)*2*w: 0) + (double) w * (3*w -1)) / sl;
461 fprintf(stdout, "Size corrections found: corpus size: %lu tokens in %lu sentences, avg. sentence size: %f, avg. window size: %f\n", total, sentences, sl, avg_window_size);
462 fclose(fp);
463 } else {
464 std::cout << "size file " << size_fname << " not found\n";
465 }
466 } else {
467 std::cout << "cannot determine size file " << size_fname << "\n";
468 }
Marc Kupietz37359b12018-01-09 21:11:37 +0100469 }
470
Marc Kupietz6aec7682018-01-10 09:47:48 +0100471 std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDbForRead(const char *name) {
Marc Kupietz6bb27762018-01-09 17:53:01 +0100472 DB* db;
473 Options options;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100474 options.env->SetBackgroundThreads(4);
475 options.create_if_missing = true;
476 options.merge_operator = std::make_shared<CountMergeOperator>();
477 options.max_successive_merges = 0;
478 // options.prefix_extractor.reset(NewFixedPrefixTransform(8));
479 options.IncreaseParallelism();
480 options.OptimizeLevelStyleCompaction();
481 options.prefix_extractor.reset(NewFixedPrefixTransform(3));
Marc Kupietz37359b12018-01-09 21:11:37 +0100482 ostringstream dbname, vocabname;
Marc Kupietz6bb27762018-01-09 17:53:01 +0100483 dbname << name << ".rocksdb";
484 auto s = DB::OpenForReadOnly(options, dbname.str(), &db);
485 if (!s.ok()) {
486 std::cerr << s.ToString() << std::endl;
487 assert(false);
488 }
Marc Kupietz37359b12018-01-09 21:11:37 +0100489 vocabname << name << ".vocab";
490 read_vocab(vocabname.str());
Marc Kupietz6bb27762018-01-09 17:53:01 +0100491 return std::shared_ptr<DB>(db);
492 }
493
Marc Kupietz6aec7682018-01-10 09:47:48 +0100494 std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDb(const char *dbname) {
Marc Kupietz4b799e92018-01-02 11:04:56 +0100495 DB* db;
496 Options options;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100497
498
499 options.env->SetBackgroundThreads(4);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100500 options.create_if_missing = true;
501 options.merge_operator = std::make_shared<CountMergeOperator>();
502 options.max_successive_merges = 0;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100503 // options.prefix_extractor.reset(NewFixedPrefixTransform(8));
504 options.IncreaseParallelism();
505 options.OptimizeLevelStyleCompaction();
506 // options.max_write_buffer_number = 48;
507 // options.max_background_jobs = 48;
508 // options.allow_concurrent_memtable_write=true;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100509 // options.memtable_factory.reset(rocksdb::NewHashLinkListRepFactory(200000));
510 // options.enable_write_thread_adaptive_yield = 1;
511 // options.allow_concurrent_memtable_write = 1;
512 // options.memtable_factory.reset(new rocksdb::SkipListFactory);
513 // options.write_buffer_size = 1 << 22;
514 // options.allow_mmap_reads = true;
515 // options.allow_mmap_writes = true;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100516 // options.max_background_compactions = 40;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100517 // BlockBasedTableOptions table_options;
518 // table_options.filter_policy.reset(NewBloomFilterPolicy(24, false));
519 // options.bloom_locality = 1;
520 // std::shared_ptr<Cache> cache = NewLRUCache(512 * 1024 * 1024);
521 // table_options.block_cache = cache;
522 // options.table_factory.reset(NewBlockBasedTableFactory(table_options));
Marc Kupietz4b799e92018-01-02 11:04:56 +0100523 Status s;
524 // DestroyDB(dbname, Options());
525 s = DB::Open(options, dbname, &db);
526 if (!s.ok()) {
527 std::cerr << s.ToString() << std::endl;
528 assert(false);
529 }
530 return std::shared_ptr<DB>(db);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100531 }
532
Marc Kupietz6aec7682018-01-10 09:47:48 +0100533 CollocatorIterator* rocksdb::CollocatorDB::SeekIterator(uint64_t w1, uint64_t w2, int8_t dist) {
Marc Kupietz18375e12017-12-24 10:11:18 +0100534 ReadOptions options;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100535 options.prefix_same_as_start = true;
Marc Kupietz18375e12017-12-24 10:11:18 +0100536 char prefixc[sizeof(uint64_t)];
537 EncodeFixed64(prefixc, encodeCollocation(w1, w2, dist));
538 Iterator *it = db_->NewIterator(options);
539 CollocatorIterator *cit = new CollocatorIterator(it);
540 cit->Seek(std::string(prefixc,3));// it->Valid() && it->key().starts_with(std::string(prefixc,3)); it->Next()) {
541 cit->setPrefix(prefixc);
542 return cit;
543 }
544
Marc Kupietz6aec7682018-01-10 09:47:48 +0100545 void rocksdb::CollocatorDB::dump(uint32_t w1, uint32_t w2, int8_t dist) {
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100546 auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, w2, dist));
547 for (; it->isValid(); it->Next()) {
548 uint64_t value = it->intValue();
549 uint64_t key = it->intKey();
550 std::cout << "w1:" << W1(key) << ", w2:" << W2(key) << ", dist:" << (int32_t) DIST(key) << " - count:" << value << std::endl;
551 }
552 std::cout << "ready dumping\n";
553 }
554
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100555 bool sortByNpmi(const Collocator &lhs, const Collocator &rhs) { return lhs.npmi > rhs.npmi; }
556 bool sortByLfmd(const Collocator &lhs, const Collocator &rhs) { return lhs.lfmd > rhs.lfmd; }
Marc Kupietzd31254c2018-01-20 21:29:30 +0100557 bool sortByLlr(const Collocator &lhs, const Collocator &rhs) { return lhs.llr > rhs.llr; }
Marc Kupietz7e3dfde2019-01-22 16:27:33 +0100558 bool sortByLogDice(const Collocator &lhs, const Collocator &rhs) { return lhs.logdice > rhs.logdice; }
Marc Kupietz3203e4c2019-02-04 12:42:45 +0100559 bool sortByLogDiceAF(const Collocator &lhs, const Collocator &rhs) { return lhs.ldaf > rhs.ldaf; }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100560
Marc Kupietz8c62c372019-01-31 12:21:01 +0100561
562 void rocksdb::CollocatorDB::applyCAMeasures(const uint32_t w1, const uint32_t w2, uint64_t *sumWindow,
563 const uint64_t sum, const int usedPositions, int true_window_size, rocksdb::Collocator *result) {
564 uint64_t f1 = _vocab[w1].freq, f2 = _vocab[w2].freq;
565 double o = sum,
566 r1 = f1 * true_window_size,
567 c1 = f2,
568 e = r1 * c1 / total,
569 pmi = log2(o/e),
570 md = log2(o*o/e),
571 lfmd = log2(o*o*o/e),
572 llr = ca_ll(f1, f2, sum, total, true_window_size);
573 double ld = ca_logdice(f1, f2, sum, total, true_window_size);
574
575 int bestWindow = usedPositions;
576 double bestAF = ld;
577 double currentAF;
578 // if(f1<75000000)
579 //#pragma omp parallel for reduction(max:bestAF)
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100580 // #pragma omp target teams distribute parallel for reduction(max:bestAF) map(tofrom:bestAF,currentAF,bestWindow,usedPositions)
Marc Kupietz8c62c372019-01-31 12:21:01 +0100581 for (int bitmask=1; bitmask < (1 << (2*WINDOW_SIZE)); bitmask++) {
582 if((bitmask & usedPositions) == 0 || (bitmask & ~usedPositions) > 0) continue;
583 uint64_t currentWindowSum=0;
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100584 // #pragma omp target teams distribute parallel for reduction(+:currentWindowSum) map(tofrom:bitmask,usedPositions)
Marc Kupietz8c62c372019-01-31 12:21:01 +0100585 for (int pos=0; pos < 2*WINDOW_SIZE; pos++) {
586 if (((1<<pos) & bitmask & usedPositions) != 0)
587 currentWindowSum+=sumWindow[pos];
588 }
589 currentAF = ca_logdice(f1, f2, currentWindowSum, total, __builtin_popcount(bitmask));
590 if(currentAF > bestAF) {
591 bestAF = currentAF;
592 bestWindow = bitmask;
593 }
594 }
595
596 *result = {w2, f2, sum,
597 pmi, pmi / (-log2(o/total/true_window_size)),
598 llr, lfmd, md,
Marc Kupietz6d9221d2021-02-26 09:34:40 +0100599 ca_pmi(f1, f2, sumWindow[WINDOW_SIZE], total, 1),
600 ca_pmi(f1, f2, sumWindow[WINDOW_SIZE-1], total, 1),
601 (double)sumWindow[WINDOW_SIZE],
602 (double)sumWindow[WINDOW_SIZE-1],
Marc Kupietz8c62c372019-01-31 12:21:01 +0100603 ca_dice(f1, f2, sum, total, true_window_size),
604 ld,
605 bestAF,
606 usedPositions,
607 bestWindow
608 };
609
610 }
611
Marc Kupietz75af60f2019-01-22 22:34:29 +0100612 std::vector<Collocator> rocksdb::CollocatorDB::get_collocators(uint32_t w1, uint32_t max_w2) {
613 std::vector<Collocator> collocators;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100614 uint64_t w2, last_w2 = 0xffffffffffffffff;
Marc Kupietz8c62c372019-01-31 12:21:01 +0100615 uint64_t maxv = 0, sum = 0;
616 uint64_t *sumWindow = (uint64_t*) malloc(sizeof(uint64_t)*2*WINDOW_SIZE);
617 memset(sumWindow, 0, sizeof(uint64_t)*2*WINDOW_SIZE);
Marc Kupietzade33222019-01-22 22:52:44 +0100618 int true_window_size = 1;
Marc Kupietz39a4fd02019-01-23 10:18:43 +0100619 int usedPositions=0;
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100620
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100621 if(w1 > _vocab.size()) {
622 std::cout << w1 << "> vocabulary size " << _vocab.size() << "\n";
623 w1 -= _vocab.size();
624 }
625 #ifdef DEBUG
626 std::cout << "Searching for collocates of " << _vocab[w1].word << "\n";
627 #endif
628 // #pragma omp parallel num_threads(40)
629 // #pragma omp single
Marc Kupietzd31254c2018-01-20 21:29:30 +0100630 for ( auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) {
631 uint64_t value = it->intValue(),
632 key = it->intKey();
Marc Kupietzbd966192018-10-13 14:14:37 +0200633 if((w2 = W2(key)) > max_w2)
634 continue;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100635 if(last_w2 == 0xffffffffffffffff) last_w2 = w2;
636 if (w2 != last_w2) {
Marc Kupietz75af60f2019-01-22 22:34:29 +0100637 if (sum >= FREQUENCY_THRESHOLD) {
Marc Kupietz8c62c372019-01-31 12:21:01 +0100638 collocators.push_back({});
639 rocksdb::Collocator *result = &(collocators[collocators.size()-1]);
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100640 // #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 +0100641 {
642 // uint64_t *nsw = (uint64_t *)malloc(sizeof(uint64_t) * 2 *WINDOW_SIZE);
643 // memcpy(nsw, sumWindow, sizeof(uint64_t) * 2 *WINDOW_SIZE);
644 applyCAMeasures(w1, last_w2, sumWindow, sum, usedPositions, true_window_size, result);
645 // free(nsw);
Marc Kupietz75af60f2019-01-22 22:34:29 +0100646 }
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100647 }
Marc Kupietz75af60f2019-01-22 22:34:29 +0100648 memset(sumWindow, 0, 2*WINDOW_SIZE * sizeof(uint64_t));
Marc Kupietz39a4fd02019-01-23 10:18:43 +0100649 usedPositions = 1 << (-DIST(key)+WINDOW_SIZE-(DIST(key)<0?1:0));
Marc Kupietz75af60f2019-01-22 22:34:29 +0100650 sumWindow[-DIST(key)+WINDOW_SIZE-(DIST(key)<0?1:0)] = value;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100651 last_w2 = w2;
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100652 maxv = value;
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100653 sum = value;
Marc Kupietzade33222019-01-22 22:52:44 +0100654 true_window_size = 1;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100655 } else {
Marc Kupietz98cbcdc2019-01-21 17:11:27 +0100656 sum += value;
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100657 if(value > maxv)
658 maxv = value;
Marc Kupietz39a4fd02019-01-23 10:18:43 +0100659 usedPositions |= 1 << (-DIST(key)+WINDOW_SIZE-(DIST(key)<0?1:0));
Marc Kupietz75af60f2019-01-22 22:34:29 +0100660 sumWindow[-DIST(key)+WINDOW_SIZE-(DIST(key)<0?1:0)] = value;
Marc Kupietzade33222019-01-22 22:52:44 +0100661 true_window_size++;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100662 }
663 }
664
Marc Kupietz6d0fa542021-02-26 09:24:35 +0100665 // #pragma omp taskwait
666 sort(collocators.begin(), collocators.end(), sortByLogDiceAF);
Marc Kupietz8c62c372019-01-31 12:21:01 +0100667
Marc Kupietz30e4d5f2021-02-26 09:27:49 +0100668 #ifdef DEBUG
Marc Kupietzd31254c2018-01-20 21:29:30 +0100669 int i=0;
670 for (Collocator c : collocators) {
671 if(i++>10) break;
Marc Kupietz8c62c372019-01-31 12:21:01 +0100672 std::cout << "w1:" << _vocab[w1].word << ", w2: *" << _vocab[c.w2].word << "*"
Marc Kupietzd31254c2018-01-20 21:29:30 +0100673 << "\t f(w1):" << _vocab[w1].freq
674 << "\t f(w2):" << _vocab[c.w2].freq
Marc Kupietz51f93792018-01-25 08:51:01 +0100675 << "\t f(w1, w2):" << c.raw
Marc Kupietzd31254c2018-01-20 21:29:30 +0100676 << "\t pmi:" << c.pmi
677 << "\t npmi:" << c.npmi
678 << "\t llr:" << c.llr
Marc Kupietz8c62c372019-01-31 12:21:01 +0100679 << "\t md:" << c.md
Marc Kupietzd31254c2018-01-20 21:29:30 +0100680 << "\t lfmd:" << c.lfmd
Marc Kupietzd31254c2018-01-20 21:29:30 +0100681 << "\t total:" << total
682 << std::endl;
683 }
Marc Kupietz30e4d5f2021-02-26 09:27:49 +0100684 #endif
Marc Kupietz8c62c372019-01-31 12:21:01 +0100685
686 return collocators;
Marc Kupietzd31254c2018-01-20 21:29:30 +0100687 }
688
Marc Kupietz8c62c372019-01-31 12:21:01 +0100689 std::vector<Collocator> rocksdb::CollocatorDB::get_collocators(uint32_t w1) {
Marc Kupietzbd966192018-10-13 14:14:37 +0200690 return get_collocators(w1, UINT32_MAX);
691 }
692
Marc Kupietz3400aa52018-06-05 10:28:55 +0200693 void rocksdb::CollocatorDB::dumpSparseLlr(uint32_t w1, uint32_t min_cooccur) {
694 std::vector<Collocator> collocators;
695 std::stringstream stream;
696 uint64_t w2, last_w2 = 0xffffffffffffffff;
697 uint64_t maxv = 0, total_w1 = 0;
698 bool first = true;
699 for ( auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) {
700 uint64_t value = it->intValue(),
701 key = it->intKey();
702 w2 = W2(key);
703 total_w1 += value;
704 if(last_w2 == 0xffffffffffffffff) last_w2 = w2;
705 if (w2 != last_w2) {
706 if(maxv >= min_cooccur) {
Marc Kupietzbbd236e2019-01-21 16:50:19 +0100707 double llr = ca_ll(_vocab[w1].freq, _vocab[last_w2].freq, maxv, total, 1);
Marc Kupietz3400aa52018-06-05 10:28:55 +0200708 if(first)
709 first = false;
710 else
711 stream << " ";
712 stream << w2 << " " << llr;
713 }
714 last_w2 = w2;
715 maxv = value;
716 } else {
717 if(value > maxv)
718 maxv = value;
719 }
720 }
721 if(first)
722 stream << "1 0.0";
723 stream << "\n";
724 std::cout << stream.str();
725 }
726
Marc Kupietz4b799e92018-01-02 11:04:56 +0100727 rocksdb::Slice rocksdb::CollocatorIterator::key() const { return base_iterator_->key(); }
728 rocksdb::Slice rocksdb::CollocatorIterator::value() const { return base_iterator_->value(); }
729 rocksdb::Status rocksdb::CollocatorIterator::status() const { return base_iterator_->status(); }
730
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100731};
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100732
Marc Kupietz4a5e08a2018-06-05 11:07:11 +0200733string rocksdb::CollocatorDB::getWord(uint32_t w1) {
734 return _vocab[w1].word;
735}
736
Marc Kupietze9627152019-02-04 12:32:12 +0100737string rocksdb::CollocatorDB::collocators2json(uint32_t w1, vector<Collocator> collocators) {
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100738 ostringstream s;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100739 int i = 0;
Marc Kupietze9627152019-02-04 12:32:12 +0100740 s << " { \"f1\": " << _vocab[w1].freq << "," <<
741 "\"w1\":\"" << string(_vocab[w1].word) << "\", " <<
742 "\"N\": " << total << ", " <<
743 "\"collocates\": [";
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100744 bool first = true;
745 for (Collocator c : collocators) {
Marc Kupietzb999ec52018-06-05 11:20:46 +0200746 if(strncmp(_vocab[c.w2].word.c_str(), "quot", 4) == 0) continue;
Marc Kupietz0dd86ef2018-01-11 22:23:17 +0100747 if (i++ > 200)
748 break;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100749 if(!first)
750 s << ",\n";
751 else
752 first = false;
753 s << "{"
Marc Kupietz7d9558f2019-01-22 16:26:50 +0100754 "\"word\":\"" << (string(_vocab[c.w2].word).compare("<num>") == 0? string("###") : string(_vocab[c.w2].word)) << "\"," <<
Marc Kupietzcc6c4592019-01-23 10:11:23 +0100755 "\"f2\":" << c.f2 << "," <<
Marc Kupietz51f93792018-01-25 08:51:01 +0100756 "\"f\":" << c.raw << "," <<
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100757 "\"npmi\":" << c.npmi << "," <<
Marc Kupietz41880452019-01-22 15:29:06 +0100758 "\"pmi\":" << c.pmi << "," <<
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100759 "\"llr\":" << c.llr << "," <<
760 "\"lfmd\":" << c.lfmd << "," <<
Marc Kupietz41880452019-01-22 15:29:06 +0100761 "\"md\":" << c.md << "," <<
762 "\"dice\":" << c.dice << "," <<
763 "\"ld\":" << c.logdice << "," <<
Marc Kupietz8e0ebea2018-01-24 09:53:26 +0100764 "\"llfmd\":" << c.left_lfmd << "," <<
765 "\"rlfmd\":" << c.right_lfmd << "," <<
766 "\"lnpmi\":" << c.left_npmi << "," <<
Marc Kupietz75af60f2019-01-22 22:34:29 +0100767 "\"rnpmi\":" << c.right_npmi << "," <<
Marc Kupietz3203e4c2019-02-04 12:42:45 +0100768 "\"af\":" << c.ldaf << "," <<
Marc Kupietze9f58932019-01-24 15:12:59 +0100769 "\"win\":" << c.window << "," <<
770 "\"afwin\":" << c.af_window <<
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100771 "}";
772 }
Marc Kupietze9627152019-02-04 12:32:12 +0100773 s << "]}\n";
Marc Kupietz8c62c372019-01-31 12:21:01 +0100774 std::cout << s.str();
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100775 return s.str();
776}
777
Marc Kupietz6aec7682018-01-10 09:47:48 +0100778typedef rocksdb::CollocatorDB COLLOCATORS;
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100779
780extern "C" {
Marc Kupietz6aec7682018-01-10 09:47:48 +0100781 COLLOCATORS *open_collocatordb_for_write(char *dbname) {
782 return new rocksdb::CollocatorDB(dbname, false);
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100783 }
784
Marc Kupietz6aec7682018-01-10 09:47:48 +0100785 COLLOCATORS *open_collocatordb(char *dbname) {
786 return new rocksdb::CollocatorDB(dbname, true);
Marc Kupietz6bb27762018-01-09 17:53:01 +0100787 }
788
Marc Kupietz6aec7682018-01-10 09:47:48 +0100789 void inc_collocator(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100790 db->inc(w1, w2, dist);
791 }
792
793 void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
794 db->dump(w1, w2, dist);
795 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100796
Marc Kupietz37359b12018-01-09 21:11:37 +0100797 void get_collocators(COLLOCATORS *db, uint32_t w1) {
798 db->get_collocators(w1);
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100799 }
800
Marc Kupietzca3a52e2018-06-05 14:16:23 +0200801 const char *get_word(COLLOCATORS *db, uint32_t w) {
802 return db->getWord(w).c_str();
803 }
804
Marc Kupietz37359b12018-01-09 21:11:37 +0100805 const char *get_collocators_as_json(COLLOCATORS *db, uint32_t w1) {
Marc Kupietze9627152019-02-04 12:32:12 +0100806 return strdup(db->collocators2json(w1, db->get_collocators(w1)).c_str());
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100807 }
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100808}