/**
* Created by David on 5/13/2016.
*/
import {Component, CUSTOM_ELEMENTS_SCHEMA} from "@angular/core";
@Component({
selector: 'tagger',
directives: [],
templateUrl: 'app/templates/tagger.html',
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class TaggerComponent {
private sentences = [];
private parallel = [];
private resultsen = "";
private resultpar = "";
private tags = [];
private index = 0;
private target = /put(s|ten|ting)?\s((.+?)\s)?up/;
private resulthidden = true;
acquire (data, tagged) {
this.sentences = data.split(/\n/);
this.sentences.push("ooo put up ooo PRESS ENTER TO FINALIZE");
this.parallel = tagged.split(/\n/);
}
private faulty = 0;
process (sentence) {
let match = this.target.exec(sentence);
if (match == null) {
this.faulty++;
return "Please press ESC to jump faulty sentence
" + sentence;
}
let startpos = match.index;
let endpos = startpos+match[0].length;
let startingtag = "";
let closingtag = "";
// insert first at endpos, then startpos, so that endpos is not changed by inserting to startpos
var withClosing = [sentence.slice(0, endpos), closingtag, sentence.slice(endpos)].join('');
return [withClosing.slice(0, startpos), startingtag, withClosing.slice(startpos)].join('');
}
evaluate(key) {
console.log(key.which);
}
getCurrent () {
return this.process(this.sentences[this.index]);
}
private cooldown = false;
activate (key) {
if (this.cooldown) {
return;
}
let tag = -1;
switch(key.which) {
case 37:
tag = 0;
this.tags.push(tag);
break;
case 39:
tag = 1;
this.tags.push(tag);
break;
case 27:
this.deleteFaulty();
this.index--;
break;
case 13:
this.finalize();
break;
default:
break;
}
this.index++;
this.cooldown = true;
let me = this;
setTimeout(function() {me.cooldown = false}, 1500);
}
deleteFaulty () {
this.sentences.splice(this.index, 1);
this.parallel.splice(this.index, 1);
}
finalize () {
this.index = 0;
this.sentences.pop();
for (let i = 0; i < this.sentences.length; i++) {
let sent = this.sentences[i];
let parr = this.parallel[i];
let tag = this.tags[i];
this.resultsen += sent + "\t" + tag + "\n";
this.resultpar += parr + "\t" + tag + "\n";
}
this.resulthidden = false;
}
}