Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / src / slm / 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 _SUN_AGC_SLM_H
40 #define _SUN_AGC_SLM_H
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 #include "../portability.h"
47
48 #include <stdio.h>
49
50 /**
51  * Thread slm make the following modifications to simple back-off language model
52  *    -# Word id are limited to 18 bits, about 240K word ids
53  *    -# Compact all float value of -log(pr) into 65536 (16 bits)
54  *       level and use a table to map the index to a float value;
55  *    -# Compact all float value of -log(pr) into 16384 (14 bits)
56  *       level and use a table to map the index to a float value;
57  *    -# threading infomation embed into binary model file. Threading include
58  *         - bol(back-off-level) from current level
59  *         - bon(back-off-node)'s index in the bol level array
60  *         .
61  *       The thread could be used:
62  *         - when leaf node are arrived, it could use (bol,bon) as history for
63  *           history node.
64  *         - when a word could not be found in current node (cl, cn)'s children,
65  *           searching could be transfered to (bol, bon) directly and continue
66  *           searching the target word
67  *    -# Add a basic type TState in Language model, a state is pair of\n
68  *           (level, array_idx_of_the level)
69  *    -# change all get probability interface to\n
70  *          double transfer(TState& history, unsigned int wid, TState& result);
71  */
72 class CThreadSlm {
73 public:
74     enum {
75         BITS_BOW        = 14,
76         BITS_PR         = 16,
77         ID_NOT_WORD     = 69,
78     };
79
80     /**
81      * (level:idx) located a state in the language model very well
82      * Please note the psuedo unigram state, with level == 0, but idx > 0
83      * it's for used with bigram cache model
84      */
85     union TState {
86         TState(const TState &b) : m_all(b.m_all) {
87         }
88         TState(unsigned level = 0, unsigned idx = 0) {
89             anony.m_Level = level; anony.m_Idx = idx;
90         }
91
92         TState& operator++()              { ++anony.m_Idx; return *this; }
93
94         void setIdx(unsigned int idx)     { anony.m_Idx = idx; }
95         void setLevel(unsigned int lvl)   { anony.m_Level = lvl; }
96
97         unsigned int getLevel() const { return anony.m_Level; }
98         unsigned int getIdx() const { return anony.m_Idx; }
99         operator unsigned() const { return m_all; }
100
101         bool isTailState() const { return getIdx() <= 1; }
102
103         bool operator==(const TState & b) const {
104             return m_all == b.m_all;
105         }
106         bool operator<(const TState & b) const {
107             return unsigned(*this) < unsigned(b);
108         }
109
110 private:
111         unsigned int m_all;
112 #ifndef WORDS_BIGENDIAN
113         struct TAnonymous {
114             unsigned m_Idx   : 24;
115             unsigned m_Level : 8;
116         } anony;
117 #else
118         struct TAnonymous {
119             unsigned m_Level : 8;
120             unsigned m_Idx   : 24;
121         } anony;
122 #endif
123     };
124
125     /**
126      * Machine dependent
127      */
128     struct TNode {
129 public:
130         unsigned int wid() const {
131             return m_wid;
132         }
133
134         unsigned int bow() const {
135             return m_bow;
136         }
137
138         unsigned int pr()  const {
139             return m_pr;
140         }
141
142         unsigned int bon() const {
143             return m_bon;
144         }
145
146         unsigned int bol() const {
147             return m_bol;
148         }
149
150         unsigned int ch()  const {
151             return((m_ch_hi << 16) + m_ch_lo);
152         }
153
154         void set_wid(unsigned int wid){
155             m_wid = wid;
156         }
157
158         void set_bow(unsigned int bow){
159             m_bow = bow;
160         }
161
162         void set_pr(unsigned int pr){
163             m_pr = pr;
164         }
165
166         void set_bon(unsigned int bon){
167             m_bon = bon;
168         }
169
170         void set_bol(unsigned int bol){
171             m_bol = bol;
172         }
173
174         void set_ch(unsigned int ch){
175             m_ch_hi = ((ch >> 16) & 0x7F);
176             m_ch_lo = (ch & 0xFFFF);
177         }
178
179 protected:
180 #ifndef WORDS_BIGENDIAN
181         unsigned m_wid       : 18;
182         unsigned m_bow       : 14;
183         unsigned m_pr        : 16;
184         unsigned m_ch_lo     : 16;
185         unsigned m_bon       : 23;
186         unsigned m_bol       : 2;
187         unsigned m_ch_hi     : 7;
188 #else
189         unsigned m_ch_hi     : 7;
190         unsigned m_bol       : 2;
191         unsigned m_bon       : 23;
192         unsigned m_ch_lo     : 16;
193         unsigned m_pr        : 16;
194         unsigned m_bow       : 14;
195         unsigned m_wid       : 18;
196 #endif
197
198 private:
199         /**
200          * Machine dependent
201            union TChildIdx {
202            public:
203             inline TChildIdx(unsigned val) : m_all(val) { }
204             inline TChildIdx(const TChildIdx& b) : m_all(b.m_all) { }
205             inline TChildIdx(unsigned int hi, unsigned lo) : m_all(0) { anony.m_hi = hi; anony.m_lo = lo; }
206
207             inline unsigned int lo() { return anony.m_lo; }
208             inline unsigned int hi() { return anony.m_hi; }
209             inline unsigned int all(){ return m_all; }
210
211             inline unsigned int set_lo(unsigned int lo) { return (anony.m_lo = lo); }
212             inline unsigned int set_hi(unsigned int hi) { return (anony.m_hi = hi); }
213             inline unsigned int set_all(unsigned int all) { return (m_all = all); }
214
215            private:
216             unsigned int m_all;
217          *#ifndef WORDS_BIGENDIAN
218             struct TAnony {
219                 unsigned m_lo :16;
220                 unsigned m_hi : 7;
221                 unsigned NOUSE: 9;
222             } anony;
223          *#else
224             struct TAnony {
225                 unsigned NOUSE: 9;
226                 unsigned m_hi : 7;
227                 unsigned m_lo :16;
228             } anony;
229          *#endif
230            };
231          */
232     };
233
234     /**
235      * Machine dependent
236      */
237     struct TLeaf {
238 public:
239         inline unsigned int wid() const { return m_wid; }
240         inline unsigned int bon() const { return m_bon; }
241         inline unsigned int bol() const { return m_bol; }
242         inline unsigned int pr()  const { return((m_pr_hi << 14) + m_pr_lo); }
243
244         inline void set_wid(unsigned int wid) { m_wid = wid; }
245         inline void set_bon(unsigned int bon) { m_bon = bon; }
246         inline void set_bol(unsigned int bol) { m_bol = bol; }
247         inline void set_pr(unsigned int pr)   { m_pr_hi = ((pr >> 14) & 0x3);
248                                                 m_pr_lo = pr & 0x3FFF; }
249
250 protected:
251 #ifndef WORDS_BIGENDIAN
252         unsigned m_wid       : 18;
253         unsigned m_pr_lo     : 14;
254         unsigned m_bon       : 23;
255         unsigned m_bol       : 2;
256         unsigned m_pr_hi     : 2;
257 #else
258         unsigned m_pr_hi     : 2;
259         unsigned m_bol       : 2;
260         unsigned m_bon       : 23;
261         unsigned m_pr_lo     : 14;
262         unsigned m_wid       : 18;
263 #endif
264
265 private:
266         /*
267             union TPr {
268             public:
269                 inline TPr(unsigned int val) : m_all(val) { }
270                 inline TPr(const TPr & b) : m_all(b.m_all) { }
271                 inline TPr(unsigned int hi, unsigned lo) : m_all(0) { anony.m_hi=hi, anony.m_lo=lo; }
272
273                 inline unsigned int lo() { return anony.m_lo; }
274                 inline unsigned int hi() { return anony.m_hi; }
275                 inline unsigned int all(){ return m_all; }
276
277                 inline unsigned int set_lo(unsigned int lo) { return (anony.m_lo = lo); }
278                 inline unsigned int set_hi(unsigned int hi) { return (anony.m_hi = hi); }
279                 inline unsigned int set_all(unsigned int all) { return (m_all = all); }
280
281             private:
282                 unsigned int m_all;
283            #ifndef WORDS_BIGENDIAN
284                 struct TAnony {
285                     unsigned m_lo  :14;
286                     unsigned m_hi  : 2;
287                     unsigned NONUSE:16;
288                 } anony;
289            #else
290                 struct TAnony {
291                     unsigned NONUSE:16;
292                     unsigned m_hi  : 2;
293                     unsigned m_lo  :14;
294                 } anony;
295            #endif
296             };
297          */
298     };
299
300 public:
301     CThreadSlm()
302         : m_N(0), m_UseLogPr(0), m_Levels(NULL), m_LevelSizes(NULL),
303           m_bowTable(NULL), m_prTable(NULL), m_bMMap(false), m_buf(NULL) { }
304
305     ~CThreadSlm() { free(); }
306
307     bool
308     load(const char* fname, bool MMap = false);
309
310     unsigned isUseLogPr() const
311     { return m_UseLogPr; }
312
313     void
314     free();
315
316     double
317     transferNegLog(TState history, unsigned int wid, TState& result);
318
319     double
320     transfer(TState history, unsigned int wid, TState& result);
321
322     TState
323     history_state_of(TState st);
324
325     TState&
326     historify(TState& st);
327
328     unsigned int
329     lastWordId(TState st);
330
331 protected:
332     double
333     rawTransfer(TState history, unsigned int wid, TState& result);
334
335 protected:
336     typedef  void*   PtrVoid;
337
338     unsigned m_N;
339     unsigned m_UseLogPr;
340     void    **m_Levels;
341     unsigned *m_LevelSizes;
342     float    *m_bowTable;
343     float    *m_prTable;
344
345 private:
346     ssize_t m_bufSize;
347     bool m_bMMap;
348     char     *m_buf;
349 };
350
351 #endif