-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngram.c
66 lines (58 loc) · 1.5 KB
/
ngram.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
#include "foreach.h"
#include "minialloc.h"
#include "ngram.h"
#define MAX_NGRAM_ORDER 1023
Ngram ngram_from_string(char *str) {
Token ng[1 + MAX_NGRAM_ORDER];
int ntok = 0;
foreach_token(word, str) {
g_assert(ntok < MAX_NGRAM_ORDER);
ng[++ntok] = token_from_string(word);
}
ng[0] = ntok;
return ngram_dup(ng);
}
Ngram ngram_dup(Ngram ng) {
gsize size = (sizeof(Token) * (ngram_size(ng)+1));
Ngram ngcopy = minialloc(size);
memcpy(ngcopy, ng, size);
return ngcopy;
}
Ngram ngram_cpy(Ngram ngcopy, Ngram ng) {
gsize size = (sizeof(Token) * (ngram_size(ng)+1));
memcpy(ngcopy, ng, size);
return ngcopy;
}
/** Ngram hash functions */
static guint ngram_hash_rnd[1 + MAX_NGRAM_ORDER];
static void init_ngram_hash_rnd() {
guint32 r;
if (*ngram_hash_rnd == 0) {
for (int i = MAX_NGRAM_ORDER; i >= 0; i--) {
/* g_random_int(): Return a random guint32 equally distributed
over the range [0..2^32-1]. */
do {
r = g_random_int();
} while (r == 0);
ngram_hash_rnd[i] = r;
}
}
}
guint ngram_hash(gconstpointer p) {
if (*ngram_hash_rnd == 0) init_ngram_hash_rnd();
const Token *ng = p;
guint hash = 0;
for (int i = ngram_size(ng); i > 0; i--) {
hash += ng[i] * ngram_hash_rnd[i];
}
return hash;
}
gboolean ngram_equal(gconstpointer pa, gconstpointer pb) {
const Token *a = pa;
const Token *b = pb;
if (ngram_size(a) != ngram_size(b)) return 0;
for (int i = ngram_size(a); i > 0; i--) {
if (a[i] != b[i]) return 0;
}
return 1;
}