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

« back to all changes in this revision

Viewing changes to src/slm/tslmpack/arpa_slm.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:
6
6
 * Distribution License ("CDDL")(collectively, the "License"). You may not use this
7
7
 * file except in compliance with the License. You can obtain a copy of the CDDL at
8
8
 * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
9
 
 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the 
 
9
 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
10
10
 * specific language governing permissions and limitations under the License. When
11
11
 * distributing the software, include this License Header Notice in each file and
12
12
 * include the full text of the License in the License file as well as the
13
13
 * following notice:
14
 
 * 
 
14
 *
15
15
 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
16
16
 * (CDDL)
17
17
 * For Covered Software in this distribution, this License shall be governed by the
19
19
 * Any litigation relating to this License shall be subject to the jurisdiction of
20
20
 * the Federal Courts of the Northern District of California and the state courts
21
21
 * of the State of California, with venue lying in Santa Clara County, California.
22
 
 * 
 
22
 *
23
23
 * Contributor(s):
24
 
 * 
 
24
 *
25
25
 * If you wish your version of this file to be governed by only the CDDL or only
26
26
 * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
27
27
 * include this software in this distribution under the [CDDL or LGPL Version 2.1]
30
30
 * Version 2.1, or to extend the choice of license to its licensees as provided
31
31
 * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
32
32
 * Version 2 license, then the option applies only if the new code is made subject
33
 
 * to such option by the copyright holder. 
 
33
 * to such option by the copyright holder.
34
34
 */
35
35
#include <string>
36
36
#include <iostream>
43
43
/**
44
44
 * the GNU extension is not always available, so we invent another wheel.
45
45
 */
46
 
size_t getline(char *buf, size_t n, FILE* stream)
 
46
size_t
 
47
getline(char *buf, size_t n, FILE* stream)
47
48
{
48
49
    char* p;
49
 
    char* end = buf+n;
 
50
    char* end = buf + n;
50
51
    for (p = buf; p != end; ++p) {
51
52
        int c = fgetc(stream);
52
53
        if (c == '\n' || c == EOF)
57
58
    if (p != end)
58
59
        *p = 0;
59
60
    else
60
 
        *(p-1) = 0;
61
 
    return p-buf;
 
61
        *(p - 1) = 0;
 
62
    return p - buf;
62
63
}
63
64
 
64
 
char* getwords(char* buf, char** next)
 
65
char*
 
66
getwords(char* buf, char** next)
65
67
{
66
68
    char* word = buf;
67
69
    char* delim = strstr(buf, "  ");
70
72
        exit(2);
71
73
    }
72
74
    *delim = '\0';
73
 
    *next = delim+2;
 
75
    *next = delim + 2;
74
76
    return word;
75
77
}
76
78
 
77
 
unsigned get_wid(const char* word, const TLexicon& lexicon)
 
79
unsigned
 
80
get_wid(const char* word, const TLexicon& lexicon)
78
81
{
79
82
    TLexicon::const_iterator lexi = lexicon.find(word);
80
83
    unsigned wid;
97
100
            assert(nword < N_GRAM);
98
101
            *end = 0;
99
102
            hw[nword++] = get_wid(word, lexicon);
100
 
            word = end+1;
 
103
            word = end + 1;
101
104
        }
102
105
    }
103
106
    if (buf != end) {
106
109
    return nword;
107
110
}
108
111
 
109
 
void 
 
112
void
110
113
CArpaSlm::TLeaf::load(istream& is, const TLexicon& lexicon)
111
114
{
112
115
    char buf[1024];
118
121
           &pr, &bol, &bon);
119
122
}
120
123
 
121
 
void 
 
124
void
122
125
CArpaSlm::TNode::load(istream& is, const TLexicon& lexicon)
123
126
{
124
127
    char buf[1024];
130
133
           &pr, &bow, &bol, &bon);
131
134
}
132
135
 
133
 
void 
 
136
void
134
137
CArpaSlm::TNode::load_level0(istream& is)
135
138
{
136
139
    hw[0] = 0;
161
164
            TNode node0;
162
165
            node0.load_level0(file);
163
166
            m_levels[0].push_back(node0);
164
 
        }
165
 
        else if (lvl < m_N) {
 
167
        } else if (lvl < m_N) {
166
168
            m_levels[lvl].reserve(size);
167
169
            for (int i = 0; i < size; ++i) {
168
170
                TNode node;
182
184
}
183
185
 
184
186
template <class NodeT>
185
 
struct CompareNode
186
 
{
 
187
struct CompareNode {
187
188
    const unsigned m_lvl;
188
 
    CompareNode(unsigned lvl) : m_lvl(lvl) {}
 
189
    CompareNode(unsigned lvl) : m_lvl(lvl)
 
190
    {
 
191
    }
189
192
    /**
190
193
     * @return true if strictly less, false otherwise
191
194
     */
192
 
    bool operator () (const NodeT& node, const TSIMWordId hw[N_GRAM]) {
 
195
    bool
 
196
    operator ()(const NodeT& node, const TSIMWordId hw[N_GRAM])
 
197
    {
193
198
        for (unsigned i = 0; i < m_lvl; ++i) {
194
199
            if (node.hw[i] < hw[i])
195
200
                return true;
200
205
        return false;
201
206
    }
202
207
};
203
 
  
204
 
void 
 
208
 
 
209
void
205
210
CArpaSlm::threading()
206
211
{
207
212
    {
211
216
    for (unsigned lvl = 1; lvl < m_N; ++lvl) {
212
217
        TNodeLevel& level = m_levels[lvl];
213
218
        unsigned last_child = 0;
214
 
        for (TNodeLevel::iterator node = level.begin(); node != level.end(); ++node) {
 
219
        for (TNodeLevel::iterator node = level.begin();
 
220
             node != level.end();
 
221
             ++node) {
215
222
            node->ch = last_child = find_1st_child(lvl, *node, last_child);
216
223
        }
217
224
    }
220
227
unsigned
221
228
CArpaSlm::find_1st_child(unsigned lvl, const TNode& node, int last_child)
222
229
{
223
 
    assert (lvl < m_N);
224
 
    if (lvl == m_N-1) {
225
 
        TLeafLevel::iterator found = lower_bound(m_lastLevel.begin(), m_lastLevel.end(), node.hw, CompareNode<TLeaf>(lvl));
 
230
    assert(lvl < m_N);
 
231
    if (lvl == m_N - 1) {
 
232
        TLeafLevel::iterator found = lower_bound(
 
233
            m_lastLevel.begin(), m_lastLevel.end(), node.hw,
 
234
            CompareNode<TLeaf>(lvl));
226
235
        return distance(m_lastLevel.begin(), found);
227
236
    } else {
228
 
        const TNodeLevel& level = m_levels[lvl+1];
229
 
        TNodeLevel::const_iterator found = lower_bound(level.begin(), level.end(), node.hw, CompareNode<TNode>(lvl));
 
237
        const TNodeLevel& level = m_levels[lvl + 1];
 
238
        TNodeLevel::const_iterator found = lower_bound(level.begin(), level.end(
 
239
                                                           ), node.hw,
 
240
                                                       CompareNode<TNode>(lvl));
230
241
        return distance(level.begin(), found);
231
242
    }
232
243
}