<commit_before>function ApplicationManager(InputManager, Actuator, StorageManager, TranslationManager) {
  // initialize components
  this.inputManager = new InputManager;
  this.actuator = new Actuator;
  this.storageManager = new StorageManager(version);
  this.translationManager = new TranslationManager(this.inputManager, this.storageManager);

  // register event handlers
  this.inputManager.on("selectQuestionnaire",           this.selectQuestionnaire.bind(this));
  this.inputManager.on("questionnaireInitialized",      this.addQuestionnaireToMenu.bind(this));
  this.inputManager.on("allQuestionnairesInitialized",  this.allQuestionnairesInitialized.bind(this));
  this.inputManager.on("languageInitialized",           this.addLanguageToMenu.bind(this));
  this.inputManager.on("allLanguageInitialized",        this.allLanguageInitialized.bind(this));
  this.inputManager.on("setQuestionpackInfos",          this.setQuestionpackInfos.bind(this));
  this.inputManager.on("translateUI",                   this.translateUI.bind(this));
  this.inputManager.on("newQuestion",                   this.generateNewQuestion.bind(this));
  this.inputManager.on("showMenu",                      this.showMenu.bind(this));
  this.inputManager.on("showQuestion",                  this.showQuestion.bind(this));
  this.inputManager.on("newColor",                      this.newBackgroundColor.bind(this));
  this.inputManager.on("setOption",                     this.setOption.bind(this));
  if(development){
    this.inputManager.on("resetApplicationManager",       this.resetApplicationManager.bind(this));
  }

  this.initializeApplicationManager();
}

/*
 * Initialization
 */

ApplicationManager.prototype.initializeApplicationManager = function(){
  // initialize variables
  this.addedLanguages             = [];
  this.addedQuestionnaire         = [];
  this.total_number_of_questions  = {};
  this.current_question_id        = 0;
  this.currentTranslation         = undefined;
  this.first_available_language   = undefined;
  this.available_colors           = [
    "#6C7A89", "#95a5a6", "#ABB7B7", "#BDC3C7",
    "#D2527F", "#BF55EC", "#9b59b6", "#BE90D4",
    "#22A7F0", "#3498db", "#2980b9", "#3A539B",
    "#2ecc71", "#27ae60", "#1abc9c", "#16a085",
    "#f1c40f", "#f9bf3b", "#F5AB35", "#e9d460",
    "#E74C3C", "#F64747", "#e67e22", "#F2784B"
  ];

  // bootstrap
  this.first_available_questionnaire = "default";
  this.translationManager.loadAvailableQuestionnaires();
  this.actuator.showVersion(version);
}

ApplicationManager.prototype.resetApplicationManager = function(){
  this.storageManager.clear();
  this.initializeApplicationManager();
}

/*
 * Questionnaire related event handlers
 */

ApplicationManager.prototype.selectQuestionnaire = function(questionnaire){
  this.currentQuestionnaire = questionnaire;
  this.storageManager.setLastUsedQuestionnaire(questionnaire);
  this.generateNewQuestion();
}

ApplicationManager.prototype.addQuestionnaireToMenu = function(questionnaire) {
    if(this.addedQuestionnaire.indexOf(questionnaire) == -1){
      this.actuator.addQuestionnaireToMenu(questionnaire, "questionnaire-" + questionnaire);
      this.addedQuestionnaire.push(questionnaire);
    }
    if(!this.first_available_questionnaire){
      this.first_available_questionnaire = questionnaire;
    }
}

ApplicationManager.prototype.allQuestionnairesInitialized = function(){
  this.currentQuestionnaire = this.storageManager.getLastUsedQuestionnaire() || this.first_available_questionnaire;
  this.translationManager.loadAvailableUILanguages();
}

/*
 * Language related event handlers
 */

ApplicationManager.prototype.allLanguageInitialized = function(){
  var ln = this.storageManager.getLastUsedLanguage() || this.first_available_language;
  var questionnaire = this.storageManager.getLastUsedQuestionnaire() || this.first_available_questionnaire;
  this.bootstrapUI(questionnaire, ln);
}

ApplicationManager.prototype.bootstrapUI = function(questionnaire, ln){
  this.translateUI(ln);
  this.generateNewQuestion();
  this.showMenu();
  this.actuator.selectQuestionnaire(questionnaire);
  this.actuator.selectLanguage(ln);
  var storedColor;
  if (this.storageManager.isSaveColor())
    storedColor = this.storageManager.getLastUsedColor()
  this.newBackgroundColor(storedColor);<commit_msg>Added automatic language detection.<commit_after>function ApplicationManager(InputManager, Actuator, StorageManager, TranslationManager) {
  // initialize components
  this.inputManager = new InputManager;
  this.actuator = new Actuator;
  this.storageManager = new StorageManager(version);
  this.translationManager = new TranslationManager(this.inputManager, this.storageManager);

  // register event handlers
  this.inputManager.on("selectQuestionnaire",           this.selectQuestionnaire.bind(this));
  this.inputManager.on("questionnaireInitialized",      this.addQuestionnaireToMenu.bind(this));
  this.inputManager.on("allQuestionnairesInitialized",  this.allQuestionnairesInitialized.bind(this));
  this.inputManager.on("languageInitialized",           this.addLanguageToMenu.bind(this));
  this.inputManager.on("allLanguageInitialized",        this.allLanguageInitialized.bind(this));
  this.inputManager.on("setQuestionpackInfos",          this.setQuestionpackInfos.bind(this));
  this.inputManager.on("translateUI",                   this.translateUI.bind(this));
  this.inputManager.on("newQuestion",                   this.generateNewQuestion.bind(this));
  this.inputManager.on("showMenu",                      this.showMenu.bind(this));
  this.inputManager.on("showQuestion",                  this.showQuestion.bind(this));
  this.inputManager.on("newColor",                      this.newBackgroundColor.bind(this));
  this.inputManager.on("setOption",                     this.setOption.bind(this));
  if(development){
    this.inputManager.on("resetApplicationManager",       this.resetApplicationManager.bind(this));
  }

  this.initializeApplicationManager();
}

/*
 * Initialization
 */

ApplicationManager.prototype.initializeApplicationManager = function(){
  // initialize variables
  this.addedLanguages             = [];
  this.addedQuestionnaire         = [];
  this.total_number_of_questions  = {};
  this.current_question_id        = 0;
  this.currentTranslation         = undefined;
  this.first_available_language   = undefined;
  this.available_colors           = [
    "#6C7A89", "#95a5a6", "#ABB7B7", "#BDC3C7",
    "#D2527F", "#BF55EC", "#9b59b6", "#BE90D4",
    "#22A7F0", "#3498db", "#2980b9", "#3A539B",
    "#2ecc71", "#27ae60", "#1abc9c", "#16a085",
    "#f1c40f", "#f9bf3b", "#F5AB35", "#e9d460",
    "#E74C3C", "#F64747", "#e67e22", "#F2784B"
  ];

  // bootstrap
  this.first_available_questionnaire = "default";
  this.translationManager.loadAvailableQuestionnaires();
  this.actuator.showVersion(version);
  this.extractBrowserLanguage();
}

ApplicationManager.prototype.resetApplicationManager = function(){
  this.storageManager.clear();
  this.initializeApplicationManager();
}

/*
 * Questionnaire related event handlers
 */

ApplicationManager.prototype.selectQuestionnaire = function(questionnaire){
  this.currentQuestionnaire = questionnaire;
  this.storageManager.setLastUsedQuestionnaire(questionnaire);
  this.generateNewQuestion();
}

ApplicationManager.prototype.addQuestionnaireToMenu = function(questionnaire) {
    if(this.addedQuestionnaire.indexOf(questionnaire) == -1){
      this.actuator.addQuestionnaireToMenu(questionnaire, "questionnaire-" + questionnaire);
      this.addedQuestionnaire.push(questionnaire);
    }
    if(!this.first_available_questionnaire){
      this.first_available_questionnaire = questionnaire;
    }
}

ApplicationManager.prototype.allQuestionnairesInitialized = function(){
  this.currentQuestionnaire = this.storageManager.getLastUsedQuestionnaire() || this.first_available_questionnaire;
  this.translationManager.loadAvailableUILanguages();
}

/*
 * Language related methods and event handlers
 */

ApplicationManager.prototype.extractBrowserLanguage = function(){
  var languages = navigator.languages;
  var res = [];
  for(var i=0;i<languages.length;i++){
    var ln = languages[i];
    if(ln.indexOf("-") >= 0){
      ln = ln.substr(0, ln.indexOf("-"));
    }
    if(!res.includes(ln)){
      res.push(ln);
    }
  }
  this.browser_languages = res;
}

ApplicationManager.prototype.allLanguageInitialized = function(){
  var ln = this.storageManager.getLastUsedLanguage();
  if(!ln){
    for(var i=0;i < this.browser_languages.length; i++){
      var tmp_ln = this.browser_languages[i];
      if(this.translationManager.isLanguageSupported(tmp_ln)){
        ln = tmp_ln;
        break;
      }
    }
  }
  if(!ln){
    ln = this.first_available_language;
  }
  var questionnaire = this.storageManager.getLastUsedQuestionnaire() || this.first_available_questionnaire;
  this.bootstrapUI(questionnaire, ln);
}

ApplicationManager.prototype.bootstrapUI = function(questionnaire, ln){
  this.translateUI(ln);
  this.generateNewQuestion();
  this.showMenu();
  this.actuator.selectQuestionnaire(questionnaire);
  this.actuator.selectLanguage(ln);
  var storedColor;
  if (this.storageManager.isSaveColor())
    storedColor = this.storageManager.getLastUsedColor()
  this.newBackgroundColor(storedColor);
{'fraction_non_alphanumeric': 0.12691771269177127, 'fraction_numerical': 0.018882094195901727, 'mean_word_length': 4.506202008269344, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '84696', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2679, 'n_words': 354}