-
Notifications
You must be signed in to change notification settings - Fork 1
/
PorterStemming.cpp
450 lines (384 loc) · 14.2 KB
/
PorterStemming.cpp
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <iostream> // std::cin, std::cout
#include <fstream> // std::ifstream
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include <stdio.h>
using namespace std;
static int k0,k;
static char *b;
char temp[20];
char add_array[5];
static int parameter;
/*Function to check individually for consonants and vowels.
Vowels here are: a,e,i,o,u and y preceded by a consonant.
k0 here is the first element.
*/
int cons(char a[],int i)
{
switch (a[i])
{ case 'a': case 'e': case 'i': case 'o': case 'u': return 0;
case 'y': {if (i==k0) return 1; else !cons(a,i-1);}
default: return 1;
}
}
/*
If C- consonant sequence (c,cc,ccc,ccc,..)
V- vowel sequence (v, vv, vvv,...)
This function m() gives the count of (VC) formed.
Example
m=0 TR, EE, TREE, Y, BY.
m=1 TROUBLE, OATS, TREES, IVY.
m=2 TROUBLES, PRIVATE, OATEN, ORRERY.
*/
int m(char a[],int j,int k)
{
int n = 0;
int r = k0;
int flag;
int c=0;
int v=0;
int value1=0;
int value2=0;
int value=0;
int loop=0;
while (1)
{
while(r<=k)
{
{
if (r > j) break;
if (! cons(a,r)) {c++; value1=1; break;} /*checks for vowel*/
r++;
}
if (value1==1) break;
}
value1=0;
r++;
while(r<=k)
{
{
if (r > j) break;
if (cons(a,r)) {v++; value2=1; break;} /*checks for consonant*/
r++;
}
if (value2==1) break;
}
value2=0;
r++;
if ((c==v)&&(r<=k+1))
{
value=value+1;c=0;v=0; /*counts the number of VC sequences*/
}
if (r>k) break;
}
return value; /*returns the value of VC count found out*/
}
/*checks in a word, if it has a vowel*/
int vowelinstem(char a[],int j)
{
int last_w=j;
int i;
for (i = k0; i <= last_w; i++) if (! cons(a,i)) return 1;
return 0;
}
/*checks for double consonants. Checks for the last position and the last but one-th position. Of the form say -ss */
int doublec(char a[],int j)
{
int last_w=j;
if (j < k0+1) return 0;
if (a[j] != a[j-1]) return 0;
return cons(a,j);
}
/*Checks for the form CVC*/
int cvc(int i,char a[],int j)
{
if (i <= k0+2)
{
if (!cons(a,i) || cons(a,i-1) || !cons(a,i-2))
{
int ch = a[i];
if (ch == 'w' || ch == 'x' || ch == 'y') return 0; /* In CVC, the last C should not be w,x,y as per the algo.*/
}
}
return 1;
}
/* Readjusts the string as per requirement. Adds new suffixes or changes to different forms as required.*/
void setto(int len_inc,char *add,char a[])
{
// cout<<"\nI am here now";
int len3=len_inc;
if (parameter==1)
{
char temp1[15];
memset(temp1,0,sizeof(temp1)); /*setting temp1 to null*/
memcpy(temp1,a,k+1); /*copying the contents of array a till the parameter k passed into temp1*/
memset(a,'\0',sizeof(a)); /*setting a to null. Basically to remove out the junk values when used later in the func*/
strcpy(a,temp1); /*Copying temp1 to a*/
//cout<<a;
parameter=0;
}
k+=len_inc;
strcat(a,add); /*Concatenating the suffix passed. Eg:pon(a)+i(add)=poni(a)*/
char temp[15];
memset(temp,0,sizeof(temp));
memcpy(temp,a,k+1);
memset(a,0,sizeof(a));
memcpy(a,temp,k+1);
return;
}
/*This part matches the suffix passed and checks if the suffix is present in the word*/
int ends(char a[],char * s,int length, int j)
{
int last_w;
int len=length;
int pos=k-length+1;
char temp[20];
memset(temp,0,sizeof(temp));
for(int m=0;m<=len-1;m++)
{
temp[m]=a[pos];
pos++;
}
int r;
if ((r=strcmp(s,temp))==0){return 1;} /*Compares and returns 1 if suffix found.*/
else
return 0;
}
/* step1ab() gets rid of plurals and -ed or -ing. e.g.
caresses -> caress
ponies -> poni
ties -> ti
caress -> caress
cats -> cat
feed -> feed
agreed -> agree
disabled -> disable
matting -> mat
mating -> mate
meeting -> meet
milling -> mill
messing -> mess
meetings -> meet
*/
void step1ab(char a[],int j)
{ //cout<<"Enter 1AB";
int last_w=j;
int h=0;
if (a[k] == 's')
{
if (ends(a,"sses",4,last_w)==1) { k-= 2;} else
if (ends(a,"ies",3,last_w)==1) { k-=3;setto(1,"i",a);} else
if (a[k-1] != 's') {k--;return;}
} else
if (ends(a,"eed",3,last_w)==1) {if (m(a,last_w,k)>1)k--;} else
if ((ends(a,"ed",2,last_w)==1)||(ends(a,"ing",3,last_w)==1))
{
if ((ends(a,"ed",2,last_w)==1)&&vowelinstem(a,last_w)) { k-=2;}
if ((ends(a,"ing",3,last_w)==1)&&vowelinstem(a,last_w)) {k-=3;}
j=k;
if (ends(a,"at",2,j)==1) { k-=2;setto(3,"ate",a);} else
if (ends(a,"bl",2,j)==1) { k-=2;setto(3,"ble",a);} else
if (ends(a,"iz",2,j)==1) { k-=2;setto(3,"ize",a);} else
if (doublec(a,k) ) { k--; {int ch=a[k]; if (ch == 'l' || ch == 's' || ch == 'z') k++;}}
}
if((ends(a,"at",2,j)==1) && (ends(a,"bl",2,j)==1) && (ends(a,"iz",2,j)==1))
{
if (m(a,j,k) == 1 && cvc(k,a,0)) {parameter=1;setto(1,"e",a);}
}
return;
}
/*turns terminal y to i when there is another vowel in the stem. */
void step1c(char a[]) {
//cout<<"Enter 1C";
//cout<<k;
if (ends(a,"y",1,k) && vowelinstem(a,k))
{ parameter=1;
k-=1;
setto(1,"i",a);
}}
/* step2() maps double suffices to single ones. so -ization ( = -ize plus
-ation) maps to -ize etc. note that the string before the suffix must give
m() > 0. */
void step2(char a[],int j) {
if ((m(a,j,k)>0)){
switch (a[k-1])
{
case 'a': if (ends(a,"ational",7,k)) {k-=7; parameter=1;cout<<"The value is "<<" "<<k; setto(3,"ate",a); break; }
if (ends(a,"tional",6,k)) {k-=6;setto(4,"tion",a); break; }
break;
case 'c': if (ends(a,"enci",4,k)) { k-=4;parameter=1;setto(4,"ence",a); break; } else
if (ends(a,"anci",4,k)) { k-=4;parameter=1;setto(4,"ance",a); break; }
break;
case 'e': if (ends(a,"izer",4,k)) {{k-=4;parameter=1;setto(3,"ize",a); break; }}
break;
case 'l': if (ends(a,"bli",3,k)) {k-=3;parameter=1;setto(3,"ble",a); break; }
if (ends(a,"eli",3,k)) {k-=3;setto(1,"e",a); break; }
if (ends(a,"alli",4,k)) {k-=4;parameter=1;setto(2,"al",a); break; }
if (ends(a,"entli",5,k)) {k-=5;parameter=1;setto(3,"ent",a); break; }
if (ends(a,"ousli",5,k)) {k-=5;parameter=1;setto(3,"ous",a); break; }
break;
case 'o': if (ends(a,"ator",4,k)) { k-=4;parameter=1;setto(3,"ate",a); break; }
if (ends(a,"ization",7,k)) { k-=7;parameter=1;setto(3,"ize",a); break; }
if (ends(a,"ation",5,k)) {k-=5;parameter=1;setto(3,"ate",a); break; }
break;
case 's': if (ends(a,"alism",5,k)) { k-=5;parameter=1;setto(2,"al",a); break; }
if (ends(a,"iveness",7,k)) { k-=7;parameter=1;setto(3,"ive",a); break; }
if (ends(a,"fulness",7,k)) { k-=7;parameter=1;setto(3,"ful",a); break; }
if (ends(a,"ousness",7,k)) { k-=7;parameter=1;setto(3,"ous",a); break; }
break;
case 't': if (ends(a,"aliti",5,k)) { k-=5;parameter=1;setto(2,"al",a); break; }
if (ends(a,"iviti",5,k)) { k-=5;parameter=1;setto(3,"ive",a); break; }
if (ends(a,"biliti",6,k)) { k-=6;parameter=1;setto(3,"ble",a); break; }
break;
case 'g': if (ends(a,"logi",4,k)) { k-=4;parameter=1;setto(3,"log",a); break; }
} }}
/* step3() deals with -ic-, -full, -ness etc. similar strategy to step2. */
void step3(char a[],int j) {
if (m(a,j,k)>0){
switch (a[k])
{
case 'e': if (ends(a,"icate",5,k)) { k-=5;parameter=1;setto(2,"ic",a); break; }
if (ends(a,"ative",5,k)) { k-=5;parameter=1;setto(0," ",a); break; }
if (ends(a,"alize",5,k)) { k-=5;parameter=1;setto(2,"al",a); break; }
break;
case 'i': if (ends(a,"iciti",5,k)) { k-=5;parameter=1;setto(2,"ic",a); break; }
break;
case 'l': if (ends(a,"ical",4,k)) { k-=4;parameter=1;setto(2,"ic",a); break; }
if (ends(a,"ful",3,k)) { k-=3;parameter=1;setto(0," ",a); break; }
break;
case 's': if (ends(a,"ness",4,k)) { k-=4;parameter=1;setto(0,"",a); break; }
break;
} }}
/* step4() takes off -ant, -ence etc., in context <c>vcvc<v>,i.e. m()>1. */
void step4(char a[],int j)
{ if ((m(a,j,k)>1))
{ switch (a[k-1])
{ case 'a': if (ends(a,"al",2,k)) {k-=2;parameter=1;setto(0," ",a);return;}
case 'c': if (ends(a,"ance",4,k)){k-=4;parameter=1;setto(0," ",a);return;}
if (ends(a,"ence",4,k)){k-=4;parameter=1;setto(0," ",a);return;}
case 'e': if (ends(a,"er",2,k)) {k-=2;parameter=1;setto(0," ",a);return;}
case 'i': if (ends(a,"ic",2,k)){k-=2;parameter=1;setto(0," ",a);return;}
case 'l': if (ends(a,"able",4,k)){k-=4;parameter=1;setto(0," ",a);return;}
if (ends(a,"ible",4,k)){k-=4;parameter=1;setto(0," ",a);return;}
case 'n': if (ends(a,"ant",3,k)) {k-=3;parameter=1;setto(0," ",a);return;}
if (ends(a,"ement",5,k)){k-=5;parameter=1;setto(0," ",a);return;}
if (ends(a,"ment",4,k)) {k-=4;parameter=1;setto(0," ",a);return;}
if (ends(a,"ent",3,k)) {k-=3;parameter=1;setto(0," ",a);return;}
case 'o': if (ends(a,"ion",3,k) && (a[k-3] == 's' || a[k-3] == 't')){k-=3;parameter=1;setto(0," ",a);return;}
if (ends(a,"ou",2,k)) {k-=2;parameter=1;setto(0," ",a);return;}
case 's': if (ends(a,"ism",3,k)) {k-=3;parameter=1;setto(0," ",a);return;}
case 't':
if (ends(a,"ate",3,k)) {k-=3;parameter=1;setto(0," ",a);return;} //========================discuss
if (ends(a,"iti",3,k)) {k-=3;parameter=1;setto(0," ",a);return;}
case 'u': if (ends(a,"ous",3,k)) {k-=3;parameter=1;setto(0," ",a);return;}
case 'v': if (ends(a,"ive",3,k)) {k-=3;parameter=1;setto(0," ",a);return;}
case 'z': if (ends(a,"ize",3,k)) {k-=3;parameter=1;setto(0," ",a);return;}
default: return;
}
if (m(a,j,k) > 1) j=k;
}}
/* step5() removes a final -e if m() > 1, and changes -ll to -l if
m() > 1. */
void step5(char a[],int j)
{
j = k;
if (a[k] == 'e')
{ int x = m(a,j,k);
if (x > 1 ){k--;} else
if ((x == 1) && !cvc(k-1,a,0)) k--;
}
if (m(a,j,k) > 1)
{
if (doublec(a,k))
{
if (a[k] == 'l') k--;
}
}
}
/*Initialisatin and calls all 5 steps. */
int stem(char a[], int i, int j)
{
k = j; k0 = i; /* copy the parameters into statics */
if (k <= k0+1) return k;
step1ab(a,j);
step1c(a); step2(a,j);
step3(a,j);
step4(a,j);
step5(a,j);
return k;
}
int main () { /*main and stemfile function.----------------------------------------------*/
char str[50];
char str1[50];
int i=0;
char *a=new char[15];
int val;
int j;char c;
int x=0;
static char * s;
char fin_char;
string line;
int warning=0;
std::cout << "Enter the name of an existing text file to test. \nExample: abc.txt\n: ";
std::cin.get (str,256); // get c-string
cout<<"============================================================================="<<endl;
//std::cout << "Enter the name of an existing output text file: ";
//std::cin.get (str1,256);
/*if(argc !=3 )
{
cout << "Bad parameter"<<endl ;
cout << "Example:\n\t" ;
cout << argv[0]<< " "<< "<input file name> <output file name>" << endl ;
return -1;
}*/
// argv[0] --> name of the executable
// argv[1] --> name of the input file
// argv[2] --> name of the output file
//strcpy(str,argv[1]);
std::ifstream is(str); // open input file
std::ofstream ofs;
ofs.open ("testfile_output.txt", std::ofstream::out);
//std::ifstream out (argv[2]) ; // open output file
if (is.fail()) {cout<<"\n ERROR: Enter a valid existing filename. Example: abc.txt\n";cout<<"\n";system("pause");}
else
{
while (is.good()) // loop while extraction from file is possible
{ memset(a,'\0',sizeof(a));
while(is.getline(a,50,'\n'))
{ memset(temp,'\0',sizeof(temp));
memcpy(temp,a,strlen(a));
memset(a,0,sizeof(a));
strcpy(a,temp);
cout<<"Word in the file:"<<" ";
puts(a);cout<<endl;
a[strlen(a)] = '\0' ;
for (int j=0; j<strlen(a); ++j) //||a[j]=='{'||a[j]=='['||a[j]=='}'||a[j]=']'||a[j]='|'||a[j]='\'||a[j]=='/'||a[j]=='.'
{
if (a[j]=='*'||a[j]=='!'||a[j]=='@'||a[j]=='#'||a[j]=='$'||a[j]=='%'||a[j]=='^'||a[j]=='&'||a[j]=='('||a[j]==')'||a[j]=='{'||a[j]=='['||a[j]=='}'||a[j]=='.'||a[j]=='/'||a[j]=='>'||a[j]=='<'||a[j]==','||a[j]==':'||a[j]==';') if (warning==0){cout<<"\nWarning: Special character found.\nSpecial character remain unaltered.\n"; warning=1;}
a[j]=tolower(a[j]);
}
cout<<"\nWord in the file after changing case:"<<" ";
cout <<a<<endl;
//system("pause");
int iCount = -1 ;
iCount = strlen(a) ;
//j = j-1;
--iCount ;
a[stem(a,0,iCount)+1] = 0; //Calling stem function and null terminating the string.
cout<<"\n";
cout<<"Stem Word:"<<"\n";
cout<<a << endl; // write in the ouput file
cout<<"\n";
cout<<"============================================================================="<<endl;
ofs<<a;
//ofs.write(a,iCount);
ofs<<endl;
}
is.close(); // close input file
ofs.close() ; // close output file
}
system("pause");
return 0;
}
}