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

« back to all changes in this revision

Viewing changes to src/slm/ids2ngram/ids2ngram.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
39
39
#include "config.h"
40
40
#endif
41
 
                                                                                                                                 
 
41
 
42
42
#ifdef HAVE_ASSERT_H
43
43
#include <assert.h>
44
44
#endif
45
 
                                                                                                                                 
 
45
 
46
46
#ifdef HAVE_GETOPT_H
47
47
#include <getopt.h>
48
48
#endif
58
58
#include "idngram_merge.h"
59
59
 
60
60
template<int N>
61
 
void WriteOut(FILE* out, std::map<CSIM_Idngram<N>, unsigned int> & map)
 
61
void
 
62
WriteOut(FILE* out, std::map<CSIM_Idngram<N>, unsigned int> & map)
62
63
{
63
 
        typedef typename std::map<CSIM_Idngram<N>,unsigned int>::iterator TMapIterator;
64
 
        TMapIterator its=map.begin(), ite=map.end();
65
 
        for (; its != ite; ++its) {
66
 
                fwrite(its->first.ids, sizeof(TSIMWordId), N, out);
67
 
                fwrite(&(its->second), sizeof(unsigned int), 1, out);
68
 
        }
69
 
        map.clear();
 
64
    typedef typename std::map<CSIM_Idngram<N>,
 
65
                              unsigned int>::iterator TMapIterator;
 
66
    TMapIterator its = map.begin(), ite = map.end();
 
67
    for (; its != ite; ++its) {
 
68
        fwrite(its->first.ids, sizeof(TSIMWordId), N, out);
 
69
        fwrite(&(its->second), sizeof(unsigned int), 1, out);
 
70
    }
 
71
    map.clear();
70
72
}
71
73
 
72
74
template<int N>
73
 
void ProcessingRead(FILE *fp, FILE* swap, std::vector<long>& para_offsets, size_t paraMax)
 
75
void
 
76
ProcessingRead(FILE *fp,
 
77
               FILE* swap,
 
78
               std::vector<long>& para_offsets,
 
79
               size_t paraMax)
74
80
{
75
 
        typedef CSIM_Idngram<N> TNgram;
76
 
        typedef typename std::map<CSIM_Idngram<N>, unsigned int> TMap;
77
 
 
78
 
        TMap map;
79
 
        TNgram ngram;
80
 
 
81
 
        TSIMWordId* ids = ngram.ids;
82
 
        fread(ids, sizeof(TSIMWordId), N-1, fp);
83
 
        while (fread(ids+N-1, sizeof(TSIMWordId), 1, fp) == 1) {
84
 
                assert (map[ngram] < UINT_MAX);
85
 
                ++map[ngram];
86
 
                if (map.size() >= paraMax)
87
 
                {
88
 
                        printf("."); fflush(stdout);
89
 
                        WriteOut(swap, map); 
90
 
                        para_offsets.push_back(ftell(swap));
91
 
                }
92
 
                for (int i=0; i<N-1; ++i) ids[i] = ids[i+1];
93
 
        }
94
 
        if (map.size() > 0) {
95
 
                printf("."); fflush(stdout);
96
 
                WriteOut(swap, map);
97
 
                para_offsets.push_back(ftell(swap));
98
 
        }
 
81
    typedef CSIM_Idngram<N> TNgram;
 
82
    typedef typename std::map<CSIM_Idngram<N>, unsigned int> TMap;
 
83
 
 
84
    TMap map;
 
85
    TNgram ngram;
 
86
 
 
87
    TSIMWordId* ids = ngram.ids;
 
88
    fread(ids, sizeof(TSIMWordId), N - 1, fp);
 
89
    while (fread(ids + N - 1, sizeof(TSIMWordId), 1, fp) == 1) {
 
90
        assert(map[ngram] < UINT_MAX);
 
91
        ++map[ngram];
 
92
        if (map.size() >= paraMax) {
 
93
            printf("."); fflush(stdout);
 
94
            WriteOut(swap, map);
 
95
            para_offsets.push_back(ftell(swap));
 
96
        }
 
97
        for (int i = 0; i < N - 1; ++i) ids[i] = ids[i + 1];
 
98
    }
 
99
    if (map.size() > 0) {
 
100
        printf("."); fflush(stdout);
 
101
        WriteOut(swap, map);
 
102
        para_offsets.push_back(ftell(swap));
 
103
    }
99
104
}
100
105
 
101
106
static struct option long_options[] =
102
107
{
103
 
        {"NMax", 1, 0, 'n'},
104
 
        {"out", 1, 0, 'o'},
105
 
        {"swap", 1, 0, 's'},
106
 
        {"para", 1, 0, 'p'},
107
 
        {0, 0, 0, 0}
 
108
    { "NMax", 1, 0, 'n' },
 
109
    { "out", 1, 0, 'o' },
 
110
    { "swap", 1, 0, 's' },
 
111
    { "para", 1, 0, 'p' },
 
112
    { 0, 0, 0, 0 }
108
113
};
109
114
 
110
 
static int N=0;
111
 
static int paraMax=0;
112
 
static char* output=NULL;
113
 
static char* swapfile=NULL;
 
115
static int N = 0;
 
116
static int paraMax = 0;
 
117
static char* output = NULL;
 
118
static char* swapfile = NULL;
114
119
 
115
 
void ShowUsage()
 
120
void
 
121
ShowUsage()
116
122
{
117
 
        printf("Usage:\n\tids2ngram options idsfile[ idsfile...]\n");
118
 
        printf("\nDescription\n");
119
 
        printf("   This program generate idngram file, which is a sorted [id1,..idN,freq] array, from binary id stream files.\n");
120
 
        printf("\nInput:\n");
121
 
        printf("\tBinary id stream files looks like [id0,...,idX]\n");
122
 
        printf("\nOptions:\n");
123
 
        printf("\t  -n N               # N-gram\n");
124
 
        printf("\t  -s swapfile        # intermedia temporary file\n");
125
 
        printf("\t  -o outputfile      # result idngram file [id1, ... idN, freq]*\n");
126
 
        printf("\t  -p para_size       # maxium ngram-items per para\n");
127
 
        printf("\nExample:\n");
128
 
        printf("   Following example will use three input idstream file idsfile[1,2,3] to generate the idngram file all.id3gram. Each para (internal map size or hash size) would be 1024000, using swap file for temp result. All temp para result would final be merged to got the final result.\n");
129
 
        printf("\tids2idngram -n 3 -s /tmp/swap -o all.id3gram -p 1024000 idsfile1 idsfile2 idsfile3\n\n");
130
 
        exit(100);
 
123
    printf("Usage:\n\tids2ngram options idsfile[ idsfile...]\n");
 
124
    printf("\nDescription\n");
 
125
    printf(
 
126
        "   This program generate idngram file, which is a sorted [id1,..idN,freq] array, from binary id stream files.\n");
 
127
    printf("\nInput:\n");
 
128
    printf("\tBinary id stream files looks like [id0,...,idX]\n");
 
129
    printf("\nOptions:\n");
 
130
    printf("\t  -n N               # N-gram\n");
 
131
    printf("\t  -s swapfile        # intermedia temporary file\n");
 
132
    printf(
 
133
        "\t  -o outputfile      # result idngram file [id1, ... idN, freq]*\n");
 
134
    printf("\t  -p para_size       # maxium ngram-items per para\n");
 
135
    printf("\nExample:\n");
 
136
    printf(
 
137
        "   Following example will use three input idstream file idsfile[1,2,3] to generate the idngram file all.id3gram. Each para (internal map size or hash size) would be 1024000, using swap file for temp result. All temp para result would final be merged to got the final result.\n");
 
138
    printf(
 
139
        "\tids2idngram -n 3 -s /tmp/swap -o all.id3gram -p 1024000 idsfile1 idsfile2 idsfile3\n\n");
 
140
    exit(100);
131
141
}
132
142
 
133
 
static void getParameters(int argc, char* const argv[])
 
143
static void
 
144
getParameters(int argc, char* const argv[])
134
145
{
135
 
        int option_index = 0;
136
 
        int c;
137
 
        while ((c=getopt_long(argc, argv, "p:n:s:o:", long_options, &option_index)) != -1)
138
 
        {
139
 
                switch (c) {
140
 
                case 'n':
141
 
                        N = atoi(strdup(optarg));
142
 
                        break;
143
 
                case 'p':
144
 
                        paraMax = atoi(strdup(optarg));
145
 
                        break;
146
 
                case 'o':
147
 
                        output = strdup(optarg);
148
 
                        break;
149
 
                case 's':
150
 
                        swapfile = strdup(optarg);
151
 
                        break;
152
 
                default:
153
 
                        ShowUsage();
154
 
                }
155
 
        }
156
 
        if (N < 1 || N > 3 || paraMax < 1024 || output == NULL || swapfile == NULL) 
157
 
                ShowUsage();
 
146
    int option_index = 0;
 
147
    int c;
 
148
    while ((c =
 
149
                getopt_long(argc, argv, "p:n:s:o:", long_options,
 
150
                            &option_index)) != -1) {
 
151
        switch (c) {
 
152
        case 'n':
 
153
            N = atoi(strdup(optarg));
 
154
            break;
 
155
        case 'p':
 
156
            paraMax = atoi(strdup(optarg));
 
157
            break;
 
158
        case 'o':
 
159
            output = strdup(optarg);
 
160
            break;
 
161
        case 's':
 
162
            swapfile = strdup(optarg);
 
163
            break;
 
164
        default:
 
165
            ShowUsage();
 
166
        }
 
167
    }
 
168
    if (N < 1 || N > 3 || paraMax < 1024 || output == NULL || swapfile == NULL)
 
169
        ShowUsage();
158
170
}
159
171
 
160
172
static std::vector<long> para_offsets;
161
173
 
162
 
int main(int argc, char* argv[])
 
174
int
 
175
main(int argc, char* argv[])
163
176
{
164
 
        getParameters(argc, argv);
165
 
        FILE *swap = fopen(swapfile, "wb+");
166
 
        FILE *out = fopen(output, "wb+");
167
 
        if (optind >= argc) ShowUsage();
168
 
   while (optind < argc) {
169
 
        printf("Processing %s:", argv[optind]); fflush(stdout);
170
 
        FILE *fp = fopen(argv[optind], "rb");
171
 
        switch (N) {
172
 
        case 1:
173
 
                        ProcessingRead<1>(fp, swap, para_offsets, paraMax);
174
 
                        break;
175
 
        case 2:
176
 
                        ProcessingRead<2>(fp, swap, para_offsets, paraMax);
177
 
                        break;
178
 
        case 3:
179
 
                        ProcessingRead<3>(fp, swap, para_offsets, paraMax);
180
 
                        break;
181
 
        }
182
 
                fclose(fp);
183
 
      printf ("\n"); fflush(stdout);
184
 
                ++optind;
185
 
   }
186
 
   printf("Merging..."); fflush(stdout);
187
 
        switch (N) {
188
 
        case 1:
189
 
                ProcessingIdngramMerge<1>(swap, out, para_offsets);
190
 
                break;
191
 
        case 2:
192
 
                ProcessingIdngramMerge<2>(swap, out, para_offsets);
193
 
                break;
194
 
        case 3:
195
 
                ProcessingIdngramMerge<3>(swap, out, para_offsets);
196
 
                break;
197
 
        }
198
 
   printf ("Done\n"); fflush(stdout);
199
 
   fclose(out);
200
 
   fclose(swap);
201
 
        return 0;
 
177
    getParameters(argc, argv);
 
178
    FILE *swap = fopen(swapfile, "wb+");
 
179
    FILE *out = fopen(output, "wb+");
 
180
    if (optind >= argc) ShowUsage();
 
181
    while (optind < argc) {
 
182
        printf("Processing %s:", argv[optind]); fflush(stdout);
 
183
        FILE *fp = fopen(argv[optind], "rb");
 
184
        switch (N) {
 
185
        case 1:
 
186
            ProcessingRead<1>(fp, swap, para_offsets, paraMax);
 
187
            break;
 
188
        case 2:
 
189
            ProcessingRead<2>(fp, swap, para_offsets, paraMax);
 
190
            break;
 
191
        case 3:
 
192
            ProcessingRead<3>(fp, swap, para_offsets, paraMax);
 
193
            break;
 
194
        }
 
195
        fclose(fp);
 
196
        printf("\n"); fflush(stdout);
 
197
        ++optind;
 
198
    }
 
199
    printf("Merging..."); fflush(stdout);
 
200
    switch (N) {
 
201
    case 1:
 
202
        ProcessingIdngramMerge<1>(swap, out, para_offsets);
 
203
        break;
 
204
    case 2:
 
205
        ProcessingIdngramMerge<2>(swap, out, para_offsets);
 
206
        break;
 
207
    case 3:
 
208
        ProcessingIdngramMerge<3>(swap, out, para_offsets);
 
209
        break;
 
210
    }
 
211
    printf("Done\n"); fflush(stdout);
 
212
    fclose(out);
 
213
    fclose(swap);
 
214
    return 0;
202
215
}
203
216