~ubuntu-branches/ubuntu/quantal/sunpinyin/quantal

« back to all changes in this revision

Viewing changes to src/slm/mmseg/mmseg.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
56
56
 
57
57
static struct option long_options[] =
58
58
{
59
 
    {"dict", 1, 0, 'd'},
60
 
    {"format", 1, 0, 'f'},
61
 
    {"show-id", 0, 0, 'i'},
62
 
    {"s-tok", 1, 0, 's'},
63
 
    {"ambiguious-id", 1, 0, 'a'},
64
 
    {0, 0, 0, 0}
 
59
    { "dict", 1, 0, 'd' },
 
60
    { "format", 1, 0, 'f' },
 
61
    { "show-id", 0, 0, 'i' },
 
62
    { "s-tok", 1, 0, 's' },
 
63
    { "ambiguious-id", 1, 0, 'a' },
 
64
    { 0, 0, 0, 0 }
65
65
};
66
66
 
67
67
static char* s_strDictFile = NULL;
76
76
ShowUsage()
77
77
{
78
78
    fprintf(stderr, "\nUsage:\n");
79
 
    fprintf(stderr, "mmseg -d dict_file [-f (text|bin)] [-i] [-s STOK_ID] [-a AMBI_ID]\n\n");
 
79
    fprintf(
 
80
        stderr,
 
81
        "mmseg -d dict_file [-f (text|bin)] [-i] [-s STOK_ID] [-a AMBI_ID]\n\n");
80
82
    fprintf(stderr, "  -f --format:\n");
81
 
    fprintf(stderr, "    Output Format, can be 'text' or 'bin'. default 'bin'\n");
82
 
    fprintf(stderr, "    Normally, in text mode, word text are output, while in binary mode,\n");
83
 
    fprintf(stderr, "    binary short integer of the word-ids are written to stdout.\n");
 
83
    fprintf(stderr,
 
84
            "    Output Format, can be 'text' or 'bin'. default 'bin'\n");
 
85
    fprintf(
 
86
        stderr,
 
87
        "    Normally, in text mode, word text are output, while in binary mode,\n");
 
88
    fprintf(stderr,
 
89
            "    binary short integer of the word-ids are written to stdout.\n");
84
90
    fprintf(stderr, "  -s --stok:\n");
85
91
    fprintf(stderr, "    Sentence token id. Default 10.\n");
86
 
    fprintf(stderr, "    It will be written to output in binary mode after every sentence.\n");
 
92
    fprintf(
 
93
        stderr,
 
94
        "    It will be written to output in binary mode after every sentence.\n");
87
95
    fprintf(stderr, "  -i --show-id:\n");
88
 
    fprintf(stderr, "    Show Id info. Under text output format mode, attach id after known.\n");
 
96
    fprintf(
 
97
        stderr,
 
98
        "    Show Id info. Under text output format mode, attach id after known.\n");
89
99
    fprintf(stderr, "    words. If under binary mode, print id(s) in text.\n");
90
100
    fprintf(stderr, "  -a --ambiguious-id:\n");
91
 
    fprintf(stderr, "    Ambiguious means ABC => A BC or AB C. If specified (AMBI-ID != 0), \n");
92
 
    fprintf(stderr, "    The sequence ABC will not be segmented, in binary mode, the AMBI-ID \n");
93
 
    fprintf(stderr, "    is written out; in text mode, <ambi>ABC</ambi> will be output. Default \n");
 
101
    fprintf(
 
102
        stderr,
 
103
        "    Ambiguious means ABC => A BC or AB C. If specified (AMBI-ID != 0), \n");
 
104
    fprintf(
 
105
        stderr,
 
106
        "    The sequence ABC will not be segmented, in binary mode, the AMBI-ID \n");
 
107
    fprintf(
 
108
        stderr,
 
109
        "    is written out; in text mode, <ambi>ABC</ambi> will be output. Default \n");
94
110
    fprintf(stderr, "    is 0.\n");
95
111
    fprintf(stderr, "\n");
96
112
    fprintf(stderr, "Notes:\n");
97
 
    fprintf(stderr, "  Under binary mode, consecutive id of 0 are merged into one 0.\n");
98
 
    fprintf(stderr, "  Under text mode, no space are inserted between unknown-words. \n");
 
113
    fprintf(stderr,
 
114
            "  Under binary mode, consecutive id of 0 are merged into one 0.\n");
 
115
    fprintf(
 
116
        stderr,
 
117
        "  Under text mode, no space are inserted between unknown-words. \n");
99
118
    fprintf(stderr, "\n");
100
119
    fprintf(stderr, "\n");
101
120
    exit(1000);
105
124
getParameters(int argc, char* argv[])
106
125
{
107
126
    int c;
108
 
    while ((c=getopt_long(argc, argv, "d:if:s:a:", long_options, NULL)) != -1)
109
 
    {
 
127
    while ((c =
 
128
                getopt_long(argc, argv, "d:if:s:a:", long_options,
 
129
                            NULL)) != -1) {
110
130
        switch (c) {
111
131
        case 'd':
112
132
            s_strDictFile = strdup(optarg);
146
166
}
147
167
 
148
168
static void
149
 
output(int len, const TWCHAR* p, TSIMWordId idprev, TSIMWordId idcur, int& nWords)
 
169
output(int len,
 
170
       const TWCHAR* p,
 
171
       TSIMWordId idprev,
 
172
       TSIMWordId idcur,
 
173
       int& nWords)
150
174
{
151
175
    static char mbword[1024];
152
176
    static TWCHAR wcword[1024];
153
177
 
154
178
    bool bRealGap = (idcur != SIM_ID_NOT_WORD || idprev != SIM_ID_NOT_WORD);
155
179
    if (s_bTextOut) {
156
 
        for (int i=0; i < len; ++i, ++p)
 
180
        for (int i = 0; i < len; ++i, ++p)
157
181
            wcword[i] = *p;
158
182
        wcword[len] = 0;
159
183
        WCSTOMBS(mbword, wcword, sizeof(mbword));
161
185
            printf("(%d)", unsigned(idprev));
162
186
        if (bRealGap && (nWords > 0))
163
187
            printf(" ");
164
 
        (s_iAmbiID && idcur == s_iAmbiID)? printf ("<ambi>%s</ambi>", mbword): 
165
 
                                           printf("%s", mbword);
 
188
        (s_iAmbiID && idcur == s_iAmbiID) ? printf("<ambi>%s</ambi>", mbword) :
 
189
        printf("%s", mbword);
166
190
        if (s_bShowId && idcur != SIM_ID_NOT_WORD)
167
191
            printf("(%d)", unsigned(idcur));
168
192
    } else {
180
204
}
181
205
 
182
206
/**
183
 
* Return 最大交集歧义长度. For example, ABCDEF if ABC CD DEF are words.
184
 
* if return len > word_len, then ambiguious exists at word [p p+len)...
185
 
*/
 
207
 * Return 最大交集歧义长度. For example, ABCDEF if ABC CD DEF are words.
 
208
 * if return len > word_len, then ambiguious exists at word [p p+len)...
 
209
 */
186
210
int
187
211
getAmbiLen(const TWCHAR* p, int word_len)
188
212
{
189
213
    const CSIMDict::TState* pstate;
190
214
 
191
 
    for (int i=1; i<word_len && *(p+i) != WCH_NULL; ++i) {
192
 
        int len = s_dict->matchLongest(s_dict->getRoot(), pstate, p+i);
193
 
        if (word_len < i+len)
194
 
            word_len = i+len;
 
215
    for (int i = 1; i < word_len && *(p + i) != WCH_NULL; ++i) {
 
216
        int len = s_dict->matchLongest(s_dict->getRoot(), pstate, p + i);
 
217
        if (word_len < i + len)
 
218
            word_len = i + len;
195
219
    }
196
220
 
197
221
    return word_len;
211
235
    if (!s_bTextOut)
212
236
        output_stok(nWords);
213
237
 
214
 
    while (true){
 
238
    while (true) {
215
239
        if (ReadSentence(sntnc, iter, false) == false)
216
240
            break;
217
241
 
218
242
        for (const TWCHAR *p = sntnc.c_str(); (*p); ) {
219
 
 
220
243
            const CSIMDict::TState* pstate;
221
244
            int len = s_dict->matchLongest(s_dict->getRoot(), pstate, p);
222
245
            if (len <= 0) {
226
249
                idcur = pstate->word_id;
227
250
 
228
251
            if (s_iAmbiID != WCH_NULL) {
229
 
                int ambiLen=getAmbiLen(p, len);
 
252
                int ambiLen = getAmbiLen(p, len);
230
253
                if (ambiLen > len) {
231
254
                    len = ambiLen;
232
255
                    idcur = s_iAmbiID;
263
286
    fprintf(stderr, "Loading lexicon..."); fflush(stderr);
264
287
    s_dict = new CSIMDict();
265
288
    if (!s_dict->parseText(s_strDictFile)) {
266
 
      fprintf(stderr, "fail\n"); fflush(stderr);
267
 
      exit(1001);
 
289
        fprintf(stderr, "fail\n"); fflush(stderr);
 
290
        exit(1001);
268
291
    }
269
292
    fprintf(stderr, "done"); fflush(stderr);
270
293
 
271
294
    if (argc == 0) {
272
295
        fprintf(stderr, "\nProcessing from stdin..."); fflush(stderr);
273
296
        processSingleFile(stdin, nWords, nAmbis);
274
 
        fprintf(stderr, "%d words, %d ambiguious. Done!\n", nWords, nAmbis); fflush(stderr);
 
297
        fprintf(stderr, "%d words, %d ambiguious. Done!\n", nWords, nAmbis);
 
298
        fflush(stderr);
275
299
    } else {
276
 
        for (int i=0; i < argc; ++i) {
 
300
        for (int i = 0; i < argc; ++i) {
277
301
            fprintf(stderr, "\nProcessing %s...", argv[i]); fflush(stderr);
278
302
            FILE *fp = fopen(argv[i], "r");
279
303
            if (fp != NULL) {
280
304
                processSingleFile(fp, nWords, nAmbis);
281
 
                fprintf(stderr, "@Offset %ld, %d words, %d ambiguious. Done!\n", ftell(fp), nWords, nAmbis); fflush(stderr);
 
305
                fprintf(stderr,
 
306
                        "@Offset %ld, %d words, %d ambiguious. Done!\n",
 
307
                        ftell(fp),
 
308
                        nWords,
 
309
                        nAmbis); fflush(stderr);
282
310
            } else {
283
311
                fprintf(stderr, "Can not Open!!!!!!!\n"); fflush(stderr);
284
312
            }