recommendations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Victor Rodrigues
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Recommendations
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'recommendations'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install recommendations
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.push 'lib'
4
+
5
+ require "recommendations"
6
+
7
+ Recommendations.redis.flushdb
8
+
9
+ i1 = Recommendations::Item.new("1", violento: 100, romantico: 0, acao: 50)
10
+ i2 = Recommendations::Item.new("2", violento: 50, romantico: 0, acao: 50) # 1.50
11
+ i3 = Recommendations::Item.new("3", violento: 100, romantico: 100, acao: 0) # 1.150, 2.200
12
+ i4 = Recommendations::Item.new("4", violento: 70, romantico: 30, acao: 20) # 1.90, 2.80
13
+
14
+ items = [i1, i2, i3, i4]
15
+
16
+ items.each(&:save!)
17
+
18
+ items.each do |item|
19
+ item = Recommendations::Item.find(item.id)
20
+ puts "item: #{item.id}"
21
+ item.update_similars
22
+ puts "similars: #{item.similars}"
23
+ end
@@ -0,0 +1,23 @@
1
+ require "redis"
2
+
3
+ require "recommendations/version"
4
+ require "recommendations/configuration"
5
+ require "recommendations/item"
6
+
7
+ module Recommendations
8
+ extend self
9
+
10
+ def configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def configure(&block)
15
+ configuration.instance_eval(&block)
16
+ end
17
+
18
+ def redis
19
+ @redis ||= begin
20
+ configuration.redis ? Redis.new(configuration.redis) : Redis.new
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ module Recommendations
2
+ class Configuration
3
+ attr_accessor :redis
4
+ attr_writer :similar_count, :tolerance
5
+
6
+ def similar_count
7
+ @similar_count || 10
8
+ end
9
+
10
+ def tolerance
11
+ @tolerance || 30
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,44 @@
1
+ module Recommendations
2
+ class Item
3
+ attr_accessor :id
4
+ attr_writer :categories
5
+
6
+ def initialize(id, categories)
7
+ @id, @categories = id, categories
8
+ end
9
+
10
+ def categories
11
+ @categories || {}
12
+ end
13
+
14
+ def similars(count = Recommendations.configuration.similar_count)
15
+ Recommendations.redis.zrange("recommendations:similars:#{id}", 0, count - 1)
16
+ end
17
+
18
+ def update_similars
19
+ Recommendations.redis.del("recommendations:similars:#{id}")
20
+
21
+ categories.each do |category, score|
22
+ items = Recommendations.redis.hgetall("recommendations:categories:#{category}")
23
+
24
+ items.reject {|k, v| k == id}.each do |item, item_score|
25
+ difference = (Float(item_score) - Float(score)).abs
26
+ Recommendations.redis.zincrby("recommendations:similars:#{id}", difference, item)
27
+ end
28
+ end
29
+ end
30
+
31
+ def save!
32
+ Recommendations.redis.hmset("recommendations:item:#{id}", *categories.flatten)
33
+
34
+ categories.each do |category, score|
35
+ Recommendations.redis.hset("recommendations:categories:#{category}", id, score)
36
+ end
37
+ end
38
+
39
+ def self.find(id)
40
+ categories = Recommendations.redis.hgetall("recommendations:item:#{id}")
41
+ new(id, categories)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Recommendations
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/recommendations/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Victor Rodrigues"]
6
+ gem.email = ["victorc.rodrigues@gmail.com"]
7
+ gem.description = "recommendations between similar items"
8
+ gem.summary = ""
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "recommendations"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Recommendations::VERSION
17
+ gem.add_development_dependency "rspec", "~> 2.10"
18
+ gem.add_runtime_dependency "redis"
19
+ end
@@ -0,0 +1,5 @@
1
+ require "spec_helper"
2
+
3
+ describe Recommendations do
4
+
5
+ end
@@ -0,0 +1 @@
1
+ require "rspec"
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: recommendations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Victor Rodrigues
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-16 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70243608433720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.10'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70243608433720
25
+ - !ruby/object:Gem::Dependency
26
+ name: redis
27
+ requirement: &70243608433280 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70243608433280
36
+ description: recommendations between similar items
37
+ email:
38
+ - victorc.rodrigues@gmail.com
39
+ executables:
40
+ - console
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - bin/console
50
+ - lib/recommendations.rb
51
+ - lib/recommendations/configuration.rb
52
+ - lib/recommendations/item.rb
53
+ - lib/recommendations/version.rb
54
+ - recommendations.gemspec
55
+ - spec/recommendations_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: ''
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.15
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: ''
81
+ test_files:
82
+ - spec/recommendations_spec.rb
83
+ - spec/spec_helper.rb