summaryrefslogtreecommitdiffstats
path: root/src/storage/chewing_large_table2.cpp
blob: aa3439990895a87d0fe649da54f1c1dfaa6199e2 (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
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
/* 
 *  libpinyin
 *  Library to deal with pinyin.
 *  
 *  Copyright (C) 2016 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "chewing_large_table2.h"
#include "pinyin_parser2.h"
#include "zhuyin_parser2.h"

void ChewingLargeTable2::init_entries() {
    assert(NULL == m_entries);

    m_entries = g_ptr_array_new();
    /* NULL for the first pointer. */
    g_ptr_array_set_size(m_entries, MAX_PHRASE_LENGTH + 1);

#define CASE(len) case len:                         \
    {                                               \
        ChewingTableEntry<len> * entry =            \
            new ChewingTableEntry<len>;             \
        g_ptr_array_index(m_entries, len) = entry;  \
        break;                                      \
    }

    for (size_t i = 1; i < m_entries->len; i++) {
        switch(i) {
            CASE(1);
            CASE(2);
            CASE(3);
            CASE(4);
            CASE(5);
            CASE(6);
            CASE(7);
            CASE(8);
            CASE(9);
            CASE(10);
            CASE(11);
            CASE(12);
            CASE(13);
            CASE(14);
            CASE(15);
            CASE(16);
        default:
            assert(false);
        }
    }

#undef CASE

}

void ChewingLargeTable2::fini_entries() {
    assert(NULL != m_entries);

    assert(MAX_PHRASE_LENGTH + 1 == m_entries->len);

#define CASE(len) case len:                     \
    {                                           \
        ChewingTableEntry<len> * entry =        \
            (ChewingTableEntry<len> *)          \
            g_ptr_array_index(m_entries, len);  \
        delete entry;                           \
        break;                                  \
    }

    for (size_t i = 1; i < m_entries->len; i++) {
        switch(i) {
            CASE(1);
            CASE(2);
            CASE(3);
            CASE(4);
            CASE(5);
            CASE(6);
            CASE(7);
            CASE(8);
            CASE(9);
            CASE(10);
            CASE(11);
            CASE(12);
            CASE(13);
            CASE(14);
            CASE(15);
            CASE(16);
        default:
            assert(false);
        }
    }

#undef CASE

    g_ptr_array_free(m_entries, TRUE);
    m_entries = NULL;
}

/* load text method */
bool ChewingLargeTable2::load_text(FILE * infile, TABLE_PHONETIC_TYPE type) {
    char pinyin[256];
    char phrase[256];
    phrase_token_t token;
    size_t freq;

    while (!feof(infile)) {
        int num = fscanf(infile, "%256s %256s %u %ld",
                         pinyin, phrase, &token, &freq);

        if (4 != num)
            continue;

        if(feof(infile))
            break;

        glong len = g_utf8_strlen(phrase, -1);

        ChewingKeyVector keys;
        ChewingKeyRestVector key_rests;

        keys = g_array_new(FALSE, FALSE, sizeof(ChewingKey));
        key_rests = g_array_new(FALSE, FALSE, sizeof(ChewingKeyRest));

        switch (type) {
        case PINYIN_TABLE: {
            PinyinDirectParser2 parser;
            pinyin_option_t options = USE_TONE;
            parser.parse(options, keys, key_rests, pinyin, strlen(pinyin));
            break;
        }

        case ZHUYIN_TABLE: {
            ZhuyinDirectParser2 parser;
            pinyin_option_t options = USE_TONE | FORCE_TONE;
            parser.parse(options, keys, key_rests, pinyin, strlen(pinyin));
            break;
        }
        };

        if (len != keys->len) {
            fprintf(stderr, "ChewingLargeTable::load_text:%s\t%s\t%u\t%ld\n",
                    pinyin, phrase, token, freq);
            continue;
        }

        add_index(keys->len, (ChewingKey *)keys->data, token);

        g_array_free(keys, TRUE);
        g_array_free(key_rests, TRUE);
    }

    return true;
}

/* search method */
int ChewingLargeTable2::search(int phrase_length,
                               /* in */ const ChewingKey keys[],
                               /* out */ PhraseIndexRanges ranges) const {
    ChewingKey index[MAX_PHRASE_LENGTH];
    assert(NULL != m_db);

    if (contains_incomplete_pinyin(keys, phrase_length)) {
        compute_incomplete_chewing_index(keys, index, phrase_length);
        return search_internal(phrase_length, index, keys, ranges);
    } else {
        compute_chewing_index(keys, index, phrase_length);
        return search_internal(phrase_length, index, keys, ranges);
    }

    return SEARCH_NONE;
}

/* add/remove index method */
int ChewingLargeTable2::add_index(int phrase_length,
                                  /* in */ const ChewingKey keys[],
                                  /* in */ phrase_token_t token) {
    ChewingKey index[MAX_PHRASE_LENGTH];
    assert(NULL != m_db);
    int result = ERROR_OK;

    /* for in-complete chewing index */
    compute_incomplete_chewing_index(keys, index, phrase_length);
    result = add_index_internal(phrase_length, index, keys, token);
    assert(ERROR_OK == result || ERROR_INSERT_ITEM_EXISTS == result);
    if (ERROR_OK != result)
        return result;

    /* for chewing index */
    compute_chewing_index(keys, index, phrase_length);
    result = add_index_internal(phrase_length, index, keys, token);
    assert(ERROR_OK == result || ERROR_INSERT_ITEM_EXISTS == result);
    return result;
}

int ChewingLargeTable2::remove_index(int phrase_length,
                                     /* in */ const ChewingKey keys[],
                                     /* in */ phrase_token_t token) {
    ChewingKey index[MAX_PHRASE_LENGTH];
    assert(NULL != m_db);
    int result = ERROR_OK;

    /* for in-complete chewing index */
    compute_incomplete_chewing_index(keys, index, phrase_length);
    result = remove_index_internal(phrase_length, index, keys, token);
    assert(ERROR_OK == result || ERROR_REMOVE_ITEM_DONOT_EXISTS == result);
    if (ERROR_OK != result)
        return result;

    /* for chewing index */
    compute_chewing_index(keys, index, phrase_length);
    result = remove_index_internal(phrase_length, index, keys, token);
    assert(ERROR_OK == result || ERROR_REMOVE_ITEM_DONOT_EXISTS == result);
    return result;
}