fst-mor.C
3.74 KB
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
/*******************************************************************/
/* */
/* FILE fst-mor.C */
/* MODULE fst-mor */
/* PROGRAM SFST */
/* AUTHOR Helmut Schmid, IMS, University of Stuttgart */
/* */
/*******************************************************************/
#include "fst.h"
using std::cerr;
using std::cout;
#ifdef READLINE
#include <readline/readline.h>
#include <readline/history.h>
#else
char *readline( const char *prompt ) {
static char buffer[10000];
printf("%s", prompt);
if ((fgets(buffer,9999,stdin)) == NULL)
return NULL;
size_t l = strlen(buffer);
if (buffer[l-1] == '\n')
buffer[l-1] = 0;
return buffer;
}
#endif
bool WithBrackets=true;
/*******************************************************************/
/* */
/* usage */
/* */
/*******************************************************************/
void usage()
{
cerr << "\nUsage: fst-mor [options] file [file [file]]\n\n";
cerr << "Options:\n";
cerr << "-n: print multi-character symbols without enclosing angle brackets\n";
cerr << "-h: print this message\n";
exit(1);
}
/*******************************************************************/
/* */
/* get_flags */
/* */
/*******************************************************************/
void get_flags( int *argc, char **argv )
{
for( int i=1; i<*argc; i++ ) {
if (strcmp(argv[i],"-h") == 0) {
usage();
argv[i] = NULL;
}
else if (strcmp(argv[i],"-n") == 0) {
WithBrackets = false;
argv[i] = NULL;
}
}
// remove flags from the argument list
int k;
for( int i=k=1; i<*argc; i++)
if (argv[i] != NULL)
argv[k++] = argv[i];
*argc = k;
}
/*******************************************************************/
/* */
/* main */
/* */
/*******************************************************************/
int main( int argc, char **argv )
{
FILE *file;
get_flags(&argc, argv);
if (argc < 2)
usage();
if (argc < 2)
usage();
if ((file = fopen(argv[1],"rb")) == NULL) {
fprintf(stderr,"\nError: Cannot open fst file %s\n\n", argv[1]);
exit(1);
}
cout << "reading transducer...\n";
try {
Transducer a(file);
fclose(file);
cout << "finished.\n";
int analyze=1;
for(;;) {
const char *prompt=(analyze)? "analyze> ": "generate> ";
char *input_string=readline(prompt);
if (input_string == NULL || strcmp(input_string,"q") == 0)
break;
#ifdef READLINE
add_history(input_string);
#endif
if (strcmp(input_string,"") == 0)
analyze = !analyze;
else if (analyze) {
if (!a.analyze_string(input_string, stdout, WithBrackets))
printf( "no result for %s\n", input_string);
}
else {
if (!a.generate_string(input_string, stdout, WithBrackets))
printf( "no result for %s\n", input_string);
}
#ifdef READLINE
free(input_string);
#endif
}
}
catch(const char* p) {
cerr << p << "\n";
return 1;
}
return 0;
}