<commit_before>/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
 * ------------------------------------------------------------------*/

package com.kotlinnlp.simplednn.helpers.validation

import com.kotlinnlp.simplednn.core.neuralprocessor.NeuralProcessor
import com.kotlinnlp.simplednn.dataset.*
import com.kotlinnlp.simplednn.core.functionalities.outputevaluation.OutputEvaluationFunction

/**
 * A helper which executes the validation of a dataset.
 *
 * @property neuralProcessor a neural processor
 * @property outputEvaluationFunction the output evaluation function
 */
abstract class ValidationHelper<ExampleType: Example>(
  open val neuralProcessor: NeuralProcessor,
  val outputEvaluationFunction: OutputEvaluationFunction) {

  /**
   * Validate examples.
   *
   * @param examples a list of examples to validate
   * @param onPrediction an optional callback called for each prediction (args: example, isCorrect)
   * @param saveContributions whether to save the contributions of each input to its output (needed to calculate
   *                          the relevance)
   *
   * @return the percentage of correct predictions
   */
  fun validate(examples: ArrayList<ExampleType>,
               onPrediction: (example: ExampleType, isCorrect: Boolean) -> Unit = {_, _ -> },
               saveContributions: Boolean = false): Double {

    var correctPredictions: Int = 0

    examples.forEach {
      val isCorrect = this.validate(it, saveContributions = saveContributions)

      if (isCorrect) {
        correctPredictions += 1
      }<commit_msg>Code refactoring<commit_after>/* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, you can obtain one at http://mozilla.org/MPL/2.0/.
 * ------------------------------------------------------------------*/

package com.kotlinnlp.simplednn.helpers.validation

import com.kotlinnlp.simplednn.core.neuralprocessor.NeuralProcessor
import com.kotlinnlp.simplednn.dataset.*
import com.kotlinnlp.simplednn.core.functionalities.outputevaluation.OutputEvaluationFunction

/**
 * A helper which executes the validation of a dataset.
 *
 * @property neuralProcessor a neural processor
 * @property outputEvaluationFunction the output evaluation function
 */
abstract class ValidationHelper<ExampleType: Example>(
  open val neuralProcessor: NeuralProcessor,
  val outputEvaluationFunction: OutputEvaluationFunction) {

  /**
   * Validate examples.
   *
   * @param examples a list of examples to validate
   * @param onPrediction an optional callback called for each prediction (args: example, isCorrect)
   * @param saveContributions whether to save the contributions of each input to its output (needed to calculate
   *                          the relevance)
   *
   * @return the percentage of correct predictions
   */
  fun validate(examples: ArrayList<ExampleType>,
               onPrediction: (example: ExampleType, isCorrect: Boolean) -> Unit = {_, _ -> },
               saveContributions: Boolean = false): Double {

    var correctPredictions = 0

    examples.forEach {
      val isCorrect = this.validate(it, saveContributions = saveContributions)

      if (isCorrect) {
        correctPredictions += 1
      }
{'fraction_non_alphanumeric': 0.11207378423700391, 'fraction_numerical': 0.005589714924538848, 'mean_word_length': 3.0258717660292462, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1222358', 'n_tokens_mistral': 950, 'n_tokens_neox': 878, 'n_words': 310}