<commit_before># coding: utf-8

module Ryodo
  class RuleSet

    def initialize
      @tree = {}
      build!
    end

    def build!
      Ryodo::SuffixList.list.each do |line|

        line.each.with_index do |node_name, idx|

          stopOK    = node_name == line.last
          exception = node_name[0] == "!"
          node_name = node_name[1..-1] if exception
          children  = {}
          node      = Ryodo::Rule.new(exception, stopOK, children)

          if idx > 0
            end_idx = idx - 1
            parent = select_rule(line[0..end_idx])
            parent.children[node_name] = node unless parent.children[node_name]

          else
            @tree[node_name] = node unless @tree[node_name]
          end

        end

      end
    end

    def select_rule(rule_path)
      if rule_path[-1]
        if rule_path[0..-2].empty?
          @tree[rule_path[-1]]
        else
          rule = select_rule(rule_path[0..-2]) and rule.children[rule_path[-1]]
        end
      end
    end

    def match(path)
      suffix, domain, match = [], [], nil

      until match || path.empty?
        match  = select_rule(path) || select_rule(path.dup.fill("*",-1))
        match  = nil if match && !match.is_suffix?
        domain.unshift path.pop
        suffix = path
      end

      suffix.push(domain.shift) if match && !match.exception

      # only if match has no children with domain and domain is present
      if match && !match.children.keys.include?(domain[0]) && domain[0]
        [ suffix, [domain.shift], domain ]
      end
    end

  end
end
<commit_msg>Reorder condition statement<commit_after># coding: utf-8

module Ryodo
  class RuleSet

    def initialize
      @tree = {}
      build!
    end

    def build!
      Ryodo::SuffixList.list.each do |line|

        line.each.with_index do |node_name, idx|

          stopOK    = node_name == line.last
          exception = node_name[0] == "!"
          node_name = node_name[1..-1] if exception
          children  = {}
          node      = Ryodo::Rule.new(exception, stopOK, children)

          if idx > 0
            end_idx = idx - 1
            parent = select_rule(line[0..end_idx])
            parent.children[node_name] = node unless parent.children[node_name]

          else
            @tree[node_name] = node unless @tree[node_name]
          end

        end

      end
    end

    def select_rule(rule_path)
      if rule_path[-1]
        if rule_path[0..-2].empty?
          @tree[rule_path[-1]]
        else
          rule = select_rule(rule_path[0..-2]) and rule.children[rule_path[-1]]
        end
      end
    end

    def match(path)
      suffix, domain, match = [], [], nil

      until match || path.empty?
        match  = select_rule(path) || select_rule(path.dup.fill("*",-1))
        match  = nil if match && !match.is_suffix?
        domain.unshift path.pop
        suffix = path
      end

      suffix.push(domain.shift) if match && !match.exception

      # only if match has no children with domain and domain is present
      if match && domain[0] && !match.children.keys.include?(domain[0])
        [ suffix, [domain.shift], domain ]
      end
    end

  end
end

{'fraction_non_alphanumeric': 0.11236660389202763, 'fraction_numerical': 0.010671688637790333, 'mean_word_length': 1.8328888888888888, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1739394', 'n_tokens_mistral': 1006, 'n_tokens_neox': 973, 'n_words': 242}