<commit_before>require 'veritas'

module Veritas
  module Sexp
    # Generator for s-expressions
    module Generator
      # Incomplete sexp formatter operation table
      REGISTRY = {
        Relation::Base                            => [ :base                                       ],
        Relation::Materialized                    => [ :materialized                               ],
        Relation::Operation::Order                => [ :binary,    :order, :operand, :directions   ],
        Relation::Header                          => [ :collect                                    ],
        Relation::Operation::Order::DirectionSet  => [ :collect                                    ],
        Relation::Operation::Order::Ascending     => [ :unary,     :asc,  :attribute               ],
        Relation::Operation::Order::Descending    => [ :unary,     :desc, :attribute               ],
        Relation::Operation::Limit                => [ :binary,    :limit, :operand, :limit        ],
        Relation::Operation::Offset               => [ :binary,    :offset, :operand, :offset      ],
        Relation::Operation::Deletion             => [ :binary,    :delete, :operand, :other       ],
        Relation::Operation::Insertion            => [ :binary,    :insert, :operand, :other       ],
        Relation::Operation::Reverse              => [ :unary,     :reverse                        ],
        Algebra::Difference                       => [ :binary,    :difference                     ],
        Algebra::Extension                        => [ :extend                                     ],
        Algebra::Intersection                     => [ :intersect                                  ],
        Algebra::Join                             => [ :binary,    :join                           ],
        Algebra::Product                          => [ :binary,    :product                        ],
        Algebra::Projection                       => [ :binary,    :project, :operand, :header     ],
        Algebra::Rename                           => [ :binary,    :rename                         ],
        Algebra::Restriction                      => [ :binary,    :restrict, :operand, :predicate ],
        Algebra::Summarization                    => [ :binary,    :summarize                      ],
        Algebra::Union                            => [ :binary,    :union                          ],
        Function::Connective::Conjunction         => [ :binary,    :and                            ],
        Function::Connective::Disjunction         => [ :binary,    :or                             ],
        Function::Connective::Negation            => [ :unary,     :not                            ],
        Function::String::Length                  => [ :unary,     :length                         ],
        Function::Predicate::Equality             => [ :binary,    :eq                             ],
        Function::Predicate::Exclusion            => [ :binary,    :ex                             ],
        Function::Predicate::GreaterThan          => [ :binary,    :gt                             ],
        Function::Predicate::GreaterThanOrEqualTo => [ :binary,    :gte                            ],
        Function::Predicate::Inclusion            => [ :binary,    :in                             ],
        Function::Predicate::Inequality           => [ :binary,    :neq                            ],
        Function::Predicate::LessThan             => [ :binary,    :lt                             ],
        Function::Predicate::LessThanOrEqualTo    => [ :binary,    :lte                            ],
        Function::Predicate::Match                => [ :binary,    :match                          ],
        Function::Predicate::NoMatch              => [ :binary,    :no_match                       ],
        Function::Numeric::Absolute               => [ :binary,    :abs                            ],
        Function::Numeric::Addition               => [ :binary,    :add                            ],
        Function::Numeric::Division               => [ :binary,    :div                            ],
        Function::Numeric::Exponentiation         => [ :binary,    :exp                            ],
        Function::Numeric::Modulo                 => [ :binary,    :mod                            ],
        Function::Numeric::Multiplication         => [ :binary,    :mul                            ],
        Function::Numeric::SquareRoot             => [ :unary,     :sqr                            ],
        Function::Numeric::Subtraction            => [ :binary,    :sub                            ],
        Function::Numeric::UnaryMinus             => [ :unary,     :unary_minus                    ],
        Function::Numeric::UnaryPlus              => [ :unary,     :unary_plus                     ],
        Function::Proposition::Tautology          => [ :static,    :true                           ],
        Function::Proposition::Contradiction      => [ :static,    :false                          ],
        Attribute::String                         => [ :attribute                                  ],
        Attribute::Integer                        => [ :attribute                                  ]
      }

      # Transform veritas relation into s-expression
      #
      # @param [Veritas::Relation] relation
      #
      # @return [Array] 
      #
      # @api private
      #
      def self.visit(relation)
        name, *options = REGISTRY.fetch(relation.class) { return relation }
        send(name, relation, *options)
      end

      # Helper method for materialized s-expressions
      #
      # @param [Veritas::Relation::Materialized] relation
      # @param [Symbol] tag
      #
      # @return [Array]
      #
      # @api private
      #
      def self.materialized(materialized)
        [ :materialized, visit(materialized.header), materialized.to_a.map(&:to_ary) ]
      end
      private_class_method :materialized

      # Helper method for static s-expressions
      #
      # @param [Veritas::Relation] relation
      # @param [Symbol] tag
      #
      # @return [Array]
      #
      # @api private
      #
      def self.static(relation, tag)
        tag
      end
      private_class_method :static

      # Helper for binary s-expressions
      #<commit_msg>Reduce complexity through method extraction<commit_after>require 'veritas'

module Veritas
  module Sexp
    # Generator for s-expressions
    module Generator
      # Incomplete sexp formatter operation table
      REGISTRY = {
        Relation::Base                            => [ :base                                       ],
        Relation::Materialized                    => [ :materialized                               ],
        Relation::Operation::Order                => [ :binary,    :order, :operand, :directions   ],
        Relation::Header                          => [ :collect                                    ],
        Relation::Operation::Order::DirectionSet  => [ :collect                                    ],
        Relation::Operation::Order::Ascending     => [ :unary,     :asc,  :attribute               ],
        Relation::Operation::Order::Descending    => [ :unary,     :desc, :attribute               ],
        Relation::Operation::Limit                => [ :binary,    :limit, :operand, :limit        ],
        Relation::Operation::Offset               => [ :binary,    :offset, :operand, :offset      ],
        Relation::Operation::Deletion             => [ :binary,    :delete, :operand, :other       ],
        Relation::Operation::Insertion            => [ :binary,    :insert, :operand, :other       ],
        Relation::Operation::Reverse              => [ :unary,     :reverse                        ],
        Algebra::Difference                       => [ :binary,    :difference                     ],
        Algebra::Extension                        => [ :extend                                     ],
        Algebra::Intersection                     => [ :intersect                                  ],
        Algebra::Join                             => [ :binary,    :join                           ],
        Algebra::Product                          => [ :binary,    :product                        ],
        Algebra::Projection                       => [ :binary,    :project, :operand, :header     ],
        Algebra::Rename                           => [ :binary,    :rename                         ],
        Algebra::Restriction                      => [ :binary,    :restrict, :operand, :predicate ],
        Algebra::Summarization                    => [ :binary,    :summarize                      ],
        Algebra::Union                            => [ :binary,    :union                          ],
        Function::Connective::Conjunction         => [ :binary,    :and                            ],
        Function::Connective::Disjunction         => [ :binary,    :or                             ],
        Function::Connective::Negation            => [ :unary,     :not                            ],
        Function::String::Length                  => [ :unary,     :length                         ],
        Function::Predicate::Equality             => [ :binary,    :eq                             ],
        Function::Predicate::Exclusion            => [ :binary,    :ex                             ],
        Function::Predicate::GreaterThan          => [ :binary,    :gt                             ],
        Function::Predicate::GreaterThanOrEqualTo => [ :binary,    :gte                            ],
        Function::Predicate::Inclusion            => [ :binary,    :in                             ],
        Function::Predicate::Inequality           => [ :binary,    :neq                            ],
        Function::Predicate::LessThan             => [ :binary,    :lt                             ],
        Function::Predicate::LessThanOrEqualTo    => [ :binary,    :lte                            ],
        Function::Predicate::Match                => [ :binary,    :match                          ],
        Function::Predicate::NoMatch              => [ :binary,    :no_match                       ],
        Function::Numeric::Absolute               => [ :binary,    :abs                            ],
        Function::Numeric::Addition               => [ :binary,    :add                            ],
        Function::Numeric::Division               => [ :binary,    :div                            ],
        Function::Numeric::Exponentiation         => [ :binary,    :exp                            ],
        Function::Numeric::Modulo                 => [ :binary,    :mod                            ],
        Function::Numeric::Multiplication         => [ :binary,    :mul                            ],
        Function::Numeric::SquareRoot             => [ :unary,     :sqr                            ],
        Function::Numeric::Subtraction            => [ :binary,    :sub                            ],
        Function::Numeric::UnaryMinus             => [ :unary,     :unary_minus                    ],
        Function::Numeric::UnaryPlus              => [ :unary,     :unary_plus                     ],
        Function::Proposition::Tautology          => [ :static,    :true                           ],
        Function::Proposition::Contradiction      => [ :static,    :false                          ],
        Attribute::String                         => [ :attribute                                  ],
        Attribute::Integer                        => [ :attribute                                  ]
      }

      # Transform veritas relation into s-expression
      #
      # @param [Veritas::Relation] relation
      #
      # @return [Array] 
      #
      # @api private
      #
      def self.visit(relation)
        name, *options = REGISTRY.fetch(relation.class) { return relation }
        send(name, relation, *options)
      end

      # Helper method for materialized s-expressions
      #
      # @param [Veritas::Relation::Materialized] relation
      # @param [Symbol] tag
      #
      # @return [Array]
      #
      # @api private
      #
      def self.materialized(materialized)
        [ :materialized, visit(materialized.header), tuples(materialized) ]
      end
      private_class_method :materialized

      # Helper method for tuple s-expressions
      #
      # @param [Relation] relation
      #
      # @return [Array]
      #
      # @api private
      #
      def self.tuples(relation)
        relation.to_a.map(&:to_ary)
      end
      private_class_method :tuples

      # Helper method for static s-expressions
      #
      # @param [Veritas::Relation] _relation
      # @param [Symbol] tag
      #
      # @return [Array]
      #
      # @api private
      #
      def self.static(_relation, tag)
        tag
      end
      private_class_method :static

      # Helper for binary s-expressions
      #
{'fraction_non_alphanumeric': 0.11042097998619738, 'fraction_numerical': 0.0, 'mean_word_length': 1.0339987523393637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 103, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2108817', 'n_tokens_mistral': 3037, 'n_tokens_neox': 2946, 'n_words': 516}