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
|
"use strict";
/**
* Parsing toki pona texts into sentences and sentence parts, and then into structured sentences that reflect the
* structure of sitelen sitelen blocks.
*
* @type {{parse}}
*/
var sitelenParser = function () {
'use strict';
/**
* Core parser into sitelen sitelen structure.
* @param parseable a sentence to parse
* @returns {*[]} a structured sentence array
*/
function getSimpleStructuredSentence(parseable) {
var tokens = parseable.split(' '),
prepositions = ['tawa', 'tan', 'lon', 'kepeken', 'sama', 'poka'],
objectMarker = ['li', 'e'],
part = {part: 'subject', tokens: []},
sentence = [part];
tokens.forEach(function (token, index) {
if (objectMarker.indexOf(token) > -1 &&
index < tokens.length - 1) {
sentence.push({part: 'objectMarker', sep: token, tokens: []});
part = sentence[sentence.length - 1];
return;
} else if (prepositions.indexOf(token) > -1 && objectMarker.indexOf(tokens[index - 1]) === -1 &&
index < tokens.length - 1 && objectMarker.indexOf(tokens[index + 1]) === -1) {
sentence.push({part: 'prepPhrase', sep: token, tokens: []});
part = sentence[sentence.length - 1];
return;
} else if (token === 'o' && part.tokens.length > 0) {
// the o token should be in a container when it is used to address something, not in commands
part.part = 'address';
part.sep = 'o';
sentence.push({part: 'subject', tokens: []});
part = sentence[sentence.length - 1];
return;
} else if (token === 'a' && part.tokens.length > 0 && part.sep) {
// the a token should never be in a container
sentence.push({part: 'interjection', sep: null, tokens: [token]});
part = sentence[sentence.length - 1];
return;
}
part.tokens.push(token);
if (allowedWords.indexOf(token) === -1){
throw {type: 'illegal token', message: 'illegal token: ' + token};
}
});
// filter out empty parts
sentence = sentence.filter(function (part) {
return part.tokens.length > 0;
});
return sentence;
}
/**
* Preformats a given text, so that it splits it on punctuation marks.
* @param text text to preformat
* @returns {{parsable: Array, raw: Array}} parsable array of raw text and punctuation
*/
function preformat(text) {
var result = text.match(/[^\.!\?#]+[\.!\?#]+/g),
punctuation = ['.', ':', '?', '!', ','];
var parsableParts = [], rawParts = [];
if (!result) { // allow sentence fractions without any punctuation
result = [text + (punctuation.indexOf(text) === -1 ? '|' : '')];
// console.log('WARNING: sentence fraction without punctuation');
}
result.forEach(function (sentence) {
sentence = sentence.trim();
var parsableSentence = [];
parsableParts.push(parsableSentence);
rawParts.push(sentence);
var body = sentence.substr(0, sentence.length - 1);
// remove the comma before the la-clause and before a repeating li clause
body = body.replace(', la ', ' la ');
body = body.replace(', li ', ' li ');
// split on context separators comma and colon
var laparts = body.split(/ la /);
laparts.forEach(function (lapart, index) {
var colonparts = lapart.split(/:/);
colonparts.forEach(function (colonpart, index) {
var commaparts = colonpart.split(/,/);
commaparts.forEach(function (commapart, index) {
commapart = commapart.trim();
parsableSentence.push({content: commapart});
if (index < commaparts.length - 1) {
parsableSentence.push({punctuation: ['comma']});
}
});
if (index < colonparts.length - 1) {
parsableSentence.push({punctuation: ['colon']});
}
});
if (laparts.length === 2 && index === 0) {
parsableSentence.push({punctuation: ['la']});
}
});
var terminator = sentence.substr(-1);
switch (terminator) {
case '.':
parsableSentence.push({punctuation: ['period']});
break;
case ':':
parsableSentence.push({punctuation: ['colon']});
break;
case '!':
parsableSentence.push({punctuation: ['exclamation']});
break;
case '?':
parsableSentence.push({punctuation: ['question']});
break;
case '#':
parsableSentence.push({punctuation: ['banner']});
break;
default:
break;
}
});
return {parsable: parsableParts, raw: rawParts};
}
/**
* Split proper names into Toki Pona syllables. It is assumed that the proper name follows standard Toki Pona rules.
* @param properName the proper name string to split into syllables
*/
function splitProperIntoSyllables(properName) {
if (properName.length === 0) {
return [];
}
var vowels = ['o', 'u', 'i', 'a', 'e'],
allowed = ['o', 'u', 'i', 'a', 'e', 'mo', 'mu', 'mi', 'ma', 'me', 'no', 'nu', 'ni', 'na', 'ne', 'po', 'pu', 'pi', 'pa', 'pe', 'to', 'tu', 'ta', 'te', 'ko', 'ku', 'ki', 'ka', 'ke', 'so', 'su', 'si', 'sa', 'se', 'wi', 'wa', 'we', 'lo', 'lu', 'li', 'la', 'le', 'jo', 'ju', 'ja', 'je', 'on', 'un', 'in', 'an', 'en', 'mon', 'mun', 'min', 'man', 'men', 'non', 'nun', 'nin', 'nan', 'nen', 'pon', 'pun', 'pin', 'pan', 'pen', 'ton', 'tun', 'tan', 'ten', 'kon', 'kun', 'kin', 'kan', 'ken', 'son', 'sun', 'sin', 'san', 'sen', 'win', 'wan', 'wen', 'lon', 'lun', 'lin', 'lan', 'len', 'jon', 'jun'
|