|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <getopt.h> |
|
|
|
#define PACKAGE "wgram" |
|
#define VERSION "0.0.4" |
|
#define MAXLINE 1024 |
|
#define MAXGRAM 32 |
|
|
|
|
|
void print_help(int exval); |
|
|
|
int main (int argc, char *argv[]) { |
|
|
|
char delim[] = ".,:;`/\"+-_(){}[]<>*&^%$#@!?~/|\\=1234567890 \t\n"; |
|
char line[MAXLINE]; |
|
char *stray = NULL; |
|
char **strarray = NULL; |
|
int i = 0; |
|
int strcount = 0; |
|
int N = 3, pos = 0; |
|
int opt = 0; |
|
int word_flag = 0; |
|
FILE *fp = stdin; |
|
|
|
while((opt = getopt(argc, argv, "hvn:wf:")) != -1) { |
|
switch(opt) { |
|
case 'h': |
|
print_help(0); |
|
break; |
|
case 'v': |
|
exit(0); |
|
break; |
|
case 'n': |
|
N = atoi(optarg); |
|
if(N > MAXGRAM || N < 2) { |
|
fprintf(stderr, "%s: Error - Ngram length `%d' out of range `0-%d'\n", |
|
PACKAGE, N, MAXGRAM); |
|
return 1; |
|
} |
|
break; |
|
case 'w': |
|
word_flag = 1; |
|
break; |
|
case 'f': |
|
if(freopen(optarg, "r", fp) == NULL) { |
|
fprintf(stderr, "%s: Error - opening `%s'\n", PACKAGE, optarg); |
|
return 1; |
|
} |
|
break; |
|
case '?': |
|
fprintf(stderr, "%s: Error - No such option: `%c'\n\n", PACKAGE, optopt); |
|
print_help(1); |
|
} |
|
} |
|
|
|
|
|
while((fgets(line, MAXLINE, fp)) != NULL) { |
|
if(strlen(line) < 2) |
|
continue; |
|
|
|
stray = strtok(line, delim); |
|
while(stray != NULL) { |
|
strarray = (char **)realloc(strarray, (strcount + 1) * sizeof(char *)); |
|
strarray[strcount++] = strdup(stray); |
|
stray = strtok(NULL, delim); |
|
} |
|
} |
|
|
|
if(word_flag == 0) { |
|
|
|
|
|
|
|
|
|
for(i = 0, pos = N; i < strcount; i++, pos--) { |
|
if(pos == 0) pos = N, i -= (N - 1), printf("\n"); |
|
printf("%s ", strarray[i]); |
|
} |
|
printf("\n"); |
|
} else { |
|
|
|
for(i = 0; i < strcount; i++) |
|
printf("%s\n", strarray[i]); |
|
} |
|
|
|
|
|
for(i = 0; i < strcount; i++) |
|
free(strarray[i]); |
|
|
|
free(strarray); |
|
return 0; |
|
} |
|
|
|
|
|
void print_help(int exval) { |
|
printf("%s,%s extract N-grams from text data\n", PACKAGE, VERSION); |
|
printf("Usage: %s [-h] [-v] [-n INT] [-w] [-f FILE]\n\n", PACKAGE); |
|
|
|
printf(" -h print this help and exit\n"); |
|
printf(" -v print version and exit\n\n"); |
|
|
|
printf(" -n INT set ngram length (default=3)\n"); |
|
printf(" -w print only the extracted words\n"); |
|
printf(" -f FILE read input from `FILE' (default=stdin)\n\n"); |
|
exit(exval); |
|
} |
|
|