This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
complete.mm
298 lines (232 loc) · 5.44 KB
/
complete.mm
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "include/segtree.hpp"
#include "include/sparsetable.hpp"
#include "include/phrase_map.hpp"
#include "include/suggest.hpp"
#include "include/types.hpp"
#include "include/utils.hpp"
#include "complete.h"
extern "C"
{
#include <gdbm.h>
}
static char* strtolower(char* str)
{
char* inp = str;
while (*str)
{
*str = tolower(*str);
str++;
}
return inp;
}
typedef int (*compar_t)(const void *, const void *);
#define MAXDBSIZE 1000
#define DB_STORE_PATH "/var/mobile/Library/Keyboard/"
static GDBM_FILE dbf = NULL;
static PhraseMap pm;
static RMQ st;
struct sword
{
char *word;
unsigned count;
};
static int sword_cmp(const struct sword *sw1, const struct sword *sw2)
{
int retval;
if(sw1->count > sw2->count)
retval = -1;
else if (sw1->count < sw2->count)
retval = 1;
else
retval = 0;
return retval;
}
static int loadtomem(struct sword **pmemstore)
{
printf("loadtomem called!\n");
datum dword, dcount;
int curword=0;
struct sword *memstore = (struct sword*)malloc(sizeof(struct sword));
dword = gdbm_firstkey(dbf);
if (!dword.dptr)
{
printf("No data in database!\n");
free(memstore);
return 0;
}
while (1)
{
datum nextword;
memstore[curword].word = (char*)malloc(dword.dsize);
if (dword.dptr)
strcpy(memstore[curword].word, dword.dptr);
else
strcpy(memstore[curword].word, "");
dcount = gdbm_fetch(dbf, dword);
if (dcount.dptr)
{
memstore[curword].count = atoi(dcount.dptr);
free(dcount.dptr);
}
else
memstore[curword].count = 0;
nextword = gdbm_nextkey(dbf, dword);
free(dword.dptr);
curword++;
if (!nextword.dptr)
break;
dword = nextword;
memstore = (struct sword*)realloc(memstore, (curword+1)*sizeof(struct sword));
}
*pmemstore = memstore;
return curword;
}
void db_refresh()
{
if (!dbf) return;
struct sword *memstore = NULL;
int numwords = loadtomem(&memstore);
if (numwords <= MAXDBSIZE)
{
free(memstore);
return; // Nothing to be done yet
}
qsort(memstore, numwords, sizeof (struct sword), (compar_t)sword_cmp);
datum dword, dcount;
dcount.dptr = (char*)malloc(16);
// Delete extra records from DB
int i;
for (i=MAXDBSIZE; i<numwords; i++)
{
fflush(stdout);
dword.dptr = memstore[i].word;
dword.dsize = strlen(dword.dptr) + 1;
if (gdbm_delete(dbf, dword))
printf("Deleting '%s' failed! Reason: %s\n", dword.dptr, gdbm_strerror(gdbm_errno));
}
// Normalize word counts and write back to DB
int mincount = memstore[MAXDBSIZE-1].count;
for (i=0; i<MAXDBSIZE; i++)
{
memstore[i].count /= mincount;
dword.dptr = memstore[i].word;
dword.dsize = strlen(dword.dptr) + 1;
sprintf(dcount.dptr, "%d", memstore[i].count);
dcount.dsize = strlen(dcount.dptr) + 1;
gdbm_store(dbf, dword, dcount, GDBM_REPLACE);
}
// Clean up and sync the DB
gdbm_reorganize(dbf);
gdbm_sync(dbf);
for (i=0; i<MAXDBSIZE; i++)
free(memstore[i].word);
free(memstore);
}
int db_store(char *wordin)
{
if (!dbf || !wordin || !*wordin) return 0;
char* word = strtolower(strdup(wordin));
unsigned count;
int retval;
// Check whether the record is already in DB
datum dword, dcount;
dword.dptr = word;
dword.dsize = strlen(word) + 1;
printf("Storing %d:%s\n", dword.dsize, dword.dptr);
NSLog(@"Storing %d:%s\n", dword.dsize, dword.dptr);
dcount = gdbm_fetch(dbf, dword);
if (dcount.dptr)
{
count = atoi(dcount.dptr) + 1;
dcount.dptr = (char*)realloc(dcount.dptr, 16);
sprintf(dcount.dptr, "%u", count);
dcount.dsize = strlen(dcount.dptr) + 1;
} else
{
dcount.dptr = (char*)malloc(4);
strcpy(dcount.dptr, "1");
dcount.dsize = 2;
}
retval = gdbm_store(dbf, dword, dcount, GDBM_REPLACE);
free(dcount.dptr);
free(word);
return retval;
}
unsigned face_init()
{
if (!dbf) return 0;
struct sword *memstore = NULL;
int numwords = loadtomem(&memstore);
pm.repr.clear();
int i;
for (i=0; i<numwords; i++)
pm.insert(memstore[i].count, std::string(memstore[i].word), NULL);
pm.finalize();
for (i=0; i<numwords; i++)
free(memstore[i].word);
if (memstore) free(memstore);
vui_t weights;
for (size_t i = 0; i < pm.repr.size(); ++i)
weights.push_back(pm.repr[i].weight);
st.initialize(weights);
return numwords;
}
vp_t face_query(const char* iline, unsigned num)
{
char* buf = strtolower(strdup(iline));
vp_t r = suggest(pm, st, std::string(buf), num);
free (buf);
return r;
}
bool db_init(const char* inpMode)
{
if (!inpMode || !*inpMode) return false;
char path[512];
sprintf(path, "%s/%s-k3adict.db", DB_STORE_PATH, inpMode);
dbf = gdbm_open(path, 8192, GDBM_WRCREAT|GDBM_NOLOCK, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH, NULL);
return dbf != NULL;
}
bool db_shutdown()
{
if (dbf) gdbm_close(dbf);
dbf = NULL;
return true;
}
bool db_sync()
{
if (dbf) gdbm_sync(dbf);
return true;
}
void db_lowercase()
{
if (!dbf) return;
NSLog(@"LOWERCASE--------");
datum dword, dcount;
int curword=0;
dword = gdbm_firstkey(dbf);
if (!dword.dptr)
{
printf("No data in database!\n");
return;
}
while (1)
{
datum nextword;
if (dword.dptr) strtolower(dword.dptr);
dcount = gdbm_fetch(dbf, dword);
gdbm_store(dbf, dword, dcount, GDBM_REPLACE);
nextword = gdbm_nextkey(dbf, dword);
free(dword.dptr);
free(dcount.dptr);
curword++;
if (!nextword.dptr)
break;
dword = nextword;
}
gdbm_sync(dbf);
NSLog(@"DONE--------");
}