<commit_before>require 'node'

class DecisionTree

  attr_reader :root

  def initialize(tree_xml)
    @id = tree_xml.attribute('id')
    @root = Node.new(tree_xml.xpath('TreeModel/Node'))
  end

  def decide(features)
    curr = @root
    while curr.decision.score == ''
      prev = curr
      curr = step(curr, features)
      return if didnt_step?(curr, prev)
    end

    curr.decision
  end

  private

  def step(curr, features)
    curr = step_on_true(curr, features, 0)
    curr = step_on_true(curr, features, 1)
    curr = step_on_true(curr, features, 2)
    curr
  end

  def step_on_true(curr, features, num)
    return curr.children[num] if curr.children && curr.children[num] && curr.children[num].true?(features)
    curr
  end

  def didnt_step?(curr, prev)
    return false if (prev.pred != curr.pred)
    Scoruby.logger.error "Null tree: #{@id}, bad feature: #{curr.children[0].pred.field }"
    true<commit_msg>fix decision tree while logic<commit_after>require 'node'

class DecisionTree

  attr_reader :root

  def initialize(tree_xml)
    @id = tree_xml.attribute('id')
    @root = Node.new(tree_xml.xpath('TreeModel/Node'))
  end

  def decide(features)
    curr = @root
    while curr.children[0]
      prev = curr
      curr = step(curr, features)
      return if didnt_step?(curr, prev)
    end

    curr.decision
  end

  private

  def step(curr, features)
    curr = step_on_true(curr, features, 0)
    curr = step_on_true(curr, features, 1)
    curr = step_on_true(curr, features, 2)
    curr
  end

  def step_on_true(curr, features, num)
    return curr.children[num] if curr.children && curr.children[num] && curr.children[num].true?(features)
    curr
  end

  def didnt_step?(curr, prev)
    return false if (prev.pred != curr.pred)
    Scoruby.logger.error "Null tree: #{@id}, bad feature: #{curr.children[0].pred.field }"
    true
{'fraction_non_alphanumeric': 0.12158543117300483, 'fraction_numerical': 0.0048205677557579, 'mean_word_length': 3.2648401826484017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '6325375', 'n_tokens_mistral': 655, 'n_tokens_neox': 643, 'n_words': 157}