~snowball-yiddish-dev/snowball-yiddish/trunk

« back to all changes in this revision

Viewing changes to snowball/examples/stemwords.c

  • Committer: richard
  • Date: 2003-03-30 12:08:09 UTC
  • Revision ID: svn-v4:633ccae0-01f4-0310-8c99-d3591da6f01f:trunk:216
This module will contain only the code and build system, and documentation
for building and running the stemming library.
All sample data will be in a separate module, and the website will be in
its own module too.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This is a simple program which uses libstemmer to provide a command
 
2
 * line interface for stemming using any of the algorithms provided.
 
3
 */
 
4
 
 
5
#include <stdio.h>
 
6
#include <stdlib.h> /* for malloc, free */
 
7
#include <string.h> /* for memmove */
 
8
#include <ctype.h>  /* for isupper, tolower */
 
9
 
 
10
#include "libstemmer.h"
 
11
 
 
12
const char * progname;
 
13
static int pretty = 1;
 
14
 
 
15
static void
 
16
stem_file(struct sb_stemmer * stemmer, FILE * f_in, FILE * f_out)
 
17
{
 
18
#define INC 10
 
19
    int lim = INC;
 
20
    sb_symbol * b = (sb_symbol *) malloc(lim * sizeof(sb_symbol));
 
21
 
 
22
    while(1) {
 
23
        int ch = getc(f_in);
 
24
        if (ch == EOF) {
 
25
            free(b); return;
 
26
        }
 
27
        {
 
28
            int i = 0;
 
29
            while(1) {
 
30
                if (ch == '\n' || ch == EOF) break;
 
31
                if (i == lim) {
 
32
                    sb_symbol * newb;
 
33
                    newb = (sb_symbol *)
 
34
                            realloc(b, (lim + INC) * sizeof(sb_symbol));
 
35
                    if (newb == 0) goto error;
 
36
                    b = newb;
 
37
                    lim = lim + INC;
 
38
                }
 
39
                /* force lower case: */
 
40
                if isupper(ch) ch = tolower(ch);
 
41
 
 
42
                b[i] = ch;
 
43
                i++;
 
44
                ch = getc(f_in);
 
45
            }
 
46
 
 
47
            if (pretty) {
 
48
                int j;
 
49
                for (j = 0; j < i; j++) fprintf(f_out, "%c", b[j]);
 
50
                fprintf(f_out, "%s", " -> ");
 
51
            }
 
52
            {
 
53
                const sb_symbol * stemmed = sb_stemmer_stem(stemmer, b, i);
 
54
                int j;
 
55
                /*for (j = 0; j < z->l; j++) */
 
56
                for (j = 0; stemmed[j] != 0; j++)
 
57
                    fprintf(f_out, "%c", stemmed[j]);
 
58
                fprintf(f_out, "\n");
 
59
            }
 
60
        }
 
61
    }
 
62
error:
 
63
    if (b != 0) free(b);
 
64
    return;
 
65
}
 
66
 
 
67
/** Display the command line syntax, and then exit.
 
68
 *  @param n The value to exit with.
 
69
 */
 
70
static void
 
71
usage(int n)
 
72
{
 
73
    printf("usage: %s [-i <input file>] [-o <output file>] [-p] [-h]\n"
 
74
          "\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"
 
78
          "used.\n"
 
79
          "\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"
 
83
          "line.\n"
 
84
          "\n"
 
85
          "-h displays this help\n",
 
86
          progname);
 
87
    exit(n);
 
88
}
 
89
 
 
90
int
 
91
main(int argc, char * argv[])
 
92
{
 
93
    char * in = 0;
 
94
    char * out = 0;
 
95
    FILE * f_in;
 
96
    FILE * f_out;
 
97
    struct sb_stemmer * stemmer;
 
98
 
 
99
    char * s;
 
100
    int i = 1;
 
101
    pretty = 0;
 
102
 
 
103
    progname = argv[0];
 
104
 
 
105
    while(i < argc) {
 
106
        s = argv[i++];
 
107
        if (s[0] == '-') {
 
108
            if (strcmp(s, "-o") == 0) {
 
109
                if (i >= argc) {
 
110
                    fprintf(stderr, "%s requires an argument\n", s);
 
111
                    exit(1);
 
112
                }
 
113
                out = argv[i++];
 
114
            } else if (strcmp(s, "-i") == 0) {
 
115
                if (i >= argc) {
 
116
                    fprintf(stderr, "%s requires an argument\n", s);
 
117
                    exit(1);
 
118
                }
 
119
                in = argv[i++];
 
120
            } else if (strcmp(s, "-p") == 0) {
 
121
                pretty = 1;
 
122
            } else if (strcmp(s, "-h") == 0) {
 
123
                usage(0);
 
124
            } else {
 
125
                fprintf(stderr, "option %s unknown\n", s);
 
126
                usage(1);
 
127
            }
 
128
        } else {
 
129
            fprintf(stderr, "unexpected parameter %s\n", s);
 
130
            usage(1);
 
131
        }
 
132
    }
 
133
 
 
134
    /* prepare the files */
 
135
    f_in = (in == 0) ? stdin : fopen(in, "r");
 
136
    if (f_in == 0) {
 
137
        fprintf(stderr, "file %s not found\n", in);
 
138
        exit(1);
 
139
    }
 
140
    f_out = (out == 0) ? stdout : fopen(out, "w");
 
141
    if (f_out == 0) {
 
142
        fprintf(stderr, "file %s cannot be opened\n", out);
 
143
        exit(1);
 
144
    }
 
145
 
 
146
    /* do the stemming process: */
 
147
    stemmer = sb_stemmer_create("english");
 
148
    if (stemmer == 0) {
 
149
        fprintf(stderr, "language `%s' not available for stemming\n");
 
150
        exit(1);
 
151
    }
 
152
    stem_file(stemmer, f_in, f_out);
 
153
    sb_stemmer_release(stemmer);
 
154
 
 
155
    if (in != 0) (void) fclose(f_in);
 
156
    if (out != 0) (void) fclose(f_out);
 
157
 
 
158
    return 0;
 
159
}
 
160