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

« back to all changes in this revision

Viewing changes to src/slm/tslminfo/tslminfo.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
63
63
    typedef std::vector<TState> iterator;
64
64
 
65
65
    int
66
 
    getLevelSize(int lvl) { return m_LevelSizes[lvl]; }
 
66
    getLevelSize(int lvl)
 
67
    {
 
68
        return m_LevelSizes[lvl];
 
69
    }
67
70
 
68
71
    int
69
 
    getN() { return m_N; }
 
72
    getN()
 
73
    {
 
74
        return m_N;
 
75
    }
70
76
 
71
77
    bool
72
78
    beginLevel(int lvl, iterator& it);
73
79
 
74
80
    void
75
 
    next(iterator& it) { ++(it.back()); adjustIterator(it); }
 
81
    next(iterator& it)
 
82
    {
 
83
        ++(it.back()); adjustIterator(it);
 
84
    }
76
85
 
77
86
    bool
78
 
    isEnd(iterator& it) { return (((it.back().getIdx()) + 1) == getLevelSize(it.back().getLevel())); }
 
87
    isEnd(iterator& it)
 
88
    {
 
89
        return(((it.back().getIdx()) + 1) == getLevelSize(it.back().getLevel()));
 
90
    }
79
91
 
80
92
    void*
81
93
    getNodePtr(TState s);
85
97
    {
86
98
        double val = m_prTable[pr_idx];
87
99
        if (log_format) {
88
 
            return (m_UseLogPr)?(val):(-log(val));
 
100
            return (m_UseLogPr) ? (val) : (-log(val));
89
101
        } else {
90
 
            return (m_UseLogPr)?(exp(-val)):(val);
 
102
            return (m_UseLogPr) ? (exp(-val)) : (val);
91
103
        }
92
104
    }
93
105
 
96
108
    {
97
109
        double val = m_bowTable[bow_idx];
98
110
        if (log_format) {
99
 
            return (m_UseLogPr)?(val):(-log(val));
 
111
            return (m_UseLogPr) ? (val) : (-log(val));
100
112
        } else {
101
 
            return (m_UseLogPr)?(exp(-val)):(val);
 
113
            return (m_UseLogPr) ? (exp(-val)) : (val);
102
114
        }
103
115
    }
104
116
 
111
123
CIterateThreadSlm::beginLevel(int lvl, iterator& it)
112
124
{
113
125
    it.clear();
114
 
    if (lvl > m_N) return false;
115
 
    for (int i=0; i <= lvl; ++i)
 
126
    if (lvl > (int) m_N) return false;
 
127
    for (int i = 0; i <= lvl; ++i)
116
128
        it.push_back(TState(i, 0));
117
129
    adjustIterator(it);
118
130
    return true;
123
135
{
124
136
    unsigned int lvl = s.getLevel();
125
137
    if (lvl == m_N) {
126
 
        return (((TLeaf*)m_Levels[lvl]) + s.getIdx());
 
138
        return(((TLeaf*)m_Levels[lvl]) + s.getIdx());
127
139
    } else {
128
 
        return (((TNode*)m_Levels[lvl]) + s.getIdx());
 
140
        return(((TNode*)m_Levels[lvl]) + s.getIdx());
129
141
    }
130
142
}
131
143
 
133
145
CIterateThreadSlm::adjustIterator(iterator& it)
134
146
{
135
147
//    if (!isEnd(it)) {
136
 
    for (int lvl = it.size()-2; lvl >= 0; --lvl) {
 
148
    for (int lvl = it.size() - 2; lvl >= 0; --lvl) {
137
149
        int sz = getLevelSize(lvl);
138
 
        unsigned child = (it[lvl+1]).getIdx();
139
 
        while ((it[lvl].getIdx() < (sz-1)) && ( (((TNode*)getNodePtr(it[lvl]))+1)->ch() <= child )) {
 
150
        unsigned child = (it[lvl + 1]).getIdx();
 
151
        while ((it[lvl].getIdx() < (sz - 1)) &&
 
152
               ((((TNode*)getNodePtr(it[lvl])) + 1)->ch() <= child)) {
140
153
            ++(it[lvl]);
141
154
        }
142
155
    }
143
156
//    }
144
157
}
145
158
 
146
 
void ShowUsage()
 
159
void
 
160
ShowUsage()
147
161
{
148
162
    printf("Usage:\n");
149
163
    printf("    tslminfo [options] threaded_slm_file\n");
150
164
    printf("\nDescription:\n");
151
 
    printf("    tslminfo tell information of a threaded back-off language model 'threaded_slm_file'. It can also print the model to ARPA format.");
152
 
    printf(" When no options given, slminfo will only print number of items in each level of the language model.\n");
 
165
    printf(
 
166
        "    tslminfo tell information of a threaded back-off language model 'threaded_slm_file'. It can also print the model to ARPA format.");
 
167
    printf(
 
168
        " When no options given, slminfo will only print number of items in each level of the language model.\n");
153
169
    printf("\nOptions:\n");
154
170
    printf("    -v             # Verbose mode, printing arpa format.\n");
155
 
    printf("    -p             # Prefer normal probability instead of -log(Pr) which is default. Valid under -v option.\n");
156
 
    printf("    -l dict_file   # Lexicon. Valid under -v option. Substitute the word-id with word-text in the output.\n");
 
171
    printf(
 
172
        "    -p             # Prefer normal probability instead of -log(Pr) which is default. Valid under -v option.\n");
 
173
    printf(
 
174
        "    -l dict_file   # Lexicon. Valid under -v option. Substitute the word-id with word-text in the output.\n");
157
175
    printf("\n");
158
176
    exit(100);
159
177
}
160
178
 
161
 
static bool  verbose = false;
 
179
static bool verbose = false;
162
180
static char *lexicon_filename = NULL;
163
 
static bool  use_log_pr = true;
 
181
static bool use_log_pr = true;
164
182
 
165
183
static struct option long_options[] =
166
184
{
167
 
    {"verbose", 0, 0, 'v'},
168
 
    {"pr", 0, 0, 'p'},
169
 
    {"lexicon", 1, 0, 'l'},
170
 
    {0, 0, 0, 0}
 
185
    { "verbose", 0, 0, 'v' },
 
186
    { "pr", 0, 0, 'p' },
 
187
    { "lexicon", 1, 0, 'l' },
 
188
    { 0, 0, 0, 0 }
171
189
};
172
190
 
173
 
static void getParameters(int argc, char* argv[])
 
191
static void
 
192
getParameters(int argc, char* argv[])
174
193
{
175
 
   int c, option_index = 0;
176
 
   while ((c=getopt_long(argc, argv, "vpl:", long_options, &option_index)) != -1)
177
 
   {
178
 
      switch (c) {
 
194
    int c, option_index = 0;
 
195
    while ((c =
 
196
                getopt_long(argc, argv, "vpl:", long_options,
 
197
                            &option_index)) != -1) {
 
198
        switch (c) {
179
199
        case 'v':
180
200
            verbose = true;
181
201
            break;
182
 
      case 'l':
 
202
        case 'l':
183
203
            lexicon_filename = strdup(optarg);
184
 
         break;
 
204
            break;
185
205
        case 'p':
186
206
            use_log_pr = false;
187
207
            break;
191
211
    }
192
212
    if (use_log_pr == false && !verbose) ShowUsage();
193
213
    if (lexicon_filename != NULL && !verbose) ShowUsage();
194
 
    if (optind != argc-1) ShowUsage();
 
214
    if (optind != argc - 1) ShowUsage();
195
215
}
196
216
 
197
217
typedef std::map<unsigned int, std::string> TReverseLexicon;
198
218
 
199
219
 
200
220
void
201
 
PrintARPA(CIterateThreadSlm& itslm, const char* lexicon_filename, bool use_log_pr)
 
221
PrintARPA(CIterateThreadSlm& itslm,
 
222
          const char* lexicon_filename,
 
223
          bool use_log_pr)
202
224
{
203
 
    static unsigned int  id;
 
225
    static unsigned int id;
204
226
    static char word[10240];
205
227
 
206
228
    TReverseLexicon* plexicon = NULL;
219
241
                while (*p == ' ' || *p == '\t')
220
242
                    ++p;
221
243
                if (!(*p >= '0' && *p <= '9')) continue;
222
 
                for (id=0; *p >= '0' && *p <= '9'; ++p)
223
 
                    id = 10*id + (*p - '0');
 
244
                for (id = 0; *p >= '0' && *p <= '9'; ++p)
 
245
                    id = 10 * id + (*p - '0');
224
246
                (*plexicon)[id] = std::string(word);
225
247
            }
226
248
        }
229
251
 
230
252
    CIterateThreadSlm::iterator it;
231
253
    for (int lvl = 0; lvl <= itslm.getN(); ++lvl) {
232
 
        printf("\\%d-gram\\%d\n", lvl, itslm.getLevelSize(lvl)-1);
233
 
        for (itslm.beginLevel(lvl,it); !itslm.isEnd(it); itslm.next(it)){
234
 
            for (int i=1; i < lvl; ++i) {
235
 
                CIterateThreadSlm::TNode*pn = (CIterateThreadSlm::TNode*)itslm.getNodePtr(it[i]);
 
254
        printf("\\%d-gram\\%d\n", lvl, itslm.getLevelSize(lvl) - 1);
 
255
        for (itslm.beginLevel(lvl, it); !itslm.isEnd(it); itslm.next(it)) {
 
256
            for (int i = 1; i < lvl; ++i) {
 
257
                CIterateThreadSlm::TNode*pn =
 
258
                    (CIterateThreadSlm::TNode*)itslm.getNodePtr(it[i]);
236
259
                if (plexicon != NULL)
237
260
                    printf("%s ", (*plexicon)[pn->wid()].c_str());
238
261
                else
239
262
                    printf("%9d ", pn->wid());
240
263
            }
241
264
            if (lvl < itslm.getN()) {
242
 
                CIterateThreadSlm::TNode*pn = (CIterateThreadSlm::TNode*)itslm.getNodePtr(it[lvl]);
 
265
                CIterateThreadSlm::TNode*pn =
 
266
                    (CIterateThreadSlm::TNode*)itslm.getNodePtr(it[lvl]);
243
267
                if (lvl > 0) {
244
268
                    if (plexicon != NULL)
245
269
                        printf("%s ", ((*plexicon)[pn->wid()]).c_str());
252
276
                printf("%16.12lf %16.12lf ", pr, bow);
253
277
                printf("(%1u,%u)\n", pn->bol(), pn->bon());
254
278
            } else {
255
 
                CIterateThreadSlm::TLeaf*pn = (CIterateThreadSlm::TLeaf*)itslm.getNodePtr(it[lvl]);
 
279
                CIterateThreadSlm::TLeaf*pn =
 
280
                    (CIterateThreadSlm::TLeaf*)itslm.getNodePtr(it[lvl]);
256
281
                if (lvl > 0) {
257
282
                    if (plexicon != NULL)
258
283
                        printf("%s ", ((*plexicon)[pn->wid()]).c_str());
281
306
 
282
307
    CIterateThreadSlm itslm;
283
308
 
284
 
    if (itslm.load(argv[argc-1], true)) {
 
309
    if (itslm.load(argv[argc - 1], true)) {
285
310
        if (!verbose) {
286
311
            printf("Total %d level ngram: ", itslm.getN());
287
 
            for (int lvl=1; lvl <=itslm.getN(); ++lvl)
288
 
                printf("%d ", itslm.getLevelSize(lvl)-1);
289
 
            printf((itslm.isUseLogPr())?" using -log(pr)\n":" using direct pr\n");
 
312
            for (int lvl = 1; lvl <= itslm.getN(); ++lvl)
 
313
                printf("%d ", itslm.getLevelSize(lvl) - 1);
 
314
            printf(
 
315
                (itslm.isUseLogPr()) ? " using -log(pr)\n" :
 
316
                " using direct pr\n");
290
317
        } else {
291
318
            PrintARPA(itslm, lexicon_filename, use_log_pr);
292
319
        }