summaryrefslogtreecommitdiffstats
path: root/tests/storage/test_flexible_ngram.cpp
blob: d7d7950caebd3f114a04b849f45c7f350bce89d4 (plain)
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
/* 
 *  libpinyin
 *  Library to deal with pinyin.
 *  
 *  Copyright (C) 2012 Peng Wu <alexepico@gmail.com>
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#include "pinyin_internal.h"

int main(int argc, char * argv[]) {
    FlexibleSingleGram<guint32, guint32> single_gram;
    typedef FlexibleSingleGram<guint32, guint32>::ArrayItemWithToken array_item_t;

    const guint32 total_freq = 16;
    assert(single_gram.set_array_header(total_freq));

    phrase_token_t tokens[6] = { 2, 6, 4, 3, 1, 3 };
    guint32 freqs[6] = { 1, 2, 4, 8, 16, 32};

    guint32 freq;

    for ( size_t i = 0; i < G_N_ELEMENTS(tokens); ++i ){
        if ( single_gram.get_array_item(tokens[i], freq) )
            assert(single_gram.set_array_item(tokens[i], freqs[i]));
        else
            assert(single_gram.insert_array_item(tokens[i], freqs[i]));
    }

    single_gram.get_array_item(3, freq);
    assert(freq == 32);

    printf("--------------------------------------------------------\n");
    PhraseIndexRange range;
    FlexibleBigramPhraseArray array = g_array_new(FALSE, FALSE, sizeof(array_item_t));
    range.m_range_begin = 0; range.m_range_end = 8;
    single_gram.search(&range, array);
    for ( size_t i = 0; i < array->len; ++i ){
        array_item_t * item = &g_array_index(array, array_item_t, i);
        printf("item:%d:%d\n", item->m_token, item->m_item);
    }

    assert(single_gram.get_array_header(freq));
    assert(freq == total_freq);

    FlexibleBigram<guint32, guint32, guint32> bigram("TEST");
    assert(bigram.attach("/tmp/training.db", ATTACH_READWRITE|ATTACH_CREATE));
    bigram.store(1, &single_gram);
    assert(single_gram.insert_array_item(5, 8));
    assert(single_gram.remove_array_item(1, freq));
    assert(single_gram.set_array_header(32));
    assert(single_gram.get_array_header(freq));
    printf("new array header:%d\n", freq);
    bigram.store(2, &single_gram);

    for (int m = 1; m <= 2; ++m ){
        printf("--------------------------------------------------------\n");
        FlexibleSingleGram<guint32, guint32> * train_gram;
        bigram.load(m, train_gram);
        g_array_set_size(array, 0);
        range.m_range_begin = 0; range.m_range_end = 8;
        train_gram->search(&range, array);
        for ( size_t i = 0; i < array->len; ++i ){
            array_item_t * item = &g_array_index(array, array_item_t, i);
            printf("item:%d:%d\n", item->m_token, item->m_item);
        }
        delete train_gram;
    }

    GArray * items = g_array_new(FALSE, FALSE, sizeof(phrase_token_t));
    bigram.get_all_items(items);
    printf("-----------------------items----------------------------\n");
    for ( size_t i = 0; i < items->len; ++i ){
        phrase_token_t * token = &g_array_index(items, phrase_token_t, i);
        printf("item:%d\n", *token);
    }

    printf("-----------------------magic header---------------------\n");
    bigram.set_magic_header(total_freq);
    bigram.get_magic_header(freq);
    assert(total_freq == freq);
    printf("magic header:%d\n", freq);

    printf("-----------------------array header---------------------\n");
    for ( int i = 1; i <= 2; ++i){
        bigram.get_array_header(i, freq);
        printf("single gram: %d, freq:%d\n", i, freq);
    }

    bigram.set_array_header(1, 1);

    printf("-----------------------array header---------------------\n");
    for ( int i = 1; i <= 2; ++i){
        bigram.get_array_header(i, freq);
        printf("single gram: %d, freq:%d\n", i, freq);
    }

    for (int m = 1; m <= 2; ++m ){
        printf("--------------------------------------------------------\n");
        FlexibleSingleGram<guint32, guint32> * train_gram;
        bigram.load(m, train_gram);
        g_array_set_size(array, 0);
        range.m_range_begin = 0; range.m_range_end = 8;
        train_gram->search(&range, array);
        for ( size_t i = 0; i < array->len; ++i ){
            array_item_t * item = &g_array_index(array, array_item_t, i);
            printf("item:%d:%d\n", item->m_token, item->m_item);
        }
        delete train_gram;
    }

    assert(bigram.remove(1));

    bigram.get_all_items(items);
    printf("-----------------------items----------------------------\n");
    for ( size_t i = 0; i < items->len; ++i ){
        phrase_token_t * token = &g_array_index(items, phrase_token_t, i);
        printf("item:%d\n", *token);
    }

    g_array_free(items, TRUE);
    g_array_free(array, TRUE);
    return 0;
}