# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection' require 'util/util' require 'json/pure' require 'App42_Ruby_API/App42Response' require 'recommend/RecommenderResponseBuilder' require 'recommend/Recommender' require 'recommend/RecommenderSimilarity' require 'bigdecimal' module App42 module Recommend # # Recommendation engine which provides recommendation based on customer id, item id and the preference # of the customer for a particular Item. # # Recommendations can be fetched based on User Similarity which finds similarity based on Users # and Item Similarity which finds similarity based on Items. # # The Recommendation Engine currently supports two types of Similarity Algorithms i.e. EuclideanDistanceSimilarity # and PearsonCorrelationSimilarity. By default when similarity is not specified PearsonCorrelationSimilarity is used e.g. # in the method itemBased(String preferenceFileName, long userId, int howMany), it uses PearsonCorrelationSimilarity. # In the method itemBasedBySimilarity(String similarity, String preferenceFileName, long userId, int howMany) one can # specify which similarity algorithm has to be used e.g. Recommender.EUCLIDEAN_DISTANCE or Recommender.PEARSON_CORRELATION. # Preference file can be loaded using the method load_preference_file(String fileName, String preferenceFilePath, String description) # in csv format. This preference file has to be uploaded once which can be a batch process # The csv format for the file is given below. # customerId, itemId, preference # # e.g. # 1,101,5.0 # 1,102,3.0 # 1,103,2.5 # # 2,101,2.0 # 2,102,2.5 # 2,103,5.0 # 2,104,2.0 # # 3,101,2.5 # 3,104,4.0 # 3,105,4.5 # 3,107,5.0 # # 4,101,5.0 # 4,103,3.0 # 4,104,4.5 # 4,106,4.0 # # 5,101,4.0 # 5,102,3.0 # 5,103,2.0 # 5,104,4.0 # 5,105,3.5 # 5,106,4.0 # The customer Id and item id can be any alphanumaric character(s) and preference values can be in any range. # # If app developers have used the Review Service. The Recommendation Engine can be used in conjunction with Review. # In this case a CSV preference file need not be uploaded. The customerId, itemId and preference will be taken from # Review where customerId is mapped with userName, itemId is mapped with itemId and preference with rating. # The methods for recommendations based on Reviews are part of the Review service # # @see ReviewService # class RecommenderService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "Recommender Service->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "recommend" @version = "1.0" end # # Uploads peference file on the cloud. The preference file should be in CSV format. # This preference file has to be uploaded once which can be a batch process. # New versions of preference file either can be uploaded in a different name or the older one # has to be removed and the uploaded in the same name. # The csv format for the file is given below. # customerId, itemId, preference # # e.g. # 1,101,5.0 # 1,102,3.0 # 1,103,2.5 # # 2,101,2.0 # 2,102,2.5 # 2,103,5.0 # 2,104,2.0 # # 3,101,2.5 # 3,104,4.0 # 3,105,4.5 # 3,107,5.0 # # 4,101,5.0 # 4,103,3.0 # 4,104,4.5 # 4,106,4.0 # # 5,101,4.0 # 5,102,3.0 # 5,103,2.0 # 5,104,4.0 # 5,105,3.5 # 5,106,4.0 # The customer Id and item id can be any alphanumaric character(s) and preference values can be in any range. # If the recommendations have to be done based on Reviews then this file need not be uploaded. # # @param fileName # - Name of the Prefeence File based on which recommendations have to be found # @param preferenceFilePath # - Path of the preference file to be loaded # @param description # - Description of the preference file to be loaded # # @return Returns the uploaded preference file details. # @raise App42Exception # def load_preference_file(preferenceFilePath) puts "load_preference_file Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(preferenceFilePath, "PreferenceFilePath"); if (FileTest.exists?(preferenceFilePath) == false) raise App42Exception.new("The file with the name #{preferenceFilePath} not found") end begin file = preferenceFilePath connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } params = query_params.clone post_params=Hash.new params = params.merge(post_params) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.recommendMultipart(signature, resource_url, query_params, params, preferenceFilePath) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Uploads preference file on the cloud via Stream. The preference file should be in CSV # format. This preference file has to be uploaded once which can be a batch # process. New versions of preference file either can be uploaded in a # different name or the older one has to be removed and the uploaded in the # same name. The csv format for the file is given below. customerId, # itemId, preference e.g. 1,101,5.0 1,102,3.0 1,103,2.5 # # 2,101,2.0 2,102,2.5 2,103,5.0 2,104,2.0 # # 3,101,2.5 3,104,4.0 3,105,4.5 3,107,5.0 # # 4,101,5.0 4,103,3.0 4,104,4.5 4,106,4.0 # # 5,101,4.0 5,102,3.0 5,103,2.0 5,104,4.0 5,105,3.5 5,106,4.0 The customer # Id and item id can be any alphanumeric character(s) and preference values # can be in any range. If the recommendations have to be done based on # Reviews then this file need not be uploaded. # # @param inputStream # - InputStream of the file to load. # # @return App42Response object. # # @raise App42Exception # def load_preference_file_stream(inputStream) puts "load_preference_file stream Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new if (FileTest.exists?(inputStream) == false) raise App42Exception.new("The file with the name #{inputStream} not found") end begin connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } params = query_params.clone signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.recommendMultipartStream(signature, resource_url, query_params, params, inputStream) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # User based recommendations based on Neighborhood. Recommendations are found based on similar users in the Neighborhood # of the given user. The size of the neighborhood can be found. # # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param userId # - The user Id for whom recommendations have to be found # @param # - size Size of the Neighborhood # @param howMany # - Specifies that how many recommendations have to be found # # @returns Recommendations # # @raise App42Exception # def user_based_neighborhood(userId, size, howMany) puts "userBasedNeighborhood Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(userId, "User Id"); util.throwExceptionIfNullOrBlank(size, "Size"); util.throwExceptionIfNullOrBlank(howMany, "HowMany"); begin connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userId", "" + (userId.to_i).to_s); params.store("size", "" + (size.to_i).to_s); params.store("howMany", "" + (howMany.to_i).to_s); signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/userBasedNeighborhood/#{userId}/#{size}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # User based neighborhood recommendations based on Threshold. Recommendations are found based on Threshold # where thereshold represents similarity threshold where user are at least that similar. # Threshold values can vary from -1 to 1 # # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param userId # - The user Id for whom recommendations have to be found # @param threshold # - Threshold size. Values can vary from -1 to 1 # @param howMany # - Specifies that how many recommendations have to be found # # @returns Recommendations # # @raise App42Exception # def user_based_threshold(userId, threshold, howMany) puts "userBasedThreshold Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(userId, "User Id"); util.throwExceptionIfNullOrBlank(threshold, "Threshold"); util.throwExceptionIfNullOrBlank(howMany, "HowMany"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userId", "" + (userId.to_i).to_s); params.store("threshold", "" + (threshold.to_f).to_s); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/userBasedThreshold/#{userId}/#{(threshold.to_f).to_s}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # User based recommendations based on Neighborhood and Similarity. Recommendations and found based on the similar users in the Neighborhood with # the specified Similarity Algorithm. Algorithim can be specified using the constants Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION # # @param recommenderSimilarity # - Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param userId # - The user Id for whom recommendations have to be found # @param size # - Size of the Neighborhood # @param howMany # - Specifies that how many recommendations have to be found # # @returns Recommendations # # @raise App42Exception # def user_based_neighborhood_by_similarity(recommenderSimilarity, userId, size, howMany) puts "userBasedNeighborhoodBySimilarity Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(recommenderSimilarity, "recommenderSimilarity"); util.throwExceptionIfNullOrBlank(userId, "User Id"); util.throwExceptionIfNullOrBlank(size, "Size"); util.throwExceptionIfNullOrBlank(howMany, "HowMany"); rs = App42::Recommend::RecommenderSimilarity.new begin if (RecommenderSimilarity.new.isAvailable(recommenderSimilarity) == nil) raise App42NotFoundException.new("The RecommenderSimilarity"+ recommenderSimilarity+ "does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userId", "" + (userId.to_i).to_s); params.store("size", "" + (size.to_i).to_s); params.store("similarity", "" + recommenderSimilarity); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/userBasedNeighborhood/#{recommenderSimilarity}/#{userId}/#{size}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # User based neighborood recommendations based on Threshold. Recommendations are found based on Threshold # where thereshold represents similarity threshold where user are at least that similar. # Threshold values can vary from -1 to 1 # # @param recommenderSimilarity # - Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param userId # - The user Id for whom recommendations have to be found # @param threshold # - Threshold size. Values can vary from -1 to 1 # @param howMany # - Specifies that how many recommendations have to be found # # @returns Recommendations # # @raise App42Exception # def user_based_neighborhood_by_similarity(recommenderSimilarity, userId, threshold, howMany) puts "userBasedThresholdBySimilarity Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(recommenderSimilarity, "recommenderSimilarity"); util.throwExceptionIfNullOrBlank(userId, "User Id"); util.throwExceptionIfNullOrBlank(threshold, "Threshold"); util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin if (RecommenderSimilarity.new.isAvailable(recommenderSimilarity) == nil) raise App42NotFoundException.new("The RecommenderSimilarity #{recommenderSimilarity} does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("similarity", "" + recommenderSimilarity); params.store("userId", "" + (userId.to_i).to_s); params.store("threshold", "" + (threshold.to_f).to_s); params.store("howMany", "" + (howMany.to_i).to_s); signature = util.sign(@secret_key, params) puts "Signature #{signature}" resource_url = "#{@version}/#{@resource}/userBasedThreshold/#{recommenderSimilarity}/#{userId}/#{(threshold.to_f)}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # Item based recommendations. Recommendations and found based item similarity # of the given user. The size of the neighborhood can be found. # # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param userId # - The user Id for whom recommendations have to be found # @param howMany # - Specifies that how many recommendations have to be found # # @returns Recommendations # # @raise App42Exception # def item_based(userId, howMany) puts "itemBased Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(userId, "User Id"); util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userId", "" + (userId.to_i).to_s); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/itemBased/#{userId}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # Recommendations based on SlopeOne Algorithm # # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param userId # - The user Id for whom recommendations have to be found # @param howMany # - Specifies that how many recommendations have to be found # # @return Recommendation object # # @raise App42Exception # def slope_one(userId, howMany) puts "slopeOne Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(userId, "User Id"); util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userId", "" + (userId.to_i).to_s); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/slopeOne/#{userId}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # User based neighborood recommendations based on Threshold for all Users. Recommendations are found based on Threshold # where thereshold represents similarity threshold where user are at least that similar. # Threshold values can vary from -1 to 1 # # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param threshold # - Threshold size. Values can vary from -1 to 1 # @param howMany # - Specifies that how many recommendations have to be found # # @return Recommendations for all Users # # @raise App42Exception # def user_based_threshold_for_all(threshold, howMany) puts "userBasedThresholdForAll Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(threshold, "Threshold"); util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("threshold", "" + (threshold.to_f).to_s); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/userBasedThreshold/all/#{(threshold.to_f).to_s}/#{(howMany.to_i).to_s}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # User based recommendations based on Neighborhood and Similarity for all Users. Recommendations and found based similar users in the Neighborhood with # the specified Similarity Algorithm. Algorithim can be specified using the constants Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION # # @param recommenderSimilarity # - Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param size # - Size of the Neighborhood # @param howMany # - Specifies that how many recommendations have to be found # # @return Recommendations for all Users # # @raise App42Exception # def user_based_neighborhood_by_similarity_for_all(recommenderSimilarity, size, howMany) puts "userBasedNeighborhoodBySimilarityForAll Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(recommenderSimilarity, "recommenderSimilarity"); util.throwExceptionIfNullOrBlank(size, "Size"); util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin if (RecommenderSimilarity.new.isAvailable(recommenderSimilarity) == nil) raise App42NotFoundException.new("The RecommenderSimilarity"+ recommenderSimilarity+ "does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("size", "" + (size.to_i).to_s); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/userBasedNeighborhood/all/#{size}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # User based neighborood recommendations based on Threshold for All. Recommendations are found based on Threshold # where thereshold represents similarity threshold where user are at least that similar. # Threshold values can vary from -1 to 1 # # @param recommenderSimilarity # - Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param threshold # - Threshold size. Values can vary from -1 to 1 # @param howMany # - Specifies that how many recommendations have to be found # # @return Recommendations for All # # @raise App42Exception # def user_based_threshold_by_similarity_for_all(recommenderSimilarity, threshold, howMany) puts "userBasedThresholdBySimilarityForAll Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(recommenderSimilarity, "recommenderSimilarity"); util.throwExceptionIfNullOrBlank(threshold, "Threshold"); util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin if (RecommenderSimilarity.new.isAvailable(recommenderSimilarity) == nil) raise App42NotFoundException.new("The RecommenderSimilarity"+ recommenderSimilarity+ "does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("similarity", "" + recommenderSimilarity); params.store("threshold", "" + (threshold.to_f).to_s); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/userBasedThreshold/all/#{recommenderSimilarity}/#{(threshold.to_f)}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # Item based recommendations for all Users. Recommendations and found based item similarity # of the given user. The size of the neighborhood can be found. # # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param howMany # - Specifies that how many recommendations have to be found # # @return Recommendations for all Users # # @raise App42Exception # def item_based_for_all(howMany) puts "itemBasedForAll Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/itemBased/all/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # Item based recommendations for all Users. Recommendations and found based one item similarity. Similarity algorithm can be specified. # of the given user. The size of the neighborhood can be found. # # @param recommenderSimilarity # - Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param howMany # - Specifies that how many recommendations have to be found # # @return Recommendations for all Users # # @raise App42Exception # def item_based_by_Similarity_for_all(recommenderSimilarity, howMany) puts "itemBasedBySimilarityForAll Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(recommenderSimilarity, "recommenderSimilarity"); util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin if (RecommenderSimilarity.new.isAvailable(recommenderSimilarity) == nil) raise App42NotFoundException.new("The RecommenderSimilarity"+ recommenderSimilarity+ "does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("similarity", "" + recommenderSimilarity); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/itemBased/all/#{recommenderSimilarity}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # Recommendations based on SlopeOne Algorithm for all Users # # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param howMany # - Specifies that how many recommendations have to be found # # @return Recommendations for all Users # # @raise App42Exception # def slope_one_for_all(howMany) puts "itemBasedBySimilarityForAll Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/slopeOne/all/#{(howMany.to_i).to_s}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # Item based recommendations. Recommendations and found based one item similarity. Similarity algorithm can be specified. # of the given user. The size of the neighborhood can be found. # # @param recommenderSimilarity # - Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param userId # - The user Id for whom recommendations have to be found # @param howMany # - Specifies that how many recommendations have to be found # # @return Recommendation Object # # @raise App42Exception # def item_based_by_similarity(recommenderSimilarity, userId, howMany) puts "itemBasedBySimilarity Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany) util.throwExceptionIfNullOrBlank(recommenderSimilarity, "recommenderSimilarity"); util.throwExceptionIfNullOrBlank(userId, "User Id"); util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin if (RecommenderSimilarity.new.isAvailable(recommenderSimilarity) == nil) raise App42NotFoundException.new("The RecommenderSimilarity"+ recommenderSimilarity+ "does not Exist "); end connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("similarity", "" + recommenderSimilarity); params.store("userId", "" + (userId.to_i).to_s); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/itemBased/#{recommenderSimilarity}/#{userId}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # User based recommendations based on Neighborhood for All Users. Recommendations and found based similar users in the Neighborhood # of the given user. The size of the neighborhood can be found. # # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # @param size # - Size of the Neighborhood # @param howMany # - Specifies that how many recommendations have to be found # # @return Recommendations for All users # # @raise App42Exception # def user_based_neighborhood_for_all(size, howMany) puts "userBasedNeighborhoodForAll Called " puts "Base url #{@base_url}" response = nil; recommenderObj = nil; recommenderObj = Recommender.new util = Util.new util.validateHowMany(howMany); util.throwExceptionIfNullOrBlank(size, "Size"); util.throwExceptionIfNullOrBlank(howMany, "How Many"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("size", "" + (size.to_i).to_s); params.store("howMany", "" + (howMany.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/userBasedNeighborhood/all/#{size}/#{howMany}" response = connection.get(signature, resource_url, query_params) recommend = RecommenderResponseBuilder.new() recommenderObj = recommend.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return recommenderObj end # # Delete existing preference file. # # @param peferenceFileName # - Name of the Prefeence File based on which recommendations have to be found # # @return File name which has been removed # # @raise App42Exception # def delete_all_preferences() puts "deletePreferenceFile Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/deleteAllPreferences/" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Add or Update preference list on the cloud. # # @param preferenceDataList # - List of PreferenceData which contains customerId, itemId, # preference # # @return App42Response object # # @raise App42Exception # # def add_or_update_preference(preferenceDataList) puts "addOrUpdatePreference Called " response = nil responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(preferenceDataList, "preferenceDataList") begin connection = App42::Connection::RESTConnection.new(@base_url) post_params=Hash.new preferenceDataArray = Array.new() for preferenceData in preferenceDataList do post_params.store("userId", preferenceData.userId()) post_params.store("itemId", preferenceData.itemId()) post_params.store("preference", preferenceData.preference()) preferenceDataArray.push(post_params) end preference = Array.new body = {'app42' => { "preferences" => preference, "preference"=> preferenceDataArray }}.to_json puts "Body #{body}" params = Hash.new query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/addOrUpdatePreference" response = connection.post(signature, resource_url, query_params, body) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end end end end