/
ic_history.cpp
423 lines (374 loc) · 11.2 KB
/
ic_history.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
*
* The contents of this file are subject to the terms of either the GNU Lesser
* General Public License Version 2.1 only ("LGPL") or the Common Development and
* Distribution License ("CDDL")(collectively, the "License"). You may not use this
* file except in compliance with the License. You can obtain a copy of the CDDL at
* http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
* http://www.opensource.org/licenses/lgpl-license.php. See the License for the
* specific language governing permissions and limitations under the License. When
* distributing the software, include this License Header Notice in each file and
* include the full text of the License in the License file as well as the
* following notice:
*
* NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
* (CDDL)
* For Covered Software in this distribution, this License shall be governed by the
* laws of the State of California (excluding conflict-of-law provisions).
* Any litigation relating to this License shall be subject to the jurisdiction of
* the Federal Courts of the Northern District of California and the state courts
* of the State of California, with venue lying in Santa Clara County, California.
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or only
* the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
* include this software in this distribution under the [CDDL or LGPL Version 2.1]
* license." If you don't indicate a single choice of license, a recipient has the
* option to distribute your version of this file under either the CDDL or the LGPL
* Version 2.1, or to extend the choice of license to its licensees as provided
* above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
* Version 2 license, then the option applies only if the new code is made subject
* to such option by the copyright holder.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <cassert>
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include "ic_history.h"
const unsigned CICHistory::DCWID = (unsigned)-1;
CICHistory::~CICHistory()
{
}
const size_t CBigramHistory::contxt_memory_size = 8192;
const double CBigramHistory::focus_memory_ratio = 0.05;
//FIXME: CBigramHistory need to be thread safe
CBigramHistory::CBigramHistory() : m_memory(), m_unifreq(), m_bifreq()
{
initStopWords();
}
CBigramHistory::~CBigramHistory()
{
}
bool
CBigramHistory::memorize(unsigned* its_wid, unsigned* ite_wid)
{
TBigram bigram(DCWID, DCWID);
// First , we insert an DC word id before the context history
// to seperated from previous stream.
if (m_memory.size() == contxt_memory_size) {
TBigram hb;
hb.first = m_memory.front();
m_memory.pop_front();
hb.second = m_memory.front();
decUniFreq(hb.first);
decBiFreq(hb);
}
m_memory.push_back(DCWID);
//Now trying to memorize new stream and forget oldest
for (; its_wid != ite_wid; ++its_wid) {
if (m_memory.size() == contxt_memory_size) {
TBigram hb;
hb.first = m_memory.front();
m_memory.pop_front();
hb.second = m_memory.front();
decUniFreq(hb.first);
decBiFreq(hb);
}
bigram.first = bigram.second;
bigram.second = *its_wid;
m_memory.push_back(*its_wid);
incUniFreq(bigram.second);
incBiFreq(bigram);
}
return true;
}
void
CBigramHistory::clear()
{
m_memory.clear();
m_unifreq.clear();
m_bifreq.clear();
}
double
CBigramHistory::pr(unsigned* its_wid, unsigned* ite_wid)
{
TBigram bigram(DCWID, DCWID);
if (its_wid != ite_wid) {
--ite_wid;
bigram.second = *ite_wid;
if (its_wid != ite_wid)
bigram.first = *(ite_wid - 1);
}
return pr(bigram);
}
double
CBigramHistory::pr(unsigned* its_wid, unsigned* ite_wid, unsigned wid)
{
TBigram bigram(DCWID, DCWID);
if (its_wid != ite_wid)
bigram.first = *(ite_wid - 1);
bigram.second = wid;
return pr(bigram);
}
inline uint16_t
swap16(uint16_t x)
{
return((x << 8) | ((x >> 8) & 0xff));
}
inline uint32_t
swap32(uint32_t x)
{
return((swap16(x) << 16) | (swap16(x >> 16) & 0xffff));
}
bool
CBigramHistory::bufferize(void** buf_ptr, size_t* sz)
{
*buf_ptr = NULL;
*sz = 0;
try {
*sz = sizeof(uint32_t) * m_memory.size();
if (*sz > 0) {
*buf_ptr = malloc(*sz); // malloc for C compatible
#ifdef WORDS_BIGENDIAN
std::copy(m_memory.begin(), m_memory.end(), (uint32_t*)*buf_ptr);
#else
std::transform(m_memory.begin(),
m_memory.end(), (uint32_t*)*buf_ptr, swap32);
#endif
}
return true;
} catch (...) {
if (*buf_ptr)
free(*buf_ptr);
*buf_ptr = NULL;
*sz = 0;
}
return false;
}
bool
CBigramHistory::loadFromFile(const char *fname)
{
m_history_path = fname;
bool suc = false;
int fd = open(fname, O_CREAT, 0600);
if (fd == -1) {
suc = loadFromBuffer(NULL, 0);
return suc;
}
struct stat info;
fstat(fd, &info);
void* buf = malloc(info.st_size);
if (buf) {
read(fd, buf, info.st_size);
suc = loadFromBuffer(buf, info.st_size);
free(buf);
}
close(fd);
return suc;
}
bool
CBigramHistory::saveToFile(const char *fname)
{
if (!fname)
fname = m_history_path.c_str();
bool suc = false;
size_t sz = 0;
void* buf = NULL;
if (bufferize(&buf, &sz) && buf) {
FILE* fp = fopen(fname, "wb");
if (fp) {
suc = (fwrite(buf, 1, sz, fp) == sz);
fclose(fp);
}
free(buf);
}
return suc;
}
bool
CBigramHistory::loadFromBuffer(void* buf_ptr, size_t sz)
{
clear();
sz /= sizeof(uint32_t);
uint32_t *pw = (uint32_t*)buf_ptr;
if (pw && sz > 0) {
#ifndef WORDS_BIGENDIAN
std::transform(pw, pw + sz, pw, swap32);
#endif
TBigram bigram(DCWID, DCWID);
for (unsigned i = 0; i < sz; ++i) {
bigram.first = bigram.second;
bigram.second = *pw++;
m_memory.push_back(bigram.second);
incUniFreq(bigram.second);
incBiFreq(bigram);
}
}
return true;
}
double
CBigramHistory::pr(TBigram& bigram)
{
int uf0 = uniFreq(bigram.first);
int bf = biFreq(bigram);
int uf1 = uniFreq(bigram.second);
double pr = 0.0;
pr += 0.68 * double(bf) / double(uf0 + 0.5);
pr += 0.32 * double(uf1) /
double(m_memory.size() + (contxt_memory_size - m_memory.size()) / 10);
#ifdef DEBUG
if (pr != 0)
fprintf(stderr, "uf0:%d bf:%d uf1:%d pr(%d|%d):%lf\n", uf0, bf, uf1,
bigram.second, bigram.first, pr);
#endif
return pr;
}
int
CBigramHistory::uniFreq(TUnigram& ug)
{
int freq = 0;
if (m_stopWords.find(ug) == m_stopWords.end()) {
TUnigramPool::iterator it = m_unifreq.find(ug);
if (it != m_unifreq.end()) {
freq = it->second;
TContextMemory::reverse_iterator rit = m_memory.rbegin();
for (int i =
0;
rit != m_memory.rend() && i < contxt_memory_size *
focus_memory_ratio;
i++) {
if (*rit == ug)
freq += 1.0 / focus_memory_ratio;
*rit++;
}
}
}
//if (freq != 0) printf("uniFreq[%d]-->%d\n", ug, freq);
return freq / 2;
}
int
CBigramHistory::biFreq(TBigram& bg)
{
int freq = 0;
//std::set<unsigned>::const_iterator ite = m_stopWords.end();
if (m_stopWords.find(bg.first) == m_stopWords.end()
&& m_stopWords.find(bg.second) == m_stopWords.end()) {
TBigramPool::const_iterator it = m_bifreq.find(bg);
if (it != m_bifreq.end()) {
freq = it->second;
TContextMemory::reverse_iterator re = m_memory.rbegin();
TContextMemory::reverse_iterator rs = re + 1;
for (int i = 0;
rs != m_memory.rend() && i < contxt_memory_size *
focus_memory_ratio;
i++) {
if (*rs == bg.first && *re == bg.second)
freq += 1.0 / focus_memory_ratio;
++rs; ++re;
}
}
}
//if (freq != 0) printf("biFreq[%d,%d]-->%d\n", bg.first, bg.second, freq);
return freq;
}
void
CBigramHistory::decUniFreq(TUnigram& ug)
{
TUnigramPool::iterator it = m_unifreq.find(ug);
if (it != m_unifreq.end()) {
if (it->second > 1)
--(it->second);
else
m_unifreq.erase(it);
}
}
bool
CBigramHistory::seenBefore(unsigned wid)
{
return(wid != DCWID && m_stopWords.find(wid) == m_stopWords.end() &&
m_unifreq.find(wid) != m_unifreq.end());
}
void
CBigramHistory::decBiFreq(TBigram& bg)
{
TBigramPool::iterator it = m_bifreq.find(bg);
if (it != m_bifreq.end()) {
if (it->second > 1)
--(it->second);
else
m_bifreq.erase(it);
}
}
void
CBigramHistory::incUniFreq(TUnigram& ug)
{
++m_unifreq[ug];
//printf("Remebering uniFreq[%d]-->%d\n", ug, m_unifreq[ug]);
}
void
CBigramHistory::incBiFreq(TBigram& bg)
{
++m_bifreq[bg];
//printf("Remebering biFreq[%d,%d]-->%d\n", bg.first, bg.second, m_bifreq[bg]);
}
// so far, it's very expensive to erase a word from bigram pairs, need to design
// a better data structure for this.
//
// And Even though, we may also need to remove the individual characters in this
// word (identified by wid), which is current infeasible,
//
// Here are what we need to do:
// 1. get the wstring by word id from userdict
// 2. iterate the character in this wstring
// 3. get the word id from each character from system lexicon (not supported yet)
// 4. remove the unigrams and bigrams of each character, and the entire word
//
void
CBigramHistory::forget(unsigned wid)
{
TUnigramPool::iterator uni_it = m_unifreq.find(wid);
if (uni_it != m_unifreq.end())
m_unifreq.erase(uni_it);
TBigramPool::iterator it = m_bifreq.begin();
TBigramPool::iterator ite = m_bifreq.end();
while (it != ite) {
TBigram bigram = it->first;
if (bigram.first == wid || bigram.second == wid)
m_bifreq.erase(it++);
else
++it;
}
}
void
CBigramHistory::forget(unsigned *its_wid, unsigned *ite_wid)
{
for (; its_wid < ite_wid; ++its_wid) {
TBigram bigram(*its_wid, DCWID);
if (its_wid + 1 != ite_wid)
bigram.second = *(its_wid + 1);
TBigramPool::iterator it = m_bifreq.find(bigram);
if (it != m_bifreq.end())
m_bifreq.erase(it);
}
}
void
CBigramHistory::addStopWords(const std::set<unsigned int>& stopWords)
{
m_stopWords.insert(stopWords.begin(), stopWords.end());
}
void
CBigramHistory::initStopWords()
{
m_stopWords.clear();
m_stopWords.insert(0); //unknown world
m_stopWords.insert(DCWID); //seperator word id used by history memory interanlly
}