rkneufeld-fuzzy-realty 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ryan Neufeld
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = fuzzy-realty
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Ryan Neufeld. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "fuzzy-realty"
8
+ gem.summary = %Q{Fuzzy search for Expert Systems Real Estate site}
9
+ gem.email = "rkneufeld@gmail.com"
10
+ gem.homepage = "http://github.com/rkneufeld/fuzzy-realty"
11
+ gem.authors = ["Ryan Neufeld"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "fuzzy-realty #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
data/lib/TODO ADDED
@@ -0,0 +1,2 @@
1
+ Listing Model needs:
2
+ * "location" as string
@@ -0,0 +1,39 @@
1
+ require 'stubs.rb'
2
+ require 'rulebase.rb'
3
+
4
+ module FuzzyRealty
5
+ class ExpertSystem
6
+ def self.scores(listings, query)
7
+
8
+ scores = []
9
+
10
+ for listing in listings
11
+ score = 0
12
+ query.params.each do |param|
13
+ #calculate score modifier of parameter using indexed method
14
+ change = FuzzyRealty::METHODS[param.type].call(listing,param)
15
+ # if score is bad and parameter is required then reduce it further
16
+ change = (change < 0.70 and param.required) ? (change - 1) : change
17
+ score += change * FuzzyRealty::WEIGHTS[param.type]
18
+ end
19
+ scores << {listing => score}
20
+ end
21
+ return scores
22
+ end
23
+ end
24
+
25
+ # Have a table of ways to calculate the score modifier
26
+ # --> i.e. price will allow cheaper, sqft will allow bigger, etc.
27
+ end
28
+
29
+ if __FILE__ == $0
30
+ # Creating a library of stub models
31
+ a = FuzzyRealty::Listing.new({:price => 112000, :sqft => 720, :location => 'A'})
32
+ b = FuzzyRealty::Listing.new({:price => 142000, :sqft => 1420, :location => 'B'})
33
+ # The user wants price around 110k and in location A,
34
+ p1 = FuzzyRealty::Parameter.new(false,:price,110000)
35
+ p2 = FuzzyRealty::Parameter.new(true,:location, 'A')
36
+
37
+ q = FuzzyRealty::Query.new([p1,p2])
38
+ puts FuzzyRealty::ExpertSystem.scores([a,b],q).inspect
39
+ end
data/lib/rulebase.rb ADDED
@@ -0,0 +1,48 @@
1
+ module FuzzyRealty
2
+ METHODS = {
3
+ # Price is match when desired is 90-105% of actual. Otherwise give a
4
+ # reduced factor.
5
+ :price =>
6
+ lambda do |listing,param|
7
+ actual,desired = param.desired,listing.price.to_f
8
+ if (desired*0.90..desired*1.05) === actual
9
+ 1
10
+ else
11
+ 1 - ((desired - actual) / actual).abs
12
+ end
13
+ end,
14
+
15
+ # Location calc. does lookup to find score for desired and actual
16
+ ## Currently just return 1 if exact, 0 otherwise
17
+ :location =>
18
+ lambda do |listing,param|
19
+ FuzzyRealty::LOCN[param.desired][listing.location]
20
+ end
21
+ }
22
+
23
+ # Chosen weights are largely arbitrary. Expert was consulted for relative ratings,
24
+ # but as the knowledge engineer I was forced to pick the crisp values.
25
+ # Experimentation with larger user groups would likely show specific values to do
26
+ # better
27
+ WEIGHTS = {
28
+ :sqft => 15,
29
+ :price => 10,
30
+ :location => 25
31
+ }
32
+
33
+ # A is a high-class suburb
34
+ # B is a middle-class area
35
+ # C is an older middle-class area
36
+ # D is a "ghetto"
37
+ # A B C D
38
+ # A 1.0 0.75 0.2 0.0
39
+ # B 0.75 1.00 0.4 0.1
40
+ # C 0.2 0.4 1.0 0.6
41
+ # D 0.0 0.1 0.6 1.0
42
+ LOCN = {
43
+ 'A' => {'A' => 1.0, 'B' => 0.75, 'C' => 0.2, 'D' => 0.0},
44
+ 'B' => {'A' => 0.75, 'B' => 1.0, 'C' => 0.4, 'D' => 0.1},
45
+ 'C' => {'A' => 0.2, 'B' => 0.4, 'C' => 1.0, 'D' => 0.6},
46
+ 'D' => {'A' => 0.0, 'B' => 0.1, 'C' => 0.6, 'D' => 1.0}
47
+ }
48
+ end
data/lib/stubs.rb ADDED
@@ -0,0 +1,22 @@
1
+ module FuzzyRealty
2
+ class Listing
3
+ attr_accessor :price, :sqft, :location
4
+ def initialize(values={})
5
+ values.each_key {|k| instance_variable_set(:"@#{k}", values[k])}
6
+ end
7
+ end
8
+
9
+ class Query
10
+ attr_accessor :params
11
+ def initialize(params)
12
+ @params = params
13
+ end
14
+ end
15
+
16
+ class Parameter
17
+ attr_accessor :required, :type, :desired
18
+ def initialize(required,type,desired)
19
+ @required,@type,@desired = required, type, desired
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class FuzzyRealtyTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'fuzzy_realty'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rkneufeld-fuzzy-realty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Neufeld
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: rkneufeld@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/TODO
31
+ - lib/fuzzy_realty.rb
32
+ - lib/rulebase.rb
33
+ - lib/stubs.rb
34
+ - test/fuzzy_realty_test.rb
35
+ - test/test_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/rkneufeld/fuzzy-realty
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Fuzzy search for Expert Systems Real Estate site
62
+ test_files:
63
+ - test/fuzzy_realty_test.rb
64
+ - test/test_helper.rb