-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmr-cli.c
186 lines (140 loc) · 3.66 KB
/
stmr-cli.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "stmr.h"
/* Utility to detect if `character` is a letter. */
#define IS_LETTER(character) (isupper(character) || islower(character))
/* Pointer to memory allocation. */
static char *value;
/* Initial value for `indexMax`. */
#define BASE_INDEX 50
/* Maximum offset in `value`. */
static int indexMax = BASE_INDEX;
/* Increase memory. */
static void
increaseValue() {
char *newValue;
int index;
indexMax *= 2;
newValue = (char *) malloc(indexMax + 1);
index = -1;
while (++index < indexMax) {
newValue[index] = value[index];
}
free(value);
value = newValue;
}
/* Tokenise and stem a file */
static void
stemFile(FILE *file) {
int character;
int index;
while (TRUE) {
character = getc(file);
if (character == EOF) {
return;
}
if (IS_LETTER(character)) {
index = 0;
while (TRUE) {
if (index == indexMax) {
increaseValue();
}
character = tolower(character);
value[index] = character;
index++;
character = getc(file);
if (!IS_LETTER(character)) {
ungetc(character, file);
break;
}
}
value[stem(value, 0, index - 1) + 1] = 0;
/* The previous line calls the stemmer and
* uses its result to zero-terminate the
* string in `value`. */
printf("%s", value);
} else {
putchar(character);
}
}
}
/**
* Evaluate a word.
*/
static void
evaluate(const char *input) {
value = strdup(input);
value[stem(value, 0, strlen(value) - 1) + 1] = 0;
printf("%s\n", value);
}
/* CLI. */
int
main(int argc, char **argv) {
char *arg;
FILE *input;
int index;
arg = argv[1];
if (argc == 2) {
if (!strcmp(arg, "-v") || !strcmp(arg, "--version")) {
printf("%s", "0.1.0\n");
return EXIT_SUCCESS;
}
if (!strcmp(arg, "-h") || !strcmp(arg, "--help")) {
printf("%s", "\n");
printf("%s", " Usage: stmr [options] file\n");
printf("%s", "\n");
printf("%s", " Options:\n");
printf("%s", "\n");
printf("%s", " -h, --help output usage information\n");
printf("%s", " -v, --version output version number\n");
printf("%s", " -e, --eval string output stemmed word\n");
printf("%s", "\n");
printf("%s", " Usage:\n");
printf("%s", "\n");
printf("%s", " # stem a word\n");
printf("%s", " $ stmr -e nationalism\n");
printf("%s", " # nation\n");
printf("%s", "\n");
printf("%s", " # print stems\n");
printf("%s", " $ stmr in.txt\n");
printf("%s", "\n");
printf("%s", " # write stems to out.txt\n");
printf("%s", " $ stmr in.txt > out.txt\n");
printf("%s", "\n");
printf("%s", " # stdin and stdout\n");
printf("%s", " $ echo \"Internationalise\" | stmr\n");
printf("%s", " # internationalis\n");
printf("%s", "\n");
return EXIT_SUCCESS;
}
}
if (argc > 1 && (!strcmp(arg, "-e") || !strcmp(arg, "--eval"))) {
arg = argv[2];
if (arg == NULL) {
fprintf(stderr, "stmr: -e requires an argument\n");
exit(EXIT_FAILURE);
}
evaluate(arg);
return EXIT_SUCCESS;
}
value = (char *) malloc(indexMax + 1);
if (argc < 2) {
stemFile(stdin);
} else {
index = 0;
while (++index < argc) {
arg = argv[index];
input = fopen(arg, "r");
if (NULL == input) {
fprintf(stderr, "Unable to open '%s': %s\n", arg, strerror(errno));
exit(EXIT_FAILURE);
}
stemFile(input);
}
}
free(value);
return EXIT_SUCCESS;
}