Class: Snuffle::Cohort

Inherits:
Object
  • Object
show all
Includes:
PoroPlus
Defined in:
lib/snuffle/cohort.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#elementObject

Returns the value of attribute element.



6
7
8
# File 'lib/snuffle/cohort.rb', line 6

def element
  @element
end

#line_numbersObject

Returns the value of attribute line_numbers.



6
7
8
# File 'lib/snuffle/cohort.rb', line 6

def line_numbers
  @line_numbers
end

#neighborsObject

Returns the value of attribute neighbors.



6
7
8
# File 'lib/snuffle/cohort.rb', line 6

def neighbors
  @neighbors
end

Class Method Details

.from(nodes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/snuffle/cohort.rb', line 8

def self.from(nodes)
  nodes = nodes.non_sends.hashes
  cohorts = Element::Hash.materialize(nodes.to_a).inject([]) do |cohorts, element|
    cohort = Cohort.new(element: element, line_numbers: element.node.line_numbers )
    if cohort.values.count > 1 && cohort.near_neighbors.count > 0
      cohorts << cohort
    end
    cohorts
  end
end

Instance Method Details

#distance(primary_matrix, token_matrix) ⇒ Object



39
40
41
# File 'lib/snuffle/cohort.rb', line 39

def distance(primary_matrix, token_matrix)
  Snuffle::Util::Correlation.distance(primary_matrix, token_matrix)
end

#has_near_neighbors?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/snuffle/cohort.rb', line 19

def has_near_neighbors?
  near_neighbors.present?
end

#near_neighborsObject



23
24
25
# File 'lib/snuffle/cohort.rb', line 23

def near_neighbors
  @near_neighbors ||= neighbors.select{ |n| (n.values & values).size == values.size }
end

#neighborObject



35
36
37
# File 'lib/snuffle/cohort.rb', line 35

def neighbor
  Struct.new(:element, :distance)
end

#valuesObject



31
32
33
# File 'lib/snuffle/cohort.rb', line 31

def values
  @values ||= self.element.values
end