Module: Locationary
- Defined in:
- lib/locationary.rb,
lib/locationary/version.rb,
lib/locationary/nearest_neighbour.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Class Method Summary collapse
- .clear_nn_data ⇒ Object
- .data ⇒ Object
- .find(query, options = {:strict => true}) ⇒ Object
- .fuzzy(query) ⇒ Object
- .nearest_neighbour(latitude, longitude, options = {}) ⇒ Object
- .nn_data ⇒ Object
- .nn_lookup ⇒ Object
- .persist_nn_structure ⇒ Object
Class Method Details
.clear_nn_data ⇒ Object
9 10 11 12 |
# File 'lib/locationary/nearest_neighbour.rb', line 9 def Locationary.clear_nn_data @kd = nil @kd_lookup = nil end |
.data ⇒ Object
41 42 43 |
# File 'lib/locationary.rb', line 41 def Locationary.data @data ||= Locationary.load_data end |
.find(query, options = {:strict => true}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/locationary.rb', line 10 def Locationary.find(query, = {:strict => true}) query.upcase result = Locationary.data[query] #if the user asked for a fuzzy lookup and we didn't find an exact match above if not [:strict] and not result result = Locationary.fuzzy(query) end result end |
.fuzzy(query) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/locationary.rb', line 22 def Locationary.fuzzy(query) best_score = 9999999999 best_hamming = 9999999999 best_match = nil Locationary.data.keys.each do |key| new_score = Levenshtein.distance(key,query) if new_score < best_score new_hamming = 0 key.dup.xor!(query).bytes.each { |b| new_hamming += b} if new_hamming < best_hamming best_score = new_score best_match = key end end end Locationary.data[best_match] end |
.nearest_neighbour(latitude, longitude, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/locationary/nearest_neighbour.rb', line 18 def Locationary.nearest_neighbour(latitude, longitude, = {}) num_matches = [:num_matches] ||= 1 results = [] Locationary.nn_data.nearestk(latitude, longitude, num_matches).each do |match| results << Locationary.data[Locationary.nn_lookup[match]] end results end |
.nn_data ⇒ Object
5 6 7 |
# File 'lib/locationary/nearest_neighbour.rb', line 5 def Locationary.nn_data @kd ||= Locationary.load_nn_data end |
.nn_lookup ⇒ Object
14 15 16 |
# File 'lib/locationary/nearest_neighbour.rb', line 14 def Locationary.nn_lookup @kd_lookup ||= Locationary.load_nn_lookup end |
.persist_nn_structure ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/locationary/nearest_neighbour.rb', line 28 def Locationary.persist_nn_structure points = [] lookup = [] i = 0 Locationary.data.each do |location| lat = location[1]['Latitude'] lon = location[1]['Longitude'] if !lat.nil? and !lon.nil? points << [Float(location[1]['Latitude']), Float(location[1]['Longitude']), i] lookup << location[0] i += 1 end end kd = Kdtree.new(points) File.open(Locationary.nn_data_location,"w") do |file| kd.persist(file) end File.open(Locationary.nn_lookup_location, "w") do |file| lookup.each { |l| file.write("#{l}\n") } end end |