rkneufeld-fuzzy-realty 0.3.1 → 0.4.0

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
- :minor: 3
4
- :patch: 1
3
+ :minor: 4
4
+ :patch: 0
data/lib/TODO CHANGED
@@ -1,2 +1,29 @@
1
- Listing Model needs:
2
- * "location" as string
1
+ Stef, this is what I have:
2
+
3
+ require fuzzy_realty then issue a query like so:
4
+ query = FuzzyRealty::Query.new
5
+ query << FuzzyRealty::Parameter(:"symbol type", value)
6
+ ...
7
+ FuzzyRealty::ExpertSystem.scores(listings_array,query)
8
+
9
+ you will receive an array of hashes arranged like so:
10
+ [ {:listing => #<...>, :score => ###}, ...]
11
+
12
+
13
+ Parameters
14
+ ==========
15
+ Parameters are init'd with a symbol type and value
16
+ | Type | Valid Values |
17
+ | :sqft | Any Numeric |
18
+ | :style | One of %W{Bungalow Bi-level Split-level Two-story Condominium} |
19
+ | :location | One of %W{A B C D} |
20
+ | :price | Any Numeric |
21
+ * Values for style and location are very sensitive atm, anything not in the two will likely not work or even error out (i've tried to avoid the erroring out)
22
+
23
+ Listings
24
+ ========
25
+ What i expect out of a listing is that it has the following properties that can be read from it:
26
+ *price
27
+ *sqft
28
+ *location
29
+ *style
data/lib/fuzzy_realty.rb CHANGED
@@ -2,7 +2,6 @@ require 'stubs.rb'
2
2
  require 'scores_table.rb'
3
3
  require 'weights.rb'
4
4
  require 'rulebase.rb'
5
- require 'rulebase.rb'
6
5
 
7
6
  # If set this flag will turn on debugging printouts
8
7
  #$debug = true;
@@ -17,7 +16,7 @@ module FuzzyRealty
17
16
  score = 0
18
17
  query.params.each do |param|
19
18
  #calculate score modifier of parameter using indexed method
20
- change = FuzzyRealty::METHODS[param.type].call(listing,param)
19
+ change = FuzzyRealty::RULES[param.type].call(listing,param.desired)
21
20
  # if score is bad and parameter is required then reduce it further
22
21
  change = (change < 0.70 and param.required) ? (change - 1) : change
23
22
 
@@ -30,9 +29,10 @@ module FuzzyRealty
30
29
  end
31
30
  end
32
31
 
32
+ #When running the library directly calculate an example
33
33
  if __FILE__ == $0
34
34
  listings = []
35
- 10000.times do |i|
35
+ 100.times do |i|
36
36
  listings << FuzzyRealty::Listing.new({
37
37
  :price => 20_000 + rand(250_000),
38
38
  :sqft => 300 + rand(2000),
@@ -40,7 +40,7 @@ if __FILE__ == $0
40
40
  :style => %W{Bungalow Bi-level Split-level Two-story Condominium}[rand(5)]
41
41
  })
42
42
  end
43
- # The user wants price around 110k and in location A,
43
+
44
44
  parameters = []
45
45
  parameters << FuzzyRealty::Parameter.new(:price,250000)
46
46
  parameters << FuzzyRealty::Parameter.new(:location, 'A',true)
data/lib/rulebase.rb CHANGED
@@ -1,16 +1,17 @@
1
1
  module FuzzyRealty
2
- METHODS = {
2
+ RULES = {
3
3
  # Not yet implemented
4
- :bathroom => 1,
5
- :garage => 1,
6
- :deck => 1,
4
+ #:bathrooms => ,
5
+ #:bedrooms => ,
6
+ #:garage => ,
7
+ #:deck => ,
7
8
 
8
9
  # Price is match when desired is 90-105% of actual. Otherwise give a
9
10
  # reduced factor.
10
11
  :price =>
11
- lambda do |listing,param|
12
+ lambda do |listing,desired|
12
13
  puts "Called price" if $debug
13
- actual,desired = listing.price.to_f, param.desired
14
+ actual = listing.price.to_f
14
15
  result = if (desired*0.90..desired*1.05) === actual
15
16
  1.0
16
17
  else
@@ -23,17 +24,17 @@ module FuzzyRealty
23
24
  # Location calc. does lookup to find score for desired and actual
24
25
  ## Currently just return 1 if exact, 0 otherwise
25
26
  :location =>
26
- lambda do |listing,param|
27
+ lambda do |listing,desired|
27
28
  puts "Called location" if $debug
28
29
  # Perform a quick lookup (i.e. FuzzyRealty::LOCN[:A][:A] => 1.0)
29
- puts FuzzyRealty::LOCN[param.desired.to_sym][listing.location.to_sym] if $debug
30
- FuzzyRealty::LOCN[param.desired.to_sym][listing.location.to_sym]
30
+ puts FuzzyRealty::LOCN[desired.to_sym][listing.location.to_sym] if $debug
31
+ FuzzyRealty::LOCN[desired.to_sym][listing.location.to_sym]
31
32
  end,
32
33
 
33
34
  :sqft =>
34
- lambda do |listing,param|
35
+ lambda do |listing,desired|
35
36
  puts "Called sqft" if $debug
36
- actual, desired = listing.sqft, param.desired
37
+ actual = listing.sqft
37
38
  result = if (actual + 50) >= desired
38
39
  1.0
39
40
  elsif (actual + 150) >= desired
@@ -49,9 +50,9 @@ module FuzzyRealty
49
50
  # Style's follow lookup table similar to Location
50
51
 
51
52
  :style =>
52
- lambda do |listing,param|
53
+ lambda do |listing,desired|
53
54
  puts "Called style" if $debug
54
- desired = FuzzyRealty::STYLE[:Symbol][param.desired]
55
+ desired = FuzzyRealty::STYLE[:Symbol][desired]
55
56
  actual = FuzzyRealty::STYLE[:Symbol][listing.style]
56
57
  puts FuzzyRealty::STYLE[desired][actual] if $debug
57
58
  FuzzyRealty::STYLE[desired][actual]
data/lib/stubs.rb CHANGED
@@ -7,16 +7,37 @@ module FuzzyRealty
7
7
  end
8
8
 
9
9
  class Query
10
- attr_accessor :params
11
- def initialize(params)
10
+ attr_reader :params
11
+ def initialize(params=[])
12
12
  @params = params
13
13
  end
14
+ def << (param)
15
+ if !param.is_a?(Parameter)
16
+ raise "Attempting to add non-Parameter to Query"
17
+ end
18
+ @params << param
19
+
20
+ end
14
21
  end
15
22
 
16
23
  class Parameter
17
- attr_accessor :required, :type, :desired, :style
24
+ attr_reader :type, :desired, :required
18
25
  def initialize(type,desired,required=false)
19
- @required,@type,@desired = required, type, desired
26
+ if !Parameter.valid_types.contains(type)
27
+ raise "Attempting to create non-existant Parameter type"
28
+ end
29
+ @type = type
30
+ @desired = desired
31
+ @required = required
32
+ end
33
+ def self.valid_types
34
+ FuzzyRealty::WEIGHTS.each_key.to_a
20
35
  end
21
36
  end
37
+ end
38
+
39
+ class Array
40
+ def contains(value)
41
+ self.any? {|array_value| array_value == value }
42
+ end
22
43
  end
data/lib/weights.rb CHANGED
@@ -9,6 +9,7 @@ module FuzzyRealty
9
9
  :location => 25,
10
10
  :style => 18
11
11
  }
12
+
12
13
  def self.max_score
13
14
  WEIGHTS.inject(0) {|sum,n| sum + n[1]}
14
15
  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.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Neufeld