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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "titanic/true_talk/tt_parser.h"
#include "titanic/true_talk/script_handler.h"
#include "titanic/true_talk/tt_sentence.h"
#include "titanic/titanic.h"
namespace Titanic {
TTparser::TTparser(CScriptHandler *owner) : _owner(owner), _sentenceSub(nullptr),
_sentence(nullptr), _fieldC(0), _field10(0), _field14(0), _field18(0) {
loadArrays();
}
void TTparser::loadArrays() {
Common::SeekableReadStream *r;
r = g_vm->_filesManager->getResource("TEXT/REPLACEMENTS1");
while (r->pos() < r->size())
_replacements1.push_back(readStringFromStream(r));
delete r;
r = g_vm->_filesManager->getResource("TEXT/REPLACEMENTS2");
while (r->pos() < r->size())
_replacements2.push_back(readStringFromStream(r));
delete r;
r = g_vm->_filesManager->getResource("TEXT/REPLACEMENTS3");
while (r->pos() < r->size())
_replacements3.push_back(readStringFromStream(r));
delete r;
r = g_vm->_filesManager->getResource("TEXT/PHRASES");
while (r->pos() < r->size())
_phrases.push_back(readStringFromStream(r));
delete r;
r = g_vm->_filesManager->getResource("TEXT/NUMBERS");
while (r->pos() < r->size()) {
NumberEntry ne;
ne._text = readStringFromStream(r);
ne._value = r->readSint32LE();
ne._flags = r->readUint32LE();
_numbers.push_back(ne);
}
delete r;
}
int TTparser::preprocess(TTsentence *sentence) {
_sentence = sentence;
if (normalize(sentence))
return 0;
// Scan for and replace common slang and contractions with verbose versions
searchAndReplace(sentence->_normalizedLine, _replacements1);
searchAndReplace(sentence->_normalizedLine, _replacements2);
// Check entire normalized line against common phrases to replace
for (uint idx = 0; idx < _phrases.size(); idx += 2) {
if (!_phrases[idx].compareTo(sentence->_normalizedLine))
sentence->_normalizedLine = _phrases[idx + 1];
}
// Do a further search and replace of roman numerals to decimal
searchAndReplace(sentence->_normalizedLine, _replacements3);
// Replace any roman numerals, spelled out words, etc. with decimal numbers
CTrueTalkManager::_v1 = -1000;
int idx = 0;
do {
idx = replaceNumbers(sentence->_normalizedLine, idx);
} while (idx >= 0);
if (CTrueTalkManager::_v1 == -1000 && !sentence->_normalizedLine.empty()) {
// Scan the text for any numeric digits
for (const char *strP = sentence->_normalizedLine.c_str(); *strP; ++strP) {
if (Common::isDigit(*strP)) {
// Found digit, so convert it and any following ones
CTrueTalkManager::_v1 = atoi(strP);
break;
}
}
}
return 0;
}
int TTparser::normalize(TTsentence *sentence) {
TTstring *destLine = new TTstring();
const TTstring &srcLine = sentence->_initialLine;
int srcSize = srcLine.size();
int savedIndex = 0;
int counter1 = 0;
int commandVal;
for (int index = 0; index < srcSize; ++index) {
char c = srcLine[index];
if (Common::isLower(c)) {
(*destLine) += c;
} else if (Common::isSpace(c)) {
if (!destLine->empty() && destLine->lastChar() != ' ')
(*destLine) += ' ';
} else if (Common::isUpper(c)) {
(*destLine) += toupper(c);
} else if (Common::isDigit(c)) {
if (c == '0' && isEmoticon(srcLine, index)) {
sentence->set38(10);
} else {
// Iterate through all the digits of the number
(*destLine) += c;
while (Common::isDigit(srcLine[index + 1]))
(*destLine) += srcLine[++index];
}
} else if (Common::isPunct(c)) {
bool flag = false;
switch (c) {
case '!':
sentence->set38(3);
break;
case '\'':
if (!normalizeContraction(srcLine, index, *destLine))
flag = true;
break;
case '.':
sentence->set38(1);
break;
case ':':
commandVal = isEmoticon(srcLine, index);
if (commandVal) {
sentence->set38(commandVal);
index += 2;
} else {
flag = true;
}
break;
case ';':
commandVal = isEmoticon(srcLine, index);
if (commandVal == 6) {
sentence->set38(7);
index += 2;
} else if (commandVal != 0) {
sentence->set38(commandVal);
index += 2;
}
break;
case '<':
++index;
commandVal = isEmoticon(srcLine, index);
if (commandVal == 6) {
sentence->set38(12);
} else {
--index;
flag = true;
}
break;
case '>':
++index;
commandVal = isEmoticon(srcLine, index);
if (commandVal == 6 || commandVal == 9) {
sentence->set38(11);
} else {
--index;
flag = true;
}
break;
case '?':
sentence->set38(2);
break;
default:
flag = true;
break;
}
if (flag && (!savedIndex || (index - savedIndex) == 1))
++counter1;
savedIndex = index;
}
}
if (counter1 >= 4)
sentence->set38(4);
// Remove any trailing spaces
while (destLine->hasSuffix(" "))
destLine->deleteLastChar();
// Copy out the normalized line
sentence->_normalizedLine = *destLine;
delete destLine;
return 0;
}
int TTparser::isEmoticon(const TTstring &str, int &index) {
if (str[index] != ':' && str[index] != ';')
return 0;
if (str[index + 1] != '-')
return 0;
index += 2;
switch (str[index]) {
case '(':
case '<':
return 8;
case ')':
case '>':
return 6;
case 'P':
case 'p':
return 9;
default:
return 5;
}
}
bool TTparser::normalizeContraction(const TTstring &srcLine, int srcIndex, TTstring &destLine) {
int startIndex = srcIndex + 1;
switch (srcLine[startIndex]) {
case 'd':
srcIndex += 2;
if (srcLine.compareAt(srcIndex, " a ") || srcLine.compareAt(srcIndex, " the ")) {
destLine += " had";
} else {
destLine += " would";
}
srcIndex = startIndex;
break;
case 'l':
if (srcLine[srcIndex + 2] == 'l') {
// 'll ending
destLine += " will";
srcIndex = startIndex;
}
break;
case 'm':
// 'm ending
destLine += " am";
srcIndex = startIndex;
break;
case 'r':
// 're ending
if (srcLine[srcIndex + 2] == 'e') {
destLine += " are";
srcIndex = startIndex;
}
break;
case 's':
destLine += "s*";
srcIndex = startIndex;
break;
case 't':
if (srcLine[srcIndex - 1] == 'n' && srcIndex >= 3) {
if (srcLine[srcIndex - 3] == 'c' && srcLine[srcIndex - 2] == 'a' &&
(srcIndex == 3 || srcLine[srcIndex - 4])) {
// can't -> can not
destLine += 'n';
} else if (srcLine[srcIndex - 3] == 'w' && srcLine[srcIndex - 2] == 'o' &&
(srcIndex == 3 || srcLine[srcIndex - 4])) {
// won't -> will not
destLine.deleteLastChar();
destLine.deleteLastChar();
destLine += "ill";
} else if (srcLine[srcIndex - 3] == 'a' && srcLine[srcIndex - 2] == 'i' &&
(srcIndex == 3 || srcLine[srcIndex - 4])) {
// ain't -> am not
destLine.deleteLastChar();
destLine.deleteLastChar();
destLine += "m";
} else if (srcLine.hasSuffix(" sha") ||
(srcIndex == 4 && srcLine.hasSuffix("sha"))) {
// shan't -> shall not
destLine.deleteLastChar();
destLine += "ll";
}
destLine += " not";
}
break;
case 'v':
// 've ending
if (srcLine[startIndex + 2] == 'e') {
destLine += " have";
srcIndex = startIndex;
}
break;
default:
break;
}
return false;
}
void TTparser::searchAndReplace(TTstring &line, const StringArray &strings) {
int charIndex = 0;
while (charIndex >= 0)
charIndex = searchAndReplace(line, charIndex, strings);
}
int TTparser::searchAndReplace(TTstring &line, int startIndex, const StringArray &strings) {
int lineSize = line.size();
if (startIndex >= lineSize)
return -1;
for (uint idx = 0; idx < strings.size(); idx += 2) {
const CString &origStr = strings[idx];
const CString &replacementStr = strings[idx + 1];
if (!strncmp(line.c_str() + startIndex, origStr.c_str(), strings[idx].size())) {
// Ensure that that a space follows the match, or the end of string,
// so the end of the string doesn't match on parts of larger words
char c = line[startIndex + strings[idx].size()];
if (c == ' ' || c == '\0') {
// Replace the text in the line with it's replacement
line = CString(line.c_str(), line.c_str() + startIndex) + replacementStr +
CString(line.c_str() + startIndex + origStr.size());
startIndex += replacementStr.size();
break;
}
}
}
// Skip to the end of the current word
while (startIndex < lineSize && line[startIndex] != ' ')
++startIndex;
if (startIndex == lineSize)
return -1;
// ..and all spaces following it until the start of the next word
while (startIndex < lineSize && line[startIndex] == ' ')
++startIndex;
if (startIndex == lineSize)
return -1;
// Return index of the start of the next word
return startIndex;
}
int TTparser::replaceNumbers(TTstring &line, int startIndex) {
int index = startIndex;
const NumberEntry *numEntry = replaceNumbers2(line, &index);
if (!numEntry || !(numEntry->_flags & NF_2))
return index;
bool flag1 = false, flag2 = false, flag3 = false;
int total = 0, factor = 0;
do {
if (numEntry->_flags & NF_1) {
flag2 = true;
if (numEntry->_flags & NF_8)
flag1 = true;
if (numEntry->_flags & NF_4) {
flag3 = true;
factor *= numEntry->_value;
}
if (numEntry->_flags & NF_2) {
if (flag3) {
total += factor;
factor = 0;
}
factor += numEntry->_value;
}
}
} while (replaceNumbers2(line, &index));
if (!flag2)
return index;
if (index >= 0) {
if (line[index - 1] != ' ')
return index;
}
total += factor;
CTrueTalkManager::_v1 = total;
if (flag1)
total = -total;
CString numStr = CString::format("%d", total);
line = CString(line.c_str(), line.c_str() + startIndex) + numStr +
CString(line.c_str() + index);
return index;
}
const NumberEntry *TTparser::replaceNumbers2(TTstring &line, int *startIndex) {
int lineSize = line.size();
int index = *startIndex;
if (index < 0 || index >= lineSize) {
*startIndex = -1;
return nullptr;
}
NumberEntry *numEntry = nullptr;
for (uint idx = 0; idx < _numbers.size(); ++idx) {
NumberEntry &ne = _numbers[idx];
if (!strncmp(line.c_str() + index, ne._text.c_str(), ne._text.size())) {
if ((ne._flags & NF_10) || (index + (int)ne._text.size()) >= lineSize ||
line[index + ne._text.size()] == ' ') {
*startIndex += ne._text.size();
numEntry = ≠
break;
}
}
}
if (!numEntry || !(numEntry->_flags & NF_10)) {
// Skip to end of current word
while (*startIndex < lineSize && !Common::isSpace(line[*startIndex]))
++*startIndex;
}
// Skip over following spaces until start of following word is reached
while (*startIndex < lineSize && Common::isSpace(line[*startIndex]))
++*startIndex;
if (*startIndex >= lineSize)
*startIndex = -1;
return numEntry;
}
int TTparser::findFrames(TTsentence *sentence) {
static bool flag;
_sentenceSub = &sentence->_sub;
_sentence = sentence;
TTstring *line = sentence->_normalizedLine.copy();
TTstring wordString;
for (;;) {
// Keep stripping words off the start of the passed input
wordString = line->tokenize(" \n");
if (wordString.empty())
break;
TTword *srcWord = nullptr;
TTword *word = _owner->_vocab->getWord(wordString, &word);
sentence->storeVocabHit(srcWord);
if (word) {
// TODO
} else {
}
}
// TODO
delete line;
return 0;
}
} // End of namespace Titanic
|