<commit_before>module DataMapper
  module Is
    module SlopeOne

      # Add the slope one recommendation algorithm to a given resource
      #
      # @example
      #
      #   class Post
      #     include DataMapper::Resource
      #
      #     property :id,      Serial
      #     property :name,    String
      #
      #     is :slope_one
      #   end
      #
      # @param [Hash] options(optional)
      #   A hash with options
      # @option options [String] :rater_model
      #   Stringified name of the DataMapper model that should become the rater
      #   default: User
      # @option options [String] :rating_model
      #   Stringified name of the DataMapper model that stores the rater's ratings 
      #   default: #{Resource}Rating
      # @option options [String] :diff_model
      #   Stringified name of the DataMapper model that stores the rating differentials
      #   default: #{Resource}Diff
      # @option options [Symbol] :rating_property
      #   The property that will be used when generating differentials
      #   default: score
      # @option options [Boolean] - If true then inserting resources and ratings will not
      #   trigger diff updates. This is useful if you plan to batch process
      #   ratings offline.
      #   default:false
      # @option options [String] :rating_type
      #   Can be Integer or Float
      #   default: Float
      # @option options [Integer] :rating_precision
      #   Only applicable to Float type
      #   default: 3
      # @option options [Integer] :rating_scale
      #   Only applicable to Float type
      #   default: 2
      #
      # @api public
      def is_slope_one(opts={})

        extend  DataMapper::Is::SlopeOne::ClassMethods
        include DataMapper::Is::SlopeOne::InstanceMethods

        inf             = DataMapper::Inflector
        resource_model  = self
        rater_model     = inf.constantize( opts[:rater_model]    || "User" )
        diff_model      = inf.classify( opts[:diff_model]        || "#{name}Diff" )
        rating_property = inf.underscore( opts[:rating_property] || "score" )
        rating_model    = inf.classify(  opts[:rating_model]     || "#{inf.underscore(name)}_ratings" ) 

        class << self; attr_reader :slope_one_options; end

        o = @slope_one_options = { 
          :resource_table   => inf.tableize(name),
          :resource_key     => inf.foreign_key(name),
          :rater_table      => inf.tableize(rater_model),
          :rater_key        => inf.foreign_key(rater_model),
          :rating_table     => inf.tableize(rating_model),
          :diff_table       => inf.tableize(diff_model),
          :diff_model       => diff_model,
          :rating_property  => rating_property,
          :rating_type      => "Float",
          :rating_precision => 3,
          :rating_scale     => 2
        }.merge(opts)

        rating_property_attributes = o[:rating_type] == "Float" ? 
          [Float, :precision => o[:rating_precision], :scale => o[:rating_scale], :default => 0.0] : 
            [Integer, :default => 0]

        # Create the diff model

        diff_model = ::Object.const_set(diff_model, Class.new(::Object) )
        diff_model.class_eval do
          include ::DataMapper::Resource unless ancestors.include?(::DataMapper::Resource)
          property :count, Integer, :required => true, :min => 0, :default => 0
          property :sum, *rating_property_attributes
          belongs_to :source, resource_model.to_s, :child_key => [:source_id], :key => true
          belongs_to :target, resource_model.to_s, :child_key => [:target_id], :key => true
        end

        # Create or enhance the rating model
        
        rating_model = begin ::Object.const_get(rating_model); rescue ::Object.const_set(rating_model, Class.new(::Object) ); end
        rating_model.class_eval do
          include ::DataMapper::Resource unless ancestors.include?(::DataMapper::Resource)
          property rating_property, *rating_property_attributes
          belongs_to inf.underscore(rater_model),    :key => true
          belongs_to inf.underscore(resource_model), :key => true
          define_singleton_method :slope_one_options do resource_model.slope_one_options; end
          extend ::DataMapper::Is::SlopeOne::Rating
        end

        # Setup relationships

        has n, inf.tableize(rating_model), :constraint => :destroy<commit_msg>Fix rescue clause<commit_after>module DataMapper
  module Is
    module SlopeOne

      # Add the slope one recommendation algorithm to a given resource
      #
      # @example
      #
      #   class Post
      #     include DataMapper::Resource
      #
      #     property :id,      Serial
      #     property :name,    String
      #
      #     is :slope_one
      #   end
      #
      # @param [Hash] options(optional)
      #   A hash with options
      # @option options [String] :rater_model
      #   Stringified name of the DataMapper model that should become the rater
      #   default: User
      # @option options [String] :rating_model
      #   Stringified name of the DataMapper model that stores the rater's ratings 
      #   default: #{Resource}Rating
      # @option options [String] :diff_model
      #   Stringified name of the DataMapper model that stores the rating differentials
      #   default: #{Resource}Diff
      # @option options [Symbol] :rating_property
      #   The property that will be used when generating differentials
      #   default: score
      # @option options [Boolean] - If true then inserting resources and ratings will not
      #   trigger diff updates. This is useful if you plan to batch process
      #   ratings offline.
      #   default:false
      # @option options [String] :rating_type
      #   Can be Integer or Float
      #   default: Float
      # @option options [Integer] :rating_precision
      #   Only applicable to Float type
      #   default: 3
      # @option options [Integer] :rating_scale
      #   Only applicable to Float type
      #   default: 2
      #
      # @api public
      def is_slope_one(opts={})

        extend  DataMapper::Is::SlopeOne::ClassMethods
        include DataMapper::Is::SlopeOne::InstanceMethods

        inf             = DataMapper::Inflector
        resource_model  = self
        rater_model     = inf.constantize( opts[:rater_model]    || "User" )
        diff_model      = inf.classify( opts[:diff_model]        || "#{name}Diff" )
        rating_property = inf.underscore( opts[:rating_property] || "score" )
        rating_model    = inf.classify(  opts[:rating_model]     || "#{inf.underscore(name)}_ratings" ) 

        class << self; attr_reader :slope_one_options; end

        o = @slope_one_options = { 
          :resource_table   => inf.tableize(name),
          :resource_key     => inf.foreign_key(name),
          :rater_table      => inf.tableize(rater_model),
          :rater_key        => inf.foreign_key(rater_model),
          :rating_table     => inf.tableize(rating_model),
          :diff_table       => inf.tableize(diff_model),
          :diff_model       => diff_model,
          :rating_property  => rating_property,
          :rating_type      => "Float",
          :rating_precision => 3,
          :rating_scale     => 2
        }.merge(opts)

        rating_property_attributes = o[:rating_type] == "Float" ? 
          [Float, :precision => o[:rating_precision], :scale => o[:rating_scale], :default => 0.0] : 
            [Integer, :default => 0]

        # Create the diff model

        diff_model = ::Object.const_set(diff_model, Class.new(::Object) )
        diff_model.class_eval do
          include ::DataMapper::Resource unless ancestors.include?(::DataMapper::Resource)
          property :count, Integer, :required => true, :min => 0, :default => 0
          property :sum, *rating_property_attributes
          belongs_to :source, resource_model.to_s, :child_key => [:source_id], :key => true
          belongs_to :target, resource_model.to_s, :child_key => [:target_id], :key => true
        end

        # Create or enhance the rating model
        
        rating_model = ::Object.const_get(rating_model) rescue ::Object.const_set(rating_model, Class.new(::Object) )
        rating_model.class_eval do
          include ::DataMapper::Resource unless ancestors.include?(::DataMapper::Resource)
          property rating_property, *rating_property_attributes
          belongs_to inf.underscore(rater_model),    :key => true
          belongs_to inf.underscore(resource_model), :key => true
          define_singleton_method :slope_one_options do resource_model.slope_one_options; end
          extend ::DataMapper::Is::SlopeOne::Rating
        end

        # Setup relationships

        has n, inf.tableize(rating_model), :constraint => :destroy
{'fraction_non_alphanumeric': 0.0985947416137806, 'fraction_numerical': 0.002039891205802357, 'mean_word_length': 2.3799310608962085, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 53, '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': '5979694', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2434, 'n_words': 637}