rkneufeld-fuzzy-realty 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 3
4
- :patch: 0
4
+ :patch: 1
data/lib/fuzzy_realty.rb CHANGED
@@ -4,6 +4,9 @@ require 'weights.rb'
4
4
  require 'rulebase.rb'
5
5
  require 'rulebase.rb'
6
6
 
7
+ # If set this flag will turn on debugging printouts
8
+ #$debug = true;
9
+
7
10
  module FuzzyRealty
8
11
  class ExpertSystem
9
12
  def self.scores(listings, query)
data/lib/rulebase.rb CHANGED
@@ -9,14 +9,14 @@ module FuzzyRealty
9
9
  # reduced factor.
10
10
  :price =>
11
11
  lambda do |listing,param|
12
- puts "Called price"
12
+ puts "Called price" if $debug
13
13
  actual,desired = listing.price.to_f, param.desired
14
14
  result = if (desired*0.90..desired*1.05) === actual
15
15
  1.0
16
16
  else
17
17
  1 - ((desired - actual) / actual).abs
18
18
  end
19
- puts result
19
+ puts result if $debug
20
20
  result
21
21
  end,
22
22
 
@@ -24,15 +24,15 @@ module FuzzyRealty
24
24
  ## Currently just return 1 if exact, 0 otherwise
25
25
  :location =>
26
26
  lambda do |listing,param|
27
- puts "Called location"
27
+ puts "Called location" if $debug
28
28
  # Perform a quick lookup (i.e. FuzzyRealty::LOCN[:A][:A] => 1.0)
29
- puts FuzzyRealty::LOCN[param.desired.to_sym][listing.location.to_sym]
29
+ puts FuzzyRealty::LOCN[param.desired.to_sym][listing.location.to_sym] if $debug
30
30
  FuzzyRealty::LOCN[param.desired.to_sym][listing.location.to_sym]
31
31
  end,
32
32
 
33
33
  :sqft =>
34
34
  lambda do |listing,param|
35
- puts "Called sqft"
35
+ puts "Called sqft" if $debug
36
36
  actual, desired = listing.sqft, param.desired
37
37
  result = if (actual + 50) >= desired
38
38
  1.0
@@ -43,17 +43,17 @@ module FuzzyRealty
43
43
  else
44
44
  0.0
45
45
  end
46
- puts result
46
+ puts result if $debug
47
47
  result
48
48
  end,
49
49
  # Style's follow lookup table similar to Location
50
50
 
51
51
  :style =>
52
52
  lambda do |listing,param|
53
- puts "Called style"
53
+ puts "Called style" if $debug
54
54
  desired = FuzzyRealty::STYLE[:Symbol][param.desired]
55
55
  actual = FuzzyRealty::STYLE[:Symbol][listing.style]
56
- puts FuzzyRealty::STYLE[desired][actual]
56
+ puts FuzzyRealty::STYLE[desired][actual] if $debug
57
57
  FuzzyRealty::STYLE[desired][actual]
58
58
  end
59
59
  }
@@ -10,6 +10,7 @@ class FuzzyRealtyTest < Test::Unit::TestCase
10
10
  :location => 'A',
11
11
  :style => 'Condominium'
12
12
  })
13
+ @listing = [@listing]
13
14
  end
14
15
 
15
16
  should "score perfect for perfect house" do
@@ -21,10 +22,48 @@ class FuzzyRealtyTest < Test::Unit::TestCase
21
22
  params << FuzzyRealty::Parameter.new(:sqft,1575,true)
22
23
  query = FuzzyRealty::Query.new(params)
23
24
 
24
- scores = FuzzyRealty::ExpertSystem.scores([@listing],query)
25
- puts "%.2f" % scores[0][:score] + "\t\t#{scores[0][:listing].inspect}"
25
+ scores = FuzzyRealty::ExpertSystem.scores(@listing,query)
26
+ # puts "%.2f" % scores[0][:score] + "\t\t#{scores[0][:listing].inspect}"
26
27
  assert_equal(scores[0][:score], FuzzyRealty.max_score)
27
28
  end
29
+
30
+ should "score zero for no parameters" do
31
+ params = []
32
+ query = FuzzyRealty::Query.new(params)
33
+ scores = FuzzyRealty::ExpertSystem.scores(@listing,query)
34
+ assert_equal(scores[0][:score], 0)
35
+ end
36
+
37
+ context "for individual parameters" do
38
+ should "score correctly for style" do
39
+ params = []
40
+ params << FuzzyRealty::Parameter.new(:style,"Condominium",true)
41
+ query = FuzzyRealty::Query.new(params)
42
+ scores = FuzzyRealty::ExpertSystem.scores(@listing,query)
43
+ assert_equal(scores[0][:score], FuzzyRealty::WEIGHTS[:style])
44
+ end
45
+ should "score correctly for sqft" do
46
+ params = []
47
+ params << FuzzyRealty::Parameter.new(:sqft,1575,true)
48
+ query = FuzzyRealty::Query.new(params)
49
+ scores = FuzzyRealty::ExpertSystem.scores(@listing,query)
50
+ assert_equal(scores[0][:score], FuzzyRealty::WEIGHTS[:sqft])
51
+ end
52
+ should "score correctly for price" do
53
+ params = []
54
+ params << FuzzyRealty::Parameter.new(:price,250_000,true)
55
+ query = FuzzyRealty::Query.new(params)
56
+ scores = FuzzyRealty::ExpertSystem.scores(@listing,query)
57
+ assert_equal(scores[0][:score], FuzzyRealty::WEIGHTS[:price])
58
+ end
59
+ should "score correctly for location" do
60
+ params = []
61
+ params << FuzzyRealty::Parameter.new(:location, 'A',true)
62
+ query = FuzzyRealty::Query.new(params)
63
+ scores = FuzzyRealty::ExpertSystem.scores(@listing,query)
64
+ assert_equal(scores[0][:score], FuzzyRealty::WEIGHTS[:location])
65
+ end
66
+ end
28
67
  end
29
68
 
30
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rkneufeld-fuzzy-realty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Neufeld