blob: 89da1c6d087946e074a9264fd05eba0284664305 [file] [log] [blame]
Marc Kupietz28cc53e2017-12-23 17:24:55 +01001#include <typeinfo>
Marc Kupietz4b799e92018-01-02 11:04:56 +01002#define EXPORT __attribute__((visibility("visible")))
3#define IMPORT
Marc Kupietz28cc53e2017-12-23 17:24:55 +01004#include <assert.h>
Marc Kupietz37359b12018-01-09 21:11:37 +01005#include <inttypes.h>
Marc Kupietz28cc53e2017-12-23 17:24:55 +01006#include <memory>
7#include <iostream>
Marc Kupietzc8ddf452018-01-07 21:33:12 +01008#include <algorithm>
9#include <vector>
Marc Kupietz28cc53e2017-12-23 17:24:55 +010010#include <stdint.h>
Marc Kupietzc8ddf452018-01-07 21:33:12 +010011#include <string>
12#include <sstream> // for ostringstream
13#include <math.h>
Marc Kupietz28cc53e2017-12-23 17:24:55 +010014#include "rocksdb/cache.h"
15#include "rocksdb/comparator.h"
16#include "rocksdb/db.h"
17#include "rocksdb/env.h"
Marc Kupietzc8ddf452018-01-07 21:33:12 +010018#include "rocksdb/table.h"
Marc Kupietz28cc53e2017-12-23 17:24:55 +010019#include <rocksdb/merge_operator.h>
Marc Kupietzc8ddf452018-01-07 21:33:12 +010020#include <rocksdb/slice_transform.h>
Marc Kupietz28cc53e2017-12-23 17:24:55 +010021#include "rocksdb/utilities/db_ttl.h"
Marc Kupietzc8ddf452018-01-07 21:33:12 +010022#include "rocksdb/filter_policy.h"
Marc Kupietz28cc53e2017-12-23 17:24:55 +010023#include "merge_operators.h"
24
Marc Kupietzc8ddf452018-01-07 21:33:12 +010025#define AVG_WINDOW_SIZE 7
Marc Kupietz06c9a9f2018-01-02 16:56:43 +010026
Marc Kupietz28cc53e2017-12-23 17:24:55 +010027#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
28#define encodeCollocation(w1, w2, dist) (((uint64_t)dist << 56) | ((uint64_t)w2 << 24) | w1)
Marc Kupietz18375e12017-12-24 10:11:18 +010029#define W1(key) (uint64_t)(key & 0xffffff)
30#define W2(key) (uint64_t)((key >> 24) & 0xffffff)
31#define DIST(key) (int8_t)((uint64_t)((key >> 56) & 0xff))
Marc Kupietzc8ddf452018-01-07 21:33:12 +010032
33typedef struct {
34 uint64_t freq;
35 char *word;
36} vocab_entry;
37
38// typedef struct Collocator {
39// uint64_t w2;
40// uint64_t sum;
41// };
42
Marc Kupietz28cc53e2017-12-23 17:24:55 +010043using namespace rocksdb;
Marc Kupietzc8ddf452018-01-07 21:33:12 +010044using namespace std;
Marc Kupietz28cc53e2017-12-23 17:24:55 +010045
Marc Kupietz4b799e92018-01-02 11:04:56 +010046namespace rocksdb {
Marc Kupietzc8ddf452018-01-07 21:33:12 +010047 class Collocator {
48 public:
49 uint64_t w2;
50 uint64_t sum;
51 double pmi;
52 double npmi;
53 double llr;
54 double md;
55 double lfmd;
56 double fpmi;
57 };
58
Marc Kupietz28cc53e2017-12-23 17:24:55 +010059 size_t num_merge_operator_calls;
60 void resetNumMergeOperatorCalls() { num_merge_operator_calls = 0; }
Marc Kupietzc8ddf452018-01-07 21:33:12 +010061
Marc Kupietz28cc53e2017-12-23 17:24:55 +010062 size_t num_partial_merge_calls;
63 void resetNumPartialMergeCalls() { num_partial_merge_calls = 0; }
Marc Kupietz28cc53e2017-12-23 17:24:55 +010064
65
Marc Kupietz4b799e92018-01-02 11:04:56 +010066 inline void EncodeFixed64(char* buf, uint64_t value) {
67 if (! IS_BIG_ENDIAN) {
68 memcpy(buf, &value, sizeof(value));
69 } else {
70 buf[0] = value & 0xff;
71 buf[1] = (value >> 8) & 0xff;
72 buf[2] = (value >> 16) & 0xff;
73 buf[3] = (value >> 24) & 0xff;
74 buf[4] = (value >> 32) & 0xff;
75 buf[5] = (value >> 40) & 0xff;
76 buf[6] = (value >> 48) & 0xff;
77 buf[7] = (value >> 56) & 0xff;
78 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +010079 }
80
Marc Kupietz4b799e92018-01-02 11:04:56 +010081 inline uint32_t DecodeFixed32(const char* ptr) {
82 if (! IS_BIG_ENDIAN) {
83 // Load the raw bytes
84 uint32_t result;
85 memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
86 return result;
87 } else {
88 return ((static_cast<uint32_t>(static_cast<unsigned char>(ptr[0])))
89 | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[1])) << 8)
90 | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[2])) << 16)
91 | (static_cast<uint32_t>(static_cast<unsigned char>(ptr[3])) << 24));
92 }
93 }
94
95 inline uint64_t DecodeFixed64(const char* ptr) {
96 if (! IS_BIG_ENDIAN) {
97 // Load the raw bytes
98 uint64_t result;
99 memcpy(&result, ptr, sizeof(result)); // gcc optimizes this to a plain load
100 return result;
101 } else {
102 uint64_t lo = DecodeFixed32(ptr);
103 uint64_t hi = DecodeFixed32(ptr + 4);
104 return (hi << 32) | lo;
105 }
106 }
107
108
109 class CountMergeOperator : public AssociativeMergeOperator {
110 public:
111 CountMergeOperator() {
112 mergeOperator_ = MergeOperators::CreateUInt64AddOperator();
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100113 }
114
Marc Kupietz4b799e92018-01-02 11:04:56 +0100115 virtual bool Merge(const Slice& key,
116 const Slice* existing_value,
117 const Slice& value,
118 std::string* new_value,
119 Logger* logger) const override {
120 assert(new_value->empty());
121 ++num_merge_operator_calls;
122 if (existing_value == nullptr) {
123 new_value->assign(value.data(), value.size());
124 return true;
125 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100126
Marc Kupietz4b799e92018-01-02 11:04:56 +0100127 return mergeOperator_->PartialMerge(
128 key,
129 *existing_value,
130 value,
131 new_value,
132 logger);
133 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100134
Marc Kupietz4b799e92018-01-02 11:04:56 +0100135 virtual const char* Name() const override {
136 return "UInt64AddOperator";
137 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100138
Marc Kupietz4b799e92018-01-02 11:04:56 +0100139 private:
140 std::shared_ptr<MergeOperator> mergeOperator_;
141 };
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100142
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100143
Marc Kupietz4b799e92018-01-02 11:04:56 +0100144 class CollocatorIterator : public Iterator {
145 private:
146 char prefixc[sizeof(uint64_t)];
147 Iterator *base_iterator_;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100148
149
Marc Kupietz4b799e92018-01-02 11:04:56 +0100150 public:
151 CollocatorIterator(Iterator* base_iterator)
152 : base_iterator_(base_iterator)
153 {}
154
155 ~CollocatorIterator();
156
157 void setPrefix(char *prefix) {
158 memcpy(prefixc, prefix, sizeof(uint64_t));
159 }
160
161 virtual void SeekToFirst() { base_iterator_->SeekToFirst(); }
162 virtual void SeekToLast() { base_iterator_->SeekToLast(); }
163 virtual void Seek(const rocksdb::Slice& s) { base_iterator_->Seek(s); }
164 virtual void Prev() { base_iterator_->Prev(); }
165 virtual void Next() { base_iterator_->Next(); }
166 virtual Slice key() const;
167 virtual Slice value() const;
168 virtual Status status() const;
169 virtual bool Valid() const;
170 bool isValid();
171 uint64_t intValue();
172 uint64_t intKey();
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100173
Marc Kupietz4b799e92018-01-02 11:04:56 +0100174 };
Marc Kupietz18375e12017-12-24 10:11:18 +0100175
Marc Kupietz4b799e92018-01-02 11:04:56 +0100176 // rocksdb::CollocatorIterator::CollocatorIterator(Iterator* base_iterator) {}
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100177
Marc Kupietz4b799e92018-01-02 11:04:56 +0100178 bool rocksdb::CollocatorIterator::Valid() const {
Marc Kupietz18375e12017-12-24 10:11:18 +0100179 return base_iterator_->Valid() && key().starts_with(std::string(prefixc,3));
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100180 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100181
Marc Kupietz4b799e92018-01-02 11:04:56 +0100182 bool rocksdb::CollocatorIterator::isValid() {
183 return base_iterator_->Valid() && key().starts_with(std::string(prefixc,3));
184 }
Marc Kupietz18375e12017-12-24 10:11:18 +0100185
Marc Kupietz4b799e92018-01-02 11:04:56 +0100186 uint64_t rocksdb::CollocatorIterator::intKey() {
187 return DecodeFixed64(base_iterator_->key().data());
188 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100189
Marc Kupietz4b799e92018-01-02 11:04:56 +0100190 uint64_t rocksdb::CollocatorIterator::intValue() {
191 return DecodeFixed64(base_iterator_->value().data());
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100192 }
193
Marc Kupietz37359b12018-01-09 21:11:37 +0100194 class VocabEntry {
195 public:
196 string word;
197 uint64_t freq;
198 };
199
Marc Kupietz6aec7682018-01-10 09:47:48 +0100200 class CollocatorDB {
Marc Kupietz4b799e92018-01-02 11:04:56 +0100201 private:
202 WriteOptions merge_option_; // for merge
203 char _one[sizeof(uint64_t)];
204 Slice _one_slice;
Marc Kupietz37359b12018-01-09 21:11:37 +0100205 vector<VocabEntry> _vocab;
206 uint64_t total;
207
Marc Kupietz4b799e92018-01-02 11:04:56 +0100208 protected:
209 std::shared_ptr<DB> db_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100210
Marc Kupietz4b799e92018-01-02 11:04:56 +0100211 WriteOptions put_option_;
212 ReadOptions get_option_;
213 WriteOptions delete_option_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100214
Marc Kupietz4b799e92018-01-02 11:04:56 +0100215 uint64_t default_;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100216
Marc Kupietz4b799e92018-01-02 11:04:56 +0100217 std::shared_ptr<DB> OpenDb(const char *dbname);
Marc Kupietz6bb27762018-01-09 17:53:01 +0100218 std::shared_ptr<DB> OpenDbForRead(const char *dbname);
Marc Kupietz37359b12018-01-09 21:11:37 +0100219 void read_vocab(string fname);
220
Marc Kupietz4b799e92018-01-02 11:04:56 +0100221 public:
Marc Kupietz6aec7682018-01-10 09:47:48 +0100222 CollocatorDB(const char *db_name, bool read_only);
223 ~CollocatorDB();
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100224
Marc Kupietz6aec7682018-01-10 09:47:48 +0100225 // public interface of CollocatorDB.
Marc Kupietz4b799e92018-01-02 11:04:56 +0100226 // All four functions return false
227 // if the underlying level db operation failed.
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100228
Marc Kupietz4b799e92018-01-02 11:04:56 +0100229 // mapped to a levedb Put
230 bool set(const std::string& key, uint64_t value) {
231 // just treat the internal rep of int64 as the string
232 char buf[sizeof(value)];
233 EncodeFixed64(buf, value);
234 Slice slice(buf, sizeof(value));
235 auto s = db_->Put(put_option_, key, slice);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100236
Marc Kupietz4b799e92018-01-02 11:04:56 +0100237 if (s.ok()) {
238 return true;
239 } else {
240 std::cerr << s.ToString() << std::endl;
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100241 return false;
242 }
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100243 }
Marc Kupietz4b799e92018-01-02 11:04:56 +0100244
245 DB *getDb() {
246 return db_.get();
247 }
248
249 // mapped to a rocksdb Delete
250 bool remove(const std::string& key) {
251 auto s = db_->Delete(delete_option_, key);
252
253 if (s.ok()) {
254 return true;
255 } else {
256 std::cerr << s.ToString() << std::endl;
257 return false;
258 }
259 }
260
261 // mapped to a rocksdb Get
262 bool get(const std::string& key, uint64_t* value) {
263 std::string str;
264 auto s = db_->Get(get_option_, key, &str);
265
266 if (s.IsNotFound()) {
267 // return default value if not found;
268 *value = default_;
269 return true;
270 } else if (s.ok()) {
271 // deserialization
272 if (str.size() != sizeof(uint64_t)) {
273 std::cerr << "value corruption\n";
274 return false;
275 }
276 *value = DecodeFixed64(&str[0]);
277 return true;
278 } else {
279 std::cerr << s.ToString() << std::endl;
280 return false;
281 }
282 }
283
284
285 uint64_t get(const uint32_t w1, const uint32_t w2, const int8_t dist) {
286 char encoded_key[sizeof(uint64_t)];
287 EncodeFixed64(encoded_key, encodeCollocation(w1,w2,dist));
288 uint64_t value = default_;
289 get(std::string(encoded_key, 8), &value);
290 return value;
291 }
292
293 virtual void inc(const std::string& key) {
294 db_->Merge(merge_option_, key, _one_slice);
295 }
296
297 void inc(const uint64_t key) {
298 char encoded_key[sizeof(uint64_t)];
299 EncodeFixed64(encoded_key, key);
300 db_->Merge(merge_option_, std::string(encoded_key, 8), _one_slice);
301 }
302
303 virtual void inc(const uint32_t w1, const uint32_t w2, const uint8_t dist);
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100304 void dump(uint32_t w1, uint32_t w2, int8_t dist);
Marc Kupietz37359b12018-01-09 21:11:37 +0100305 vector<Collocator> get_collocators(uint32_t w1);
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100306 string collocators2json(vector<Collocator> collocators);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100307
Marc Kupietz4b799e92018-01-02 11:04:56 +0100308 // mapped to a rocksdb Merge operation
309 virtual bool add(const std::string& key, uint64_t value) {
310 char encoded[sizeof(uint64_t)];
311 EncodeFixed64(encoded, value);
312 Slice slice(encoded, sizeof(uint64_t));
313 auto s = db_->Merge(merge_option_, key, slice);
314
315 if (s.ok()) {
316 return true;
317 } else {
318 std::cerr << s.ToString() << std::endl;
319 return false;
320 }
321 }
322
323 CollocatorIterator* SeekIterator(uint64_t w1, uint64_t w2, int8_t dist);
324 };
325
Marc Kupietz6aec7682018-01-10 09:47:48 +0100326 rocksdb::CollocatorDB::CollocatorDB(const char *db_name, bool read_only = false) {
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100327 // merge_option_.sync = true;
Marc Kupietz6bb27762018-01-09 17:53:01 +0100328 if(read_only)
329 db_ = OpenDbForRead(db_name);
330 else
331 db_ = OpenDb(db_name);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100332 assert(db_);
333 uint64_t one = 1;
334 EncodeFixed64(_one, one);
335 _one_slice = Slice(_one, sizeof(uint64_t));
336 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100337
Marc Kupietz4b799e92018-01-02 11:04:56 +0100338 rocksdb::CollocatorIterator::~CollocatorIterator() {
339 std::cout << "destroying itera\n";
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100340 }
341
Marc Kupietz6aec7682018-01-10 09:47:48 +0100342 rocksdb::CollocatorDB::~CollocatorDB() {
Marc Kupietz4b799e92018-01-02 11:04:56 +0100343 std::cout << "destroying coll\n";
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100344 }
345
Marc Kupietz6aec7682018-01-10 09:47:48 +0100346 void rocksdb::CollocatorDB::inc(const uint32_t w1, const uint32_t w2, const uint8_t dist) {
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100347 inc(encodeCollocation(w1, w2, dist));
348 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100349
Marc Kupietz6aec7682018-01-10 09:47:48 +0100350 void rocksdb::CollocatorDB::read_vocab(string fname) {
Marc Kupietz37359b12018-01-09 21:11:37 +0100351 char strbuf[2048];
352 uint64_t freq;
353 FILE *fin = fopen(fname.c_str(), "rb");
354 if (fin == NULL) {
355 cout << "Vocabulary file " << fname <<" not found\n";
356 exit(1);
357 }
358 uint64_t i = 0;
359 while(!feof(fin)) {
360 fscanf(fin, "%s %" PRIu64, strbuf, &freq);
361 _vocab.push_back({strbuf, freq});
362 total += freq;
363 i++;
364 }
365 fclose(fin);
366 }
367
Marc Kupietz6aec7682018-01-10 09:47:48 +0100368 std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDbForRead(const char *name) {
Marc Kupietz6bb27762018-01-09 17:53:01 +0100369 DB* db;
370 Options options;
Marc Kupietz37359b12018-01-09 21:11:37 +0100371 ostringstream dbname, vocabname;
Marc Kupietz6bb27762018-01-09 17:53:01 +0100372 dbname << name << ".rocksdb";
373 auto s = DB::OpenForReadOnly(options, dbname.str(), &db);
374 if (!s.ok()) {
375 std::cerr << s.ToString() << std::endl;
376 assert(false);
377 }
Marc Kupietz37359b12018-01-09 21:11:37 +0100378 vocabname << name << ".vocab";
379 read_vocab(vocabname.str());
Marc Kupietz6bb27762018-01-09 17:53:01 +0100380 return std::shared_ptr<DB>(db);
381 }
382
Marc Kupietz6aec7682018-01-10 09:47:48 +0100383 std::shared_ptr<DB> rocksdb::CollocatorDB::OpenDb(const char *dbname) {
Marc Kupietz4b799e92018-01-02 11:04:56 +0100384 DB* db;
385 Options options;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100386
387
388 options.env->SetBackgroundThreads(4);
Marc Kupietz4b799e92018-01-02 11:04:56 +0100389 options.create_if_missing = true;
390 options.merge_operator = std::make_shared<CountMergeOperator>();
391 options.max_successive_merges = 0;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100392 // options.prefix_extractor.reset(NewFixedPrefixTransform(8));
393 options.IncreaseParallelism(70);
394 // options.OptimizeLevelStyleCompaction();
395 options.max_write_buffer_number = 48;
396 options.max_background_jobs = 48;
397 options.allow_concurrent_memtable_write=true;
398 // options.memtable_factory.reset(rocksdb::NewHashLinkListRepFactory(200000));
399 // options.enable_write_thread_adaptive_yield = 1;
400 // options.allow_concurrent_memtable_write = 1;
401 // options.memtable_factory.reset(new rocksdb::SkipListFactory);
402 // options.write_buffer_size = 1 << 22;
403 // options.allow_mmap_reads = true;
404 // options.allow_mmap_writes = true;
405 options.max_background_compactions = 40;
406 // BlockBasedTableOptions table_options;
407 // table_options.filter_policy.reset(NewBloomFilterPolicy(24, false));
408 // options.bloom_locality = 1;
409 // std::shared_ptr<Cache> cache = NewLRUCache(512 * 1024 * 1024);
410 // table_options.block_cache = cache;
411 // options.table_factory.reset(NewBlockBasedTableFactory(table_options));
Marc Kupietz4b799e92018-01-02 11:04:56 +0100412 Status s;
413 // DestroyDB(dbname, Options());
414 s = DB::Open(options, dbname, &db);
415 if (!s.ok()) {
416 std::cerr << s.ToString() << std::endl;
417 assert(false);
418 }
419 return std::shared_ptr<DB>(db);
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100420 }
421
Marc Kupietz6aec7682018-01-10 09:47:48 +0100422 CollocatorIterator* rocksdb::CollocatorDB::SeekIterator(uint64_t w1, uint64_t w2, int8_t dist) {
Marc Kupietz18375e12017-12-24 10:11:18 +0100423 ReadOptions options;
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100424 options.prefix_same_as_start = true;
Marc Kupietz18375e12017-12-24 10:11:18 +0100425 char prefixc[sizeof(uint64_t)];
426 EncodeFixed64(prefixc, encodeCollocation(w1, w2, dist));
427 Iterator *it = db_->NewIterator(options);
428 CollocatorIterator *cit = new CollocatorIterator(it);
429 cit->Seek(std::string(prefixc,3));// it->Valid() && it->key().starts_with(std::string(prefixc,3)); it->Next()) {
430 cit->setPrefix(prefixc);
431 return cit;
432 }
433
Marc Kupietz6aec7682018-01-10 09:47:48 +0100434 void rocksdb::CollocatorDB::dump(uint32_t w1, uint32_t w2, int8_t dist) {
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100435 auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, w2, dist));
436 for (; it->isValid(); it->Next()) {
437 uint64_t value = it->intValue();
438 uint64_t key = it->intKey();
439 std::cout << "w1:" << W1(key) << ", w2:" << W2(key) << ", dist:" << (int32_t) DIST(key) << " - count:" << value << std::endl;
440 }
441 std::cout << "ready dumping\n";
442 }
443
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100444 double calculateLLR(uint64_t f_X_, uint64_t uintN, uint64_t f_X_Y_, uint64_t f_Y_) {
445 double f_e_, f_o_;
446 double A=0.0, B=0.0, C=0.0, D=0.0, N=0.0;
447 double LLR=0.0, statVal=0.0, minusDiffCoeff=0.0;
448 double BlogB=0.0, ClogC=0.0;
449
450 N = (double)uintN;
451 A = (double)f_X_Y_;
452 B = (double)f_X_ -A;
453 C = (double)f_Y_ -A;
454 D = (double)N -A-B-C;;
455
456 if (B > 0.) BlogB = B*log(B);
457 if (C > 0.) ClogC = C*log(C);
458
459 if ((A>0.) && (D>0.) && (N>0.)) {
460 f_e_ = (double)f_X_ /(double)N;
461 f_o_ = (double)f_X_Y_/(double)f_Y_;
462
463 minusDiffCoeff =
464 ( f_X_==0 ? (double)((-1)*f_X_Y_) :
465 ( f_X_Y_==0 ? (double)((+1)*f_X_) :
466 (f_e_-f_o_)/(f_e_+f_o_)
467 )
468 );
469
470 /* log likelihood ratio */
471 LLR = 2*( A*log(A)
472 +BlogB
473 +ClogC
474 +D*log(D)
475 -(A+B)*log(A+B)
476 -(A+C)*log(A+C)
477 -(B+D)*log(B+D)
478 -(C+D)*log(C+D)
479 +N*log(N)
480 );
481 }
482 return(minusDiffCoeff > 0 ? 0 : (statVal=LLR));
483 }
484
485
486 bool sortByNpmi(const Collocator &lhs, const Collocator &rhs) { return lhs.npmi > rhs.npmi; }
487 bool sortByLfmd(const Collocator &lhs, const Collocator &rhs) { return lhs.lfmd > rhs.lfmd; }
488
Marc Kupietz6aec7682018-01-10 09:47:48 +0100489 std::vector<Collocator> rocksdb::CollocatorDB::get_collocators(uint32_t w1) {
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100490 std::vector<Collocator> collocators;
491 uint64_t w2, last_w2 = 0xffffffffffffffff;
492 uint64_t sum = 0, total_w1 = 0;
493 for ( auto it = std::unique_ptr<CollocatorIterator>(SeekIterator(w1, 0, 0)); it->isValid(); it->Next()) {
494 uint64_t value = it->intValue(),
495 key = it->intKey();
496 w2 = W2(key);
497 total_w1 += value;
498 if(last_w2 == 0xffffffffffffffff) last_w2 = w2;
499 if (w2 != last_w2) {
500 double pmi = log2( total * ((double) sum) /
Marc Kupietz37359b12018-01-09 21:11:37 +0100501 (AVG_WINDOW_SIZE * ((double)_vocab[w1].freq) * ((double)_vocab[last_w2].freq) ));
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100502 // Thanopoulos, A., Fakotakis, N., Kokkinakis, G.: Comparative evaluation of collocation extraction metrics. In: International Conference on Language Resources and Evaluation (LREC-2002). (2002) 620–625
Marc Kupietz37359b12018-01-09 21:11:37 +0100503 // double md = log2(pow((double)sum * AVG_WINDOW_SIZE / total, 2) / (AVG_WINDOW_SIZE * ((double)_vocab[w1].freq/total) * ((double)_vocab[last_w2].freq/total)));
504 double md = log2((double)sum * sum / ((double) total * AVG_WINDOW_SIZE * AVG_WINDOW_SIZE * _vocab[w1].freq * _vocab[last_w2].freq));
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100505 collocators.push_back ( {last_w2, sum, pmi, pmi / (-log2(((double) sum / AVG_WINDOW_SIZE / total))), /* normalize to [-1,1] */
Marc Kupietz37359b12018-01-09 21:11:37 +0100506 calculateLLR(_vocab[w1].freq, total, sum, _vocab[last_w2].freq), md, md + log2((double)sum / AVG_WINDOW_SIZE / total), pmi*sum/total/AVG_WINDOW_SIZE} );
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100507 last_w2 = w2;
508 sum = value;
509 } else {
510 sum += value;
511 }
512 }
513
514 sort(collocators.begin(), collocators.end(), sortByLfmd);
515
516 int i=0;
517 for (Collocator c : collocators) {
518 if(i++>10) break;
Marc Kupietz37359b12018-01-09 21:11:37 +0100519 std::cout << "w1:" << _vocab[w1].word << ", w2:" << _vocab[c.w2].word
520 << "\t f(w1):" << _vocab[w1].freq
521 << "\t f(w2):" << _vocab[c.w2].freq
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100522 << "\t f(w1, x):" << total_w1
523 << "\t f(w1, w2):" << c.sum
524 << "\t pmi:" << c.pmi
525 << "\t npmi:" << c.npmi
526 << "\t llr:" << c.llr
527 << "\t md:" << c.md
528 << "\t lfmd:" << c.lfmd
529 << "\t fpmi:" << c.fpmi
530 << "\t total:" << total
531 << std::endl;
532 }
533 return collocators;
534 }
535
Marc Kupietz4b799e92018-01-02 11:04:56 +0100536 rocksdb::Slice rocksdb::CollocatorIterator::key() const { return base_iterator_->key(); }
537 rocksdb::Slice rocksdb::CollocatorIterator::value() const { return base_iterator_->value(); }
538 rocksdb::Status rocksdb::CollocatorIterator::status() const { return base_iterator_->status(); }
539
Marc Kupietz28cc53e2017-12-23 17:24:55 +0100540};
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100541
Marc Kupietz6aec7682018-01-10 09:47:48 +0100542string rocksdb::CollocatorDB::collocators2json(vector<Collocator> collocators) {
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100543 ostringstream s;
544 s << "[";
545 bool first = true;
546 for (Collocator c : collocators) {
547 if(!first)
548 s << ",\n";
549 else
550 first = false;
551 s << "{"
552 "\"word\":\"" << string(_vocab[c.w2].word) << "\"," <<
553 "\"rank\":" << c.w2 << "," <<
554 "\"npmi\":" << c.npmi << "," <<
555 "\"llr\":" << c.llr << "," <<
556 "\"lfmd\":" << c.lfmd << "," <<
557 "\"mi\":" << c.fpmi <<
558 "}";
559 }
560 s << "]\n";
561 return s.str();
562}
563
Marc Kupietz6aec7682018-01-10 09:47:48 +0100564typedef rocksdb::CollocatorDB COLLOCATORS;
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100565
566extern "C" {
Marc Kupietz6aec7682018-01-10 09:47:48 +0100567 COLLOCATORS *open_collocatordb_for_write(char *dbname) {
568 return new rocksdb::CollocatorDB(dbname, false);
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100569 }
570
Marc Kupietz6aec7682018-01-10 09:47:48 +0100571 COLLOCATORS *open_collocatordb(char *dbname) {
572 return new rocksdb::CollocatorDB(dbname, true);
Marc Kupietz6bb27762018-01-09 17:53:01 +0100573 }
574
Marc Kupietz6aec7682018-01-10 09:47:48 +0100575 void inc_collocator(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100576 db->inc(w1, w2, dist);
577 }
578
579 void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
580 db->dump(w1, w2, dist);
581 }
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100582
Marc Kupietz37359b12018-01-09 21:11:37 +0100583 void get_collocators(COLLOCATORS *db, uint32_t w1) {
584 db->get_collocators(w1);
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100585 }
586
Marc Kupietz37359b12018-01-09 21:11:37 +0100587 const char *get_collocators_as_json(COLLOCATORS *db, uint32_t w1) {
588 return strdup(db->collocators2json(db->get_collocators(w1)).c_str());
Marc Kupietzc8ddf452018-01-07 21:33:12 +0100589 }
Marc Kupietz06c9a9f2018-01-02 16:56:43 +0100590}