Commit 99cf024d authored by Coraline Ehmke's avatar Coraline Ehmke

Fixed flickering failure finally. Phew.

parent 62282bbc
......@@ -5,16 +5,21 @@ module Snuffle
include PoroPlus
attr_accessor :element, :neighbors
MAX_NEIGHBOR_DISTANCE = 3.0
def has_near_neighbors?
near_neighbors.present?
end
def near_neighbors
neighbors.select{|n| n.distance <= 1.0}
@near_neighbors ||= neighbors.select{|n| n.distance <= MAX_NEIGHBOR_DISTANCE && n.distance > 0}
end
def neighbor=(element, distance)
@neighbors << neighbor(element, distance)
def neighbors
@neighbors ||= element.node.siblings.map do |sibling|
sibling_element = Element::Hash.materialize([sibling]).first
neighbor.new(sibling_element, distance(element.matrix, sibling_element.matrix))
end
end
def values
......@@ -25,8 +30,8 @@ module Snuffle
Struct.new(:element, :distance)
end
def neighbors
@neighbors ||= []
def distance(primary_matrix, token_matrix)
Snuffle::Util::Correlation.distance(primary_matrix, token_matrix)
end
end
......
......@@ -23,25 +23,11 @@ module Snuffle
elements.to_a.each do |outer_element|
cohort = Cohort.new(element: outer_element)
next unless cohort.values.count > 1
siblings = nodes.by_type(outer_element.node.type).to_a
cohort.neighbors = siblings.map do |sibling|
next unless inner_element = Element::Hash.materialize([sibling]).first
print "."
neighbor = cohort.neighbor.new(
inner_element,
distance(outer_element.matrix, inner_element.matrix)
)
neighbor
end.compact
clusters << cohort if cohort.values.count > 1
clusters << cohort if cohort.near_neighbors.count > 1
end
clusters
end
def distance(primary_matrix, token_matrix)
Snuffle::Util::Correlation.distance(primary_matrix, token_matrix)
end
def hashes
Element::Hash.materialize(self.nodes.where(type: :hash).to_a.compact)
end
......@@ -50,8 +36,6 @@ module Snuffle
Element::String.materialize(self.nodes.where(type: :dstr).to_a.compact)
end
end
end
......@@ -27,7 +27,7 @@ module Snuffle
end
def matrix
keys.map(&:to_s).sort.map(&:hash)
keys.map(&:to_s).sort.map(&:bytes).flatten
end
def sorted_pairs
......
......@@ -7,9 +7,8 @@ module Snuffle
attr_accessor :id, :name, :type, :child_ids, :parent_id
scope :by_id, lambda{|id| where(:id => id)}
scope :by_type, lambda{|type| where(:type => type) }
scope :hashes, {type: :hash}
scope :by_id, lambda{|id| where(:id => id)}
scope :by_type, lambda{|type| where(:type => type) }
scope :with_parent, lambda{|parent_id| where(parent_id: parent_id) }
def self.nil
......@@ -27,13 +26,16 @@ module Snuffle
def name
@name ||= @name.to_s.gsub(/[^a-zA-Z0-9\_]/,'').gsub(/[ ]+/, ' ').gsub(/\_+, '_'/, '_')
@name
end
def parent
Snuffle::Node.where(id: self.parent_id).first
end
def siblings
@siblings ||= Snuffle::Node.by_type(self.type).to_a - [self]
end
def children
Snuffle::Node.where(parent_id: self.id)
end
......
# TODO factor out poroplus here
require "parser/current"
module Snuffle
......
......@@ -15,15 +15,15 @@ class Customer
end
def get_coords_for_address
fake_api_call(city: city, state: state, postal_code: postal_code)
# fake_api_call(city: city, state: state, postal_code: postal_code)
end
def neighborhood
# make_neighborhood_api_call(state: self.state, city: self.city, postal_code: self.postal_code)
make_neighborhood_api_call(state: self.state, city: self.city, postal_code: self.postal_code)
end
def something_else
fake_neighborhood_api_call(city: self.city, state: self.state)
# fake_neighborhood_api_call(city: self.city, state: self.state)
end
def and_something_else_again
......
......@@ -3,8 +3,8 @@ require 'pry'
describe Snuffle::SourceFile do
let(:source_file) {Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_2.rb") }
let(:source_file_2) {Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_3.rb") }
let(:program_2) {Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_2.rb") }
let(:program_3) {Snuffle::SourceFile.new(path_to_file: "spec/fixtures/program_3.rb") }
describe "weighting" do
......@@ -16,14 +16,15 @@ describe Snuffle::SourceFile do
# Fixture file contains two instances of city, postal_code, state
# One is args to attr_accessor, one is args hash to method
it "does not match hash values with non-hash values" do
attr_accessor_args = ['city', 'postal_code', 'state']
values = source_file.summary.object_candidates
expect(values.include?(attr_accessor_args)).to be_falsey
args = ['city', 'postal_code', 'state']
values = program_2.summary.object_candidates
p values
expect(values.include?(args)).to be_falsey
end
it "matches elements with different parents" do
args = ['company_name', 'customer_name']
values = source_file.summary.object_candidates
values = program_3.summary.object_candidates
expect(values.include?(args)).to be_truthy
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment