window.addEventListener('load', function () {
    init();
})
var tokens = [];
var sentence_pairs = [];
var index = 0;

function init(){
    sentence_pairs = document.getElementsByClassName("sentence_pair");

    for (i= 0; i < sentence_pairs.length; i++){
        sentences = sentence_pairs[i].children;
        for (i2= 0; i2 < sentences.length; i2++){
            words = sentences[i2].children;
            for (i3= 0; i3 < words.length; i3++){
                token = words[i3];
                var token_split = token.id.split("_");
                var token_attributes = {};
                token_attributes["div"] = token;
                token_attributes["sp"] = sentence_pairs[i];
                token_attributes["sentence"] = sentences[i2];
                token_attributes["text"] = token_split[1];
                token_attributes["lemma"] = token_split[2];
                token_attributes["both_sentences"] = token_split[3];
                token_attributes["pos"] = token_split[4];
                token_attributes["tag"] = token_split[5];
                token_attributes["dep"] = token_split[6];
                score = parseFloat(token_split[7]);
                if (Number.isNaN(score)){
                    score = 0;
                }
                token_attributes["score"] = score;
                tokens.push(token_attributes);
            }
        }
    }
}

function getAverageAll(){
    pos_list = [];
    dep_list = [];
    tag_list = [];
    
    for (i = 0; i < tokens.length; i++){
        pos = tokens[i]["pos"];
        dep = tokens[i]["dep"];
        tag = tokens[i]["tag"];
        if ((!pos_list.includes(pos)) && pos != undefined){
            pos_list.push(pos);
        }
        if ((!dep_list.includes(dep)) && pos != undefined){
            dep_list.push(dep);
        }
        if ((!tag_list.includes(tag)) && pos != undefined){
            tag_list.push(tag);
        }

    }
    var intervalId = window.setInterval(function(){

        var first = pos_list.length;
        var second = first + dep_list.length;
        var third = second + tag_list.length;

        if (index < first){
            attributes = [["pos",pos_list[index]]];
            getAverageScript(attributes);
        } else if (index < second){
            attributes = [["dep",dep_list[index-first]]];
            getAverageScript(attributes);
        } else if (index < third){
            attributes = [["tag",tag_list[index-second]]];
            getAverageScript(attributes);
        } else{
            clearInterval(intervalId);
        }
        
        index++;
    }, 1000);


}
function getAverageScript(attributes){

  specified_tokens = 0;
  average_score = 0;
  //attributes.push(["both_sentences","True"]);
  for (i = 0; i < tokens.length; i++){
      if (attributes.length == 0){
          average_score = average_score + tokens[i]["score"];
          specified_tokens++;
          continue;
      }
      for (i2 = 0; i2 < attributes.length; i2++){
        add_token = 1;
        if (tokens[i][attributes[i2][0]] != attributes[i2][1]){
            add_token = 0;
            break;
        }
      }
      if (add_token == 1){
        average_score = average_score + tokens[i]["score"];
        specified_tokens++;
      }
      
  }
    result = average_score/specified_tokens;
    result = Number(result.toFixed(3));

    document.getElementById("results").innerHTML = document.getElementById("results").innerHTML + "<br>" + attributes + ";" + result +";" + specified_tokens;
}
        
function getAverage(){

for (i = 0; i < sentence_pairs.length; i++){
    sentence_pairs[i].style.display= "none";

}
  specified_tokens = 0
  average_score = 0
  attributes = []

  word = document.getElementById("word");
  lemma = document.getElementById("lemma");
  pos = document.getElementById("pos");
  tag = document.getElementById("tag");
  dep = document.getElementById("dep");
  both_sentences = document.getElementById("both_sentences");
  
  if (word.value != ""){
      attributes.push(["text",word.value]);
  }
  if (lemma.value != ""){
      attributes.push(["lemma",lemma.value]);
  }
  if (pos.value != ""){
      attributes.push(["pos",pos.value]);
  }
  if (tag.value != ""){
      attributes.push(["tag",tag.value]);
  }
  if (dep.value != ""){
      attributes.push(["dep",dep.value]);
  }
  if (both_sentences.value != ""){
      attributes.push(["both_sentences",both_sentences.value]);
  }
  for (i = 0; i < tokens.length; i++){
      if (attributes.length == 0){
          average_score = average_score + tokens[i]["score"];
          specified_tokens++;
          tokens[i]["sp"].style.display="";
          continue;
      }
      for (i2 = 0; i2 < attributes.length; i2++){
        add_token = 1;
        if (tokens[i][attributes[i2][0]] != attributes[i2][1]){
            add_token = 0;
            break;
        }
      }
      if (add_token == 1){
        average_score = average_score + tokens[i]["score"];
        specified_tokens++;
        tokens[i]["sp"].style.display="";
        for (i2 = 0; i2 < attributes.length; i2++){
            tokens[i]["div"].innerHTML = tokens[i]["div"].innerHTML + "<br>" + attributes[i2];
        }
      }
      
  }
    result = average_score/specified_tokens;
    result = Number(result.toFixed(3));

    document.getElementById("results").innerHTML = "Results<br>Average Deviation:" + result;
    document.getElementById("specified_tokens").innerHTML = "Specified tokens:" + specified_tokens;
}