Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / src / slm / sim_slm.h
1 // -*- mode: c++ -*-
2 /*
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4  *
5  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
6  *
7  * The contents of this file are subject to the terms of either the GNU Lesser
8  * General Public License Version 2.1 only ("LGPL") or the Common Development and
9  * Distribution License ("CDDL")(collectively, the "License"). You may not use this
10  * file except in compliance with the License. You can obtain a copy of the CDDL at
11  * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
12  * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
13  * specific language governing permissions and limitations under the License. When
14  * distributing the software, include this License Header Notice in each file and
15  * include the full text of the License in the License file as well as the
16  * following notice:
17  *
18  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
19  * (CDDL)
20  * For Covered Software in this distribution, this License shall be governed by the
21  * laws of the State of California (excluding conflict-of-law provisions).
22  * Any litigation relating to this License shall be subject to the jurisdiction of
23  * the Federal Courts of the Northern District of California and the state courts
24  * of the State of California, with venue lying in Santa Clara County, California.
25  *
26  * Contributor(s):
27  *
28  * If you wish your version of this file to be governed by only the CDDL or only
29  * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
30  * include this software in this distribution under the [CDDL or LGPL Version 2.1]
31  * license." If you don't indicate a single choice of license, a recipient has the
32  * option to distribute your version of this file under either the CDDL or the LGPL
33  * Version 2.1, or to extend the choice of license to its licensees as provided
34  * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
35  * Version 2 license, then the option applies only if the new code is made subject
36  * to such option by the copyright holder.
37  */
38
39 #ifndef _SIM_SLM_H
40 #define _SIM_SLM_H
41
42 #include "../portability.h"
43
44 class CSlmBuilder;
45
46 /* only used as middle-result, please use thread slm for applications. */
47 class CSIMSlm {
48     friend class CSlmBuilder;
49 public:
50     typedef unsigned int FREQ_TYPE;
51     typedef float PR_TYPE;
52
53     struct TLeaf {
54         TSIMWordId id;
55         union {
56             FREQ_TYPE freq;     //for builder, not for use
57             PR_TYPE pr;
58         };
59 public:
60         TLeaf(TSIMWordId theId = 0, FREQ_TYPE fr = 0) : id(theId), freq(fr)
61         { }
62         bool operator<(const TLeaf & r) const
63         { return id < r.id; }
64         bool operator>(const TLeaf & r) const
65         { return id > r.id; }
66         bool operator==(const TLeaf & r) const
67         { return id == r.id; }
68     };
69
70     struct TNode : public TLeaf {
71         int child;
72         PR_TYPE bow;
73 public:
74         TNode(TSIMWordId theId = 0,
75               int ch = 0,
76               FREQ_TYPE fr = 0,
77               PR_TYPE theBOW = 0.0)
78             : TLeaf(theId, fr), child(ch), bow(theBOW)
79         { }
80     };
81
82 public:
83     bool Load(const char* fname);
84     void Free();
85
86     unsigned isUseLogPr() const
87     { return bUseLogPr; }
88
89     double getPr(int n, TSIMWordId* hw);
90     double getNegLogPr(int n, TSIMWordId* hw);
91
92     CSIMSlm() : N(0), sz(NULL), level(NULL), bUseLogPr(0) {}
93
94 protected:
95     double getPrAsLog(int n, TSIMWordId* hw);
96     double getPrDirect(int n, TSIMWordId* hw);
97
98 protected:
99     int N;
100     int* sz;
101     void** level;
102     unsigned bUseLogPr;
103 };
104
105
106 template<class _NodeT_>
107 _NodeT_* binary_find(_NodeT_* base, int h, int t, const _NodeT_ & val){
108     while (h < t) {
109         int m = (h + t) / 2;
110         _NodeT_* pm = base + m;
111         if (*pm < val)
112             h = m + 1;
113         else if (*pm == val)
114             return pm;
115         else
116             t = m;
117     }
118     return NULL;
119 }
120
121 template <class _NodeT_>
122 _NodeT_* binary_find_id(_NodeT_ *ph, _NodeT_* pt, TSIMWordId id){
123     int h = 0, t = pt - ph;
124     while (h < t) {
125         int m = (h + t) / 2;
126         _NodeT_ * pm = ph + m;
127         if (pm->id == id)
128             return pm;
129         else if (pm->id < id)
130             h = m + 1;
131         else
132             t = m;
133     }
134     return NULL;
135 }
136
137 #endif
138