slide_rule 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: 006868b9cca39594d800bf786a49dc9fd3b733cd
4
+ data.tar.gz: 09282b0628ea97599120d36e53602b9ec7819a9a
5
+ SHA512:
6
+ metadata.gz: 22599f361ab8bd3a3cb514f786b3e314ef4e8f7c20c74732cf225e471722d0c8262c67fb57b4b3fc30905ba34c5719ca773e7185f363ce4911b5cdf2bcff0216
7
+ data.tar.gz: 3a712c9ff14c6d08e26847ff0d8308524528a00e2e10eed5ffd03884affee53acc8c42b498e3d03d2555ad6d648f3ab25991bbdd27fe4d96317cbef53f8791de
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .rspec
2
+ **.DS_Store
3
+ Guardfile
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ inherit_from:
2
+ - .rubocop_todo.yml
3
+
4
+ AllCops:
5
+ DisplayCopNames: true
6
+ Exclude:
7
+ - 'bin/**/*'
8
+ - 'coverage/**/*'
9
+ - 'db/**/*'
10
+ - 'vendor/**/*'
11
+ - 'lib/core_ext/pkcs12.rb'
12
+
13
+ Metrics/LineLength:
14
+ Exclude:
15
+ - 'spec/**/*'
16
+
17
+ Style/Documentation:
18
+ Enabled: false
19
+
20
+ Metrics/LineLength:
21
+ Max: 100
22
+ Exclude:
23
+ - 'spec/**/*'
24
+ - 'slide_rule.gemspec'
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,41 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2015-11-02 16:38:15 -0700 using RuboCop version 0.34.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ Metrics/AbcSize:
11
+ Max: 21
12
+
13
+ # Offense count: 2
14
+ Metrics/CyclomaticComplexity:
15
+ Max: 8
16
+
17
+ # Offense count: 1
18
+ # Configuration parameters: CountComments.
19
+ Metrics/MethodLength:
20
+ Max: 11
21
+
22
+ # Offense count: 1
23
+ Metrics/PerceivedComplexity:
24
+ Max: 8
25
+
26
+ # Offense count: 1
27
+ Style/MultilineBlockChain:
28
+ Exclude:
29
+ - 'lib/slide_rule/distance_calculator.rb'
30
+
31
+ # Offense count: 1
32
+ # Configuration parameters: EnforcedStyle, MinBodyLength, SupportedStyles.
33
+ Style/Next:
34
+ Exclude:
35
+ - 'lib/slide_rule/distance_calculator.rb'
36
+
37
+ # Offense count: 1
38
+ # Configuration parameters: Methods.
39
+ Style/SingleLineBlockParams:
40
+ Exclude:
41
+ - 'lib/slide_rule/distance_calculator.rb'
@@ -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
+ source 'http://gems.moneydesktop.com'
3
+
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ slide_rule (0.0.1)
5
+ levee (~> 2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ remote: http://gems.moneydesktop.com/
10
+ specs:
11
+ ast (2.1.0)
12
+ astrolabe (1.3.1)
13
+ parser (~> 2.2)
14
+ coderay (1.1.0)
15
+ diff-lcs (1.2.5)
16
+ ffi (1.9.10)
17
+ ffi (1.9.10-java)
18
+ levee (2.0.0)
19
+ ffi
20
+ method_source (0.8.2)
21
+ parser (2.2.2.6)
22
+ ast (>= 1.1, < 3.0)
23
+ powerpack (0.1.1)
24
+ pry (0.10.1)
25
+ coderay (~> 1.1.0)
26
+ method_source (~> 0.8.1)
27
+ slop (~> 3.4)
28
+ pry (0.10.1-java)
29
+ coderay (~> 1.1.0)
30
+ method_source (~> 0.8.1)
31
+ slop (~> 3.4)
32
+ spoon (~> 0.0)
33
+ rainbow (2.0.0)
34
+ rake (10.4.2)
35
+ rspec (3.3.0)
36
+ rspec-core (~> 3.3.0)
37
+ rspec-expectations (~> 3.3.0)
38
+ rspec-mocks (~> 3.3.0)
39
+ rspec-core (3.3.2)
40
+ rspec-support (~> 3.3.0)
41
+ rspec-expectations (3.3.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.3.0)
44
+ rspec-mocks (3.3.2)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.3.0)
47
+ rspec-support (3.3.0)
48
+ rubocop (0.34.1)
49
+ astrolabe (~> 1.3)
50
+ parser (>= 2.2.2.5, < 3.0)
51
+ powerpack (~> 0.1)
52
+ rainbow (>= 1.99.1, < 3.0)
53
+ ruby-progressbar (~> 1.4)
54
+ ruby-progressbar (1.7.5)
55
+ slop (3.6.0)
56
+ spoon (0.0.4)
57
+ ffi
58
+
59
+ PLATFORMS
60
+ java
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ pry (~> 0)
65
+ rake (~> 10)
66
+ rspec (~> 3)
67
+ rubocop (~> 0)
68
+ slide_rule!
69
+
70
+ BUNDLED WITH
71
+ 1.10.6
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Matt Nichols
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 all
13
+ 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 THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # SlideRule
2
+ Ruby object distance calculator
3
+
4
+ ##Distance
5
+
6
+ The distance between 2 objects is calculated as a float between 0.0 (perfect match) and 1.0 (farthest distance).
7
+
8
+ All calculators result in a distance between 0.0 and 1.0.
9
+
10
+ Total distance = sum of all weighted distances.
11
+
12
+ Weights are normalized as follows:
13
+
14
+ weight = weight * (% of sum of all weights)
15
+
16
+ _Note: weights are assumed to be equal if not provided_
17
+
18
+ #API
19
+
20
+ ##Describe the field calculators
21
+
22
+ Each field to be considered in the distance calculation should be described
23
+ with a calculation method and weight(optional)
24
+
25
+ Valid calculators:
26
+
27
+ * day_of_month (this needs to be factored into configurable date_recurrence)
28
+ * float_range_distance
29
+
30
+ ```ruby
31
+ distance_rules = {
32
+ :description => {
33
+ :weight => 0.80,
34
+ :type => :levenshtein,
35
+ },
36
+ :date => {
37
+ :weight => 0.90,
38
+ :type => :day_of_month,
39
+ },
40
+ }
41
+ ```
42
+
43
+ ## Build the object distance calculator
44
+
45
+ ```ruby
46
+ matcher = ::SlideRule::DistanceCalculator.new(distance_rules)
47
+ ```
48
+
49
+ ## Use the calculator
50
+
51
+ ```ruby
52
+ # Example data
53
+ example = ::ExampleTransaction.new(
54
+ :amount => 25.00,
55
+ :date => '2015-02-05',
56
+ :description => 'Audible.com'
57
+ )
58
+ example2 = ::ExampleTransaction.new(
59
+ :amount => 250.00,
60
+ :date => '2015-02-16',
61
+ :description => 'Wells Fargo Dealer Services'
62
+ )
63
+ candidate = ::ExampleTransaction.new(
64
+ :amount => 25.00,
65
+ :date => '2015-06-08',
66
+ :description => 'Audible Inc'
67
+ )
68
+
69
+ # Calculate distance
70
+ matcher.calculate_distance(example, candidate)
71
+ => 0.2318181818181818
72
+
73
+ # Find closest match to examples in an array
74
+ matcher.closest_match(candidate, [example, example2])
75
+ => example
76
+
77
+ # Find closest match to examples in an array, using a threshold
78
+ matcher.closest_match(candidate, [example, example2], 0.2)
79
+ => example
80
+
81
+ ```
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,80 @@
1
+ module SlideRule
2
+ class DistanceCalculator
3
+ def initialize(rules)
4
+ @rules = normalize_weights(rules)
5
+ end
6
+
7
+ # TODO: Figure this out. Very inefficient!
8
+ # Probably should calculate using a suggestions algorythm
9
+ def group(array)
10
+ array.map do |item|
11
+ {
12
+ item: item,
13
+ matches: (array - [item]).map do |item_cmp|
14
+ {
15
+ match: item_cmp,
16
+ distance: calculate_distance(item, item_cmp)
17
+ }
18
+ end
19
+ }
20
+ end
21
+ end
22
+
23
+ def closest_match(obj, array, threshold)
24
+ matches(obj, array, threshold).sort { |match| match[:distance] }.first
25
+ end
26
+
27
+ def matches(obj, array, threshold)
28
+ array.map do |item|
29
+ distance = calculate_distance(obj, item)
30
+ if distance < threshold
31
+ {
32
+ item: item,
33
+ distance: distance
34
+ }
35
+ end
36
+ end.compact
37
+ end
38
+
39
+ # All distances represented as 0..1
40
+ # 0 = perfect match
41
+ # 1 = farthest distance
42
+ # Calculate distances for all attributes, then apply weight, and average them out.
43
+ # Rules format:
44
+ # {
45
+ # :attribute_name => {
46
+ # :weight => 0.90,
47
+ # :type => :distance_calculator,
48
+ # }
49
+ # }
50
+ def calculate_distance(i1, i2)
51
+ @rules.map do |attribute, rule|
52
+ val1 = i1.send(attribute)
53
+ val2 = i2.send(attribute)
54
+ calculator = get_calculator(rule[:type])
55
+ calculator.calculate(val1, val2).to_f * rule[:weight]
56
+ end.inject(0.0) { |calculated_dist, dist| calculated_dist + dist }
57
+ end
58
+
59
+ def get_calculator(calculator)
60
+ return calculator.new if calculator.is_a?(Class)
61
+
62
+ klass_name = "#{calculator.to_s.split('_').collect(&:capitalize).join}"
63
+ klass = ::SlideRule::DistanceCalculators.const_get(klass_name)
64
+
65
+ fail ArgumentError, "Unable to find calculator #{klass_name}" if klass.nil?
66
+
67
+ klass.new
68
+ end
69
+
70
+ # Ensures all weights add up to 1.0
71
+ #
72
+ def normalize_weights(rules_hash)
73
+ rules = rules_hash.dup
74
+ weight_total = rules.map { |_attr, rule| rule[:weight] }.reduce(0.0, &:+)
75
+ rules.each do |_attr, rule|
76
+ rule[:weight] = rule[:weight] / weight_total
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,27 @@
1
+ module SlideRule
2
+ module DistanceCalculators
3
+ class DayOfMonth
4
+ MAX_DAYS = 15
5
+
6
+ #
7
+ # Calculates distance using 15 as the max point.
8
+ # Does not take into account the number of days in the actual month being considered.
9
+ #
10
+ def calculate(first, second)
11
+ first = Date.parse(first) unless first.is_a?(::Date) || first.is_a?(::Time)
12
+ second = Date.parse(second) unless second.is_a?(::Date) || second.is_a?(::Time)
13
+
14
+ first = first.to_date if first.is_a?(::Time)
15
+ second = second.to_date if second.is_a?(::Time)
16
+
17
+ difference_in_days(first, second).to_f / MAX_DAYS
18
+ end
19
+
20
+ def difference_in_days(first, second)
21
+ distance = (first.mday - second.mday).abs
22
+ return distance if distance <= MAX_DAYS
23
+ MAX_DAYS - (distance - MAX_DAYS)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ module SlideRule
2
+ module DistanceCalculators
3
+ class DayOfYear
4
+ DAYS_IN_YEAR = 365
5
+
6
+ def calculate(date_1, date_2)
7
+ date_1 = Date.parse(date_1) unless date_1.is_a?(::Date) || date_1.is_a?(::Time)
8
+ date_2 = Date.parse(date_2) unless date_2.is_a?(::Date) || date_2.is_a?(::Time)
9
+
10
+ date_1 = date_1.to_date if date_1.is_a?(::Time)
11
+ date_2 = date_2.to_date if date_2.is_a?(::Time)
12
+
13
+ days_apart = (date_1.mjd - date_2.mjd).abs
14
+
15
+ return 1 if days_apart >= DAYS_IN_YEAR
16
+
17
+ distance = days_apart.to_f / DAYS_IN_YEAR
18
+ distance.round(2)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ require 'levee'
2
+
3
+ module SlideRule
4
+ module DistanceCalculators
5
+ class Levenshtein
6
+ def calculate(first, second)
7
+ distance = Levee.distance(first, second).to_f
8
+
9
+ # Lower bound is difference in length
10
+ # distance = matrix.last.last.to_f - (first.length - second.length).abs
11
+
12
+ # Upper bound is length of longest string
13
+ # This will decrease distance more for longer strings.
14
+ distance / ([first.length, second.length].max)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module SlideRule
2
+ VERSION = "0.0.1"
3
+ end
data/lib/slide_rule.rb ADDED
@@ -0,0 +1,5 @@
1
+ require_relative './slide_rule/version.rb'
2
+ require_relative './slide_rule/distance_calculator.rb'
3
+ require_relative './slide_rule/distance_calculators/day_of_month.rb'
4
+ require_relative './slide_rule/distance_calculators/day_of_year.rb'
5
+ require_relative './slide_rule/distance_calculators/levenshtein.rb'
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+ require 'slide_rule/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'slide_rule'
7
+ s.version = ::SlideRule::VERSION
8
+ s.authors = %w(mattnichols fergmastaflex)
9
+ s.email = ['dev@mx.com']
10
+ s.homepage = 'https://github.com/mattnichols/slide_rule'
11
+ s.summary = 'Ruby object distance calculator'
12
+ s.description = 'Calculates the distance between 2 arbitrary objects using specified fields and algorithms.'
13
+ s.license = 'MIT'
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.require_paths = ['lib']
17
+
18
+ ##
19
+ # Dependencies
20
+ #
21
+ s.add_runtime_dependency 'levee', '~> 2'
22
+
23
+ ##
24
+ # Development Dependencies
25
+ #
26
+ s.add_development_dependency 'rake', '~> 10'
27
+ s.add_development_dependency 'pry', '~> 0'
28
+ s.add_development_dependency 'rspec', '~> 3'
29
+ s.add_development_dependency 'rubocop', '~> 0'
30
+ end
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slide_rulee.gemspec
4
+ gemspec
@@ -0,0 +1,41 @@
1
+ # SlideRulee
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/slide_rulee`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'slide_rulee'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install slide_rulee
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/slide_rulee. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "slide_rulee"
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
@@ -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
@@ -0,0 +1,3 @@
1
+ module SlideRulee
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "slide_rulee/version"
2
+
3
+ module SlideRulee
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slide_rulee/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slide_rulee"
8
+ spec.version = SlideRulee::VERSION
9
+ spec.authors = ["matthew-nichols"]
10
+ spec.email = ["matthew.nichols@mx.com"]
11
+
12
+ spec.summary = %q{TODO: Write a short summary, because Rubygems requires one.}
13
+ spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "TODO: Put your gem's website or public repo URL here."
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe SlideRulee do
4
+ it 'has a version number' do
5
+ expect(SlideRulee::VERSION).not_to be nil
6
+ end
7
+
8
+ it 'does something useful' do
9
+ expect(false).to eq(true)
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'slide_rulee'
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::SlideRule::DistanceCalculator do
4
+ class ExampleTransaction
5
+ attr_accessor :amount
6
+ attr_accessor :description
7
+ attr_accessor :date
8
+
9
+ def initialize(attrs = {})
10
+ @amount = attrs[:amount]
11
+ @description = attrs[:description]
12
+ @date = attrs[:date]
13
+ end
14
+ end
15
+
16
+ class CustomCalc
17
+ def calculate(_first, _second)
18
+ 0.9
19
+ end
20
+ end
21
+
22
+ let(:examples) do
23
+ [
24
+ ::ExampleTransaction.new(amount: 25.00, date: '2015-02-05', description: 'Audible.com'),
25
+ ::ExampleTransaction.new(amount: 34.89, date: '2015-04-01', description: 'Questar Gas'),
26
+ ::ExampleTransaction.new(amount: 1200.00, date: '2015-05-04', description: 'US Bank'),
27
+ ::ExampleTransaction.new(amount: 560.00, date: '2015-06-17', description: 'Wells Fargo Dealer Services'),
28
+ ::ExampleTransaction.new(amount: 25.44, date: '2015-06-03', description: 'Walmart'),
29
+ ::ExampleTransaction.new(amount: 6.55, date: '2015-06-01', description: 'Taco Bell'),
30
+ ::ExampleTransaction.new(amount: 45.30, date: '2015-06-26', description: 'Shell')
31
+ ]
32
+ end
33
+
34
+ describe '#closest_match' do
35
+ let(:calculator) do
36
+ ::SlideRule::DistanceCalculator.new(
37
+ description: {
38
+ weight: 0.80,
39
+ type: :levenshtein
40
+ },
41
+ date: {
42
+ weight: 0.90,
43
+ type: :day_of_month
44
+ }
45
+ )
46
+ end
47
+
48
+ it 'finds recurring transaction' do
49
+ example = ExampleTransaction.new(description: 'Wells Fargo Dealer SVC', date: '2015-06-17')
50
+ expect(calculator.closest_match(example, examples, 0.2)[:item]).to eq(examples[3])
51
+
52
+ example = ExampleTransaction.new(description: 'Audible.com', date: '2015-06-05')
53
+ expect(calculator.closest_match(example, examples, 0.2)[:item]).to eq(examples[0])
54
+ end
55
+ end
56
+
57
+ describe '#calculate_distance' do
58
+ context 'uses built-in calculator' do
59
+ it 'should calculate perfect match' do
60
+ calculator = ::SlideRule::DistanceCalculator.new(
61
+ description: {
62
+ weight: 1.00,
63
+ type: :levenshtein
64
+ },
65
+ date: {
66
+ weight: 0.50,
67
+ type: :day_of_month
68
+ }
69
+ )
70
+ example = ::ExampleTransaction.new(amount: 25.00, date: '2015-02-05', description: 'Audible.com')
71
+ candidate = ::ExampleTransaction.new(amount: 25.00, date: '2015-06-05', description: 'Audible.com')
72
+ expect(calculator.calculate_distance(example, candidate)).to eq(0.0)
73
+ end
74
+
75
+ it 'should calculate imperfect match' do
76
+ calculator = ::SlideRule::DistanceCalculator.new(
77
+ description: {
78
+ weight: 0.50,
79
+ type: :levenshtein
80
+ },
81
+ date: {
82
+ weight: 0.50,
83
+ type: :day_of_month
84
+ }
85
+ )
86
+ example = ::ExampleTransaction.new(amount: 25.00, date: '2015-02-05', description: 'Audible.com')
87
+ candidate = ::ExampleTransaction.new(amount: 25.00, date: '2015-06-08', description: 'Audible Inc')
88
+
89
+ # <--------------------------------------->
90
+ # Distance Calculation:
91
+ # <--------------------------------------->
92
+ # + Day of month distance = 3 * 0.5 / 15
93
+ # + Levenshtein distance = 4 * 0.5 / 11
94
+ # -----------------------------------------
95
+ # = 0.2318181818181818
96
+ distance = calculator.calculate_distance(example, candidate)
97
+ expect(distance).to eq((3.0 * 0.5 / 15) + (4.0 * 0.5 / 11))
98
+ end
99
+ end
100
+
101
+ context 'uses custom calculator' do
102
+ it 'should load custom calculator' do
103
+ calculator = ::SlideRule::DistanceCalculator.new(
104
+ description: {
105
+ weight: 1.00,
106
+ type: CustomCalc
107
+ }
108
+ )
109
+ example = ::ExampleTransaction.new
110
+ candidate = ::ExampleTransaction.new
111
+
112
+ distance = calculator.calculate_distance(example, candidate)
113
+ expect(distance).to eq(0.9)
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::SlideRule::DistanceCalculators::DayOfMonth do
4
+ describe '#calculate' do
5
+ it 'should give 0 on exact match' do
6
+ expect(described_class.new.calculate('2012-03-19', '2014-08-19')).to eq(0.0)
7
+ end
8
+
9
+ it 'should calculate when date is in the same month' do
10
+ expect(described_class.new.calculate('2012-03-19', '2014-08-22')).to eq(3.0 / 15)
11
+ expect(described_class.new.calculate('2012-03-19', '2014-08-09')).to eq(10.0 / 15)
12
+ end
13
+
14
+ it 'should calculate when date wraps to next month, using 15 as max number of days' do
15
+ expect(described_class.new.calculate('2012-03-30', '2014-04-02')).to eq(2.0 / 15)
16
+ end
17
+ end
18
+
19
+ describe '#difference_in_days' do
20
+ describe 'calculates using 15 day max' do
21
+ [
22
+ ['same day', Date.parse('2012-03-30'), Date.parse('2014-04-30'), 0],
23
+ ['one day after', Date.parse('2012-03-19'), Date.parse('2014-04-20'), 1],
24
+ ['one day before', Date.parse('2012-03-18'), Date.parse('2014-04-19'), 1],
25
+
26
+ ['14 days before', Date.parse('2012-03-15'), Date.parse('2014-04-01'), 14],
27
+ ['14 days after', Date.parse('2012-03-15'), Date.parse('2014-04-01'), 14],
28
+
29
+ ['15 days before', Date.parse('2012-03-16'), Date.parse('2014-04-01'), 15],
30
+ ['15 days after', Date.parse('2012-03-01'), Date.parse('2014-04-16'), 15],
31
+
32
+ ['16 days before', Date.parse('2012-03-17'), Date.parse('2014-04-01'), 14],
33
+ ['16 days after', Date.parse('2012-03-01'), Date.parse('2014-04-17'), 14],
34
+
35
+ ['last day of month and first of next month (30 day month)', Date.parse('2012-04-30'), Date.parse('2014-05-01'), 1],
36
+
37
+ ## Not sure how to account for these
38
+ ['last day of month and first of next month (leap year)', Date.parse('2012-02-29'), Date.parse('2014-04-01'), 2], # should be 1 day?
39
+ ['last day of month and first of next month (31 day month)', Date.parse('2012-03-31'), Date.parse('2014-04-01'), 0], # should be 1 day?
40
+ ['last day of month and first of next month (28 day month)', Date.parse('2012-02-28'), Date.parse('2014-04-01'), 3], # should be 1 day?
41
+ ].each do |example|
42
+ it "when dates are #{example[0]}" do
43
+ expect(described_class.new.difference_in_days(example[1], example[2])).to eq(example[3])
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::SlideRule::DistanceCalculators::DayOfYear do
4
+ context 'when dates are an exact match' do
5
+ it 'should return a 0 distance' do
6
+ expect(described_class.new.calculate('2015-10-8', '2015-10-8')).to eq(0.0)
7
+ end
8
+ end
9
+
10
+ context 'when dates are more than a year apart' do
11
+ it 'should return a 1 distance' do
12
+ expect(described_class.new.calculate('2015-10-8', '2016-10-8')).to eq(1)
13
+ end
14
+ end
15
+
16
+ context 'when dates are in the same year but different' do
17
+ it 'should return a calculated distance distance' do
18
+ expect(described_class.new.calculate('2015-10-8', '2015-11-8')).to eq(0.08)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::SlideRule::DistanceCalculators::Levenshtein do
4
+ it 'should calculate perfect match' do
5
+ expect(described_class.new.calculate('this is a test', 'this is a test')).to eq(0.0)
6
+ end
7
+
8
+ it 'should calculate distance as distance divided by length of longest string' do
9
+ expect(described_class.new.calculate('this is a test', 'this is a test!')).to eq(1.0 / 15)
10
+ end
11
+ end
@@ -0,0 +1,97 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+
20
+ require_relative '../lib/slide_rule.rb'
21
+
22
+ RSpec.configure do |config|
23
+ # rspec-expectations config goes here. You can use an alternate
24
+ # assertion/expectation library such as wrong or the stdlib/minitest
25
+ # assertions if you prefer.
26
+ config.expect_with :rspec do |expectations|
27
+ # This option will default to `true` in RSpec 4. It makes the `description`
28
+ # and `failure_message` of custom matchers include text for helper methods
29
+ # defined using `chain`, e.g.:
30
+ # be_bigger_than(2).and_smaller_than(4).description
31
+ # # => "be bigger than 2 and smaller than 4"
32
+ # ...rather than:
33
+ # # => "be bigger than 2"
34
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
+ end
36
+
37
+ # rspec-mocks config goes here. You can use an alternate test double
38
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
39
+ config.mock_with :rspec do |mocks|
40
+ # Prevents you from mocking or stubbing a method that does not exist on
41
+ # a real object. This is generally recommended, and will default to
42
+ # `true` in RSpec 4.
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+
46
+ # The settings below are suggested to provide a good initial experience
47
+ # with RSpec, but feel free to customize to your heart's content.
48
+ # # These two settings work together to allow you to limit a spec run
49
+ # # to individual examples or groups you care about by tagging them with
50
+ # # `:focus` metadata. When nothing is tagged with `:focus`, all examples
51
+ # # get run.
52
+ # config.filter_run :focus
53
+ # config.run_all_when_everything_filtered = true
54
+ #
55
+ # # Allows RSpec to persist some state between runs in order to support
56
+ # # the `--only-failures` and `--next-failure` CLI options. We recommend
57
+ # # you configure your source control system to ignore this file.
58
+ # config.example_status_persistence_file_path = "spec/examples.txt"
59
+ #
60
+ # # Limits the available syntax to the non-monkey patched syntax that is
61
+ # # recommended. For more details, see:
62
+ # # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
63
+ # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
64
+ # # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
65
+ # config.disable_monkey_patching!
66
+ #
67
+ # # This setting enables warnings. It's recommended, but in some cases may
68
+ # # be too noisy due to issues in dependencies.
69
+ # config.warnings = true
70
+ #
71
+ # # Many RSpec users commonly either run the entire suite or an individual
72
+ # # file, and it's useful to allow more verbose output when running an
73
+ # # individual spec file.
74
+ # if config.files_to_run.one?
75
+ # # Use the documentation formatter for detailed output,
76
+ # # unless a formatter has already been configured
77
+ # # (e.g. via a command-line flag).
78
+ # config.default_formatter = 'doc'
79
+ # end
80
+ #
81
+ # # Print the 10 slowest examples and example groups at the
82
+ # # end of the spec run, to help surface which specs are running
83
+ # # particularly slow.
84
+ # config.profile_examples = 10
85
+ #
86
+ # # Run specs in random order to surface order dependencies. If you find an
87
+ # # order dependency and want to debug it, you can fix the order by providing
88
+ # # the seed, which is printed after each run.
89
+ # # --seed 1234
90
+ # config.order = :random
91
+ #
92
+ # # Seed global randomization in this process using the `--seed` CLI option.
93
+ # # Setting this allows you to use `--seed` to deterministically reproduce
94
+ # # test failures related to randomization by passing the same `--seed` value
95
+ # # as the one that triggered the failure.
96
+ # Kernel.srand config.seed
97
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slide_rule
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - mattnichols
8
+ - fergmastaflex
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-11-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: levee
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10'
42
+ - !ruby/object:Gem::Dependency
43
+ name: pry
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rubocop
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: Calculates the distance between 2 arbitrary objects using specified fields
85
+ and algorithms.
86
+ email:
87
+ - dev@mx.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rubocop.yml"
94
+ - ".rubocop_todo.yml"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - Gemfile.lock
98
+ - LICENSE
99
+ - README.md
100
+ - Rakefile
101
+ - lib/slide_rule.rb
102
+ - lib/slide_rule/distance_calculator.rb
103
+ - lib/slide_rule/distance_calculators/day_of_month.rb
104
+ - lib/slide_rule/distance_calculators/day_of_year.rb
105
+ - lib/slide_rule/distance_calculators/levenshtein.rb
106
+ - lib/slide_rule/version.rb
107
+ - slide_rule.gemspec
108
+ - slide_rulee/.gitignore
109
+ - slide_rulee/.travis.yml
110
+ - slide_rulee/Gemfile
111
+ - slide_rulee/README.md
112
+ - slide_rulee/bin/console
113
+ - slide_rulee/bin/setup
114
+ - slide_rulee/lib/slide_rulee.rb
115
+ - slide_rulee/lib/slide_rulee/version.rb
116
+ - slide_rulee/slide_rulee.gemspec
117
+ - slide_rulee/spec/slide_rulee_spec.rb
118
+ - slide_rulee/spec/spec_helper.rb
119
+ - spec/slide_rule/distance_calculator_spec.rb
120
+ - spec/slide_rule/distance_calculators/day_of_month_spec.rb
121
+ - spec/slide_rule/distance_calculators/day_of_year_spec.rb
122
+ - spec/slide_rule/distance_calculators/levenshtein_spec.rb
123
+ - spec/spec_helper.rb
124
+ homepage: https://github.com/mattnichols/slide_rule
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.6
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Ruby object distance calculator
148
+ test_files:
149
+ - spec/slide_rule/distance_calculator_spec.rb
150
+ - spec/slide_rule/distance_calculators/day_of_month_spec.rb
151
+ - spec/slide_rule/distance_calculators/day_of_year_spec.rb
152
+ - spec/slide_rule/distance_calculators/levenshtein_spec.rb
153
+ - spec/spec_helper.rb