Class: App42::Recommend::RecommenderService

Inherits:
Object
  • Object
show all
Defined in:
lib/recommend/RecommenderService.rb

Overview

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 Also:

  • ReviewService

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret_key, base_url) ⇒ RecommenderService

this is a constructor that takes

Parameters:

  • apiKey
  • secretKey
  • baseURL


77
78
79
80
81
82
83
84
# File 'lib/recommend/RecommenderService.rb', line 77

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

Instance Method Details

#add_or_update_preference(preferenceDataList) ⇒ Object

Add or Update preference list on the cloud.

preference

Parameters:

  • preferenceDataList
    • List of PreferenceData which contains customerId, itemId,

Returns:

  • App42Response object

Raises:

  • App42Exception



1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
# File 'lib/recommend/RecommenderService.rb', line 1045

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

#delete_all_preferencesObject

Delete existing preference file.

Parameters:

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

Returns:

  • File name which has been removed

Raises:

  • App42Exception



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
# File 'lib/recommend/RecommenderService.rb', line 1003

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

#item_based(userId, howMany) ⇒ Object

Item based recommendations. Recommendations and found based item similarity of the given user. The size of the neighborhood can be found.

Parameters:

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • userId
    • The user Id for whom recommendations have to be found

  • howMany
    • Specifies that how many recommendations have to be found

Raises:

  • App42Exception



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/recommend/RecommenderService.rb', line 483

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

#item_based_by_similarity(recommenderSimilarity, userId, howMany) ⇒ Object

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.

Parameters:

  • recommenderSimilarity
    • Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • userId
    • The user Id for whom recommendations have to be found

  • howMany
    • Specifies that how many recommendations have to be found

Returns:

  • Recommendation Object

Raises:

  • App42Exception



901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
# File 'lib/recommend/RecommenderService.rb', line 901

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

#item_based_by_Similarity_for_all(recommenderSimilarity, howMany) ⇒ Object

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.

Parameters:

  • recommenderSimilarity
    • Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • howMany
    • Specifies that how many recommendations have to be found

Returns:

  • Recommendations for all Users

Raises:

  • App42Exception



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/recommend/RecommenderService.rb', line 799

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

#item_based_for_all(howMany) ⇒ Object

Item based recommendations for all Users. Recommendations and found based item similarity of the given user. The size of the neighborhood can be found.

Parameters:

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • howMany
    • Specifies that how many recommendations have to be found

Returns:

  • Recommendations for all Users

Raises:

  • App42Exception



750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
# File 'lib/recommend/RecommenderService.rb', line 750

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

#load_preference_file(preferenceFilePath) ⇒ Object

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.

Parameters:

  • fileName
    • Name of the Prefeence File based on which recommendations have to be found

  • preferenceFilePath
    • Path of the preference file to be loaded

  • description
    • Description of the preference file to be loaded

Returns:

  • Returns the uploaded preference file details.

Raises:

  • App42Exception



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/recommend/RecommenderService.rb', line 134

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

#load_preference_file_stream(inputStream) ⇒ Object

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.

Parameters:

  • inputStream
    • InputStream of the file to load.

Returns:

  • App42Response object.

Raises:

  • App42Exception



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/recommend/RecommenderService.rb', line 197

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

#slope_one(userId, howMany) ⇒ Object

Recommendations based on SlopeOne Algorithm

Parameters:

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • userId
    • The user Id for whom recommendations have to be found

  • howMany
    • Specifies that how many recommendations have to be found

Returns:

  • Recommendation object

Raises:

  • App42Exception



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/recommend/RecommenderService.rb', line 533

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

#slope_one_for_all(howMany) ⇒ Object

Recommendations based on SlopeOne Algorithm for all Users

Parameters:

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • howMany
    • Specifies that how many recommendations have to be found

Returns:

  • Recommendations for all Users

Raises:

  • App42Exception



850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'lib/recommend/RecommenderService.rb', line 850

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

#user_based_neighborhood(userId, size, howMany) ⇒ Object

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.

Parameters:

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • userId
    • The user Id for whom recommendations have to be found

  • -

    size Size of the Neighborhood

  • howMany
    • Specifies that how many recommendations have to be found

Raises:

  • App42Exception



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/recommend/RecommenderService.rb', line 247

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_by_similarity(recommenderSimilarity, userId, threshold, howMany) ⇒ Object

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

Parameters:

  • recommenderSimilarity
    • Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • userId
    • The user Id for whom recommendations have to be found

  • threshold
    • Threshold size. Values can vary from -1 to 1

  • howMany
    • Specifies that how many recommendations have to be found

Raises:

  • App42Exception



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/recommend/RecommenderService.rb', line 360

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_neighborhood_by_similarity_for_all(recommenderSimilarity, size, howMany) ⇒ Object

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

Parameters:

  • recommenderSimilarity
    • Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • size
    • Size of the Neighborhood

  • howMany
    • Specifies that how many recommendations have to be found

Returns:

  • Recommendations for all Users

Raises:

  • App42Exception



638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
# File 'lib/recommend/RecommenderService.rb', line 638

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_neighborhood_for_all(size, howMany) ⇒ Object

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.

Parameters:

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • size
    • Size of the Neighborhood

  • howMany
    • Specifies that how many recommendations have to be found

Returns:

  • Recommendations for All users

Raises:

  • App42Exception



957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/recommend/RecommenderService.rb', line 957

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

#user_based_threshold(userId, threshold, howMany) ⇒ Object

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

Parameters:

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • userId
    • The user Id for whom recommendations have to be found

  • threshold
    • Threshold size. Values can vary from -1 to 1

  • howMany
    • Specifies that how many recommendations have to be found

Raises:

  • App42Exception



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/recommend/RecommenderService.rb', line 303

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_threshold_by_similarity_for_all(recommenderSimilarity, threshold, howMany) ⇒ Object

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

Parameters:

  • recommenderSimilarity
    • Similarity algorithm e.g. Recommender.EUCLIDEAN_DISTANCE and Recommender.PEARSON_CORRELATION

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • threshold
    • Threshold size. Values can vary from -1 to 1

  • howMany
    • Specifies that how many recommendations have to be found

Returns:

  • Recommendations for All

Raises:

  • App42Exception



696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/recommend/RecommenderService.rb', line 696

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

#user_based_threshold_for_all(threshold, howMany) ⇒ Object

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

Parameters:

  • peferenceFileName
    • Name of the Prefeence File based on which recommendations have to be found

  • threshold
    • Threshold size. Values can vary from -1 to 1

  • howMany
    • Specifies that how many recommendations have to be found

Returns:

  • Recommendations for all Users

Raises:

  • App42Exception



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# File 'lib/recommend/RecommenderService.rb', line 585

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