-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappendixE.tr
More file actions
265 lines (265 loc) · 7.25 KB
/
appendixE.tr
File metadata and controls
265 lines (265 loc) · 7.25 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
.ce 2
Приложение E
.sp 1
.
\fBФайл main.c\fP
.sp 1
.
.ps 10p
.vs 11p
.
.fam C
1 #include <stdio.h>
2 #include <locale.h>
3 #include <string.h>
4 #include <wctype.h>
5 #include <wchar.h>
6 #include <getopt.h>
7
8 #include "markov.h"
9 #include "hashtb.h"
10 #include "xstdlib.h"
11
12 #define DEF_EOS ".;!?"
13 #define DEF_IGN "'\"[]{}(),:"
14
15 enum
16 {
17 EM = 0x0100,
18 EA,
19 IM,
20 IA,
21 };
22
23 struct arguments
24 {
25 wchar_t *eos;
26 wchar_t *ign;
27 char *word;
28 } args;
29
30 typedef struct dictionary {
31 char **word;
32 int pos;
33 int len;
34 } DICT;
35
36 void argsinit(int argc, char **argv);
37 char *getword(void);
38 int isweos(wchar_t wc);
39 int iswign(wchar_t wc);
40
41 int main(int argc, char **argv)
42 {
43 int i, size;
44 int prenw = -1, numword;
45 MARK mword;
46 char *word;
47 DICT dict = {NULL, 0, 0};
48
49 setlocale(LC_CTYPE, "");
50
51 mword = minit();
52 argsinit(argc, argv);
53
54 for (i = 1; (word = getword()) != NULL; ++i) {
55 if ((numword = getnum(word)) == -1) {
56 if (dict.len == dict.pos) {
57 dict.len += 1000;
58 size = (dict.len + 1) * sizeof(char**);
59 dict.word = xrealloc(dict.word, size);
60 }
61
62 numword = dict.pos;
63 dict.word[dict.pos] = word;
64 ++dict.pos;
65 }
66
67 addentry(numword, word);
68 mcount(mword, prenw, numword);
69
70 prenw = numword;
71 }
72
73 if (dict.pos <= 1) {
74 fprintf(stderr, "File is not parsed\n");
75 exit(EXIT_FAILURE);
76 }
77
78 xrealloc(dict.word, dict.len * sizeof(char**));
79 normalize(mword);
80
81 numword = 0;
82
83 if (args.word != NULL && (numword = getnum(args.word)) == -1) {
84 fprintf(stderr, "The word \"%s\" not found\n", args.word);
85 exit(EXIT_FAILURE);
86 }
87
88 for (i = 0; i < 10; ++i) {
89 printf("%s ", dict.word[numword]);
90
91 if ((numword = getel(mword, numword)) == -1)
92 break;
93 }
94 putchar('\n');
95
96 return 0;
97 }
98
99 char *getword(void)
100 {
101 int i;
102 wchar_t wc;
103 char *word;
104 int lenword = 128;
105
106 word = xmalloc(lenword);
107
108 for (; (wc = fgetwc(stdin)) != WEOF && iswspace(wc);)
109 ;
110
111 if (isweos(wc)) {
112 i = wctomb(word, wc);
113 word[i] = '\0';
114 xrealloc(word, lenword - i - 1);
115
116 return word;
117 }
118
119 ungetwc(wc, stdin);
120
121 for (i = 0; ((wc = fgetwc(stdin)) != WEOF);) {
122 if (lenword < i + MB_CUR_MAX + 1) {
123 lenword *= 2;
124 xrealloc(word, lenword);
125 }
126
127 if (iswign(wc))
128 continue;
129 else if (iswalpha(wc) || wc == '-') {
130 wc = towlower(wc);
131 i += wctomb(&(word[i]), wc);
132 }
133 else if (wc == '\n' && word[i - 1] == '-')
134 --i;
135 else if ((iswspace(wc) || isweos(wc)) && i > 0) {
136 ungetwc(wc, stdin);
137
138 break;
139 }
140 }
141
142 xrealloc(word, i + 1);
143 word[i] = '\0';
144
145 if (i == 0)
146 return NULL;
147 else
148 return word;
149 }
150
151 int isweos(wchar_t wc)
152 {
153 int i;
154
155 for (i = 0; args.eos[i] != '\0'; ++i) {
156 if (wc == args.eos[i])
157 return ++i;
158 }
159
160 return 0;
161 }
162
163 int iswign(wchar_t wc)
164 {
165 int i;
166
167 for (i = 0; args.ign[i] != '\0'; ++i) {
168 if (wc == args.ign[i])
169 return ++i;
170 }
171
172 return 0;
173 }
174
175 void printhelp(void)
176 {
177 printf("Usage: markov [OPTION]...\n\n");
178
179 printf("OPTIONS:\n");
180 printf("--eosA EOS\t\tadd End-Of-Sentence characters\n");
181 printf("--eosM EOS\t\tmodify End-Of-Sentence characters\n");
182 printf("-h, --help\t\tgive this help list and exit\n");
183 printf("--ignA IGN\t\tadd IGNored characters\n");
184 printf("--ignM IGN\t\tmodify IGNored characters\n");
185 printf("\n");
186
187 printf("DEFAULT:\n");
188 printf("EOS: %s\n", DEF_EOS);
189 printf("IGN: %s\n", DEF_IGN);
190 }
191
192 void argsinit(int argc, char **argv)
193 {
194 int size;
195 int neos;
196 int nign;
197
198 int opt;
199 char sopt[] = "hw:";
200 const struct option lopt[] = {
201 {"help", no_argument, NULL, 'h'},
202 {"eosM", required_argument, NULL, EM },
203 {"eosA", required_argument, NULL, EA },
204 {"ignM", required_argument, NULL, IM },
205 {"ignA", required_argument, NULL, IA },
206 {NULL, 0, NULL, 0 },
207 };
208
209 size = sizeof (wchar_t);
210 args.eos = xmalloc((strlen(DEF_EOS) + 1) * size);
211 args.ign = xmalloc((strlen(DEF_IGN) + 1) * size);
212
213 neos = mbstowcs(args.eos, DEF_EOS, sizeof (DEF_EOS));
214 nign = mbstowcs(args.ign, DEF_IGN, sizeof (DEF_IGN));
215
216 for (opterr = 0; (opt = getopt_long(argc, argv, sopt, lopt, NULL)) != -1;) {
217 switch (opt) {
218 case 'h':
219 printhelp();
220 exit(EXIT_SUCCESS);
221
222 case 'w':
223 args.word = optarg;
224
225 break;
226
227 case EM:
228 neos = 0;
229 case EA:
230 args.eos = realloc(args.eos, (neos + 1025) * size);
231 neos += mbstowcs(args.eos + neos, optarg, 1024);
232 args.eos = realloc(args.eos, (neos + 1) * size);
233
234 break;
235
236 case IM:
237 nign = 0;
238 case IA:
239 args.ign = realloc(args.ign, (nign + 1025) * size);
240 nign += mbstowcs(args.ign + nign, optarg, 1024);
241 args.ign = realloc(args.ign, (nign + 1) * size);
242
243 break;
244
245 default:
246 fprintf(stderr, "markov: Unknown argument:\n");
247 exit(EXIT_FAILURE);
248 }
249 }
250 }
.sp 1
.fam T
.ps 13
.vs 16