-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_v8_old - 複製.cpp
More file actions
376 lines (277 loc) · 10 KB
/
main_v8_old - 複製.cpp
File metadata and controls
376 lines (277 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* ver. 7
for ver.8 : be aware that for testing purpose, the loop in main
only execute 5 times rather than DICT_SIZE times.
1.)
Nicole creates dictionaryCpp.cpp .
2.)
add this line of code in rmvStop( ) : clear_array( token );
3.)
For stopwordfile.txt,
main( ) knows the existence of function txtToAarray( ).
But for every xxx.txt,
main( ) DOES NOT KNOW the existence of function txtToAarray( ).
Only rmvStop( ) knows it.
4.)
porter( ) now recives one more argument : FILE * f
Therefore, this line of code is discarded in porter( ) :
f = fopen( "afile.txt" ,"r");
And then, these lines of code are added into main( ) :
FILE * stemFile; pointer stemFile will be passed to poter( )
stemFile = fopen( "afile.txt" ,"r");
porter( 1, stemByPorter, tokenSize, stemFile );
fclose(stemFile);
5.)
Nicole adds this line of code in porter( ) : rewind(f);
( Because if f is not rewound, then int & tokenSize won't get the correct value )
*/
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <strings.h>
#include <unistd.h>
#include <ctype.h>
#include "porter.cpp"
#include "dictCpp.cpp"
#define MAX_STR_LEN 30
#define MAX_STOP_SIZE 500
#define APPEND 0
#define RMV 1
#define MAX_WORD_NUM 10000
#define DOC_NUM 1095
#define DICT_SIZE 1000/* dictionary size : total number of tokens of all docs.*/
using namespace std;
/*((((((((((((((((((((((((((((((((((((((((((((((((*/
/*((((((((((((((((((((((((((((((((((((((((((((((((*/
/*((((((((((((((((((((((((((((((((((((((((((((((((*/
/*((((((((((((((((((((((((((((((((((((((((((((((((*/
/*((((((((((((((((((((((((((((((((((((((((((((((((*/
/*((((((((((((((((((((((((((((((((((((((((((((((((*/
/******************************************************************************/
void clear_array( char inputArr[ ][ MAX_STR_LEN ] ){
int i = 0;
int j = 0;
for( i = 0; i < MAX_WORD_NUM; i++ ){
for( j = 0; j < MAX_STR_LEN; j++ ){
inputArr[i][j] = '\0';
} // End 2-for
} // End-1-for
}
/******************************************************************************/
char tempCharacter_tTA; /* _tTA is the abbrev. for _textToArray */
char oneDimArr[10000];
char temp_tTA[ MAX_STR_LEN ];
char *tokenPtr_tTA;
int twoDim_i = 0;
int i = 0;
/* ~ ~ ~ ~ ~ ~ ~ ~ */
void txtToArray( FILE *txtFile, char twoDimArr[][ MAX_STR_LEN ], int & wordCount )
{
printf("\nEnter txtToArray" );
/* wordCout must be reset to zero ! */
wordCount = 0;
twoDim_i = 0;
i = 0;
/*--------------------------------------------*/
if ( txtFile==NULL ) perror ( "\nIn txtToArray : File Cannot Be Opened.\n" );
else
{
do {
tempCharacter_tTA = fgetc ( txtFile );
oneDimArr[ i ] = tempCharacter_tTA;
if ( tempCharacter_tTA == '\n' ){
oneDimArr[ i ] = ' ';
}
i++;
} while ( tempCharacter_tTA != EOF);
oneDimArr[ i ] = tempCharacter_tTA;
/*--------------------------------------------*/
}
tokenPtr_tTA = strtok( oneDimArr, " ,.-" );
while( tokenPtr_tTA != NULL ){
strncpy( &temp_tTA[ 0 ], tokenPtr_tTA, MAX_STR_LEN );
strncpy( twoDimArr[ twoDim_i ], temp_tTA, MAX_STR_LEN );
i++;
tokenPtr_tTA = strtok( NULL, " ,.-" );
twoDim_i ++;
wordCount ++;
}
fclose(txtFile);
} /* End txtToArray( ) */
/******************************************************************************/
/* ~ ~ ~ ~ ~ ~ */
FILE * pFile;
char tempCharacter;
/* int i = 0; */
int n = 0;
char content[10000];
int tokenNum = 0;
int stop_i = 0;
int token_i = 0;
int token_j = 0;
int rmv_i = 0;
int tokenIndex = 0;
int tokenCount = 0;
int temp_i = 0;
int flag = APPEND;
char * tokenPtr;
char temp[ MAX_STR_LEN ];
char token[ 10000 ][ MAX_STR_LEN ];
/* ~ ~ ~ ~ ~ ~ */
int rmvStop( char rmvStop[][ MAX_STR_LEN ], char stopList[][ MAX_STR_LEN ], int & stopNum, int & tokenSize )
{
printf("\nEnter rvmStop( ) ; " );
tokenSize = 0; /* tokenSize must be reset to zero ! */
i = 0;
n = 0;
stop_i = 0;
token_i = 0;
token_j = 0;
rmv_i = 0;
tokenIndex = 0;
tokenCount = 0;
temp_i = 0;
flag = APPEND;
pFile=fopen ("input.txt","r");
if (pFile==NULL) perror ("\nIn rmvStop( ) : Error opening file");
else{
printf("\nIn rmvStop( ) : Successfully open pFile");
clear_array( token );
txtToArray( pFile, token, tokenSize );
printf("\nIn rmvStop( ) : txtToArray done" );
fclose (pFile);
printf("\nIn rmvStop( ) : stopNum is : %d .", stopNum );
printf("\nIn rmvStop( ) : After txtToArray( ) tokenSize is : %d .", tokenSize );
/*-------------------------------------------------------------*/
for ( token_i = 0; token_i < tokenSize; token_i++ ){
flag = APPEND;
for ( stop_i = 0; stop_i <= stopNum; stop_i++ ){
if ( !strcmp( token[ token_i ], stopList[ stop_i ] ) ){
flag = RMV;
} // END if
} //END for
if( flag == APPEND ){
strncpy( rmvStop[ rmv_i ], token[ token_i ], MAX_STR_LEN );
rmv_i ++;
} // END if
} // END for
printf("\nIn rmvStop( ) : ending this function");
rmvStop[ rmv_i ][0] = '\0';
} // END fopen( );
return 0;
}
/******************************************************************************/
/*------------------------------------------------*/
int main()
{
FILE * stopFile;
FILE * aFile;
FILE * stemFile; /* pointer stemFile will be passed to poter( ) */
/* - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - */
char stopArr[ MAX_STOP_SIZE ][ MAX_STR_LEN ];
char article[ 10000 ][ MAX_STR_LEN ];
char check[ 10000 ][ MAX_STR_LEN ];
char stemByPorter[ 10000 ][ MAX_STR_LEN ];
/* PPS. Don't name the array as "porter" because it would be confused */
/* with "poter.cpp" and "poter( );" */
char dict[ DICT_SIZE ][ MAX_STR_LEN ];
char **dptr = NULL; /* dictionary pointer */
dptr = (char**)malloc(sizeof(char*)*20);
int stopSize = 0;
int tokenSize = 0;
int stemSize = 0;
int currDictSize = 0;
int i = 0;
char * buf = "s""p";
char buf2[ MAX_STR_LEN ];
/* 哇哈哈哈哈哈哈哈 竟然這麼爽快 */
itoa( 2, buf2, 10 );
string sym( 1, buf2[0] );
const char * CONST_firstHalfOfFileName;
CONST_firstHalfOfFileName = sym.c_str();
const char * stopwordfile ;
stopwordfile = CONST_firstHalfOfFileName;
/*正確 : stopwordfile = CONST_firstHalfOfFileName; */
/* 錯誤 : stopwordfile = CONST_firstHalfOfFileName + "Hello World"; */
stopFile=fopen( "stopwordfile.txt", "r" );
txtToArray( stopFile, stopArr, stopSize );
/* Testing arrayToListNode */
printf("\n\n\n\n\n ------------------------------" );
printf("\n Testing arrayToListNode\n\n\n\n\n" );
clear_array( article );
rmvStop( article, stopArr, stopSize, tokenSize );
/* PPS. Now rmvStop() resets tokenSize to zero and then updates tokenSize*/
printf("\nrmvStop( ) done ! " );
aFile = fopen( "afile.txt" , "w" );
printf("\nSuccessfully open file. aFile is now : %d", aFile);
printf("\naFile = fopen afile.txt, w" );
if ( aFile==NULL ) perror ( "\n In main() : File Cannot Be Opened.\n" );
else
{
clear_array( stemByPorter );
/* -> write the stuff in article[][] into aFile */
while( i <= tokenSize ){ /* tokenSize has already been updated by rmvStop()*/
fprintf( aFile, "%s ", article[ i ] );
i++;
} /* End 3-while */
fclose( aFile );
printf("\nIn main( ) : fclose aFile." );
/*--------------------------------------------------------*/
stemFile = fopen( "afile.txt" ,"r");
rewind(stemFile);
porter( 1, stemByPorter, tokenSize, stemFile );
fclose(stemFile);
printf("In main( ) : porter is done.\n And tokenSize is %d\n", tokenSize );
/* pre-condition : after porter() is done, stemByPorter[][] now */
/* holds the stemming result of afile.txt. */
/* pre-condition : tokenSize is correctly updated*/
arrayToListNode( stemByPorter, tokenSize );
/*--------------------------------------------------------*/
} /* END 2-if-else */
printf("\n\n\n\n\n E N D - Testing arrayToListNode - E N D - \n\n\n\n\n" );
/* E N D - Testing arrayToListNode - E N D - */
for( int docLoop = 1; docLoop <= 5; docLoop ++ )
{
printf("\n\n\n\n\n------------------------------" );
printf("\ndocument %d.txt", docLoop );
clear_array( article );
rmvStop( article, stopArr, stopSize, tokenSize );
/* PPS. Now rmvStop() resets tokenSize to zero and then updates tokenSize*/
printf("\nrmvStop( ) done ! " );
aFile = fopen( "afile.txt" , "w" );
printf("\nSuccessfully open file. aFile is now : %d", aFile);
printf("\naFile = fopen afile.txt, w" );
if ( aFile==NULL ) perror ( "\n In main() : File Cannot Be Opened.\n" );
else
{
clear_array( stemByPorter );
/* -> write the stuff in article[][] into aFile */
while( i <= tokenSize ){ /* tokenSize has already been updated by rmvStop()*/
fprintf( aFile, "%s ", article[ i ] );
i++;
} /* End 3-while */
fclose( aFile );
printf("\nIn main( ) : fclose aFile." );
/*--------------------------------------------------------*/
stemFile = fopen( "afile.txt" ,"r");
rewind(stemFile);
porter( 1, stemByPorter, tokenSize, stemFile );
fclose(stemFile);
printf("In main( ) : porter is done.\n And tokenSize is %d\n", tokenSize );
/* - - - - - - - - - - - - - - - */
/* dictCpp.cpp*/
/* - - - - - - - - - - - - - - - */
/* pre-condition : after porter() is done, stemByPorter[][] now */
/* holds the stemming result of afile.txt. */
/* pre-condition : tokenSize is correctly updated*/
arrayToListNode( stemByPorter, tokenSize );
/* - - - - - - - - - - - - - - - */
/*--------------------------------------------------------*/
} /* END 2-if-else */
} /* End 1-for */
printf( "\n\n\n\n\n\n\n" );
printf( " -------------\n" );
printf( " | Thank you! |\n" );
printf( " -------------\n\n\n\n\n\n\n\n\n\n\n\n\n" );
system("PAUSE");
} /* End main( ) */