00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 using namespace std;
00022
00023 #include <math.h>
00024 #include <assert.h>
00025 #include "mfstream.h"
00026 #include "mempool.h"
00027 #include "htable.h"
00028 #include "dictionary.h"
00029 #include "n_gram.h"
00030 #include "doc.h"
00031
00032
00033
00034 doc::doc(dictionary* d,char* docfname)
00035 {
00036 dict=d;
00037 n=0;
00038 m=0;
00039 V=new int[dict->size()];
00040 N=new int[dict->size()];
00041 T=new int[dict->size()];
00042 cd=-1;
00043 dfname=docfname;
00044 df=NULL;
00045 };
00046
00047 doc::~doc()
00048 {
00049 delete [] V;
00050 delete [] N;
00051 delete [] T;
00052 }
00053
00054
00055
00056 int doc::open()
00057 {
00058
00059 df=new mfstream(dfname,ios::in);
00060
00061 char header[100];
00062 df->getline(header,100);
00063 if (sscanf(header,"DoC %d",&n) && n>0)
00064 binary=true;
00065 else if (sscanf(header,"%d",&n) && n>0)
00066 binary=false;
00067 else {
00068 cerr << "doc::open error wrong header\n";
00069 exit(0);
00070 }
00071
00072 cerr << "opening: " << n << (binary?" bin-":" txt-") << "docs\n";
00073 cd=-1;
00074
00075 return 1;
00076 }
00077
00078
00079 int doc::reset()
00080 {
00081
00082 cd=-1;
00083 m=0;
00084 df->close();
00085 delete df;
00086 open();
00087 return 1;
00088 }
00089
00090
00091 int doc::read()
00092 {
00093
00094
00095 if (cd >=(n-1))
00096 return 0;
00097
00098 m=0;
00099
00100 for (int i=0; i<dict->size(); i++) N[i]=0;
00101
00102 if (binary) {
00103 df->read((char *)&m,sizeof(int));
00104 df->read((char *)V,m * sizeof(int));
00105 df->read((char *)T,m * sizeof(int));
00106 for (int i=0; i<m; i++) {
00107 N[V[i]]=T[i];
00108 }
00109 } else {
00110
00111 int eod=dict->encode(dict->EoD());
00112 int bod=dict->encode(dict->BoD());
00113
00114 ngram ng(dict);
00115
00116 while((*df) >> ng) {
00117 if (ng.size>0) {
00118 if (*ng.wordp(1)==bod) {
00119 ng.size=0;
00120 continue;
00121 }
00122 if (*ng.wordp(1)==eod) {
00123 ng.size=0;
00124 break;
00125 }
00126 N[*ng.wordp(1)]++;
00127 if (N[*ng.wordp(1)]==1)V[m++]=*ng.wordp(1);
00128 }
00129 }
00130 }
00131 cd++;
00132 return 1;
00133 }
00134
00135
00136 int doc::savernd(char* fname,int num)
00137 {
00138
00139 assert((df!=NULL) && (cd==-1));
00140
00141 srand(100);
00142
00143 mfstream out(fname,ios::out);
00144 out << "DoC\n";
00145 out.write((const char*) &n,sizeof(int));
00146
00147 cerr << "n=" << n << "\n";
00148
00149
00150 char taken[n];
00151 int r;
00152 for (int i=0; i<n; i++) taken[i]=0;
00153
00154 for (int d=0; d<num; d++) {
00155 while((r=(rand() % n)) && taken[r]) {};
00156 cerr << "random document found " << r << "\n";
00157 taken[r]++;
00158 reset();
00159 for (int i=0; i<=r; i++) read();
00160 out.write((const char *)&m,sizeof(int));
00161 out.write((const char*) V,m * sizeof(int));
00162 for (int i=0; i<m; i++)
00163 out.write((const char*) &N[V[i]],sizeof(int));
00164 }
00165
00166
00167 reset();
00168 for (int d=0; d<n; d++) {
00169 read();
00170 if (!taken[d]) {
00171 out.write((const char*)&m,sizeof(int));
00172 out.write((const char*)V,m * sizeof(int));
00173 for (int i=0; i<m; i++)
00174 out.write((const char*)&N[V[i]],sizeof(int));
00175 } else {
00176 cerr << "do not save doc " << d << "\n";
00177 }
00178 }
00179
00180
00181 reset();
00182 return 1;
00183 }
00184
00185 int doc::save(char* fname)
00186 {
00187
00188 assert((df!=NULL) && (cd==-1));
00189
00190 mfstream out(fname,ios::out);
00191 out << "DoC "<< n << "\n";
00192 for (int d=0; d<n; d++) {
00193 read();
00194 out.write((const char*)&m,sizeof(int));
00195 out.write((const char*)V,m * sizeof(int));
00196 for (int i=0; i<m; i++)
00197 out.write((const char*)&N[V[i]],sizeof(int));
00198 }
00199
00200
00201 reset();
00202 return 1;
00203 }
00204
00205
00206 int doc::save(char* fname, int bsz)
00207 {
00208
00209 assert((df!=NULL) && (cd==-1));
00210
00211 char name[100];
00212 int i=0;
00213
00214 while (cd < (n-1)) {
00215 sprintf(name,"%s.%d",fname,++i);
00216 mfstream out(name,ios::out);
00217 int csz=(cd+bsz)<n?bsz:(n-cd-1);
00218 out << "DoC "<< csz << "\n";
00219 for (int d=0; d<csz; d++) {
00220 read();
00221 out.write((const char*)&m,sizeof(int));
00222 out.write((const char*)V,m * sizeof(int));
00223 for (int i=0; i<m; i++)
00224 out.write((const char*)&N[V[i]],sizeof(int));
00225 }
00226 out.close();
00227 }
00228
00229 reset();
00230 return 1;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239