-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRPGParser.cs
369 lines (283 loc) · 10.9 KB
/
RPGParser.cs
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System.Text;
using System.Collections;
namespace RPGParser
{
public class RPGParser
{
const string s_BRACKET = "[";
const string s_DELIM = "|";
const string s_SPACE = " ";
const char c_BRACKET = '[';
const char c_CLOSE = ']';
public string parsed_content;
public RPGParser(string s)
{
this.parsed_content = CorrectPunctuation(Parse(s));
}
private string[] TokenizeChoices(string s)
{
//print ("Tokenizing : " + s);
if (!s.Contains(s_BRACKET))
{
//print ("no brackets: " + s);
return s.Split('|');
}
ArrayList all_tokens = new ArrayList();
int closing_bracket_counter = 0;
//need to figure out the regex for this
//Regex pipeNotInBrackets = new Regex("*|*");
StringBuilder token = new StringBuilder();
foreach (char c in s)
{
//collecting all characters while
//waiting for closing bracket
//---------------------- NESTED -----------------------
if (closing_bracket_counter > 0)
{
//nested brackets.
if (c == c_BRACKET)
{
closing_bracket_counter++;
token.Append(c);//also save the character in the token
continue;
}
//closing brack and not nest. Save token
if (c == ']' && closing_bracket_counter == 1)
{
//closing bracket has been found
//and not in a nested bracket
//set this counter back to zero.
closing_bracket_counter--;
if (token.Length > 0)
{
token.Append(c);//------------------------------- infinite loop?
all_tokens.Add(token.ToString());
token = new StringBuilder();//reset StringBuilder for next token
continue;
}
}
//seeing a closing bracket while in a nested situation
else if (c == ']')
{
//decriment the counter for next iteration
closing_bracket_counter--;
token.Append(c);//also save the character in the token
continue;
}
else
{
//normal character.
token.Append(c);
continue;
}
}
//------------------ NESTED ----------------------------
//---------- NON NESTED; NO BRACKETS SEEN YET ----------
if (c != ' ' && c != '[' && c != ']' && c != '|')
{
token.Append(c);
}
else if (c == '[')
{
//we will now continue within brackets
closing_bracket_counter = 1;
token.Append(c);//------------------------------- infinite loop?
}
else if (c == '|')
{
//if seeing |
//and the token has content
//then we hit an optional segment so append.
if (token.Length > 0)
{
all_tokens.Add(token.ToString());
token = new StringBuilder();
}
//but if the content in the token is empty,
//just continue because this is a |
//appearing after a bracket
continue;
}
else
{
//token is completed; add it to the main list
if (token.Length > 0)
{
all_tokens.Add(token.ToString());
token = new StringBuilder();
}
}
}
//if token has content left in it then we need to add
//it as a valid tokey. in situations when you get
//to the end of the string it was getting left off.
if (token.Length > 0)
{
all_tokens.Add(token.ToString());
token = new StringBuilder();
}
string[] found_tokens = new string[all_tokens.Count];
for (int i = 0; i < all_tokens.Count; i++)
{
found_tokens[i] = all_tokens[i].ToString();
}
return found_tokens;
}
public static string ParseString(string s)
{
return (new RPGParser(s).parsed_content);
}
private string Parse(string s)
{
string[] tokens = TokenizeChoices(s);
StringBuilder final_result = new StringBuilder();
foreach (string token in tokens)
{
if (token.StartsWith(s_BRACKET))
{
if (token.Length - 2 < 0) return "bad token. check brackets";
string without_brckets = token.Substring(1, token.Length - 2);
string pick;
string[] special_tokens = SplitAvoidingBrackets(without_brckets);
pick = PickRandomly(special_tokens);
if (pick.Contains(s_BRACKET))
{
pick = Parse(pick);
}
final_result.Append(pick);
if (!(final_result[final_result.Length - 1] == ' '))
final_result.Append(s_SPACE);
}
else
{
final_result.Append(token);
if (!(final_result[final_result.Length - 1] == ' '))
final_result.Append(s_SPACE);
}
}
return final_result.ToString();
}
private string[] SplitAvoidingBrackets(string s)
{
string[] picks;
if (!s.Contains(s_BRACKET))
{
picks = s.Split('|');
return picks;
}
ArrayList all_tokens = new ArrayList();
StringBuilder token = new StringBuilder();
int closing_bracket_counter = 0;
for (int i = 0; i < s.Length; i++)
{
//not in brackets mode
if (closing_bracket_counter == 0)
{
if (s[i] != '[' && s[i] != '|')
{
token.Append(s[i]);
}
else if (s[i] == '|')
{
if (token.Length > 0)
all_tokens.Add(token);
token = new StringBuilder();
}
else if (s[i] == '[')
{
closing_bracket_counter++;
if (token.Length > 0)
{
//all_tokens.Add(token); //lemon test
//don't start a new token,
//but make sure there is a space
token.Append(' ');
}
//token = new StringBuilder();
token.Append('[');
}
}//end if for not inside brackets
//in brackets now
else
{
if (s[i] == '[')
{
//in a nested bracket
closing_bracket_counter++;
token.Append(s[i]);
}
else if (s[i] == ']')
{
closing_bracket_counter--;
token.Append(s[i]);
if (closing_bracket_counter == 0)
{
//bracket is completely closed. no nests.
//all_tokens.Add(token); ------------------------------ 11-21-16 testing BUG
//token = new StringBuilder();------------------------------ 11-21-16
//attempting to parse inner brackets immediate
//and then keep building token
string inner_result = Parse(token.ToString());
token = new StringBuilder();
token.Append(inner_result);
//-----------
}
}
else
{
//an ignored or regular character
token.Append(s[i]);
}
}
}//end for loop
//if we've falled out of the loop and anything was in the token,
//add it to the list now
if (token.Length > 0)
all_tokens.Add(token);
string[] found_tokens = new string[all_tokens.Count];
for (int i = 0; i < all_tokens.Count; i++)
{
found_tokens[i] = all_tokens[i].ToString();
}
return found_tokens;
}
private string PickRandomly(string s)
{
string[] picks = s.Split('|');
System.Random rand = new System.Random();
return picks[rand.Next(0, picks.Length)];
}
private string PickRandomly(string[] ss)
{
System.Random rand = new System.Random();
return ss[rand.Next(0, ss.Length)];
}
private string CorrectPunctuation(string s)
{
StringBuilder corrected_string = new StringBuilder();
char c, c_next;
for (int i = 0; i < s.Length; i++)
{
c = s[i];
if (i + 1 <= s.Length - 1)
c_next = s[i + 1];
else
c_next = ' ';
if (c == ' ')
{
if (c_next == '.' || c_next == ',' || c_next == '?' || c_next == '!' || c_next == '"')
{
corrected_string.Append(c_next);
i++;
}
else corrected_string.Append(c);
}
else
{
corrected_string.Append(c);
}
}
return corrected_string.ToString();
}
}
}