-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwords-table.c
1314 lines (1111 loc) · 40.9 KB
/
words-table.c
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Functions to handle the words store
*
* Copyright (C) Navaneeth.K.N
*
* This is part of libvarnam. See LICENSE.txt for the license
*/
#include <assert.h>
#include <string.h>
#include "symbol-table.h"
#include "util.h"
#include "vtypes.h"
#include "result-codes.h"
#include "api.h"
#include "token.h"
#include "rendering.h"
#include "varray.h"
#include "vword.h"
#include "words-table.h"
#include "deps/parson.h"
#define MINIMUM_CHARACTER_LENGTH_FOR_SUGGESTION 3
int
vwt_ensure_schema_exists(varnam *handle)
{
const char *pragmas =
"pragma page_size=4096;"
"pragma journal_mode=wal;";
const char *tables =
"create table if not exists metadata (key TEXT UNIQUE, value TEXT);"
"create table if not exists words (id integer primary key, word text unique, confidence integer default 1, learned_on integer);"
#ifdef VARNAM_DISABLE_WITHOUT_ROW_ID_OPTIMIZATION
"create table if not exists patterns_content (pattern text, word_id integer, learned integer default 0, primary key(pattern, word_id));";
#else
"create table if not exists patterns_content (pattern text, word_id integer, learned integer default 0, primary key(pattern, word_id))without rowid;";
#endif
char *zErrMsg = 0;
int rc;
rc = sqlite3_exec(v_->known_words, pragmas, NULL, 0, &zErrMsg);
if( rc != SQLITE_OK ){
set_last_error (handle, "Failed to initialize file for storing known words. Pragma setting failed. : %s", zErrMsg);
sqlite3_free(zErrMsg);
return VARNAM_ERROR;
}
rc = sqlite3_exec(v_->known_words, tables, NULL, 0, &zErrMsg);
if( rc != SQLITE_OK ){
set_last_error (handle, "Failed to initialize file for storing known words. Schema creation failed. : %s", zErrMsg);
sqlite3_free(zErrMsg);
return VARNAM_ERROR;
}
return VARNAM_SUCCESS;
}
static int
execute_sql(varnam *handle, sqlite3 *db, const char *sql)
{
char *zErrMsg = 0;
int rc;
rc = sqlite3_exec(db, sql, NULL, 0, &zErrMsg);
if( rc != SQLITE_OK ){
set_last_error (handle, "Failed to execute : %s", zErrMsg);
sqlite3_free(zErrMsg);
return VARNAM_ERROR;
}
return VARNAM_SUCCESS;
}
int
vwt_start_changes(varnam *handle)
{
assert (v_->known_words);
return execute_sql(handle, v_->known_words, "BEGIN;");
}
int
vwt_end_changes(varnam *handle)
{
assert (v_->known_words);
return execute_sql(handle, v_->known_words, "COMMIT;");
}
int
vwt_discard_changes(varnam *handle)
{
assert (v_->known_words);
return execute_sql(handle, v_->known_words, "ROLLBACK;");
}
int
vwt_optimize_for_huge_transaction(varnam *handle)
{
assert (handle);
assert (v_->known_words);
return execute_sql (handle, v_->known_words, "PRAGMA synchronous = OFF;");
}
int
vwt_turn_off_optimization_for_huge_transaction(varnam *handle)
{
/* const char *sql = */
/* "pragma journal_mode=wal;"; */
/* assert (handle); */
/* assert (v_->known_words); */
/* return execute_sql (handle, v_->known_words, sql); */
return VARNAM_SUCCESS;
}
int
vwt_compact_file (varnam *handle)
{
const char *sql =
"VACUUM;";
assert (handle);
assert (v_->known_words);
return execute_sql (handle, v_->known_words, sql);
}
int
vwt_persist_pattern(varnam *handle, const char *pattern, sqlite3_int64 word_id, bool is_prefix)
{
int rc;
const char *sql = "insert or ignore into patterns_content (pattern, word_id) values (trim(lower(?1)), ?2)";
assert (v_->known_words);
#ifdef _VARNAM_VERBOSE
if (!is_prefix) printf(" %s\n", pattern);
#endif
if (v_->learn_pattern == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, sql, -1, &v_->learn_pattern, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to learn word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->learn_pattern);
return VARNAM_ERROR;
}
}
sqlite3_bind_text (v_->learn_pattern, 1, pattern, -1, NULL);
sqlite3_bind_int64 (v_->learn_pattern, 2, word_id);
rc = sqlite3_step (v_->learn_pattern);
if (rc != SQLITE_DONE) {
set_last_error (handle, "Failed to learn pattern : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->learn_pattern);
return VARNAM_ERROR;
}
sqlite3_reset (v_->learn_pattern);
if (!is_prefix)
{
if (v_->update_learned_flag == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, "update patterns_content set learned = 1 where pattern = trim(lower(?1)) and word_id = ?2 and learned = 0",
-1, &v_->update_learned_flag, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to learn word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->update_learned_flag);
return VARNAM_ERROR;
}
}
sqlite3_bind_text (v_->update_learned_flag, 1, pattern, -1, NULL);
sqlite3_bind_int64 (v_->update_learned_flag, 2, word_id);
rc = sqlite3_step (v_->update_learned_flag);
if (rc != SQLITE_DONE) {
set_last_error (handle, "Failed to learn pattern : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->update_learned_flag);
return VARNAM_ERROR;
}
sqlite3_reset (v_->update_learned_flag);
}
return VARNAM_SUCCESS;
}
int
vwt_get_word_id (varnam *handle, const char *word, sqlite3_int64 *word_id)
{
int rc;
assert (v_->known_words);
if (v_->lastLearnedWord != NULL && strcmp (word, strbuf_to_s (v_->lastLearnedWord)) == 0) {
*word_id = v_->lastLearnedWordId;
return VARNAM_SUCCESS;
}
if (v_->get_word == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, "select id, word, confidence, learned_on from words where word = ?1 limit 1", -1, &v_->get_word, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to get word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->get_word);
return VARNAM_ERROR;
}
}
*word_id = -1;
sqlite3_bind_text (v_->get_word, 1, word, -1, NULL);
rc = sqlite3_step (v_->get_word);
if (rc == SQLITE_ROW) {
*word_id = sqlite3_column_int64 (v_->get_word, 0);
}
else if (rc != SQLITE_DONE) {
set_last_error (handle, "Failed to get word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->get_word);
return VARNAM_ERROR;
}
sqlite3_reset (v_->get_word);
return VARNAM_SUCCESS;
}
/* Learns the pattern. strbuf* is passed in because of memory optimizations.
* See comments in the learn_prefixes() function */
static int
learn_pattern (varnam *handle, varray *tokens, const char *word, strbuf *pattern, bool is_prefix)
{
int rc, i;
sqlite3_int64 word_id;
vtoken *token;
rc = vwt_get_word_id (handle, word, &word_id);
if (rc) return rc;
strbuf_clear (pattern);
for (i = 0; i < varray_length (tokens); i++)
{
token = varray_get (tokens, i);
if (token->type != VARNAM_TOKEN_NON_JOINER && token->type != VARNAM_TOKEN_JOINER)
strbuf_add (pattern, token->pattern);
}
rc = vwt_persist_pattern(handle, strbuf_to_s (pattern), word_id, is_prefix);
if (rc)
return rc;
return VARNAM_SUCCESS;
}
int
vwt_try_insert_new_word (varnam* handle, const char* word, int confidence, sqlite3_int64* new_word_id) {
int rc;
const char *sql = "insert or ignore into words (word, confidence, learned_on) values(trim(?1), ?2, strftime('%s', datetime(), 'localtime'));";
*new_word_id = -1;
if (v_->learn_word == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, sql, -1, &v_->learn_word, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to learn word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->learn_word);
return VARNAM_ERROR;
}
}
sqlite3_bind_text (v_->learn_word, 1, word, -1, NULL);
sqlite3_bind_int (v_->learn_word, 2, confidence);
rc = sqlite3_step (v_->learn_word);
if (rc != SQLITE_DONE) {
set_last_error (handle, "Failed to learn word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->learn_word);
return VARNAM_ERROR;
}
if (sqlite3_changes (v_->known_words) != 0) {
*new_word_id = sqlite3_last_insert_rowid (v_->known_words);
}
sqlite3_reset (v_->learn_word);
return VARNAM_SUCCESS;
}
static int
try_update_word_confidence (varnam* handle, const char* word, bool* updated) {
int rc;
*updated = false;
if (v_->update_confidence == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words,
"update words set confidence = confidence + 1 where word = ?1;", -1,
&v_->update_confidence, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to learn word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->update_confidence);
return VARNAM_ERROR;
}
}
sqlite3_bind_text (v_->update_confidence, 1, word, -1, NULL);
rc = sqlite3_step (v_->update_confidence);
if (rc != SQLITE_DONE) {
set_last_error (handle, "Failed to learn word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->update_confidence);
return VARNAM_ERROR;
}
*updated = sqlite3_changes (v_->known_words);
sqlite3_reset (v_->update_confidence);
return VARNAM_SUCCESS;
}
static int
learn_word (varnam *handle, const char *word, int confidence, bool *new_word)
{
int rc;
bool confidence_updated;
sqlite3_int64 new_word_id;
assert (v_->known_words);
if (v_->lastLearnedWord == NULL) {
v_->lastLearnedWord = strbuf_init (20);
}
strbuf_clear (v_->lastLearnedWord);
if (!v_->_config_mostly_learning_new_words) {
rc = try_update_word_confidence (handle, word, &confidence_updated);
if (rc) return rc;
if (!confidence_updated) {
rc = vwt_try_insert_new_word (handle, word, confidence, &new_word_id);
if (rc) return rc;
strbuf_add (v_->lastLearnedWord, word);
v_->lastLearnedWordId = new_word_id;
}
}
else {
/* This assumes new words won't be already learned. So attempts insert first and fallback to update
* confidence later */
rc = vwt_try_insert_new_word (handle, word, confidence, &new_word_id);
if (rc) return rc;
if (new_word_id == -1) {
rc = try_update_word_confidence (handle, word, &confidence_updated);
if (rc) return rc;
}
else {
strbuf_add (v_->lastLearnedWord, word);
v_->lastLearnedWordId = new_word_id;
}
}
/*varnam_log (handle, "Learned word %s", word);*/
return VARNAM_SUCCESS;
}
/* Learns all the prefixes. This won't learn single tokens and the word itself
* tokens_tmp - Is passed is for memory usage optimization. This function gets
* called inside a cartesion product finder which means there will be a lot of
* instances of array required. To optimize this, we pass in this array which
* will be allocated from cartesian product finder */
static int
learn_prefixes(varnam *handle, varray *tokens, strbuf *pattern, bool word_already_learned)
{
int i, rc, tokens_len = 0;
vword *word;
vtoken *token;
bool new_word;
varray *tokens_tmp = get_pooled_array (handle);
for (i = 0; i < varray_length (tokens); i++)
{
token = varray_get (tokens, i);
assert (token != NULL);
varray_push (tokens_tmp, token);
tokens_len = varray_length (tokens_tmp);
/* We don't learn if it is only one token.
* We don't learn the full word here because it would have already learned before this method is called */
if (tokens_len > 1 && tokens_len != varray_length(tokens))
{
rc = resolve_tokens (handle, tokens_tmp, &word);
if (rc) {
return_array_to_pool (handle, tokens_tmp);
return rc;
}
if (!word_already_learned)
{
rc = learn_word (handle, word->text, 1, &new_word);
if (rc) {
return_array_to_pool (handle, tokens_tmp);
return rc;
}
}
rc = learn_pattern (handle, tokens_tmp, word->text, pattern, true);
if (rc) {
return_array_to_pool (handle, tokens_tmp);
return rc;
}
}
}
return_array_to_pool (handle, tokens_tmp);
return VARNAM_SUCCESS;
}
void
print_tokens_array(varray *tokens)
{
varray *tmp;
vtoken *token;
int i, j;
printf("Tokens\n");
for (i = 0; i < varray_length(tokens); i++)
{
tmp = varray_get (tokens, i);
assert (tmp);
printf("[");
for (j = 0; j < varray_length(tmp); j++)
{
token = varray_get (tmp, j);
assert (token);
if (j + 1 == varray_length(tmp))
printf("%d - '%s'", token->type, token->pattern);
else
printf("%d - '%s', ", token->type, token->pattern);
}
printf("]\n");
}
}
/* This function learns all possibilities of writing the word and it's prefixes.
* It finds cartesian product of the tokens passed in and process each product.
* tokens will be a multidimensional array */
static int
learn_all_possibilities(varnam *handle, varray *tokens, const char *word)
{
int rc, array_cnt, *offsets, i, last_array_offset, total = 0;
varray *array, *tmp;
strbuf *pattern;
bool word_already_learned = false;
array_cnt = varray_length (tokens);
offsets = xmalloc(sizeof(int) * (size_t) array_cnt);
for (i = 0; i < array_cnt; i++) offsets[i] = 0;
array = get_pooled_array (handle);
pattern = get_pooled_string (handle);
for (;;)
{
varray_clear (array);
for (i = 0; i < array_cnt; i++)
{
tmp = varray_get (tokens, i);
assert (tmp);
varray_push (array, varray_get (tmp, offsets[i]));
}
rc = learn_pattern (handle, array, word, pattern, false);
if (rc)
goto finished;
rc = learn_prefixes (handle, array, pattern, word_already_learned);
if (rc)
goto finished;
word_already_learned = true;
if (++total == MAXIMUM_PATTERNS_TO_LEARN) {
goto finished;
}
last_array_offset = array_cnt - 1;
offsets[last_array_offset]++;
while (offsets[last_array_offset] == varray_length ((varray*) varray_get (tokens, last_array_offset)))
{
offsets[last_array_offset] = 0;
if (--last_array_offset < 0) goto finished;
offsets[last_array_offset]++;
}
}
finished:
xfree (offsets);
return rc;
}
int
vwt_persist_possibilities(varnam *handle, varray *tokens, const char *word, int confidence)
{
int rc;
bool new_word;
rc = learn_word (handle, word, confidence, &new_word);
if (rc) return rc;
rc = learn_all_possibilities (handle, tokens, word);
if (rc) return rc;
return VARNAM_SUCCESS;
}
int
vwt_get_best_match (varnam *handle, const char *input, varray *words)
{
int rc;
vword *word;
const char *sql = "select word, confidence from words where rowid in "
"(SELECT word_id FROM patterns_content as pc where pc.pattern = lower(?1) and learned = 1 limit 5) "
"order by confidence desc";
assert (handle);
assert (words);
if (v_->known_words == NULL)
return VARNAM_SUCCESS;
if (strlen(input) < MINIMUM_CHARACTER_LENGTH_FOR_SUGGESTION)
return VARNAM_SUCCESS;
if (v_->get_best_match == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, sql, -1, &v_->get_best_match, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to get best matches : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->get_best_match);
return VARNAM_ERROR;
}
}
sqlite3_bind_text (v_->get_best_match, 1, input, -1, NULL);
for (;;)
{
rc = sqlite3_step (v_->get_best_match);
if (rc == SQLITE_ROW)
{
word = get_pooled_word (handle,
(const char*) sqlite3_column_text(v_->get_best_match, 0),
(int) sqlite3_column_int(v_->get_best_match, 1));
varray_push (words, word);
}
else if (rc == SQLITE_DONE)
{
break;
}
else
{
set_last_error (handle, "Failed to get best match : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->get_best_match);
return VARNAM_ERROR;
}
}
sqlite3_clear_bindings (v_->get_best_match);
sqlite3_reset (v_->get_best_match);
return VARNAM_SUCCESS;
}
int
vwt_get_suggestions (varnam *handle, const char *input, varray *words)
{
int rc;
vword *word;
const char *sql = "select word, confidence from words where rowid in "
"(SELECT distinct(word_id) FROM patterns_content as pc where pc.pattern > lower(?1) and pc.pattern <= lower(?1) || 'z' and learned = 1 limit 5) "
"order by confidence desc";
assert (handle);
assert (words);
if (v_->known_words == NULL)
return VARNAM_SUCCESS;
if (strlen(input) < MINIMUM_CHARACTER_LENGTH_FOR_SUGGESTION)
return VARNAM_SUCCESS;
if (v_->get_suggestions == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, sql, -1, &v_->get_suggestions, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to get suggestions : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->get_suggestions);
return VARNAM_ERROR;
}
}
sqlite3_bind_text (v_->get_suggestions, 1, input, -1, NULL);
for (;;)
{
rc = sqlite3_step (v_->get_suggestions);
if (rc == SQLITE_ROW)
{
word = get_pooled_word (handle,
(const char*) sqlite3_column_text(v_->get_suggestions, 0),
(int) sqlite3_column_int(v_->get_suggestions, 1));
if (!varray_exists (words, word, &word_equals))
{
varray_push (words, word);
}
}
else if (rc == SQLITE_DONE)
{
break;
}
else
{
set_last_error (handle, "Failed to get suggestions : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->get_suggestions);
return VARNAM_ERROR;
}
}
sqlite3_clear_bindings (v_->get_suggestions);
sqlite3_reset (v_->get_suggestions);
return VARNAM_SUCCESS;
}
static int
get_matches (varnam *handle, strbuf *lookup, varray *matches, bool *found)
{
int rc;
const char *sql = "select word from words where rowid in (select distinct(word_id) from patterns_content where pattern = ?1 limit 3);";
strbuf *word;
bool cleared = false;
assert (v_->known_words);
if (v_->get_matches_for_word == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, sql, -1, &v_->get_matches_for_word, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to get matches : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->get_matches_for_word);
return VARNAM_ERROR;
}
}
sqlite3_bind_text (v_->get_matches_for_word, 1, strbuf_to_s(lookup), -1, NULL);
*found = false;
for (;;)
{
rc = sqlite3_step (v_->get_matches_for_word);
if (rc == SQLITE_ROW)
{
if (!cleared) {
varray_clear (matches);
cleared = true;
}
word = get_pooled_string (handle);
strbuf_add (word, (const char*) sqlite3_column_text(v_->get_matches_for_word, 0));
varray_push (matches, word);
*found = true;
}
else if (rc == SQLITE_DONE)
{
break;
}
else
{
set_last_error (handle, "Failed to get matches : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->get_matches_for_word);
return VARNAM_ERROR;
}
}
sqlite3_clear_bindings (v_->get_matches_for_word);
sqlite3_reset (v_->get_matches_for_word);
return VARNAM_SUCCESS;
}
static int
can_find_possible_matches (varnam *handle, strbuf *lookup, bool *possible)
{
int rc;
const char *sql = "SELECT distinct(word_id) FROM patterns_content as pc where pc.pattern > ?1 and pc.pattern <= ?1 || 'z' limit 1;";
assert (v_->known_words);
if (v_->possible_to_find_matches == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, sql, -1, &v_->possible_to_find_matches, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to check for possible matches : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->possible_to_find_matches);
return VARNAM_ERROR;
}
}
sqlite3_bind_text (v_->possible_to_find_matches, 1, strbuf_to_s(lookup), -1, NULL);
*possible = false;
rc = sqlite3_step (v_->possible_to_find_matches);
if (rc == SQLITE_ROW)
{
*possible = true;
}
else if (rc != SQLITE_DONE)
{
set_last_error (handle, "Failed to check for possible matches : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->possible_to_find_matches);
return VARNAM_ERROR;
}
sqlite3_clear_bindings (v_->possible_to_find_matches);
sqlite3_reset (v_->possible_to_find_matches);
return VARNAM_SUCCESS;
}
/* Gets the first element of each array in the specified multidimensional array */
static varray*
get_first_elements(varnam *handle, varray *source)
{
int i, j;
varray *a;
varray *result = get_pooled_array (handle);
for (i = 0; i < varray_length (source); i++)
{
a = varray_get (source, i);
for (j = 0; j < varray_length (a); j++)
{
varray_push (result, varray_get (a, j));
break;
}
}
return result;
}
/* tokens will be a multidimensional array */
static void
add_tokens (varnam *handle, varray *tokens, varray *result, bool first_match)
{
varray *tmp, *item;
int j , k;
tmp = get_first_elements (handle, tokens);
if (first_match) {
varray_push (result, tmp);
}
else
{
/* Append tokens to each element in the result */
for (j = 0; j < varray_length (result); j++)
{
item = varray_get (result, j);
for (k = 0; k < varray_length (tmp); k++)
{
varray_push (item, varray_get (tmp, k));
}
}
}
}
static int
symbols_tokenize_add_to_result(varnam *handle, strbuf *lookup, varray *result)
{
int rc;
varray *tokens;
#ifdef _VARNAM_VERBOSE
varnam_debug (handle, "Symbols tokenizing - %s", strbuf_to_s(lookup));
#endif
tokens = get_pooled_array (handle);
if (!strbuf_is_blank (lookup))
{
rc = vst_tokenize (handle, strbuf_to_s (lookup), VARNAM_TOKENIZER_PATTERN, VARNAM_MATCH_EXACT, tokens);
if (rc) return rc;
add_tokens (handle, tokens, result, false);
strbuf_clear (lookup);
}
return_array_to_pool (handle, tokens);
return VARNAM_SUCCESS;
}
/* Finds the longest possible match from the words table. Remaining string will be
converted using symbols tokenization */
int
vwt_tokenize_pattern (varnam *handle, const char *pattern, varray *result)
{
int rc, matchpos = 0, pos = 0, i;
strbuf *lookup, *for_symbols_tokenization, *match;
varray *matches; /* contains strbuf* instances */
varray *tokens; /* Contains arrays that contains vtoken* instances */
bool found = false, possible = false, first_match = true;
const char *pc;
varray_clear (result);
if (v_->known_words == NULL)
return VARNAM_SUCCESS;
if (pattern == NULL || *pattern == '\0')
return VARNAM_SUCCESS;
lookup = get_pooled_string (handle);
matches = get_pooled_array (handle);
tokens = get_pooled_array (handle);
varnam_debug (handle, "Tokenizing '%s' with words tokenizer", pattern);
pc = pattern;
while (*pc != '\0')
{
strbuf_addc (lookup, *pc);
++pos; ++pc;
rc = get_matches (handle, lookup, matches, &found);
if (rc != VARNAM_SUCCESS)
return rc;
if (found) {
matchpos = pos;
}
rc = can_find_possible_matches (handle, lookup, &possible);
if (rc)
return rc;
if (possible)
continue;
else
break;
}
/* At this point we will have the longest possible match. If nothing is available,
* there is no words that matches the prefix. In that case, exiting early */
if (varray_length (matches) == 0 && varray_length(result) == 0) {
return VARNAM_SUCCESS;
}
for(i = 0; i < varray_length (matches); i++)
{
/* Tokenize the match */
match = varray_get (matches, i);
assert (match);
#ifdef _VARNAM_VERBOSE
varnam_debug (handle, "Tokenizing longest prefix match - %s", strbuf_to_s (match));
#endif
rc = vst_tokenize (handle, strbuf_to_s(match), VARNAM_TOKENIZER_VALUE, VARNAM_MATCH_EXACT, tokens);
if (rc) return rc;
add_tokens (handle, tokens, result, first_match);
varray_clear (tokens);
}
pattern = pattern + matchpos;
/* Remaining text will be tokenized literally */
for_symbols_tokenization = get_pooled_string (handle);
strbuf_add (for_symbols_tokenization, pattern);
rc = symbols_tokenize_add_to_result (handle, for_symbols_tokenization, result);
if (rc) return rc;
strbuf_clear (lookup);
strbuf_clear (for_symbols_tokenization);
/* At this point, result will look like
* [[t1,t2,t3], [t4,t5,t6]]*/
return VARNAM_SUCCESS;
}
int
vwt_delete_word(varnam *handle, const char *word)
{
int rc = 0;
sqlite3_int64 word_id = 0;
const char *pattern_sql = "delete from patterns_content where word_id = ?1;";
const char *word_sql = "delete from words where id = ?1;";
rc = vwt_get_word_id (handle, word, &word_id);
if (rc != VARNAM_SUCCESS) {
return rc;
}
if (v_->delete_pattern == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, pattern_sql, -1, &v_->delete_pattern, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to delete word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->delete_pattern);
return VARNAM_ERROR;
}
}
if (v_->delete_word == NULL)
{
rc = sqlite3_prepare_v2( v_->known_words, word_sql, -1, &v_->delete_word, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to delete word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->delete_word);
return VARNAM_ERROR;
}
}
rc = vwt_start_changes (handle);
if (rc != VARNAM_SUCCESS) {
return rc;
}
sqlite3_bind_int64 (v_->delete_pattern, 1, word_id);
rc = sqlite3_step (v_->delete_pattern);
if (rc != SQLITE_DONE) {
set_last_error (handle, "Failed to delete pattern : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->delete_pattern);
vwt_discard_changes (handle);
return VARNAM_ERROR;
}
sqlite3_reset (v_->delete_pattern);
sqlite3_bind_int64 (v_->delete_word, 1, word_id);
rc = sqlite3_step (v_->delete_word);
if (rc != SQLITE_DONE) {
set_last_error (handle, "Failed to delete word : %s", sqlite3_errmsg(v_->known_words));
sqlite3_reset (v_->delete_word);
vwt_discard_changes (handle);
return VARNAM_ERROR;
}
sqlite3_reset (v_->delete_word);
rc = vwt_end_changes (handle);
if (rc != VARNAM_SUCCESS) {
return rc;
}
return VARNAM_SUCCESS;
}
int
vwt_get_words_count(varnam *handle, bool onlyLearned, int *words_count)
{
int rc = 0;
const char *sql;
sqlite3_stmt *stmt = NULL;
if (onlyLearned) {
sql = "select count(distinct(word_id)) as cnt from patterns_content where learned = 1;";
if (v_->learned_words_count == NULL) {
rc = sqlite3_prepare_v2( v_->known_words, sql, -1, &v_->learned_words_count, NULL );
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to get learned words count : %s", sqlite3_errmsg(v_->known_words));
sqlite3_finalize (v_->learned_words_count);
v_->learned_words_count = NULL;
return VARNAM_ERROR;
}
}
stmt = v_->learned_words_count;
}
else {
sql = "select count(id) as cnt from words;";
if (v_->all_words_count == NULL) {
rc = sqlite3_prepare_v2( v_->known_words, sql, -1, &v_->all_words_count, NULL);
if (rc != SQLITE_OK) {
set_last_error (handle, "Failed to get all words count : %s", sqlite3_errmsg(v_->known_words));
sqlite3_finalize (v_->all_words_count);
v_->all_words_count = NULL;
return VARNAM_ERROR;
}
}
stmt = v_->all_words_count;
}
*words_count = 0;
rc = sqlite3_step (stmt);
if (rc == SQLITE_ROW) {
*words_count = sqlite3_column_int (stmt, 0);
}
sqlite3_reset (stmt);
return VARNAM_SUCCESS;
}
static int