<commit_before>module SPARQL; module Algebra
  class Operator
    ##
    # A (non-standard) SPARQL relational `<=>` comparison operator.
    #
    # @see http://www.w3.org/TR/rdf-sparql-query/#OperatorMapping
    # @see http://www.w3.org/TR/xpath-functions/#func-compare
    class Compare < Operator::Binary
      NAME = :<=>

      ##
      # Returns -1, 0, or 1, depending on whether the first operand is
      # respectively less than, equal to, or greater than the second
      # operand.
      #
      # SPARQL also fixes an order between some kinds of RDF terms that would not otherwise be ordered:
      #
      #   (Lowest) no value assigned to the variable or expression in this solution.
      #   Blank nodes
      #   IRIs
      #   RDF literals
      #
      # @param  [RDF::Literal] left
      #   a literal
      # @param  [RDF::Literal] right
      #   a literal
      # @return [RDF::Literal::Integer] `-1`, `0`, or `1`
      # @raise  [TypeError] if either operand is not a literal
      def apply(left, right)
        case
        # @see http://www.w3.org/TR/rdf-sparql-query/#OperatorMapping
        # @see http://www.w3.org/TR/rdf-sparql-query/#modOrderBy
        when left.is_a?(RDF::Literal) && right.is_a?(RDF::Literal)
          case
          # @see http://www.w3.org/TR/xpath-functions/#string-compare
          # @see http://www.w3.org/TR/xpath-functions/#comp.numeric
          # @see http://www.w3.org/TR/xpath-functions/#op.boolean
          # @see http://www.w3.org/TR/xpath-functions/#comp.duration.datetime
          when (left.simple? && right.simple?) ||
               (left.is_a?(RDF::Literal::Numeric) && right.is_a?(RDF::Literal::Numeric)) ||
               (left.datatype == right.datatype && left.language == right.language)
            RDF::Literal(left.send(self.class.const_get(:NAME), right))

          #when left.datatype == right.datatype && left.language == right.language
          #  RDF::Literal(left.send(self.class.const_get(:NAME), right))            

          else raise TypeError, "unable to compare #{left.inspect} and #{right.inspect}"
          end
          
        when left.is_a?(RDF::URI) && right.is_a?(RDF::URI)
          # Pairs of IRIs are ordered by comparing them as simple literals.
          RDF::Literal(RDF::Literal(left.to_s).send(self.class.const_get(:NAME), RDF::Literal(right.to_s)))
        when left.is_a?(RDF::Node) && right.is_a?(RDF::Node)
          # BNode comparison is undefined.
          RDF::Literal(0)
        when left.is_a?(RDF::Node) && right.is_a?(RDF::Term)
          RDF::Literal(-1)
        when right.is_a?(RDF::Node) && left.is_a?(RDF::Term)
          RDF::Literal(1)
        when left.is_a?(RDF::URI) && right.is_a?(RDF::Term)
          RDF::Literal(-1)
        when right.is_a?(RDF::URI) && left.is_a?(RDF::Term)
          RDF::Literal(1)
        else raise TypeError, "expected two RDF::Term operands, but got #{left.inspect} and #{right.inspect}"
        end
      end
    end # Compare
  end # Operator
end; end # SPARQL::Algebra<commit_msg>Add compare case for plain literals and xsd:string literals.<commit_after>module SPARQL; module Algebra
  class Operator
    ##
    # A (non-standard) SPARQL relational `<=>` comparison operator.
    #
    # @see http://www.w3.org/TR/rdf-sparql-query/#OperatorMapping
    # @see http://www.w3.org/TR/xpath-functions/#func-compare
    class Compare < Operator::Binary
      NAME = :<=>

      ##
      # Returns -1, 0, or 1, depending on whether the first operand is
      # respectively less than, equal to, or greater than the second
      # operand.
      #
      # SPARQL also fixes an order between some kinds of RDF terms that would not otherwise be ordered:
      #
      #   (Lowest) no value assigned to the variable or expression in this solution.
      #   Blank nodes
      #   IRIs
      #   RDF literals
      #
      # @param  [RDF::Literal] left
      #   a literal
      # @param  [RDF::Literal] right
      #   a literal
      # @return [RDF::Literal::Integer] `-1`, `0`, or `1`
      # @raise  [TypeError] if either operand is not a literal
      def apply(left, right)
        case
        # @see http://www.w3.org/TR/rdf-sparql-query/#OperatorMapping
        # @see http://www.w3.org/TR/rdf-sparql-query/#modOrderBy
        when left.is_a?(RDF::Literal) && right.is_a?(RDF::Literal)
          case
          # @see http://www.w3.org/TR/xpath-functions/#string-compare
          # @see http://www.w3.org/TR/xpath-functions/#comp.numeric
          # @see http://www.w3.org/TR/xpath-functions/#op.boolean
          # @see http://www.w3.org/TR/xpath-functions/#comp.duration.datetime
          when (left.simple? && right.simple?) ||
               (left.is_a?(RDF::Literal::Numeric) && right.is_a?(RDF::Literal::Numeric)) ||
               (left.datatype == right.datatype && left.language == right.language)
            RDF::Literal(left.send(self.class.const_get(:NAME), right))

          # A plain literal is lower than an RDF literal with type xsd:string of the same lexical form.
          when left.simple? && right.is_a?(RDF::Literal::String) && left.value == right.value
            RDF::Literal(-1)
          when right.simple? && left.is_a?(RDF::Literal::String) && right.value == left.value
            RDF::Literal(-1)

          else raise TypeError, "unable to compare #{left.inspect} and #{right.inspect}"
          end
          
        when left.is_a?(RDF::URI) && right.is_a?(RDF::URI)
          # Pairs of IRIs are ordered by comparing them as simple literals.
          RDF::Literal(RDF::Literal(left.to_s).send(self.class.const_get(:NAME), RDF::Literal(right.to_s)))
        when left.is_a?(RDF::Node) && right.is_a?(RDF::Node)
          # BNode comparison is undefined.
          RDF::Literal(0)
        when left.nil? && right.nil?
          RDF::Literal(0)
        
        # SPARQL also fixes an order between some kinds of RDF terms that would not otherwise be ordered:
        # 2. Blank nodes
        when left.is_a?(RDF::Node) && right.is_a?(RDF::Term)
          RDF::Literal(-1)
        when right.is_a?(RDF::Node) && left.is_a?(RDF::Term)
          RDF::Literal(1)

        # 3. IRIs
        when left.is_a?(RDF::URI) && right.is_a?(RDF::Term)
          RDF::Literal(-1)
        when right.is_a?(RDF::URI) && left.is_a?(RDF::Term)
          RDF::Literal(1)
        else raise TypeError, "expected two RDF::Term operands, but got #{left.inspect} and #{right.inspect}"
        end
      end
    end # Compare
  end # Operator
end; end # SPARQL::Algebra
{'fraction_non_alphanumeric': 0.14444953428004276, 'fraction_numerical': 0.006565887921820125, 'mean_word_length': 2.3384301732925588, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 16, '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': '1870492', 'n_tokens_mistral': 2156, 'n_tokens_neox': 2161, 'n_words': 477}