avsd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bee7f10608a60d9182ab32aee8f010312b983525
4
+ data.tar.gz: 698a2edb8bc3086a7683eb1973706665590353aa
5
+ SHA512:
6
+ metadata.gz: 45b83e80eb117f1070a6de2c28db55b2ec474b32a35672aa75cfdea02c56f561d0abfc165d0b82ccc163cb01a9011abc6ab199a90f7f091b387ada1a36e5955d
7
+ data.tar.gz: bd7eebcfd5f33b9c9366b7553c82c3f07501f0583d94e2407024ba60c7957ec074f3d3ef8ed1d5939ddc1bcdebb58874405c8ea23c8fc452337c493ee845c878
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in avsd.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 arcoyk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Avsd
2
+
3
+ Avsd is AVSD Recommend Algorithm implimatation on Ruby gem. AVSD Recommend Algorithm recommends sets of items based on multiple exisiting sets of items.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'avsd'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install avsd
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ avsd = Avsd.new [['carotte','potate','beaf','curry paste'],['beaf','potates','oregano'],['oregano', 'tomato', 'garlic']]
25
+ avsd.sample 3
26
+ # [{"tomato"=>6, "garlic"=>7, "curry paste"=>3}, [2.0, 6.0, 6.0], 4.666666666666667, 1.8856180831641267]
27
+ # Here the creativity is 1.8 / 4.6 = 0.3
28
+ ```
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/arcoyk/avsd.
39
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/avsd.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'avsd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "avsd"
8
+ spec.version = Avsd::VERSION
9
+
10
+ spec.authors = ["Yui Kita"]
11
+ spec.email = ["yuikita21@gmail.com"]
12
+
13
+ spec.summary = %q{ Avsd recommend engine }
14
+ spec.description = %q{ Avsd recommends items based on input items. }
15
+ spec.homepage = "http://github.com/arcoyk/avsd"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "avsd"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/avsd.rb ADDED
@@ -0,0 +1,165 @@
1
+ require "rubygems"
2
+ require "sqlite3"
3
+ require "matrix"
4
+ require "priority_queue"
5
+
6
+
7
+ class Matrix
8
+ def []=(i, j, x)
9
+ @rows[i][j] = x
10
+ end
11
+
12
+ def fold
13
+ for col in 0..self.column_size - 1
14
+ for row in col..self.row_size - 1
15
+ sum = self[row, col] + self[col, row]
16
+ self[row, col] = sum
17
+ self[col, row] = sum
18
+ end
19
+ end
20
+ self
21
+ end
22
+
23
+ def show
24
+ puts [self.column_size, self.row_size].inspect
25
+ for col in 0..self.column_size - 1
26
+ for row in 0..self.row_size - 1
27
+ print self[row, col] == nil ? "n" : self[row, col]
28
+ print " "
29
+ end
30
+ puts
31
+ end
32
+ self
33
+ end
34
+
35
+ def mirror_diagonal
36
+ self.fold
37
+ for col in 0..self.column_size - 1
38
+ for row in 0..self.row_size - 1
39
+ self[col, row] /= 2.0
40
+ end
41
+ end
42
+ self
43
+ end
44
+
45
+ def zero_diagonal
46
+ for col in 0..self.column_size - 1
47
+ self[col, col] = 0
48
+ end
49
+ self
50
+ end
51
+
52
+ def flip
53
+ m = self.flatten.max + 1
54
+ for col in 0..self.column_size - 1
55
+ for row in 0..self.row_size - 1
56
+ next if self[col, row] == 0
57
+ self[col, row] -= m
58
+ self[col, row] *= -1
59
+ end
60
+ end
61
+ self
62
+ end
63
+
64
+ def flatten
65
+ arr = []
66
+ for col in 0..self.column_size - 1
67
+ for row in 0..self.row_size - 1
68
+ arr << self[col, row]
69
+ end
70
+ end
71
+ arr
72
+ end
73
+ end
74
+
75
+ class Array
76
+ def sum
77
+ self.reduce(0) { |accum, i| accum + i }
78
+ end
79
+
80
+ def mean
81
+ self.sum / self.length.to_f
82
+ end
83
+
84
+ def sample_variance
85
+ m = self.mean
86
+ sum = self.reduce(0) { |accum, i| accum + (i - m) ** 2 }
87
+ sum / self.length.to_f
88
+ end
89
+
90
+ def sd
91
+ Math.sqrt(self.sample_variance)
92
+ end
93
+ end
94
+
95
+ class Avsd
96
+ def initialize(records)
97
+ @labels = records.flatten.uniq
98
+ @co_mat = Matrix.unit(labels.length)
99
+ self.band_matrix records
100
+ self.g_short_mat
101
+ end
102
+
103
+ def band label1, label2
104
+ @co_mat[@labels.index(label1), @labels.index(label2)] += 1
105
+ end
106
+
107
+ def band_matrix records
108
+ @labels.each do |label_a|
109
+ records.each do |record|
110
+ if record.include? label_a
111
+ record.each do |label_b|
112
+ band(label_a, label_b)
113
+ end
114
+ end
115
+ end
116
+ end
117
+ @co_mat.mirror_diagonal.zero_diagonal
118
+ end
119
+
120
+ def g_short_mat
121
+ @short_mat = Matrix.zero(@co_mat.column_size)
122
+ @short_mat = @short_mat.collect { |x| x = nil }
123
+ for s_id in 0..@co_mat.column_size - 1
124
+ q = PriorityQueue.new
125
+ q[s_id] = 0
126
+ while not q.empty?
127
+ f = q.delete_min
128
+ @short_mat[s_id, f[0]] = f[1]
129
+ @short_mat.inspect
130
+ @co_mat.row(f[0]).each_with_index do |val, i|
131
+ next if i == f[0] or val == 0 or @short_mat[s_id, i] != nil
132
+ if q[i] == nil or q[i] < f[1] + val
133
+ q[i] = f[1] + val
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+ def sample num
141
+ arr = Array.new(@short_mat.column_size) { |idx| idx }
142
+ set = arr.sample num
143
+ arr = []
144
+ set.length.times do |i|
145
+ for k in i + 1..set.length - 1
146
+ arr << [set[i], set[k]]
147
+ end
148
+ end
149
+ sampled_labels = Hash.new
150
+ set.each do |id|
151
+ sampled_labels[@labels[id]] = id
152
+ end
153
+ vals = []
154
+ arr.each do |id_set|
155
+ vals << @short_mat[id_set[0], id_set[1]]
156
+ end
157
+ [sampled_labels, vals, vals.mean, vals.sd].inspect
158
+ end
159
+
160
+ attr_accessor :short_mat, :co_mat, :labels
161
+ end
162
+
163
+
164
+ # avsd = Avsd::Avsd.new [['carotte','potate','beaf','curry paste'],['beaf','potates','oregano'],['oregano', 'tomato', 'garlic']]
165
+ # puts avsd.sample 3
@@ -0,0 +1,3 @@
1
+ module Avsd
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: avsd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yui Kita
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: " Avsd recommends items based on input items. "
42
+ email:
43
+ - yuikita21@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - avsd.gemspec
57
+ - bin/console
58
+ - bin/setup
59
+ - lib/avsd.rb
60
+ - lib/avsd/version.rb
61
+ - pkg/avsd-0.1.0.gem
62
+ homepage: http://github.com/arcoyk/avsd
63
+ licenses: []
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.4.8
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Avsd recommend engine
85
+ test_files: []