Class: FpGrowth::FpTree::Builder::SecondPass

Inherits:
Object
  • Object
show all
Defined in:
lib/fpgrowth/fp_tree/builder/second_pass.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(supports, threshold = 1) ⇒ SecondPass

Returns a new instance of SecondPass.



9
10
11
12
# File 'lib/fpgrowth/fp_tree/builder/second_pass.rb', line 9

def initialize(supports, threshold=1)
  @supports = supports
  @fp_tree = FpTree.new(supports, threshold)
end

Instance Attribute Details

#fp_treeObject

Returns the value of attribute fp_tree.



7
8
9
# File 'lib/fpgrowth/fp_tree/builder/second_pass.rb', line 7

def fp_tree
  @fp_tree
end

Instance Method Details

#continue_pattern(cursor_tree, transaction) ⇒ Object



62
63
64
65
# File 'lib/fpgrowth/fp_tree/builder/second_pass.rb', line 62

def continue_pattern(cursor_tree, transaction)
  cursor_tree.support+=1
  traverse(cursor_tree, transaction[1..transaction.size])
end

#execute(transactions) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/fpgrowth/fp_tree/builder/second_pass.rb', line 14

def execute(transactions)
  @fp_tree = FpTree.new(@supports)
  for transaction in transactions
    transaction = sort_by_support(transaction)
    #Look for leaf
    traverse(@fp_tree.root, transaction)

  end
  return @fp_tree
end

#fork_pattern(cursor_tree, transaction) ⇒ Object



54
55
56
57
58
59
# File 'lib/fpgrowth/fp_tree/builder/second_pass.rb', line 54

def fork_pattern(cursor_tree, transaction)
  node = Node.new(transaction.first, 1)
  @fp_tree.append_node(cursor_tree, node)
  cursor_tree = node
  traverse(cursor_tree, transaction[1..transaction.size])
end

#sort_by_support(transaction) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/fpgrowth/fp_tree/builder/second_pass.rb', line 25

def sort_by_support(transaction)
  lookup = @fp_tree.item_order_lookup

  transaction.sort_by! do |item|
    lookup.fetch(item, lookup.size + 1)
  end
end

#traverse(cursor_tree, transaction) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fpgrowth/fp_tree/builder/second_pass.rb', line 35

def traverse(cursor_tree, transaction)
  if transaction and transaction.size > 0
    found = false
    if cursor_tree.item == transaction.first
      continue_pattern(cursor_tree, transaction)
      found = true
    end
    i = 0
    while found == false and i < cursor_tree.children.size
      if cursor_tree.children[i].item == transaction[0] then
        continue_pattern(cursor_tree.children[i], transaction)
        found = true
      end
      i+=1
    end
    fork_pattern(cursor_tree, transaction) unless found
  end
end