00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <cstdlib>
00025 #include <stdlib.h>
00026 #include <iomanip>
00027 #include <cassert>
00028 #include "mempool.h"
00029 #include "htable.h"
00030 #include "dictionary.h"
00031 #include "n_gram.h"
00032 #include "index.h"
00033
00034 using namespace std;
00035
00036 ngram::ngram(dictionary* d,int sz)
00037 {
00038 dict=d;
00039 size=sz;
00040 succ=0;
00041 freq=0;
00042 info=0;
00043 pinfo=0;
00044 link=NULL;
00045 isym=-1;
00046 memset(word,0,sizeof(int)*MAX_NGRAM);
00047 memset(midx,0,sizeof(int)*MAX_NGRAM);
00048 memset(path,0,sizeof(char *)*MAX_NGRAM);
00049 }
00050
00051 ngram::ngram(ngram& ng)
00052 {
00053 size=ng.size;
00054 freq=ng.freq;
00055 succ=0;
00056 info=0;
00057 pinfo=0;
00058 link=NULL;
00059 isym=-1;
00060 dict=ng.dict;
00061 memcpy(word,ng.word,sizeof(int)*MAX_NGRAM);
00062 memcpy(midx,ng.word,sizeof(int)*MAX_NGRAM);
00063
00064 }
00065
00066 void ngram::trans (const ngram& ng)
00067 {
00068 size=ng.size;
00069 freq=ng.freq;
00070 if (dict == ng.dict) {
00071 info=ng.info;
00072 isym=ng.isym;
00073 memcpy(word,ng.word,sizeof(int)*MAX_NGRAM);
00074 memcpy(midx,ng.midx,sizeof(int)*MAX_NGRAM);
00075 } else {
00076 info=0;
00077 memset(midx,0,sizeof(int)*MAX_NGRAM);
00078 isym=-1;
00079 for (int i=1; i<=size; i++)
00080 word[MAX_NGRAM-i]=dict->encode(ng.dict->decode(*ng.wordp(i)));
00081 }
00082 }
00083
00084
00085 void ngram::invert (const ngram& ng)
00086 {
00087 size=ng.size;
00088 for (int i=1; i<=size; i++) {
00089 *wordp(i)=*ng.wordp(size-i+1);
00090 }
00091 }
00092
00093 void ngram::shift ()
00094 {
00095 memmove((void *)&word[MAX_NGRAM-size+1],(void *)&word[MAX_NGRAM-size],(size-1) * sizeof(int));
00096 size--;
00097 }
00098
00099 void ngram::shift (int sz)
00100 {
00101 if (sz>size) sz=size;
00102 memmove((void *)&word[MAX_NGRAM-size+sz],(void *)&word[MAX_NGRAM-size],(size-sz) * sizeof(int));
00103 size-=sz;
00104 }
00105
00106
00107 ifstream& operator>> ( ifstream& fi , ngram& ng)
00108 {
00109 char w[MAX_WORD];
00110 memset(w,0,MAX_WORD);
00111 w[0]='\0';
00112
00113 if (!(fi >> setw(MAX_WORD) >> w))
00114 return fi;
00115
00116 if (strlen(w)==(MAX_WORD-1))
00117 cerr << "ngram: a too long word was read ("
00118 << w << ")\n";
00119
00120 int c=ng.dict->encode(w);
00121
00122 if (c == -1 ) {
00123 cerr << "ngram: " << w << " is OOV \n";
00124 exit(1);
00125 }
00126
00127 memcpy(ng.word,ng.word+1,(MAX_NGRAM-1)*sizeof(int));
00128
00129 ng.word[MAX_NGRAM-1]=(int)c;
00130 ng.freq=1;
00131
00132 if (ng.size<MAX_NGRAM) ng.size++;
00133
00134 return fi;
00135
00136 }
00137
00138
00139 int ngram::pushw(const char* w)
00140 {
00141
00142 assert(dict!=NULL);
00143
00144 int c=dict->encode(w);
00145
00146 if (c == -1 ) {
00147 cerr << "ngram: " << w << " is OOV \n";
00148 exit(1);
00149 }
00150
00151 pushc(c);
00152
00153 return 1;
00154
00155 }
00156
00157 int ngram::pushc(int c)
00158 {
00159
00160 size++;
00161 if (size>MAX_NGRAM) size=MAX_NGRAM;
00162 size_t len = size - 1;
00163 size_t src = MAX_NGRAM - len;
00164
00165 memmove((void *)&word[src - 1],(void *)&word[src], len * sizeof(int));
00166
00167
00168
00169
00170
00171
00172 word[MAX_NGRAM-1]=c;
00173
00174 return 1;
00175
00176 }
00177
00178 int ngram::pushc(int* codes, int codes_len)
00179 {
00180
00181
00182 assert (codes_len <= MAX_NGRAM);
00183
00184 size+=codes_len;
00185
00186
00187
00188
00189
00190
00191 if (size>MAX_NGRAM) size=MAX_NGRAM;
00192 size_t len = size - codes_len;
00193 size_t src = MAX_NGRAM - len;
00194
00195
00196
00197
00198
00199
00200
00201
00202 if (len > 0) memmove((void *)&word[src - codes_len],(void *)&word[src], len * sizeof(int));
00203
00204 memcpy((void *)&word[MAX_NGRAM - codes_len],(void*)&codes[0],codes_len*sizeof(int));
00205
00206 return 1;
00207 }
00208
00209
00210 istream& operator>> ( istream& fi , ngram& ng)
00211 {
00212 char w[MAX_WORD];
00213 memset(w,0,MAX_WORD);
00214 w[0]='\0';
00215
00216 assert(ng.dict != NULL);
00217
00218 if (!(fi >> setw(MAX_WORD) >> w))
00219 return fi;
00220
00221 if (strlen(w)==(MAX_WORD-1))
00222 cerr << "ngram: a too long word was read ("
00223 << w << ")\n";
00224
00225 ng.pushw(w);
00226
00227 ng.freq=1;
00228
00229 return fi;
00230
00231 }
00232
00233 ofstream& operator<< (ofstream& fo,ngram& ng)
00234 {
00235
00236 assert(ng.dict != NULL);
00237
00238 for (int i=ng.size; i>0; i--)
00239 fo << ng.dict->decode(ng.word[MAX_NGRAM-i]) << (i>1?" ":"");
00240 fo << "\t" << ng.freq;
00241 return fo;
00242 }
00243
00244 ostream& operator<< (ostream& fo,ngram& ng)
00245 {
00246
00247 assert(ng.dict != NULL);
00248
00249 for (int i=ng.size; i>0; i--)
00250 fo << ng.dict->decode(ng.word[MAX_NGRAM-i]) << (i>1?" ":"");
00251 fo << "\t" << ng.freq;
00252
00253 return fo;
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270