ajax-cat 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ class Utils
2
+
3
+ @random_string: ->
4
+ chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"
5
+ ret = ""
6
+ for i in [0..16]
7
+ rnum = Math.floor(Math.random() * chars.length)
8
+ ret += chars.substring(rnum,rnum+1)
9
+ return ret
10
+
11
+ @tokenize: (input) =>
12
+ input = input.toLowerCase()
13
+ input = input.replace(/\,/g, " , ")
14
+ input = input.replace(/\./g, " . ")
15
+ input = input.replace(/\n/g, "")
16
+ input = input.replace(/\ \ /g, " ")
17
+ return input
18
+
19
+ @split_source: (text) ->
20
+ delimiters = [".","!","?"]
21
+ line = ""
22
+ spliting = false
23
+ firstSplit = false
24
+ input = new Array()
25
+ white = 0
26
+ last = ' '
27
+ for c in text
28
+ #c = text[i]
29
+ if (spliting == true)
30
+ if (c != ' ' && c != '\n' && c != '\t')
31
+ if (white > 0 || last == '\n')
32
+ input[input.length] = line
33
+ line = ""
34
+ spliting = false
35
+ else
36
+ ++white
37
+ line += c
38
+ if (c == '.' || c == '!' || c == '?' || c == '\n')
39
+ spliting = true
40
+ white = 0
41
+ last = c
42
+ input[input.length] = line
43
+ return input
44
+
45
+ @trim: (text) =>
46
+ return text.replace(/^\s+|\s+$/g, "")
@@ -0,0 +1,2 @@
1
+ # coffee -wbl -j "ajax-cat.js" -c *.coffee
2
+ # ssh -L 8888:10.10.24.118:8888 odchazel@ufallab.ms.mff.cuni.cz
@@ -0,0 +1,628 @@
1
+ var AjaxCatList, AjaxCatTranslation, Suggestions, TranslationTable, Utils,
2
+ __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
3
+
4
+ AjaxCatList = (function() {
5
+
6
+ function AjaxCatList() {
7
+ this.create_new_translation = __bind(this.create_new_translation, this);
8
+ this.delete_document = __bind(this.delete_document, this);
9
+ this.show_translations = __bind(this.show_translations, this);
10
+ this.new_translation = __bind(this.new_translation, this);
11
+ var _this = this;
12
+ $('#new-translation-modal').hide();
13
+ $('#new-translation').on('click', this.new_translation);
14
+ $('#create-new-translation').on('click', this.create_new_translation);
15
+ this.show_translations();
16
+ $(".example").click(function(event) {
17
+ var text;
18
+ text = $(event.currentTarget).data("text");
19
+ $('#new-translation-text').val(text);
20
+ return false;
21
+ });
22
+ }
23
+
24
+ AjaxCatList.prototype.new_translation = function() {
25
+ $('#new-translation-name').val('Name');
26
+ $('#new-translation-text').val('');
27
+ $('#new-translation-modal').modal('show');
28
+ return $('#new-translation-text').focus();
29
+ };
30
+
31
+ AjaxCatList.prototype.show_translations = function() {
32
+ var doc, i, ids, _i, _len,
33
+ _this = this;
34
+ $("#translation-list").html('');
35
+ if (!localStorage['ac-data']) return;
36
+ ids = JSON.parse(localStorage['ac-data']);
37
+ for (_i = 0, _len = ids.length; _i < _len; _i++) {
38
+ i = ids[_i];
39
+ doc = JSON.parse(localStorage[i]);
40
+ $("#translation-list").append("<tr><td width='100%'><a href='/translation.html#" + doc.id + "'>" + doc.name + "</a></td><td><button data-id='" + doc.id + "' class='btn btn-danger btn-mini delete-button'>delete</button></td></tr>");
41
+ }
42
+ return $(".delete-button").click(function(event) {
43
+ var id;
44
+ id = $(event.currentTarget).data("id");
45
+ if (confirm("Delete this translation?")) {
46
+ _this.delete_document(id);
47
+ return _this.show_translations();
48
+ }
49
+ });
50
+ };
51
+
52
+ AjaxCatList.prototype.delete_document = function(id) {
53
+ var i, ids, new_ids, _i, _len;
54
+ if (!localStorage['ac-data']) return;
55
+ ids = JSON.parse(localStorage['ac-data']);
56
+ new_ids = [];
57
+ for (_i = 0, _len = ids.length; _i < _len; _i++) {
58
+ i = ids[_i];
59
+ if (i !== id) new_ids.push(i);
60
+ }
61
+ localStorage.removeItem(id);
62
+ return localStorage.setItem('ac-data', JSON.stringify(new_ids));
63
+ };
64
+
65
+ AjaxCatList.prototype.create_new_translation = function() {
66
+ var doc, docs, text;
67
+ text = $('#new-translation-text').val();
68
+ if (localStorage['ac-data']) {
69
+ docs = JSON.parse(localStorage['ac-data']);
70
+ } else {
71
+ docs = [];
72
+ }
73
+ doc = {};
74
+ doc.id = Utils.random_string();
75
+ doc['name'] = $('#new-translation-name').val();
76
+ doc['pair'] = $('#new-translation-pair').val();
77
+ doc.source = Utils.split_source(text);
78
+ doc.target = new Array(doc.source.length);
79
+ docs.push(doc.id);
80
+ localStorage.setItem('ac-data', JSON.stringify(docs));
81
+ localStorage.setItem(doc.id, JSON.stringify(doc));
82
+ $('#new-translation-modal').modal('hide');
83
+ return this.show_translations();
84
+ };
85
+
86
+ return AjaxCatList;
87
+
88
+ })();
89
+
90
+ AjaxCatTranslation = (function() {
91
+
92
+ AjaxCatTranslation.prototype.cur_position = false;
93
+
94
+ AjaxCatTranslation.prototype.pair = "en-cs";
95
+
96
+ AjaxCatTranslation.prototype.host = "localhost";
97
+
98
+ function AjaxCatTranslation() {
99
+ this.add_words = __bind(this.add_words, this);
100
+ this.change_position = __bind(this.change_position, this);
101
+ this.load_translation_table = __bind(this.load_translation_table, this);
102
+ this.save_target = __bind(this.save_target, this);
103
+ this.show_preview = __bind(this.show_preview, this);
104
+ this.bind_events = __bind(this.bind_events, this);
105
+ this.resize = __bind(this.resize, this);
106
+ var data, hash, i, s, t, _i, _j, _len, _len2, _ref, _ref2,
107
+ _this = this;
108
+ this.host = window.location.hostname;
109
+ this.suggestions = new Suggestions(this);
110
+ $('#translation-preview-modal').hide();
111
+ $('#myTab').tab('show');
112
+ hash = window.location.hash.substr(1);
113
+ data = localStorage.getItem(hash);
114
+ if (!data) {
115
+ alert("No such document.");
116
+ return;
117
+ }
118
+ this.doc = JSON.parse(data);
119
+ this.pair = this.doc.pair;
120
+ $('h1').html("" + this.doc.name + " <small>" + this.pair + "</small>");
121
+ $("title").html("" + this.doc.name + " - AJAX-CAT");
122
+ i = 0;
123
+ _ref = this.doc.source;
124
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
125
+ s = _ref[_i];
126
+ $("#source-top").append("<span class='ac-element ac-source' data-position='" + i + "'>" + s + "</span>");
127
+ $("#source-bottom").append("<span class='ac-element ac-source' data-position='" + i + "'>" + s + "</span>");
128
+ i += 1;
129
+ }
130
+ i = 0;
131
+ _ref2 = this.doc.target;
132
+ for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
133
+ t = _ref2[_j];
134
+ if (!t) t = "";
135
+ $("#target-top").append("<span class='ac-element ac-target' data-position='" + i + "'>" + t + "</span>");
136
+ $("#target-bottom").append("<span class='ac-element ac-target' data-position='" + i + "'>" + t + "</span>");
137
+ i += 1;
138
+ }
139
+ $('.ac-element').click(function(event) {
140
+ var position;
141
+ position = $(event.target).data('position');
142
+ return _this.change_position(position);
143
+ });
144
+ this.length = $("#source-top").children().length;
145
+ this.change_position(0);
146
+ this.bind_events();
147
+ this.resize();
148
+ }
149
+
150
+ AjaxCatTranslation.prototype.resize = function() {
151
+ var width;
152
+ width = $(window).width();
153
+ return $("#translation-table-container").width(width - 60);
154
+ };
155
+
156
+ AjaxCatTranslation.prototype.bind_events = function() {
157
+ var _this = this;
158
+ $("#target-sentence").on('keydown', function(event) {
159
+ var ar, text, word;
160
+ switch (event.which) {
161
+ case 13:
162
+ return _this.suggestions.take_suggestion();
163
+ case 32:
164
+ text = $("#target-sentence").val();
165
+ text = Utils.trim(text);
166
+ if (text.length > 0) {
167
+ ar = text.split(/[ ]+/);
168
+ word = ar[ar.length - 1];
169
+ return _this.table.mark_words(word);
170
+ }
171
+ break;
172
+ case 33:
173
+ if (_this.cur_position > 0) {
174
+ _this.change_position(_this.cur_position - 1);
175
+ }
176
+ return false;
177
+ case 34:
178
+ if ((_this.cur_position + 1) < _this.length) {
179
+ _this.change_position(_this.cur_position + 1);
180
+ }
181
+ return false;
182
+ case 38:
183
+ return _this.suggestions.up();
184
+ case 40:
185
+ return _this.suggestions.down();
186
+ default:
187
+ return $(window).trigger('loadSuggestions');
188
+ }
189
+ });
190
+ $("#preview").click(function() {
191
+ _this.save_target();
192
+ _this.show_preview();
193
+ return false;
194
+ });
195
+ return $("#save").click(function() {
196
+ _this.save_target();
197
+ return alert("Translation was saved into your browser.");
198
+ });
199
+ };
200
+
201
+ AjaxCatTranslation.prototype.show_preview = function() {
202
+ $("#source").text(this.doc.source.join(''));
203
+ $("#target").text(this.doc.target.join(''));
204
+ return $('#translation-preview-modal').modal('show');
205
+ };
206
+
207
+ AjaxCatTranslation.prototype.save_target = function() {
208
+ var el, tar, _i, _len, _ref;
209
+ $("#target-top .ac-target[data-position=" + this.cur_position + "]").text($("#target-sentence").val());
210
+ $("#target-bottom .ac-target[data-position=" + this.cur_position + "]").text($("#target-sentence").val());
211
+ tar = [];
212
+ _ref = $("#target-top .ac-target");
213
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
214
+ el = _ref[_i];
215
+ tar.push($(el).text());
216
+ }
217
+ this.doc.target = tar;
218
+ return localStorage.setItem(this.doc.id, JSON.stringify(this.doc));
219
+ };
220
+
221
+ AjaxCatTranslation.prototype.load_translation_table = function(sentence) {
222
+ var _this = this;
223
+ sentence = Utils.tokenize(sentence);
224
+ if (sentence.match(/^[\ \t]*$/)) {
225
+ $("#translation-table-container").html("");
226
+ this.suggestions.clear();
227
+ return;
228
+ }
229
+ $("#translation-table-container").text("");
230
+ return $.ajax("http://" + this.host + ":8888/table", {
231
+ data: {
232
+ pair: this.pair,
233
+ q: sentence
234
+ },
235
+ success: function(data) {
236
+ _this.table = new TranslationTable(_this, data);
237
+ $("#translation-table-container").html(_this.table.get_table());
238
+ return $(window).trigger('loadSuggestions');
239
+ },
240
+ error: function() {}
241
+ });
242
+ };
243
+
244
+ AjaxCatTranslation.prototype.change_position = function(position) {
245
+ if (this.cur_position !== false) this.save_target();
246
+ $("#source-top").children().slice(0, position).show();
247
+ $("#source-top").children().slice(position, this.length).hide();
248
+ $("#source-bottom").children().slice(0, position + 1).hide();
249
+ $("#source-bottom").children().slice(position + 1, this.length).show();
250
+ $("#target-top").children().slice(0, position).show();
251
+ $("#target-top").children().slice(position, this.length).hide();
252
+ $("#target-bottom").children().slice(0, position + 1).hide();
253
+ $("#target-bottom").children().slice(position + 1, this.length).show();
254
+ $("#source-sentence").text($("#source-top .ac-source[data-position=" + position + "]").text());
255
+ $("#target-sentence").val($("#target-top .ac-target[data-position=" + position + "]").text());
256
+ this.cur_position = position;
257
+ $("#target-sentence").focus();
258
+ return this.load_translation_table($("#source-top .ac-source[data-position=" + position + "]").text());
259
+ };
260
+
261
+ AjaxCatTranslation.prototype.add_words = function(words, change_covered) {
262
+ var text;
263
+ if (change_covered == null) change_covered = false;
264
+ text = $("#target-sentence").val();
265
+ text = Utils.trim(text);
266
+ words = Utils.trim(words);
267
+ text = Utils.trim(text);
268
+ text += " " + words + " ";
269
+ $("#target-sentence").val(text);
270
+ return $("#target-sentence").click();
271
+ };
272
+
273
+ return AjaxCatTranslation;
274
+
275
+ })();
276
+
277
+ Suggestions = (function() {
278
+
279
+ function Suggestions(translation, limit) {
280
+ var i, sug,
281
+ _this = this;
282
+ this.translation = translation;
283
+ if (limit == null) limit = 5;
284
+ this.down = __bind(this.down, this);
285
+ this.up = __bind(this.up, this);
286
+ this.set_position = __bind(this.set_position, this);
287
+ this.get_position = __bind(this.get_position, this);
288
+ this.positions = __bind(this.positions, this);
289
+ this.process_suggestions = __bind(this.process_suggestions, this);
290
+ this.take_suggestion = __bind(this.take_suggestion, this);
291
+ this.load_suggestions = __bind(this.load_suggestions, this);
292
+ this.clear = __bind(this.clear, this);
293
+ i = 0;
294
+ while (i < limit) {
295
+ sug = $("<div>", {
296
+ "class": 'ac-suggestion',
297
+ 'data-suggestion-position': i,
298
+ click: function() {
299
+ return _this.take_suggestion();
300
+ },
301
+ mouseover: function(event) {
302
+ var target;
303
+ target = $(event.currentTarget);
304
+ if (target.hasClass('suggestion-enabled')) {
305
+ return _this.set_position(target.data('suggestion-position'));
306
+ }
307
+ }
308
+ });
309
+ $("#suggestions-container").append(sug);
310
+ i += 1;
311
+ }
312
+ $(window).bind('loadSuggestions', function() {
313
+ _this.load_suggestions();
314
+ return $("#target-sentence").focus();
315
+ });
316
+ }
317
+
318
+ Suggestions.prototype.clear = function() {
319
+ $(".ac-suggestion").text("");
320
+ $(".ac-suggestion").removeClass('suggestion-enabled');
321
+ return $(".ac-suggestion").removeClass('suggestion-active');
322
+ };
323
+
324
+ Suggestions.prototype.load_suggestions = function() {
325
+ var covered, sentence, translated,
326
+ _this = this;
327
+ this.clear();
328
+ sentence = $("#source-sentence").text();
329
+ sentence = Utils.tokenize(sentence);
330
+ translated = Utils.tokenize($("#source-target").text());
331
+ covered = this.translation.table.covered_vector();
332
+ return $.ajax("http://" + this.translation.host + ":8888/suggestion", {
333
+ data: {
334
+ pair: this.translation.pair,
335
+ q: Utils.tokenize(sentence),
336
+ covered: covered,
337
+ translated: translated
338
+ },
339
+ success: function(data) {
340
+ data = JSON.parse(data);
341
+ return _this.process_suggestions(data);
342
+ },
343
+ error: function() {}
344
+ });
345
+ };
346
+
347
+ Suggestions.prototype.take_suggestion = function() {
348
+ var text;
349
+ if (this.get_position() === false) return;
350
+ text = $(".suggestion-active span").text();
351
+ this.translation.add_words(text);
352
+ return this.translation.table.mark_words(text);
353
+ };
354
+
355
+ Suggestions.prototype.process_suggestions = function(data) {
356
+ var el, i, suggestion, translation, _i, _len, _ref, _results;
357
+ i = 0;
358
+ _ref = data.suggestions;
359
+ _results = [];
360
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
361
+ suggestion = _ref[_i];
362
+ translation = $("#target-sentence").val();
363
+ el = $(".ac-suggestion").slice(i, i + 1);
364
+ el.html("" + translation + " <span>" + suggestion + "</span>");
365
+ el.addClass('suggestion-enabled');
366
+ _results.push(i += 1);
367
+ }
368
+ return _results;
369
+ };
370
+
371
+ Suggestions.prototype.positions = function() {
372
+ return $(".suggestion-enabled").length;
373
+ };
374
+
375
+ Suggestions.prototype.get_position = function() {
376
+ var el;
377
+ el = $(".suggestion-active");
378
+ if (!el.length) return false;
379
+ return el.data('suggestion-position');
380
+ };
381
+
382
+ Suggestions.prototype.set_position = function(position) {
383
+ $(".suggestion-active").removeClass('suggestion-active');
384
+ return $(".suggestion-enabled[data-suggestion-position=" + position + "]").addClass('suggestion-active');
385
+ };
386
+
387
+ Suggestions.prototype.up = function() {
388
+ var position;
389
+ position = this.get_position();
390
+ if (position === false) return;
391
+ return this.set_position(position - 1);
392
+ };
393
+
394
+ Suggestions.prototype.down = function() {
395
+ var position;
396
+ position = this.get_position();
397
+ if (position === false) {
398
+ this.set_position(0);
399
+ return;
400
+ }
401
+ if ((position + 1) < this.positions()) return this.set_position(position + 1);
402
+ };
403
+
404
+ return Suggestions;
405
+
406
+ })();
407
+
408
+ TranslationTable = (function() {
409
+
410
+ function TranslationTable(translation, data) {
411
+ this.translation = translation;
412
+ this.covered_vector = __bind(this.covered_vector, this);
413
+ this.unmark_position = __bind(this.unmark_position, this);
414
+ this.mark_position = __bind(this.mark_position, this);
415
+ this.mark_interval = __bind(this.mark_interval, this);
416
+ this.mark_words = __bind(this.mark_words, this);
417
+ this.position_marked = __bind(this.position_marked, this);
418
+ this.get_row = __bind(this.get_row, this);
419
+ this.get_header = __bind(this.get_header, this);
420
+ this.get_table = __bind(this.get_table, this);
421
+ this.data = JSON.parse(data);
422
+ }
423
+
424
+ TranslationTable.prototype.get_table = function() {
425
+ var ret, row, _i, _len, _ref;
426
+ ret = $("<table>", {
427
+ "class": 'translation-table'
428
+ });
429
+ ret.append(this.get_header());
430
+ _ref = this.data.target;
431
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
432
+ row = _ref[_i];
433
+ ret.append(this.get_row(row));
434
+ }
435
+ return ret;
436
+ };
437
+
438
+ TranslationTable.prototype.get_header = function() {
439
+ var i, ret, src, word, _i, _len, _ref,
440
+ _this = this;
441
+ ret = $("<tr>");
442
+ i = 0;
443
+ _ref = this.data.source;
444
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
445
+ word = _ref[_i];
446
+ src = $("<th>", {
447
+ "class": 'ac-source',
448
+ html: word,
449
+ "data-position": i,
450
+ click: function(event) {
451
+ var position;
452
+ position = parseInt($(event.currentTarget).data('position'));
453
+ if (_this.position_marked(position)) {
454
+ _this.unmark_position(position);
455
+ } else {
456
+ _this.mark_position(position);
457
+ }
458
+ return $(window).trigger('loadSuggestions');
459
+ }
460
+ });
461
+ ret.append(src);
462
+ i += 1;
463
+ }
464
+ return ret;
465
+ };
466
+
467
+ TranslationTable.prototype.get_row = function(row) {
468
+ var cell, content, i, len, ret, word, _i, _len,
469
+ _this = this;
470
+ ret = $("<tr>");
471
+ i = 0;
472
+ for (_i = 0, _len = row.length; _i < _len; _i++) {
473
+ word = row[_i];
474
+ len = parseInt(word.s);
475
+ if (word.empty) {
476
+ ret.append("<td colspan='" + word.s + "' class='ac-empty'></td>");
477
+ } else {
478
+ cell = $("<td colspan='" + word.s + "' class='ac-word'></td>");
479
+ content = $("<div>", {
480
+ 'data-position-from': i,
481
+ 'data-position-to': i + len - 1,
482
+ html: word.t,
483
+ click: function(event) {
484
+ i = parseInt($(event.currentTarget).data('position-from'));
485
+ while (i <= parseInt($(event.currentTarget).data('position-to'))) {
486
+ _this.mark_position(i);
487
+ i += 1;
488
+ }
489
+ _this.translation.add_words($(event.currentTarget).text());
490
+ return $(window).trigger('loadSuggestions');
491
+ }
492
+ });
493
+ cell.append(content);
494
+ ret.append(cell);
495
+ }
496
+ i += len;
497
+ }
498
+ return ret;
499
+ };
500
+
501
+ TranslationTable.prototype.position_marked = function(position) {
502
+ if ($("th.ac-source").slice(position, position + 1).hasClass('ac-selected')) {
503
+ return true;
504
+ }
505
+ return false;
506
+ };
507
+
508
+ TranslationTable.prototype.mark_words = function(words) {
509
+ var el, el_text, _i, _len, _ref;
510
+ words = "" + words + " ";
511
+ _ref = $(".ac-word div");
512
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
513
+ el = _ref[_i];
514
+ el_text = Utils.trim($(el).text());
515
+ el_text = "" + el_text + " ";
516
+ if ((words.indexOf(el_text) === 0) && (words.length >= el_text.length)) {
517
+ this.mark_interval($(el).data('position-from'), $(el).data('position-to'));
518
+ $(window).trigger('loadSuggestions');
519
+ words = words.substr(el_text.length);
520
+ words = Utils.trim(words);
521
+ words = "" + words + " ";
522
+ if (!(words.length > 0)) return;
523
+ }
524
+ }
525
+ };
526
+
527
+ TranslationTable.prototype.mark_interval = function(from, to) {
528
+ var i, _results;
529
+ i = from;
530
+ _results = [];
531
+ while (i <= to) {
532
+ this.mark_position(i);
533
+ _results.push(i += 1);
534
+ }
535
+ return _results;
536
+ };
537
+
538
+ TranslationTable.prototype.mark_position = function(position) {
539
+ return $("th.ac-source").slice(position, position + 1).addClass('ac-selected');
540
+ };
541
+
542
+ TranslationTable.prototype.unmark_position = function(position) {
543
+ return $("th.ac-source").slice(position, position + 1).removeClass('ac-selected');
544
+ };
545
+
546
+ TranslationTable.prototype.covered_vector = function() {
547
+ var el, ret, _i, _len, _ref;
548
+ ret = "";
549
+ _ref = $("th.ac-source");
550
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
551
+ el = _ref[_i];
552
+ if ($(el).hasClass('ac-selected')) {
553
+ ret += "1";
554
+ } else {
555
+ ret += "0";
556
+ }
557
+ }
558
+ return ret;
559
+ };
560
+
561
+ return TranslationTable;
562
+
563
+ })();
564
+
565
+ Utils = (function() {
566
+
567
+ function Utils() {}
568
+
569
+ Utils.random_string = function() {
570
+ var chars, i, ret, rnum;
571
+ chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
572
+ ret = "";
573
+ for (i = 0; i <= 16; i++) {
574
+ rnum = Math.floor(Math.random() * chars.length);
575
+ ret += chars.substring(rnum, rnum + 1);
576
+ }
577
+ return ret;
578
+ };
579
+
580
+ Utils.tokenize = function(input) {
581
+ input = input.toLowerCase();
582
+ input = input.replace(/\,/g, " , ");
583
+ input = input.replace(/\./g, " . ");
584
+ input = input.replace(/\n/g, "");
585
+ input = input.replace(/\ \ /g, " ");
586
+ return input;
587
+ };
588
+
589
+ Utils.split_source = function(text) {
590
+ var c, delimiters, firstSplit, input, last, line, spliting, white, _i, _len;
591
+ delimiters = [".", "!", "?"];
592
+ line = "";
593
+ spliting = false;
594
+ firstSplit = false;
595
+ input = new Array();
596
+ white = 0;
597
+ last = ' ';
598
+ for (_i = 0, _len = text.length; _i < _len; _i++) {
599
+ c = text[_i];
600
+ if (spliting === true) {
601
+ if (c !== ' ' && c !== '\n' && c !== '\t') {
602
+ if (white > 0 || last === '\n') {
603
+ input[input.length] = line;
604
+ line = "";
605
+ }
606
+ spliting = false;
607
+ } else {
608
+ ++white;
609
+ }
610
+ }
611
+ line += c;
612
+ if (c === '.' || c === '!' || c === '?' || c === '\n') {
613
+ spliting = true;
614
+ white = 0;
615
+ }
616
+ last = c;
617
+ }
618
+ input[input.length] = line;
619
+ return input;
620
+ };
621
+
622
+ Utils.trim = function(text) {
623
+ return text.replace(/^\s+|\s+$/g, "");
624
+ };
625
+
626
+ return Utils;
627
+
628
+ }).call(this);