~ubuntu-branches/ubuntu/quantal/sunpinyin/quantal

« back to all changes in this revision

Viewing changes to src/slm/thread/slmthread.cpp

  • Committer: Package Import Robot
  • Author(s): YunQiang Su
  • Date: 2012-03-30 15:31:55 UTC
  • mfrom: (1.1.3) (1.2.7 sid)
  • Revision ID: package-import@ubuntu.com-20120330153155-qgls77sogzgtg9zp
Tags: 2.0.3+git20120222-1
* Team upload: git snapshot 20120222.
   - fix breaks if LDFLAGS in environment contains
       multiple words (Closese #646001).
   - rm patches merged to upstream:
       append-os-environ-toenv.patch
       fix-ftbfs-on-sh.patch
       remove-10-candidate-words-limitation.patch
   - refresh disable-lm-dict-compile.patch.
* Bump stardard version to 3.9.3: no modify needed.
* add libsunpinyin3-dbg and python-sunpinyin packages.
* debian/compat to 9, multiarch it.
* rewrite debian/rules with dh 7 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 
 * 
 
3
 *
4
4
 * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
5
 
 * 
 
5
 *
6
6
 * The contents of this file are subject to the terms of either the GNU Lesser
7
7
 * General Public License Version 2.1 only ("LGPL") or the Common Development and
8
8
 * Distribution License ("CDDL")(collectively, the "License"). You may not use this
9
9
 * file except in compliance with the License. You can obtain a copy of the CDDL at
10
10
 * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
11
 
 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the 
 
11
 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
12
12
 * specific language governing permissions and limitations under the License. When
13
13
 * distributing the software, include this License Header Notice in each file and
14
14
 * include the full text of the License in the License file as well as the
15
15
 * following notice:
16
 
 * 
 
16
 *
17
17
 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
18
18
 * (CDDL)
19
19
 * For Covered Software in this distribution, this License shall be governed by the
21
21
 * Any litigation relating to this License shall be subject to the jurisdiction of
22
22
 * the Federal Courts of the Northern District of California and the state courts
23
23
 * of the State of California, with venue lying in Santa Clara County, California.
24
 
 * 
 
24
 *
25
25
 * Contributor(s):
26
 
 * 
 
26
 *
27
27
 * If you wish your version of this file to be governed by only the CDDL or only
28
28
 * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
29
29
 * include this software in this distribution under the [CDDL or LGPL Version 2.1]
32
32
 * Version 2.1, or to extend the choice of license to its licensees as provided
33
33
 * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
34
34
 * Version 2 license, then the option applies only if the new code is made subject
35
 
 * to such option by the copyright holder. 
 
35
 * to such option by the copyright holder.
36
36
 */
37
37
 
38
38
#ifdef HAVE_CONFIG_H
56
56
 
57
57
#include "ValueCompress.h"
58
58
 
59
 
class CSIMSlmWithIteration : public CSIMSlm{
 
59
class CSIMSlmWithIteration : public CSIMSlm {
60
60
public:
61
61
    struct TLevelIterator {
62
62
        std::vector<int>    m_history;
64
64
 
65
65
public:
66
66
    int
67
 
    getLevelSize(int lvl) { return sz[lvl]; }
 
67
    getLevelSize(int lvl)
 
68
    {
 
69
        return sz[lvl];
 
70
    }
68
71
 
69
72
    int
70
 
    getN() { return N; }
 
73
    getN()
 
74
    {
 
75
        return N;
 
76
    }
71
77
 
72
78
    void
73
79
    getIdString(TLevelIterator& it, std::vector<TSIMWordId>& history);
102
108
    if (n == 0) return 0;
103
109
 
104
110
    int m = -1;
105
 
        while (n > N) { --n; ++hw; }
 
111
    while (n > N) {
 
112
        --n; ++hw;
 
113
    }
106
114
 
107
 
        void* pstate = ((TNode*)level[0]);
108
 
        for (int lvl=0; lvl < n && pstate != NULL; ++lvl) {
109
 
            int h = ((TNode*)pstate)->child;
110
 
            int t = (((TNode*)pstate)+1)->child;
111
 
            if (lvl == N-1) {
112
 
                TLeaf* p = (TLeaf*)level[lvl+1];
113
 
                pstate = (void*)binary_find_id(p+h, p+t, hw[lvl]);
114
 
                m = (pstate != NULL)?(((TLeaf*)pstate) - p):(-1);
115
 
            } else {
116
 
                TNode* p = (TNode*)level[lvl+1];
117
 
                pstate = (void*)binary_find_id(p+h, p+t, hw[lvl]);
118
 
                m = (pstate != NULL)?(((TNode*)pstate) - p):(-1);
119
 
            }
 
115
    void* pstate = ((TNode*)level[0]);
 
116
    for (int lvl = 0; lvl < n && pstate != NULL; ++lvl) {
 
117
        int h = ((TNode*)pstate)->child;
 
118
        int t = (((TNode*)pstate) + 1)->child;
 
119
        if (lvl == N - 1) {
 
120
            TLeaf* p = (TLeaf*)level[lvl + 1];
 
121
            pstate = (void*)binary_find_id(p + h, p + t, hw[lvl]);
 
122
            m = (pstate != NULL) ? (((TLeaf*)pstate) - p) : (-1);
 
123
        } else {
 
124
            TNode* p = (TNode*)level[lvl + 1];
 
125
            pstate = (void*)binary_find_id(p + h, p + t, hw[lvl]);
 
126
            m = (pstate != NULL) ? (((TNode*)pstate) - p) : (-1);
120
127
        }
 
128
    }
121
129
    return m;
122
130
}
123
131
 
124
132
void
125
 
CSIMSlmWithIteration::findBackOffState(int n, TSIMWordId*hw, unsigned & bol, unsigned& bon)
 
133
CSIMSlmWithIteration::findBackOffState(int n,
 
134
                                       TSIMWordId*hw,
 
135
                                       unsigned & bol,
 
136
                                       unsigned& bon)
126
137
{
127
138
    while (n > 1) {
128
139
        --n; ++hw;
129
140
        int idx = findState(n, hw);
130
 
        if (idx >= 0 && ((TNode*)(level[n]))[idx].child < ((TNode*)(level[n]))[idx+1].child) {
 
141
        if (idx >= 0 && ((TNode*)(level[n]))[idx].child <
 
142
            ((TNode*)(level[n]))[idx + 1].child) {
131
143
            bol = n; bon = idx; return;
132
144
        }
133
145
    }
136
148
}
137
149
 
138
150
void
139
 
CSIMSlmWithIteration::getIdString(TLevelIterator& it, std::vector<TSIMWordId>& history)
 
151
CSIMSlmWithIteration::getIdString(TLevelIterator& it,
 
152
                                  std::vector<TSIMWordId>& history)
140
153
{
141
154
    history.clear();
142
 
    for (int i=1, tmp_sz=it.m_history.size(); i < tmp_sz; ++i) {
 
155
    for (int i = 1, tmp_sz = it.m_history.size(); i < tmp_sz; ++i) {
143
156
        int idx = it.m_history[i];
144
157
        if (i == N)
145
158
            history.push_back(((TLeaf*)(level[i]))[idx].id);
152
165
CSIMSlmWithIteration::beginLevelIteration(int lvl, TLevelIterator& it)
153
166
{
154
167
    it.m_history.clear();
155
 
    for (int i=0, tmp_sz=lvl; i <= tmp_sz; ++i)
 
168
    for (int i = 0, tmp_sz = lvl; i <= tmp_sz; ++i)
156
169
        it.m_history.push_back(0);
157
170
    adjustIterator(it);
158
171
}
167
180
bool
168
181
CSIMSlmWithIteration::isEnd(TLevelIterator& it)
169
182
{
170
 
    return ((it.m_history.back()+1 >= sz[it.m_history.size()-1]));
 
183
    return((it.m_history.back() + 1 >= sz[it.m_history.size() - 1]));
171
184
}
172
185
 
173
186
void
174
187
CSIMSlmWithIteration::adjustIterator(TLevelIterator& it)
175
188
{
176
189
    int ch = it.m_history.back();
177
 
    for (int i= it.m_history.size()-2; i >= 0; --i) {
 
190
    for (int i = it.m_history.size() - 2; i >= 0; --i) {
178
191
        int len = sz[i];
179
192
        int& parent = it.m_history[i];
180
193
        TNode* pn = (TNode*)(level[i]);
181
 
        while (parent < len && pn[parent+1].child <= ch)
 
194
        while (parent < len && pn[parent + 1].child <= ch)
182
195
            ++parent;
183
196
        ch = parent;
184
197
    }
187
200
CSIMSlm::TLeaf*
188
201
CSIMSlmWithIteration::getNodePtr(TLevelIterator& it)
189
202
{
190
 
    int lvl = it.m_history.size()-1;
 
203
    int lvl = it.m_history.size() - 1;
191
204
    int idx = it.m_history.back();
192
205
    if (lvl == N)
193
 
        return (((TLeaf*)(level[lvl]))+idx);
 
206
        return(((TLeaf*)(level[lvl])) + idx);
194
207
    else
195
 
        return (((TNode*)(level[lvl]))+idx);
 
208
        return(((TNode*)(level[lvl])) + idx);
196
209
}
197
210
 
198
211
 
199
212
 
200
 
void ShowUsage()
 
213
void
 
214
ShowUsage()
201
215
{
202
216
    printf("Usage:\n");
203
217
    printf("    slmthread primitive_slm threaded_slm\n");
204
218
    printf("\nDescription:\n");
205
 
    printf("    slmthread add back-off-state for each slm node in the primitive_slm. ");
 
219
    printf(
 
220
        "    slmthread add back-off-state for each slm node in the primitive_slm. ");
206
221
    printf("Also it compresses 32-bit float into 16 bit representation.\n\n");
207
222
    exit(100);
208
223
}
211
226
CThreadSlm::TNode* levels[16];
212
227
CThreadSlm::TLeaf* lastLevel;
213
228
 
214
 
int main(int argc, char* argv[])
 
229
int
 
230
main(int argc, char* argv[])
215
231
{
216
232
    CValueCompressor vc;
217
233
    unsigned int bol, bon;
234
250
 
235
251
    bool usingLogPr = slm.isUseLogPr();
236
252
 
237
 
    #define EffectivePr(a)  (float((usingLogPr)?((a)/log(2.0)):(-log2((a)))))
238
 
    #define OriginalPr(b)   (float((usingLogPr)?((b)*log(2.0)):(exp2(-(b)))))
239
 
    #define EffectiveBow(a) (float((usingLogPr)?(exp(-(a))):((a))))
240
 
    #define OriginalBow(b)  (float((usingLogPr)?(-log((b))):((b))))
 
253
    #define EffectivePr(a)  (float((usingLogPr) ? ((a) / log(2.0)) : (-log2((a)))))
 
254
    #define OriginalPr(b)   (float((usingLogPr) ? ((b) * log(2.0)) : (exp2(-(b)))))
 
255
    #define EffectiveBow(a) (float((usingLogPr) ? (exp(-(a))) : ((a))))
 
256
    #define OriginalBow(b)  (float((usingLogPr) ? (-log((b))) : ((b))))
241
257
 
242
258
    printf("\nfirst pass..."); fflush(stdout);
243
 
    for (int lvl=0; lvl <= slm.getN(); ++lvl) {
 
259
    for (int lvl = 0; lvl <= slm.getN(); ++lvl) {
244
260
        CSIMSlmWithIteration::TLevelIterator it;
245
261
        slm.beginLevelIteration(lvl, it);
246
262
        for (; !slm.isEnd(it); slm.next(it)) {
269
285
    // Following pr value should not be grouped, or as milestone values.
270
286
    static float msprs[] = {
271
287
        0.9, 0.8, 0.7, 0.6,
272
 
        1.0/2, 1.0/4, 1.0/8, 1.0/16, 1.0/32, 1.0/64, 1.0/128,
273
 
        1.0/256, 1.0/512, 1.0/1024, 1.0/2048, 1.0/4096, 1.0/8192,
274
 
        1.0/16384, 1.0/32768, 1.0/65536
 
288
        1.0 / 2, 1.0 / 4, 1.0 / 8, 1.0 / 16, 1.0 / 32, 1.0 / 64, 1.0 / 128,
 
289
        1.0 / 256, 1.0 / 512, 1.0 / 1024, 1.0 / 2048, 1.0 / 4096, 1.0 / 8192,
 
290
        1.0 / 16384, 1.0 / 32768, 1.0 / 65536
275
291
    };
276
292
 
277
 
    for (unsigned i=0, sz=sizeof(msprs)/sizeof(float); i < sz; ++i) {
278
 
        float real_pr = (usingLogPr)?(-log(msprs[i])):(msprs[i]);
 
293
    for (unsigned i = 0, sz = sizeof(msprs) / sizeof(float); i < sz; ++i) {
 
294
        float real_pr = (usingLogPr) ? (-log(msprs[i])) : (msprs[i]);
279
295
        float eff_pr = EffectivePr(real_pr);
280
296
        if (pr_eff.find(eff_pr) == pr_eff.end()) {
281
297
            pr_eff[eff_pr] = real_pr;
292
308
        0.00005, 0.00001, 0.000005, 0.000001, 0.0000005, 0.0000001
293
309
    };
294
310
 
295
 
    for (unsigned i=0, sz=sizeof(msbows)/sizeof(float); i < sz; ++i) {
296
 
        float real_bow = (usingLogPr)?(-log(msbows[i])):(msbows[i]);
 
311
    for (unsigned i = 0, sz = sizeof(msbows) / sizeof(float); i < sz; ++i) {
 
312
        float real_bow = (usingLogPr) ? (-log(msbows[i])) : (msbows[i]);
297
313
        float eff_bow = EffectiveBow(real_bow);
298
314
        if (bow_eff.find(eff_bow) == bow_eff.end()) {
299
315
            bow_eff[eff_bow] = real_bow;
324
340
 
325
341
 
326
342
    printf("\nThreading the new model..."); fflush(stdout);
327
 
    for (int lvl=0; lvl < slm.getN(); ++lvl) {
 
343
    for (int lvl = 0; lvl < slm.getN(); ++lvl) {
328
344
        levels[lvl] = new CThreadSlm::TNode[slm.getLevelSize(lvl)];
329
345
 
330
346
        CSIMSlmWithIteration::TLevelIterator it;
366
382
 
367
383
            nn.set_ch(pn->child);
368
384
 
369
 
            assert(usingLogPr || (pr_table[idx_pr] > 0.0 && pr_table[idx_pr] < 1.0));
 
385
            assert(usingLogPr ||
 
386
                   (pr_table[idx_pr] > 0.0 && pr_table[idx_pr] < 1.0));
370
387
            assert(!usingLogPr || pr_table[idx_pr] > 0.0);
371
388
        }
372
389
        CSIMSlm::TNode* pn = (CSIMSlm::TNode*)slm.getNodePtr(it);
373
390
        CThreadSlm::TNode& nn = levels[lvl][it.m_history.back()];
374
391
        nn.set_ch(pn->child);
375
 
    };
 
392
    }
 
393
    ;
376
394
 
377
395
 
378
396
    lastLevel = new CThreadSlm::TLeaf [slm.getLevelSize(slm.getN())];
379
397
    CSIMSlmWithIteration::TLevelIterator it;
380
398
    slm.beginLevelIteration(slm.getN(), it);
381
 
    for (int lvl=slm.getN(); !slm.isEnd(it); slm.next(it)) {
 
399
    for (int lvl = slm.getN(); !slm.isEnd(it); slm.next(it)) {
382
400
        CSIMSlm::TLeaf* pn = slm.getNodePtr(it);
383
401
        slm.getIdString(it, history);
384
402
        slm.findBackOffState(lvl, &history[0], bol, bon);
415
433
        fwrite(&len, sizeof(int), 1, fp);
416
434
    }
417
435
    fwrite(&pr_table[0], sizeof(float), pr_table.size(), fp);
418
 
    for (int i = pr_table.size(), sz=(1 << CThreadSlm::BITS_PR); i < sz; ++i)
 
436
    for (int i = pr_table.size(), sz = (1 << CThreadSlm::BITS_PR); i < sz; ++i)
419
437
        fwrite(&dummy, sizeof(float), 1, fp);
420
438
 
421
439
    fwrite(&bow_table[0], sizeof(float), bow_table.size(), fp);
422
 
    for (int i = bow_table.size(), sz=(1 << CThreadSlm::BITS_BOW); i < sz; ++i)
 
440
    for (int i = bow_table.size(), sz = (1 << CThreadSlm::BITS_BOW);
 
441
         i < sz;
 
442
         ++i)
423
443
        fwrite(&dummy, sizeof(float), 1, fp);
424
444
 
425
 
    for (int lvl=0; lvl < N; ++lvl)
426
 
        fwrite(levels[lvl], sizeof(CThreadSlm::TNode), slm.getLevelSize(lvl), fp);
 
445
    for (int lvl = 0; lvl < N; ++lvl)
 
446
        fwrite(levels[lvl], sizeof(CThreadSlm::TNode), slm.getLevelSize(
 
447
                   lvl), fp);
427
448
    fwrite(lastLevel, sizeof(CThreadSlm::TLeaf), slm.getLevelSize(N), fp);
428
449
    fclose(fp);
429
450
 
430
451
    printf("done!\n"); fflush(stdout);
431
452
 
432
453
    delete [] lastLevel;
433
 
    for (int lvl=0; lvl < N; ++lvl)
434
 
        delete []levels[lvl];
 
454
    for (int lvl = 0; lvl < N; ++lvl)
 
455
        delete [] levels[lvl];
435
456
 
436
457
    bow_values.clear();
437
458
    bow_map.clear();