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 +2 -2
- data/lib/TODO +29 -2
- data/lib/fuzzy_realty.rb +4 -4
- data/lib/rulebase.rb +14 -13
- data/lib/stubs.rb +25 -4
- data/lib/weights.rb +1 -0
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/TODO
CHANGED
@@ -1,2 +1,29 @@
|
|
1
|
-
|
2
|
-
|
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::
|
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
|
-
|
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
|
-
|
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
|
-
|
2
|
+
RULES = {
|
3
3
|
# Not yet implemented
|
4
|
-
|
5
|
-
|
6
|
-
|
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,
|
12
|
+
lambda do |listing,desired|
|
12
13
|
puts "Called price" if $debug
|
13
|
-
actual
|
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,
|
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[
|
30
|
-
FuzzyRealty::LOCN[
|
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,
|
35
|
+
lambda do |listing,desired|
|
35
36
|
puts "Called sqft" if $debug
|
36
|
-
actual
|
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,
|
53
|
+
lambda do |listing,desired|
|
53
54
|
puts "Called style" if $debug
|
54
|
-
desired = FuzzyRealty::STYLE[:Symbol][
|
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
|
-
|
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
|
-
|
24
|
+
attr_reader :type, :desired, :required
|
18
25
|
def initialize(type,desired,required=false)
|
19
|
-
|
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