~ubuntu-branches/ubuntu/raring/sunpinyin/raring

« back to all changes in this revision

Viewing changes to src/ime-core/ic_history.h

  • 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
#ifndef _SUNPINYIN_CONTEXT_HISTORY_H
45
45
#include <set>
46
46
 
47
47
/**
48
 
* A forget all history memory
49
 
*/
 
48
 * A forget all history memory
 
49
 */
50
50
class CICHistory {
51
51
public:
52
52
    /** don't care word id, or seperator word id */
53
 
    static const unsigned int DCWID;
 
53
    static const uint32_t DCWID;
54
54
 
55
55
    virtual ~CICHistory();
56
56
 
57
 
    virtual bool seenBefore(unsigned int wid) = 0;
 
57
    virtual bool seenBefore(uint32_t wid) = 0;
58
58
 
59
59
    /**
60
 
    * memorize the context stream pointed by [its_wid, ite_wid)
61
 
    */
62
 
    virtual bool memorize(unsigned int* its_wid, unsigned int* ite_wid) = 0;
 
60
     * memorize the context stream pointed by [its_wid, ite_wid)
 
61
     */
 
62
    virtual bool memorize(uint32_t* its_wid, uint32_t* ite_wid) = 0;
63
63
    virtual void clear() = 0;
64
64
 
65
65
    /**
66
 
    * remove a word id from history cache
67
 
    */
68
 
    virtual void forget(unsigned wid) = 0;
69
 
    virtual void forget(unsigned int* its_wid, unsigned int* ite_wid) = 0;
70
 
 
71
 
    /**
72
 
    * @param its_wid is the first word pointer of the context stream
73
 
    * @param ite_wid is the last (exclusive) word pointer of the context stream
74
 
    * @return pr(*(ite_wid-1) | *its_wid, ..., *(ite_wid-2))
75
 
    * The return value could be zero, i.e. no need to smooth the probabilities
76
 
    */
77
 
    virtual double pr(unsigned int* its_wid, unsigned int* ite_wid) = 0;
78
 
 
79
 
    /**
80
 
    * @param its_wid is the first word pointer of the history stream
81
 
    * @param ite_wid is the last (exclusive) word pointer of the history stream
82
 
    * @return pr(*wid | *its_wid, ..., *(ite_wid-1))
83
 
    * The return value could be zero, i.e. no need to smooth the probabilities
84
 
    */
85
 
    virtual double pr(unsigned int* its_wid, unsigned int* ite_wid, unsigned int wid) = 0;
86
 
 
87
 
    /**
88
 
    * allocate a buffer, and put the context memory's contect into it
89
 
    * @param buf_ptr would be stored the buffer pointer
90
 
    * @param sz would be the size in byte of the buffer allocated
91
 
    * @return false on error
92
 
    * Note: the buf_ptr should be used free(*buf_ptr) to free after usage
93
 
    */
 
66
     * remove a word id from history cache
 
67
     */
 
68
    virtual void forget(uint32_t wid) = 0;
 
69
    virtual void forget(uint32_t* its_wid, uint32_t* ite_wid) = 0;
 
70
 
 
71
    /**
 
72
     * @param its_wid is the first word pointer of the context stream
 
73
     * @param ite_wid is the last (exclusive) word pointer of the context stream
 
74
     * @return pr(*(ite_wid-1) | *its_wid, ..., *(ite_wid-2))
 
75
     * The return value could be zero, i.e. no need to smooth the probabilities
 
76
     */
 
77
    virtual double pr(uint32_t* its_wid, uint32_t* ite_wid) = 0;
 
78
 
 
79
    /**
 
80
     * @param its_wid is the first word pointer of the history stream
 
81
     * @param ite_wid is the last (exclusive) word pointer of the history stream
 
82
     * @return pr(*wid | *its_wid, ..., *(ite_wid-1))
 
83
     * The return value could be zero, i.e. no need to smooth the probabilities
 
84
     */
 
85
    virtual double pr(uint32_t* its_wid,
 
86
                      uint32_t* ite_wid,
 
87
                      uint32_t wid) = 0;
 
88
 
 
89
    /**
 
90
     * allocate a buffer, and put the context memory's contect into it
 
91
     * @param buf_ptr would be stored the buffer pointer
 
92
     * @param sz would be the size in byte of the buffer allocated
 
93
     * @return false on error
 
94
     * Note: the buf_ptr should be used free(*buf_ptr) to free after usage
 
95
     */
94
96
    virtual bool
95
97
    bufferize(void** buf_ptr, size_t* sz) = 0;
96
98
 
97
99
    /**
98
 
    * Load context memory according to the buf
99
 
    * @param buf_ptr uffer pointer
100
 
    * @param sz is the size in byte of the buffer
101
 
    * @return false on error
102
 
    * call with buf_ptr with NULL value would clear the context memory
103
 
    */
 
100
     * Load context memory according to the buf
 
101
     * @param buf_ptr uffer pointer
 
102
     * @param sz is the size in byte of the buffer
 
103
     * @return false on error
 
104
     * call with buf_ptr with NULL value would clear the context memory
 
105
     */
104
106
    virtual bool
105
107
    loadFromBuffer(void* buf_ptr, size_t sz) = 0;
106
108
 
107
109
    virtual void
108
 
    addStopWords(const std::set<unsigned int>& stopWords) = 0;
 
110
    addStopWords(const std::set<uint32_t>& stopWords) = 0;
109
111
 
110
112
    virtual void
111
113
    initStopWords() = 0;
119
121
 
120
122
    virtual ~CBigramHistory();
121
123
 
122
 
    virtual bool seenBefore(unsigned int wid);
 
124
    virtual bool seenBefore(uint32_t wid);
123
125
 
124
 
    virtual bool memorize(unsigned int* its_wid, unsigned int* ite_wid);
 
126
    virtual bool memorize(uint32_t* its_wid, uint32_t* ite_wid);
125
127
    virtual void clear();
126
128
 
127
 
    virtual void forget(unsigned wid);
128
 
    virtual void forget(unsigned int* its_wid, unsigned int* ite_wid);
129
 
 
130
 
    /**
131
 
    * @param its_wid is the first word pointer of the context stream
132
 
    * @param ite_wid is the last (exclusive) word pointer of the context stream
133
 
    * @return pr(*(ite_wid-1) | *(ite_wid-2))
134
 
    */
135
 
    virtual double pr(unsigned int* its_wid, unsigned int* ite_wid);
136
 
 
137
 
    /**
138
 
    * @param its_wid is the first word pointer of the history stream
139
 
    * @param ite_wid is the last (exclusive) word pointer of the history stream
140
 
    * @return pr(*wid | *(ite_wid-1))
141
 
    */
142
 
    virtual double pr(unsigned int* its_wid, unsigned int* ite_wid, unsigned int wid);
 
129
    virtual void forget(uint32_t wid);
 
130
    virtual void forget(uint32_t* its_wid, uint32_t* ite_wid);
 
131
 
 
132
    /**
 
133
     * @param its_wid is the first word pointer of the context stream
 
134
     * @param ite_wid is the last (exclusive) word pointer of the context stream
 
135
     * @return pr(*(ite_wid-1) | *(ite_wid-2))
 
136
     */
 
137
    virtual double pr(uint32_t* its_wid, uint32_t* ite_wid);
 
138
 
 
139
    /**
 
140
     * @param its_wid is the first word pointer of the history stream
 
141
     * @param ite_wid is the last (exclusive) word pointer of the history stream
 
142
     * @return pr(*wid | *(ite_wid-1))
 
143
     */
 
144
    virtual double pr(uint32_t* its_wid,
 
145
                      uint32_t* ite_wid,
 
146
                      uint32_t wid);
143
147
 
144
148
    virtual bool
145
149
    bufferize(void** buf_ptr, size_t* sz);
148
152
    loadFromBuffer(void* buf_ptr, size_t sz);
149
153
 
150
154
    bool
151
 
    loadFromFile (const char *fname);
 
155
    loadFromFile(const char *fname);
152
156
 
153
157
    bool
154
 
    saveToFile (const char *fname = NULL);
 
158
    saveToFile(const char *fname = NULL);
155
159
 
156
 
    virtual void addStopWords (const std::set<unsigned int>& stopWords);
157
 
    virtual void initStopWords ();
 
160
    virtual void addStopWords(const std::set<uint32_t>& stopWords);
 
161
    virtual void initStopWords();
158
162
 
159
163
protected:
160
 
    typedef unsigned                              TWordId;
 
164
    typedef uint32_t TWordId;
161
165
    typedef std::pair<TWordId, TWordId>           TBigram;
162
 
    typedef TWordId                               TUnigram;
 
166
    typedef TWordId TUnigram;
163
167
    typedef std::map<TBigram, int>                TBigramPool;
164
168
    typedef std::map<TUnigram, int>               TUnigramPool;
165
169
    typedef std::deque<TWordId>                   TContextMemory;
167
171
    static const size_t contxt_memory_size;
168
172
    static const double focus_memory_ratio;
169
173
 
170
 
    TContextMemory          m_memory;
171
 
    TUnigramPool            m_unifreq;
172
 
    TBigramPool             m_bifreq;
 
174
    TContextMemory m_memory;
 
175
    TUnigramPool m_unifreq;
 
176
    TBigramPool m_bifreq;
173
177
 
174
 
    std::string             m_history_path;
175
 
    std::set<unsigned int>  m_stopWords;
 
178
    std::string m_history_path;
 
179
    std::set<uint32_t>  m_stopWords;
176
180
 
177
181
protected:
178
182
    double pr(TBigram& bg);