1
/* This is a simple program which uses libstemmer to provide a command
2
* line interface for stemming using any of the algorithms provided.
6
#include <stdlib.h> /* for malloc, free */
7
#include <string.h> /* for memmove */
8
#include <ctype.h> /* for isupper, tolower */
10
#include "libstemmer.h"
12
const char * progname;
13
static int pretty = 1;
16
stem_file(struct sb_stemmer * stemmer, FILE * f_in, FILE * f_out)
20
sb_symbol * b = (sb_symbol *) malloc(lim * sizeof(sb_symbol));
30
if (ch == '\n' || ch == EOF) break;
34
realloc(b, (lim + INC) * sizeof(sb_symbol));
35
if (newb == 0) goto error;
39
/* force lower case: */
40
if isupper(ch) ch = tolower(ch);
49
for (j = 0; j < i; j++) fprintf(f_out, "%c", b[j]);
50
fprintf(f_out, "%s", " -> ");
53
const sb_symbol * stemmed = sb_stemmer_stem(stemmer, b, i);
55
/*for (j = 0; j < z->l; j++) */
56
for (j = 0; stemmed[j] != 0; j++)
57
fprintf(f_out, "%c", stemmed[j]);
67
/** Display the command line syntax, and then exit.
68
* @param n The value to exit with.
73
printf("usage: %s [-i <input file>] [-o <output file>] [-p] [-h]\n"
75
"The input file consists of a list of words to be stemmed, one per\n"
76
"line. Words should be in lower case, but (for English) A-Z letters\n"
77
"are mapped to their a-z equivalents anyway. If omitted, stdin is\n"
80
"If -p is given the output file consists of each word of the input\n"
81
"file followed by \"->\" followed by its stemmed equivalent.\n"
82
"Otherwise, the output file consists of the stemmed words, one per\n"
85
"-h displays this help\n",
91
main(int argc, char * argv[])
97
struct sb_stemmer * stemmer;
108
if (strcmp(s, "-o") == 0) {
110
fprintf(stderr, "%s requires an argument\n", s);
114
} else if (strcmp(s, "-i") == 0) {
116
fprintf(stderr, "%s requires an argument\n", s);
120
} else if (strcmp(s, "-p") == 0) {
122
} else if (strcmp(s, "-h") == 0) {
125
fprintf(stderr, "option %s unknown\n", s);
129
fprintf(stderr, "unexpected parameter %s\n", s);
134
/* prepare the files */
135
f_in = (in == 0) ? stdin : fopen(in, "r");
137
fprintf(stderr, "file %s not found\n", in);
140
f_out = (out == 0) ? stdout : fopen(out, "w");
142
fprintf(stderr, "file %s cannot be opened\n", out);
146
/* do the stemming process: */
147
stemmer = sb_stemmer_create("english");
149
fprintf(stderr, "language `%s' not available for stemming\n");
152
stem_file(stemmer, f_in, f_out);
153
sb_stemmer_release(stemmer);
155
if (in != 0) (void) fclose(f_in);
156
if (out != 0) (void) fclose(f_out);